球体/地球仪上的点与多边形之间的最短大圆距离

2024-02-21

我有一组由地理(WGS84)坐标指定的多边形:它们位于球体上。

我有一个由纬度-经度对指定的点。

我想(有效地)找到点和多边形之间的最小大圆距离。

我当前的堆栈包括 fiona、shapely、gdal 和 proj。

StackOverflow 上的类似问题似乎大多是将特征投影到平面上并找到那里的距离,或者(令人不安地)完全忽略投影或缺乏投影。


这并不是特别有效,因为很多操作都是在 Python 中进行的,而不是在编译的库中,但它确实完成了工作:

import shapely
import numpy as np
import math

def Pairwise(iterable):
  """
  Iterate through an itertable returning adjacent pairs
  :param iterable   An iterable
  :returns: Pairs of sequential, adjacent entries from the iterable
  """
  it    = iter(iterable)
  a     = next(it, None)
  for b in it:
    yield (a, b)
    a = b

def LatLonToXYZ(lat,lon,radius):
  """
  Convert a latitude-longitude pair to 3D-Cartesian coordinates
  :param lat    Latitude in degrees
  :param lon    Longitude in degrees
  :param radius Radius in arbitrary units
  :returns: x,y,z coordinates in arbitrary units
  """
  lat = np.radians(lat)
  lon = np.radians(lon)
  x   = radius * np.cos(lon) * np.cos(lat)
  y   = radius * np.sin(lon) * np.cos(lat)
  z   = radius * np.sin(lat)
  return x,y,z

def XYZtoLatLon(x,y,z):
  """
  Convert 3D-Cartesian coordinates to a latitude-longitude pair
  :param x      x-coordinate in arbitrary units
  :param y      y-coordinate in arbitrary units
  :param z      z-coordinate in arbitrary units
  :returns A (lat,lon) pair in degrees
  """
  radius = np.sqrt(x*x+y*y+z*z)
  lat    = np.degrees(np.arcsin(z/radius))
  lon    = np.degrees(np.arctan2(y, x))   
  return lat,lon

def Haversine(lat1, lon1, lat2, lon2):
  """
  Calculate the Great Circle distance on Earth between two latitude-longitude
  points
  :param lat1 Latitude of Point 1 in degrees
  :param lon1 Longtiude of Point 1 in degrees
  :param lat2 Latitude of Point 2 in degrees
  :param lon2 Longtiude of Point 2 in degrees
  :returns Distance between the two points in kilometres
  """
  Rearth = 6371
  lat1   = np.radians(lat1)
  lon1   = np.radians(lon1)
  lat2   = np.radians(lat2)
  lon2   = np.radians(lon2)
  #Haversine formula 
  dlon = lon2 - lon1 
  dlat = lat2 - lat1 
  a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
  c = 2 * np.arcsin(np.sqrt(a)) 
  return Rearth*c

#http://stackoverflow.com/a/1302268/752843
def NearestPointOnGC(alat1,alon1,alat2,alon2,plat,plon):
  """
  Calculate the location of the nearest point on a Great Circle to a query point
  :param lat1 Latitude of start of arc in degrees
  :param lon1 Longtiude of start of arc in degrees
  :param lat2 Latitude of end of arc in degrees
  :param lon2 Longtiude of end of arc in degrees
  :param plat Latitude of query point in degrees
  :param plon Longitude of query point in degrees
  :returns: A (lat,lon) pair in degrees of the closest point
  """
  Rearth    = 6371 #km
  #Convert everything to Cartesian coordinates
  a1        = np.array(LatLonToXYZ(alat1,alon1,Rearth))
  a2        = np.array(LatLonToXYZ(alat2,alon2,Rearth))
  p         = np.array(LatLonToXYZ(plat, plon, Rearth))
  G         = np.cross(a1,a2) #Plane of the Great Circle containing A and B
  F         = np.cross(p,G)   #Plane perpendicular to G that passes through query pt
  T         = np.cross(G,F)   #Vector marking the intersection of these planes
  T         = Rearth*T/LA.norm(T) #Normalize to lie on the Great Circle
  tlat,tlon = XYZtoLatLon(*T)
  return tlat,tlon

