python - 船舶到海岸线的距离(以海里为单位)

标签 python distance geopandas

我正在尝试使用 this zip file 计算从任何船舶位置到海岸线的最短距离(以海里为单位) .

我尝试过使用GeoPandasshapelygeopy,但我还没有找到正确工作的方法。我尝试使用下面的代码来找到最近的点,然后计算距离,但它总是读取错误的linestring。我感谢任何帮助。


import pandas as pd
import geopandas as gpd
from geopandas import GeoSeries
from shapely.geometry import Point
p1=GeoSeries(Point(8.41655010639208, -83.11475151369626))
coastline=gpd.read_file('/Users/Danilo/Documents/Python/panama_coastline/panama_coastline.shp')
coastline=coastline[coastline['NATURAL']=='shoreline']
coastline=coastline['geometry']
coastline.reset_index(drop=True,inplace=True)
#coastline.geom_type
#coastline
#p1=Point(8.41655010639208, -83.11475151369626)
#coastline.to_crs(epsg=5368,inplace=True)
#coastline.crs
#coastline.geometry.distance(p1).min()
line=coastline[p1.distance(coastline)==p1.distance(coastline).min()]
line.reset_index
#type(ls1)
#line.geom_type
from shapely.ops import nearest_points
p=nearest_points(p1,line[0])
from geopy.distance import geodesic, Point
p0=Point(p[0].y,p[0].x)
p1=Point(p1.y,p1.x)
geodesic(p0,p1).nautical
coastline.plot()
p0
#geodesic(Point(8.41655010639208, -83.11475151369626),Point(8.41388, -83.11114)).nautical
coastline[0].distance(p1)

最佳答案

我不确定问题到底是什么,但这是一个计算距离并在 map 上绘制点的基本示例。

import geopandas as gpd
import numpy as np

from shapely.geometry import Point, LineString
from shapely.ops import nearest_points

数据导入

panama = gpd.read_file("panama_coastline/panama_coastline.shp")

我定义函数来获取最近的线点。这不是最佳的,如果您计算 +1M 点,您需要优化它。

def closest_line(point, linestrings):
    return np.argmin( [p.distance(linestring) for linestring in  panama.geometry] )

这样我们就可以定义一个点并进行测试

p = Point(-80,9)

closest_linestring = panama.geometry[ closest_line(p, panama.geometry) ]

closest_point = nearest_points(p, closest_linestring)

并将它们绘制在 map 上

%matplotlib inline
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1, figsize=(10, 20))

_ = panama.plot(ax=ax)
_ = gpd.GeoDataFrame(closest_point, columns=['geometry']).plot(ax=ax,c='red')

编辑:添加了计算英里距离的示例

from sklearn.neighbors import DistanceMetric

dist = DistanceMetric.get_metric('haversine')
points_as_floats = [ np.array([p.y, p.x]) for p in closest_point ]

EARTH_RADIUS_IN_MILES = 3960

haversine_distances = dist.pairwise(np.radians(points_as_floats), np.radians(points_as_floats) )
haversine_distances *= EARTH_RADIUS_IN_MILES

print( haversine_distances[0][1] )

关于python - 船舶到海岸线的距离(以海里为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65300026/

相关文章:

mysql - 列表中纬度/经度之间的最长距离

java - 在Java中使用距离公式

python - 在 GeoPandas 或 Shapely 中合并多边形(形成一个几何图形)

python - 如何让爬虫在 cron 作业上运行?

python - Pandas 系列名称

python - 在 python 中获取 deb 包中的 conffiles 列表

python - 将 numpy 数组作为变量添加到 Xarray 数据集

Python嵌套循环和更新集

python - cx_freeze exe 文件在 anaconda 提示符下有效,但在 Windows cmd 命令提示符下无效?

azure - MS Fabric 和地理空间数据