确定纬度/经度是否在地球表面的多边形内

发布于 2024-12-17 01:01:26 字数 388 浏览 5 评论 0原文

我试图弄清楚纬度/经度点是否包含在由代表地球上的点(也是纬度/经度,按顺时针顺序)的顶点定义的多边形内。这对于可以映射到 2D 纬度/经度空间的多边形来说是微不足道的。

这变得越来越困难的是圆(现在切换回 3D),它可能从一个极点到另一个极点,覆盖半个地球。纬度/经度的转换看起来像正弦波。多边形测试中的 2D 点不再适用于这种情况。是否有一种算法可以解决这个问题?

================== 对以下评论的澄清:==================== 多边形定义为以度为单位的 (lon, lat) 对,即 (60, 90)、(60, 110)、(-30, 110)、(-30, 90)。

我确实有实现光线投射算法的代码,并且可以工作。然而,地球表面上的某些多边形不会转换为二维空间中的闭合多边形。

I am trying to figure out if a latitude/longitude point is contained within a polygon defined by vertexes that represent points on the earth (also lat/lon's, in clockwise order). This is trivial for polygons that can be mapped to the 2D lat/lon space.

Where this becomes increasingly difficult is circle's (now switching back to 3D) that may go from pole to pole covering half the earth. The translation to lat/lon looks like a sine wave. The 2D point in polygon test no longer applies to this case. Is there an algorithm that exists that solves this problem?

================== Clarifications on comments below: ===================
The polygon is defined as (lon, lat) pairs in degrees, i.e., (60, 90), (60, 110), (-30, 110), (-30, 90).

I do have code that implements the ray casting algorithm, and that works. however, certain polygons on the surface of the earth do not translate to closed polygons in the 2D space.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾冷 2024-12-24 01:01:26

正如 denniston.t 所说,如果您只对圆感兴趣,并且有半径,您可以简单地检查 大圆 中心点与点之间的距离小于半径。要查找大圆距离,通常使用Haversine 公式。以下是我在Python中的实现:

from math import radians, sin, cos, asin, sqrt

def haversine(point1, point2):
    """Gives the distance between two points on earth.

    The haversine formula, given two sets of latitude and longitude,
    returns the distance along the surface of the earth in miles,
    ignoring potential changes in elevation. The points must be in
    decimal degrees.
    """
    earth_radius_miles = 3956
    lat1, lon1 = (radians(coord) for coord in point1)
    lat2, lon2 = (radians(coord) for coord in point2)
    dlat, dlon = (lat2 - lat1, lon2 - lon1)
    a = sin(dlat/2.0)**2 + cos(lat1) * cos(lat2) * sin(dlon/2.0)**2
    great_circle_distance = 2 * asin(min(1,sqrt(a)))
    d = earth_radius_miles * great_circle_distance
    return d

As stated by denniston.t, if you are only interested in circles, and you have a radius, you can simply check if the Great Circle Distance between the center point and the point is less than the radius. To find the great circle distance you typically use the Haversine Formula. The following is my implementation in python:

from math import radians, sin, cos, asin, sqrt

def haversine(point1, point2):
    """Gives the distance between two points on earth.

    The haversine formula, given two sets of latitude and longitude,
    returns the distance along the surface of the earth in miles,
    ignoring potential changes in elevation. The points must be in
    decimal degrees.
    """
    earth_radius_miles = 3956
    lat1, lon1 = (radians(coord) for coord in point1)
    lat2, lon2 = (radians(coord) for coord in point2)
    dlat, dlon = (lat2 - lat1, lon2 - lon1)
    a = sin(dlat/2.0)**2 + cos(lat1) * cos(lat2) * sin(dlon/2.0)**2
    great_circle_distance = 2 * asin(min(1,sqrt(a)))
    d = earth_radius_miles * great_circle_distance
    return d
放血 2024-12-24 01:01:26

如果您在球体表面绘制了圆的中心点和半径,请计算Great-中心点与目标点之间的圆距离。如果小于圆的半径,则目标点位于圆内。

这不会推广到球体上绘制的任意多边形,但您只询问了圆形,所以我不知道这对您是否重要。

If you have the center point and radius of your circle drawn on the surface of the sphere, calculate the Great-circle distance between the center point and target point. If it's less than the radius of the circle, the target point lies in the circle.

This will not generalize to arbitrary polygons drawn on your sphere, but you only asked about circles, so I don't know if it matters to you.

没︽人懂的悲伤 2024-12-24 01:01:26
containsLocation(point:LatLng, polygon:Polygon)
containsLocation(point:LatLng, polygon:Polygon)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文