LatLong hits a given polygon in D3 + Leaflet

I am trying to learn how to use the Javascript leaflet library with d3 to create various map visualizations.

I followed this tutorial , which creates a choroid map of the United States with some interactivity. This provides some of what I need, but the main functionality that I want is to have a list of full / long coordinates, classified depending on which area they belong to.

This would mean, for example, on a textbook map, if I had a long lat value (55, -3) that fell into the state of the Arizona polygon, the program could classify this point as belonging to Arizona.

Is there a function in the library leaflet (or d3) that will allow me to enter the long lat coordinate as a parameter and return the name of the function to which it belongs? In the tutorial below, you can attach a function to each function using the onEveryFeature property, and you can trigger mouseover events when each function freezes. Of course, is there a way to extend this functionality to numeric data instead of mouse points?

+2
source share
1 answer

The flyer will need some kind of customization if you want to do this. This leaves the mouseclicks with a browser and therefore does not need logic to determine if a point is inside the polygon.

I'm not very good at d3, but it doesn't seem obvious to me how this will be done out of the box. Looking at the polygonal code, I find the clipping algorithm and the intersection of infinite lines.

If you add a third library, this should be pretty simple. The OpenLayers geometry library can determine if a point is inside a polygon .

EDIT: I got this to work, see also http://jsfiddle.net/VaY3E/4/

var parser = new OpenLayers.Format.GeoJSON(); var vectors = parser.read(statesData); var lat = 36; var lon = -96; var point = new OpenLayers.Geometry.Point(lon, lat); for( var i = 0; i< vectors.length; i++ ){ if(vectors[i].geometry.intersects(point)){ alert(vectors[i].attributes['name']); } } 

Or you can use https://github.com/maxogden/geojson-js-utils , a slightly more specific library. It looks like he knows how to read GeoJSON, and has a gju.pointInPolygon method. I have not tested it though.

+5
source

All Articles