确定给定的纬度/经度是否在美国境内
如何确定给定的纬度/经度是否在美国境内?我希望后端代码在美国境内以一种方式运行,在美国境外以另一种方式运行。由于应用程序的要求,IP 地理定位在这里不是一个选项。
理想情况下,我希望在不影响外部服务的情况下发生这种情况。
How can I determine if a given lat/long is within the borders of the USA? I'd like for backend code to behave in one way if within the US and another outside of the USA. Due to requirements of the application, IP Geolocation is not an option here.
Ideally I'd like this to happen without hitting an external service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将美国表示为多边形,则可以使用“多边形中的点”算法,例如 交叉数测试,测试点是否位于多边形内。对于具有
n
条边的多边形,此类查询的运行时间为O(n)
时间。如果您想要更快但近似的东西,您可以通过四叉树之类的东西对多边形进行(离线)空间分解,并确定树中的哪些叶子框位于边界内。对于具有 n 个框的树来说,找到一个点的封闭叶框的平均(在线)运行时间将为 O(log(n)) 。
希望这有帮助。
If you have a representation of the US as a polygon you could then use a 'point-in-polygon' algorithm, such as a crossing number test, to test whether the point lies within the polygon or not. This type of query runs in
O(n)
time, for a polygon withn
edges.If you want something faster, but approximate, you could do an (offline) spatial decomposition of your polygon, via something like a quadtree and determine which leaf boxes in the tree lie within the borders. The average (online) runtime to find the enclosing leaf box for a point would then be
O(log(n))
for a tree withn
boxes.Hope this helps.
如果您仍然对这个问题感兴趣,您可以下载美国的 shapefile。
例如,
https://www.weather.gov/gis/USStates 包括所有州和领土。因此这个 shapefile 由 57 个多边形组成。
https://www.census.gov/geo/maps- data/data/cbf/cbf_nation.html 有一个包含一个多多边形的形状文件。
此外,至少还有一种用于海岸线(包括近海)的其他形状文件。
使用这些数据集,您应该编写一个程序来确定(纬度,经度)是否位于多多边形中。那是另一个话题了。
If you are still interested in the question, you can download shapefiles for the US.
For example,
https://www.weather.gov/gis/USStates includes all the states and territories. Thus this shapefile consists of 57 multi-polygons.
https://www.census.gov/geo/maps-data/data/cbf/cbf_nation.html has a shapefile containing one multi-polygon.
Also, there are at least one other shapefile for coastal lines (to include offshore).
With those dataset, you should make a program to determine if a (lat, lon) is located in the multi-polygon(s). That is another subject.