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

Problem with GeoJSON converted from EsriJSON and Multipolygons #15

Closed chrsnlsn closed 8 years ago

chrsnlsn commented 9 years ago

Hello Thanks for the very useful plugin. I recently ran into an issue where i'm using data from an arcgis rest service that's spitting out ESRI JSON that I then convert to GeoJSON with ogr2ogr. Because there is no 'Multipolygon' in ESRI the multipolygons get converted as just 'Polygon' in the GeoJSON. Leaflet renders them fine as multipoly's on the map, but the leaflet-pip function would fail to check all of the associated polygons in the layer.

I re-wrote the function to add a check to see if there was more than one polygon in the function and if there were to loop through and check all of them. Is this worth a pull request? Not sure if this solves any of the other multipoly issues.

gju.pointInPolygon = function (p, poly) {
    var coords = (poly.type == "Polygon") ? [ poly.coordinates ] : poly.coordinates
    var insideBox = false
    var insidePoly = false

    if(coords[0].length>1){ //check if multipoly
      for (var i = 0; i < coords[0].length; i++){
        if (!insideBox){
          var coordsMulti = [];
          coordsMulti.push(coords[0][i]);
          if (gju.pointInBoundingBox(p, boundingBoxAroundPolyCoords(coordsMulti))) insideBox = true
        }
      }

      if (!insideBox) return false

      for (var i = 0; i < coords[0].length; i++){  
        if (!insidePoly){
          var coordsMulti = [];
          coordsMulti.push(coords[0][i]);
           if (pnpoly(p.coordinates[1], p.coordinates[0], coordsMulti)) insidePoly = true
        }
      }
    }
    else{ //is singlepoly
      var insideBox = false
      for (var i = 0; i < coords.length; i++) {
        if (!insideBox){
          if (gju.pointInBoundingBox(p, boundingBoxAroundPolyCoords(coords[i]))) insideBox = true
        }
      }
      if (!insideBox) return false

      for (var i = 0; i < coords.length; i++) {
        if (!insidePoly){
          if (pnpoly(p.coordinates[1], p.coordinates[0], coords[i])) insidePoly = true
        }
      }
    }

    return insidePoly
  }
tmcw commented 8 years ago

Closing; this is a bug in ogr2ogr, not leaflet-pip.