makinacorpus/Leaflet.LayerIndex

Name: Leaflet.LayerIndex

Owner: Makina Corpus

Description: Spatial index of layer objects using RTree.js

Created: 2013-03-07 11:42:57.0

Updated: 2017-12-03 16:11:28.0

Pushed: 2017-12-06 13:27:38.0

Homepage: http://makinacorpus.github.io/Leaflet.LayerIndex/

Size: 20

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Leaflet.LayerIndex

Efficient spatial index for Leaflet layers. It works recursively for L.FeatureGroup objects.

Requires the Magnificient RTree.js

Check out the live demo

Usage
On L.Map objects
L.Map.include(L.LayerIndexMixin);

var map = L.map(...);
...
var layer = L.GeoJSON(data).addTo(map);
map.indexLayer(layer);

// Search visible features for example
map.on('moveend', function () {
    var shown = map.search(map.getBounds());
    console.log(shown.length + ' objects shown.');
});

// remove the spatial index when the layer is unused
map.unindexLayer(layer);

map.unindexLayer() function accepts an options object as an optional second parameter to define the bounds to be removed from the index. It should have one of the following attributes:

If no option is provided, the function will use the bounds of the layer's geometry.

Using inherited class
L.IndexedGeoJSON = L.GeoJSON.extend({
    includes: L.LayerIndexMixin,

    initialize: function (geojson, options) {
        // Decorate onEachFeature to index layers
        var onEachFeature = function (geojson, layer) {
            this.indexLayer(layer);
            if (this._onEachFeature) this._onEachFeature(geojson, layer);
        };
        this._onEachFeature = options.onEachFeature;
        options.onEachFeature = L.Util.bind(onFeatureParse, this);

        // Parent initialization
        L.GeoJSON.prototype.initialize.call(this, geojson, options);
    },

    removeLayer: function (layer) {
      if (this.hasLayer(layer)) {
        this.unindexLayer(layer)
      }
      return window.L.GeoJSON.prototype.removeLayer.call(this, layer)
    }
});


var layer = L.IndexedGeoJSON(data).addTo(map);

var aroundToulouse = layer.searchBuffer(L.latLng([43.60, 1.44]), 0.1);
Changelog
0.0.2
0.0.1
Authors

Makina Corpus


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.