def DistanceToGCArc(alat,alon,blat,blon,plat,plon):
  """
  Calculate the distance from a query point to the nearest point on a
  Great Circle Arc
  :param lat1 Latitude of start of arc in degrees
  :param lon1 Longtiude of start of arc in degrees
  :param lat2 Latitude of end of arc in degrees
  :param lon2 Longtiude of end of arc in degrees
  :param plat Latitude of query point in degrees
  :param plon Longitude of query point in degrees
  :returns: The distance in kilometres from the query point to the great circle
            arc
  """
  tlat,tlon = NearestPointOnGC(alat,alon,blat,blon,plat,plon) #Nearest pt on GC
  abdist    = Haversine(alat,alon,blat,blon)  #Length of arc
  atdist    = Haversine(alat,alon,tlat,tlon)  #Distance arc start to nearest pt
  tbdist    = Haversine(tlat,tlon,blat,blon)  #Distance arc end to nearest pt
  #If the nearest point T on the Great Circle lies within the arc, then the
  #length of the arc is approximately equal to the distance from T to each end
  #of the arc, accounting for floating-point errors
  PRECISION = 1e-3 #km 
  #We set the precision to a relatively high value because super-accuracy is not
  #to needed here and a failure to catch this can lead to vast under-estimates
  #of distance
  if np.abs(abdist-atdist-tbdist)<PRECISION: #Nearest point was on the arc
    return Haversine(tlat,tlon,plat,plon)
  #Okay, the nearest point wasn't on the arc, so the nearest point is one of the
  #ends points of the arc
  apdist = Haversine(alat,alon,plat,plon)
  bpdist = Haversine(blat,blon,plat,plon)
  return min(apdist,bpdist)

def Distance3dPointTo3dPolygon(lat,lon,geom):
  """
  Calculate the closest distance between a latitude-longitude query point
  and a `shapely` polygon defined by latitude-longitude points using only 
  spherical mathematics
  :param lat  Latitude of query point in degrees
  :param lon  Longitude of query point in degrees
  :param geom A `shapely` geometry whose points are in latitude-longitude space
  :returns: The minimum distance in kilometres between the polygon and the
            query point
  """
  if geom.type == 'Polygon':
    dist = math.inf
    xy   = features[0]['geometry'][0].exterior.xy
    #Polygons are closed rings, so the first-last pair is automagically delt with
    for p1, p2 in Pairwise(zip(*xy)):
      dist = min(dist,DistanceToGCArc(p1[1],p1[0],p2[1],p2[0],lat,lon))
  elif geom.type == 'MultiPolygon':
    dist = min(*[Distance3dPointTo3dPolygon(lat,lon,part) for part in geom])
  return dist

当然,您可以通过仅考虑点并忽略大圆弧来加快速度,这对于具有合适密集边界规范的多边形来说是合适的:

def Distance3dPointTo3dPolygonQuick(lat,lon,geom):
  """
  Calculate the closest distance between a polygon and a latitude-longitude
  point, using only spherical considerations. Ignore edges.
  :param lat  Latitude of query point in degrees
  :param lon  Longitude of query point in degrees
  :param geom A `shapely` geometry whose points are in latitude-longitude space
  :returns: The minimum distance in kilometres between the polygon and the
            query point
  """
  if geom.type == 'Polygon':
    dist = math.inf
    x,y  = geom.exterior.xy
    #Polygons are closed rings, so the first-last pair is automagically delt with
    dist = np.min(Haversine(x,y,lat,lon))
  elif geom.type == 'MultiPolygon':
    dist = min(*[Distance3dPointTo3dPolygonQuick(lat,lon,part) for part in geom])
  return dist
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

