mapbox / leaflet-pip

point in polygon intersections for leaflet
https://mapbox.github.io/leaflet-pip/
BSD 2-Clause "Simplified" License
199 stars 46 forks source link

Doen't seem to support GeometryCollection #30

Open karlwettin opened 6 years ago

karlwettin commented 6 years ago

Leaflet-pip does not seem to find polygons inside of a GeometryCollection. If I get it right then not only mush layers be iterated, if the layer contains a GeometryCollection then all items within that must also be subjected to isPoly(l), but that probably require a bit of refactoring to process GeoJSON objects rather than layers.

So currently a Polygon as the one the below will never be found. { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { "class": "LYRESTAD" }, "geometry": { "type": "GeometryCollection", "geometries": [{ "type": "Polygon", "coordinates": [ [ [14.00881897363853, 58.759130043177166], [13.928815204319006, 58.93581644468062], [14.120750873038237, 58.8843341034047], [14.121862332723687, 58.88126014491872], [14.174039271588722, 58.78656397216331], [14.145110948923927, 58.767173887639935], [14.12508839353722, 58.758541330342304], [14.11335950201629, 58.76108407639584], [14.06140521182085, 58.77226018822536], [14.00881897363853, 58.759130043177166] ] ] }] } }

karlwettin commented 6 years ago

You're not going to like this, but here is my hack to fix it. It now returns the actual polygon objects rather than the layers.

` var leafletPip = { bassackwards: false, pointInLayer: function(p, layer, first) { if (typeof p.lat === 'number') p = [p.lng, p.lat]; else if (leafletPip.bassackwards) p = p.concat().reverse();

  var results = [];

  layer.eachLayer(function(l) {

    if (l.feature &&
        l.feature.geometry &&
        l.feature.geometry.type &&
        ['GeometryCollection'].indexOf(l.feature.geometry.type) !== -1) {
      for (var i=0;i<l.feature.geometry.geometries.length;i++) {
        var geometry=l.feature.geometry.geometries[i];
        if (first && results.length) return;
        if (['Polygon', 'MultiPolygon'].indexOf(geometry.type) !== -1 && gju.pointInPolygon({
              type: 'Point',
              coordinates: p
            }, geometry)) {
          results.push(geometry);
        }
      }
    } else {
      if (first && results.length) return;
      if (isPoly(l) && gju.pointInPolygon({
            type: 'Point',
            coordinates: p
          }, l.toGeoJSON().geometry)) {
        results.push(l.toGeoJSON().geometry);
      }

    }
  });
  return results;
}

};

`