0

I have trouble with using leaflet-pip: i get geojson data from here: http://polygons.openstreetmap.fr/get_geojson.py?id=1320234&params=0

How should i call leafletPip.pointInLayer with such data? I have several attempts, but it returns empty array, but i know that there are some markers inside those layers.

Kirill
  • 67
  • 9

1 Answers1

0

Just in case, note first that Leaflet-PIP needs an L.GeoJSON group to be passed as 2nd argument. It is trivial to build one from your GeoJSON data (L.geoJson(myGeoJSONdata)). Actually, it even just needs an L.LayerGroup.

Then, it seems that Leaflet-PIP does not handle nested Layer Groups.

Your data is made of MultiPolygons, which Leaflet converts into groups of L.Polygon within the L.GeoJSON group. Therefore Leaflet-PIP does not process them.

So you just have to "flatten" your group before being able to use it correctly with Leaflet-PIP. Parse the group and extract all non-group layers into another group.

var gjLayer = L.geoJson(myGeoJSONdata).addTo(map);

var groupOfNonGroup = L.layerGroup();

function copyToGroupOfNonGroup(group) {
  group.eachLayer(function (layer) {
    if (layer instanceof L.LayerGroup) {
      copyToGroupOfNonGroup(layer);
    } else {
      layer.addTo(groupOfNonGroup);
    }
  });
}

copyToGroupOfNonGroup(gjLayer);

var results = leafletPip.pointInLayer([lng, lat], groupOfNonGroup);

Demo: https://plnkr.co/edit/6hRKHHtvWOVdWg4jZ8AJ?p=preview

ghybs
  • 47,565
  • 6
  • 74
  • 99
  • I've updated the leaflet-pip to a working link but this doesn't seem to work anymore, the resulting array is empty http://plnkr.co/edit/WtLDY9eL6ZpBUdeV8VQm – Sam Mar 02 '17 at 09:43
  • @Sam thanks for the heads up. I have updated the demo to use the new CDN for leaflet-pip and leaflet, but still version 0.7.7. Not sure what changed in leaflet-pip for leaflet 1.0? – ghybs Mar 02 '17 at 12:33
  • Seems like leaflet 1.0 isn't supported by pip (yet?) due to changes in how Polygon classes are typed in the 'draw:created' argument (e.layer isn't the same signature anymore) ... shame :( – Sam Mar 02 '17 at 12:49