从海上一点到海岸的最短距离,带有纬度/经度信息

问题描述 投票:0回答:2

我需要找到从船只到海岸的最短距离。我只有纬度和经度信息。有没有任何库或代码可以做到这一点?

from shapely.geometry import Point
from shapely.ops import nearest_points
from geopy.distance import geodesic
import geopandas as gpd

# Read coastline data
coastline_data = gpd.read_file('./ne_10m_coastline.shp')
coastline = coastline_data.geometry.unary_union

# Define target coordinate
target_coordinate = Point(36.20972222, 125.7061111)

# Find nearest point on coastline
nearest_point = nearest_points(target_coordinate, coastline)[0]

# Calculate distance
distance = geodesic((target_coordinate.x, target_coordinate.y), (nearest_point.x, nearest_point.y)).kilometers

print(f"The closest point to the coast is at {nearest_point} and the distance is {distance} kilometers.")

我尝试了这段代码,但是最近的点是相同的并且距离为零。这里有什么问题吗?

python distance
2个回答
0
投票
from shapely.geometry import Point
from shapely.ops import nearest_points
from geopy.distance import geodesic
import geopandas as gpd

# Read coastline data
coastline_data = gpd.read_file('path/to/coastline.shp')
coastline = coastline_data.geometry.unary_union

# Define target coordinate
target_coordinate = Point(latitude, longitude)

# Find nearest point on coastline
nearest_point = nearest_points(target_coordinate, coastline)[0]

# Calculate distance
distance = geodesic((target_coordinate.y, target_coordinate.x (nearest_point.y, nearest_point.x)).kilometers

print(f"The closest point to the coast is at {nearest_point} and the distance is {distance} kilometers.")

您可以从https://www.naturalearthdata.com/

获取地图数据

我的代码不起作用,请尝试以下步骤:

检查文件路径:确保 ne_10m_coastline.shp 文件位于正确的位置,并且 Coastline_data 中提供的文件路径准确。仔细检查文件扩展名 (.shp) 并验证该文件是否存在于指定位置。

坐标系对齐:确认海岸线数据的坐标系与目标坐标的坐标系相符。您可以使用 crs 属性检查 Coastline_data 的坐标参考系 (CRS),并将其与 target_coordinate 的 CRS 进行比较。如果它们不匹配,您可能需要转换其中之一以确保它们对齐。

查看海岸线数据的CRS

打印(海岸线数据.crs)

检查目标坐标的CRS

打印(目标坐标.crs)

如果CRS不匹配,可以使用to_crs方法转换target_coordinate以匹配海岸线数据的CRS。

确保有效的几何图形:验证海岸线数据是否包含有效的几何图形。您可以使用 is_valid 属性来检查是否存在任何无效几何图形。如果发现无效的几何图形,您可能需要修复或删除它们。

检查海岸线数据是否包含任何无效的几何图形

打印(海岸线数据.几何.is_valid)

如果存在无效的几何图形,您可以使用 buffer(0) 方法来修复它们。将 Coastline = Coastline_data.geometry.unary_union 行替换为以下内容:

海岸线= Coastline_data.geometry.buffer(0).unary_union


0
投票

不知道你是否还在寻找答案。然而,我遇到了类似的问题,并尝试了克里斯蒂安的代码,并且也为每个“最近”点得到了“0.0”。所以我偶然发现了 geopandas“GeoSeries”,它也有“距离”功能。我猜这个距离的单位是度,所以我使用半正弦公式将其转换为公里:

import geopandas as gpd
import numpy as np
from shapely.geometry import Point

coastline_data = gpd.read_file('/path/to/coastline.shp')
coastline = gpd.GeoSeries(coastline_data.geometry.unary_union)

from math import cos, sin, asin, sqrt, radians
def calc_distance(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees):
    from: https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points/4913653#4913653
    """
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) # convert decimal degrees to radians
    dlon = lon2 - lon1 
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2  haversine formula
    c = 2 * asin(sqrt(a))
    km = 6371 * c
    return km

def calc_distance_to_coastline(longitude,latitude ):
    target_coordinate=Point(longitude,latitude )
    return coastline.distance(target_coordinate).values[0]

def distance_degrees_to_kilometers(distance,coord=[0,0]):
    coord_plus=[c+distance for c in coord]
    coord_minus=[c-distance for c in coord]
    return (calc_distance(*coord,*coord_plus)+calc_distance(*coord,*coord_minus))*0.5

def calc_distance_to_coastline_km(longitude,latitude ):
    target_coordinate=Point(longitude,latitude )
    return distance_degrees_to_kilometers(coastline.distance(target_coordinate).values[0],[longitude,latitude])

据此,我计算了气象站到最近海岸线的距离,这些气象站向 IGRA 报告其大气无线电探空仪探测结果(见图)。我认为距离不是非常准确,而且经常被高估,但也许这对于您的应用程序来说仍然足够准确,这绝对适合我的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.