球体/地球仪上的点与多边形之间的最短大圆距离 的相关文章

  • 在Python中将大文件(25k条目)加载到dict中很慢?

    我有一个大约有 25000 行的文件 它是 s19 格式的文件 每行就像 S214780010 00802000000010000000000A508CC78C 像这样的事情怎么样 我做了一个测试文件 只有一行S21478001000802
  • 有向无环图的拓扑排序为阶段

    是否有一种算法 给定一个未加权的有向无环图 将所有节点排序到节点集列表中 使得 拓扑顺序被保留 即 对于所有边u gt v v出现在列表中更靠下的集合中u and 列表的长度是最小的 这个问题有名字吗 Example 下图的一种可能的排序是
  • boost::graph 算法是否能够使用以前的解决方案更快地解决密切相关的新问题?

    我在下图中定义了最大流量问题 最初 所有四个边缘的容量均为 4 个单位 我求从 0 到 3 的最大流量值 答案是 8 沿路径 0 gt 1 gt 3 4 个单位 沿路径 0 gt 2 gt 3 4 个单位 以下代码创建图表并查找最大流量 i
  • 获取字符串模板中所有标识符列表的函数(Python)

    对于标准库string template在Python中 有没有一个函数可以获取所有标识符的列表 例如 使用以下 xml 文件
  • 在多核上运行 python 线程

    我知道Python 2 7不允许在不同的内核上运行多个线程 你需要使用multiprocessing模块以实现某种程度的并发性 我正在看concurrent futuresPython 3 4 中的模块 是否使用ThreadPoolExec
  • 快速排序优化

    我正在学习排序算法 下一步 我试图让我的实现接近std sort 到目前为止我还很远 我有 3 个快速排序的实现 标准快速排序 使用临时数组 quicksort with following optimizations median3 用于
  • 将列表值转换为 pandas 中的行

    我有数据帧 其中一列具有相同长度的 numpy ndarray 值 df list 0 Out 92 array 0 0 0 0 29273096 0 30691767 0 27531403 我想将这些列表值转换为数据框并从 df iloc
  • matplotlib:渲染到缓冲区/访问像素数据

    我想使用 matplotlib 生成的图作为 OpenGL 中的纹理 到目前为止 我遇到的 matplotlib 的 OpenGL 后端要么不成熟 要么已经停止使用 所以我想避免使用它们 我当前的方法是将图形保存到临时 png 文件中 并从
  • argparse 不检查位置参数

    我正在创建一个脚本 它使用 argparse 接受位置参数和可选参数 我已经阅读了 Doug 的教程和 python 文档 但找不到答案 parser argparse ArgumentParser description script t
  • 当元组列表中相同项目的值是字符串时,对它们的值求和

    如果我有这样的元组列表 my list books 5 books 10 ink 20 paper 15 paper 20 paper 15 我怎样才能把列表变成这样 books 15 ink 20 paper 50 即添加同一项目的费用
  • 类变量:“类列表”与“类布尔值”[重复]

    这个问题在这里已经有答案了 我不明白以下示例的区别 一次类的实例可以更改另一个实例的类变量 而另一次则不能 示例1 class MyClass object mylist def add self self mylist append 1
  • Kivy TextInput 水平和垂直对齐(文本居中)

    如何在 Kivy 的 TextInput 中水平居中文本 I have the following screen But I want to centralize my text like this 这是我的 kv 语言的一部分 BoxLa
  • Django 1.7:如何使用 html/css 文件作为模板发送电子邮件

    从 Django 1 7 开始 可以send email 使用新参数 html message 不幸的是 没有关于如何使用它的全面指南 新手友好 或者至少我找不到它 我需要使发送的电子邮件变得漂亮 因此 我试图弄清楚如何将我的消息包含到 h
  • 如何将 pandas DataFrame 转换为 TimeSeries?

    我正在寻找一种将 DataFrame 转换为 TimeSeries 而不拆分索引和值列的方法 有任何想法吗 谢谢 In 20 import pandas as pd In 21 import numpy as np In 22 dates
  • 列表中的特定范围(python)

    我有一个从文本字符串中提取的整数列表 因此当我打印该列表 我称之为test I get 135 2256 1984 3985 1991 1023 1999 我想打印或制作一个仅包含特定范围内的数字的新列表 例如1000 2000之间 我尝试
  • 如何将 django ModelForm 字段显示为不可编辑

    接受我的初步教训django ModelForm 我想让用户能够编辑博客中的条目 BlogEntry has a date postedTime title and content 我想向用户展示一个编辑表单 其中显示所有这些字段 但仅包含
  • 将二进制数据视为文件对象?

    在此代码片段 由另一个人编写 中 self archive是一个大文件的路径并且raw file是以二进制数据形式读取的文件内容 with open self archive rb as f f seek offset raw file s
  • 如何使用 python 模块的多个 git 分支?

    我想使用 git 来同时处理我正在编写的模块中的多个功能 我目前正在使用 SVN 只有一个工作区 因此我的 PYTHONPATH 上只有该工作区 我意识到这不太理想 所以我想知道是否有人可以建议一种更 正确 的方法来做到这一点 让我用一个假
  • Django 中使用外键的抽象基类继承

    我正在尝试在 Django 支持的网站上进行模型继承 以遵守 DRY 我的目标是使用一个名为 BasicCompany 的抽象基类来为三个子类提供通用信息 Butcher Baker CandlestickMaker 它们位于各自的应用程序
  • 尝试 numba 时出现巨大错误

    我在使用 numba 时遇到了大量错误 讽刺的是 正确的结果是在错误之后打印的 我正在使用最新的 Anaconda python 并安装了 numba conda install numba 一次在 Ubuntu 13 64 位和 anac

