是否可以使用 JavaScript 确定 GeoJSON 点是否在 GeoJSON 多边形内?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33802197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:55:56  来源:igfitidea点击:

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

javascriptd3.jsgeojsontopojsonpoint-in-polygon

提问by user5325596

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)?

是否可以仅使用 JavaScript(通过 d3、topojson 或任何其他方式)确定给定经纬度的 GeoJSON 点是否位于给定的 GeoJSON 多边形内?

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?

我可以在浏览器中找出哪个国家/地区包含每个点,还是需要使用 PostGIS 或类似工具在服务器上预处理我的点数据?

采纳答案by thedude

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

好像d3你已经涵盖了: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.

d3.geoContains(对象,点)

当且仅当指定的 GeoJSON 对象包含指定的点时返回 true,如果对象不包含该点,则返回 false。该点必须指定为以度为单位的二元素数组 [longitude, latitude]。对于点和多点几何,使用精确测试;对于 Sphere,始终返回 true;对于其他几何图形,应用 epsilon 阈值。

回答by simonepri

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

如果您需要在给定的 GeoJSON 上多次执行此检查,则扩展@thedude答案,您还可以使用geojson-lookup