pceccon
pceccon

Reputation: 9844

Point in Polygon using leaflet-pip

I'm trying to, given a .json containing a lot of points, determine how many there are in each region (probably returning a dictionary), which are defined in another .json file.

I'm doing this based on this example:

https://www.mapbox.com/mapbox.js/example/v1.0.0/point-in-polygon/

However, I can't get it to work.

This line:

var layer = leafletPip.pointInLayer(this.getLatLng(), states, true);

Returns empty for my test case. Here is a jsfiddle reproducing my code:

http://jsfiddle.net/Pe5xU/346/

map = L.map('map').setView([40.658528, -73.952551], 10);

// Load a tile layer  
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>',
  maxZoom: 18,
  minZoom: 10
}).addTo(map);

geojson = L.geoJson(data).addTo(map);

var all_markers = [];
var layers = {};
$.each(dots, function(index, rec) {
  var markers = {}
  if (rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")) {
    var marker = L.circleMarker([rec.latitude, rec.longitude], marker_style()).addTo(map);
    all_markers.push(marker);
  }
});

var all_layers = L.featureGroup(all_markers);
map.fitBounds(all_layers.getBounds());

function marker_style() {
  return {
    radius: 4,
    weight: 0,
    opacity: 1,
    color: 'white',
    dashArray: '3',
    fillOpacity: 0.7
  };
}

$.each(dots, function(index, rec) {
  if (rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")) {
    var layer = leafletPip.pointInLayer([rec.latitude, rec.longitude], geojson, true);
    console.log(layer);
  }
});

Upvotes: 0

Views: 3325

Answers (1)

tmcw
tmcw

Reputation: 11882

This code example provides coordinates in latitude, longitude order. As documented in the leaflet-pip readme, leaflet-pip expects coordinates in longitude, latitude order, the same as GeoJSON and other geospatial formats.

Upvotes: 1

Related Questions