随机推荐

  • 如何刷新“RandomAccessFile”(java)?

    我在java中使用RandomAccessFile file new RandomAccessFile filename rw file writeBytes 如何确保这些数据刷新到操作系统 没有 file flush 方法 请注意 我实际
  • 调整大小时出现黑色边框

    我开始了我的WPF学习之旅 经过几天的编码 我发现每当我调整任何 WPF 表单的大小时 调整大小时底部和右侧都会出现黑色边框 就像一个伪影 就好像屏幕太慢一样 在使用 winform 时我从未注意到这一点 就像这样 这是一个已知问题吗 有什
  • VSCode的默认设置文件的位置在哪里?

    在 Windows 计算机上 VS Code 用户设置文件位于 AppData Code User settings json 当我们从上述位置打开用户设置文件或转到左侧窗格中时 包含默认设置的文件的位置是什么 文件 gt 首选项 gt 设
  • Scada-Lts - “未指定数据源”错误

    我尝试使用 jdk 1 7 在 Tomcat 7 上运行 Scada Lts 但出现以下错误 SEVERE Exception sending context initialized event to listener instance o
  • 如何使用 Process.Start("outlook.exe") 运行 Outlook 并重新获得控制权

    我的 C 程序需要启动 Office Outlook 并获取当前的 正在运行的 Outlook 应用程序 为了做到这一点 我实现了以下简单的程序 所以如果你愿意 你可以简单地测试它 using Outlook Microsoft Offic
  • .NET 的状态机框架

    我工作中的系统基本上是一个消息驱动的状态机 它接收各种类型的消息 根据消息查找某些上下文 状态 然后根据消息和当前状态决定要做什么 通常结果是一条消息被发送到系统之外 有没有好的开源框架可以在 NET 中实现状态机 我研究了最新版本的 Wi
  • Oracle sql MERGE INTO 带有单个 where 子句

    我有以下 SQL 代码 这是我到目前为止所得到的 MERGE INTO SCHEMA1 TABLE 1 table1 USING SELECT DISTINCT table2 column1 view1 column2 FROM SCHEM
  • 如何在 PHP 中使用 RegexIterator

    我还没有找到如何使用 php RegexIterator 递归遍历目录的好例子 最终结果是我想指定一个目录并查找其中具有某些给定扩展名的所有文件 例如只说 html php 扩展 此外 我想过滤掉 Trash 0 Trash 500 等类型
  • 可以禁用@media查询或强制解决吗?原因:允许 iPhone 查看桌面网站吗?

    我通过 media 查询对我的网站进行了大幅修改 以在手机上显示得非常精简 但是 我的用户要求提供该网站的桌面版本 可通过链接获取 更进一步 桌面站点本身也会根据分辨率被 media 查询修改 我正在考虑选择一种 桌面 分辨率 例如 144
  • 未选取环回 4 测试配置

    我跟着Loopback4 数据源 https loopback io doc en lb4 DataSources html文档并放置样本 数据源 json and 样本 测试 数据源 json文件下src 数据源 每当我跑步时npm ru
  • Java CGI 与 Servlet [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Tweepy 错误代码 400

    我正在尝试下载一些推文用于研究目的 直到几天前 代码都运行得很好 错误信息 gt Traceback most recent call last gt gt File
  • 编译时如何使用通配符包含 JAR 文件?

    我在 java 文件 MyRtmpClient java 中有以下内容 import org apache mina common ByteBuffer and ByteBuffer位于 JAR 文件内 当然具有正确的目录结构 该 jar
  • JQuery Ajax post 参数有时不会在 IE 上发送

    我遇到的问题是 当我使用 jquery ajax post 时 频率非常低 当我将呼叫从 post 类型切换为 get 类型时 问题就消失了 有没有其他人在 IE 上见过这种奇怪的行为 谢谢 我已经在各种 ajax 调用中看到过这种情况 但
  • 使用 Azure Function 删除 CosmosDB 条目

    我一直在寻找一种技术 通过浏览器内的代码编辑器 使用 Azure Functions 删除 Cosmos 数据库中的项目 我不想在 VS 上使用本地开发的代码有多种原因 我正在使用的代码可用here https pastebin com X
  • 向 ASP.NET Web API 控制器添加显式操作路由

    我有一个 ASP NET Web API 项目ApiController提供了一个User端点具有以下操作 GET api User POST api User DELETE api user 我想提供以下端点 GET api user m
  • 单元测试与验收测试

    你支持其中之一吗 或两者 我的理解是单元测试 从开发人员的角度验证系统 帮助开发者实践TDD 保持代码模块化 协助检测低粒度的错误 验收测试 从业务和 QC QA 角度验证系统 往往是高级别的 因为它们通常是由不熟悉代码内部工作原理的人编写
  • WebRTC - 禁用所有音频处理

    我目前正在尝试通过 webrtc 获得尽可能干净的音频通道 通过 getUserMedia mediaconstraints 对象 我设置了以下选项 constraints audio mandatory echoCancellation
  • git pre-receive hook - 获取最新代码

    我正在尝试写一个pre receive hook对于 git 来说 它将拉取正在推送的最新版本的代码并对其运行单元测试 我的代码如下 但是当它到达 git checkout newrev 时 我得到 远程 致命 引用不是树 188de39c
  • 球体/地球仪上的点与多边形之间的最短大圆距离

    我有一组由地理 WGS84 坐标指定的多边形 它们位于球体上 我有一个由纬度 经度对指定的点 我想 有效地 找到点和多边形之间的最小大圆距离 我当前的堆栈包括 fiona shapely gdal 和 proj StackOverflow