Is it possible to determine if a GeoJSON point is inside a GeoJSON polygon using JavasScript?

家住魔仙堡 提交于 2019-11-29 11:26:30

问题


Is it possible to determine whether a GeoJSON point at a given lat,lon lies within a given GeoJSON polygon using only JavaScript (either through d3, topojson, or any other way)?

For example, I can draw a map showing countries in the UK based on the tutorial here.

I then have a some points which have coordinates but no indication as to which country they lie within.

I would like to display a total point count for each country.

Can I work out which country contains each point in the browser, or do I need to preprocess my point data on the server using PostGIS or similar?


回答1:


Seems like d3 has you covered: https://github.com/d3/d3-geo#geoContains

d3.geoContains(object, point)

Returns true if and only if the specified GeoJSON object contains the specified point, or false if the object does not contain the point. The point must be specified as a two-element array [longitude, latitude] in degrees. For Point and MultiPoint geometries, an exact test is used; for a Sphere, true is always returned; for other geometries, an epsilon threshold is applied.




回答2:


Expanding the @thedude answer's you can also use geojson-lookup if you need to do this check more than once on a given GeoJSON.



来源:https://stackoverflow.com/questions/33802197/is-it-possible-to-determine-if-a-geojson-point-is-inside-a-geojson-polygon-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!