如何计算两个地理数据帧之间的距离

2024-09-28 03:17:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找出两个地理数据帧之间的距离,如下代码所示。 下面发布的代码提供了与我从postgis数据库获得的不同的距离值。通过在qgis上可视化距离,从数据库postgis获得的距离比我从下面发布的代码获得的距离更真实、更可靠。 请让我知道如何正确找到两个地理数据帧之间的距离

代码

crs = {'init': 'epsg:4326'}
df = pd.DataFrame({
    #  Polygon([(4.338074,50.848677), (4.344961,50.833264), (4.366227,50.840809), (4.367945,50.852455), (4.346693,50.858306)])
     'Latitude': [ 51.08423775429969, 51.08158745820981, 51.08233499299334, 51.08440763989611,51.08423775429969],
     'Longitude': [6.741879696309871, 6.742907378503366, 6.746964018740842, 6.746152690693346, 6.741879696309871]

 })
 gdfPoly = gpd.GeoDataFrame(df, crs=crs,geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
 df2 = pd.DataFrame({
     'Latitude': [51.13297272331544],
     'Longitude': [6.692247842659104]
 })
 gdfPoint = gpd.GeoDataFrame(df2, crs=crs,geometry=gpd.points_from_xy(df2.Longitude, df2.Latitude))
print("{0}".format(gdfPoint.distance(gdfPoly)))

更新结果

queryPostgreSQLForDistancesFromPointsToPolygon:14.9634941931567
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.6916900697392245 51.132691895123486)
buildDataFrameFromPointAsWKTFor.longitudes:[6.6916900697392245]
buildDataFrameFromPointAsWKTFor.latitudes:[51.132691895123486]
dist:0    84.684914
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:0.0
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.69226128514392 51.13270317349424)
buildDataFrameFromPointAsWKTFor.longitudes:[6.69226128514392]
buildDataFrameFromPointAsWKTFor.latitudes:[51.13270317349424]
dist:0    51.996894
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:0.0
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.69240408905273 51.13270599265131)
buildDataFrameFromPointAsWKTFor.longitudes:[6.69240408905273]
buildDataFrameFromPointAsWKTFor.latitudes:[51.13270599265131]
dist:0    45.68055
1         NaN
2         NaN
3         NaN
4         NaN
5         NaN
6         NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:27.9534455951444
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.691551748111223 51.13259922519638)
buildDataFrameFromPointAsWKTFor.longitudes:[6.691551748111223]
buildDataFrameFromPointAsWKTFor.latitudes:[51.13259922519638]
dist:0    90.195441
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:19.7325568769629
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.691694551628079 51.13260204521571)
buildDataFrameFromPointAsWKTFor.longitudes:[6.691694551628079]
buildDataFrameFromPointAsWKTFor.latitudes:[51.13260204521571]
dist:0    80.7357
1        NaN
2        NaN
3        NaN
4        NaN
5        NaN
6        NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:0.0
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.692265765926083 51.1326133235505)
buildDataFrameFromPointAsWKTFor.longitudes:[6.692265765926083]
buildDataFrameFromPointAsWKTFor.latitudes:[51.1326133235505]
dist:0    45.281294
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
dtype: float64
queryPostgreSQLForDistancesFromPointsToPolygon:0.0
polygonAsWKT:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
buildDataFrameFromPolygonAsWKTFor.longitudes:[6.692790084436616, 6.6918971115756305, 6.6922145189906725, 6.6926758177672, 6.692650425173997, 6.692430356032901, 6.692790084436616]
buildDataFrameFromPolygonAsWKTFor.latitudes:[51.13237486727857, 51.132725423664596, 51.13301489625002, 51.13291397940796, 51.1327121450621, 51.132520932762816, 51.13237486727857]
pointAsWKT:POINT(6.692408569558222 51.132616142698566)
buildDataFrameFromPointAsWKTFor.longitudes:[6.692408569558222]
buildDataFrameFromPointAsWKTFor.latitudes:[51.132616142698566]
dist:0    37.860682
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
dtype: float64

更新-1

对于下面发布的代码,我收到了错误消息:

AttributeError: 'GeoDataFrame' object has no attribute 'estimate_utm_crs'

Geopandas版本为“0.10.2”

import pandas as pd
import geopandas as gpd
import shapely.geometry, json
import shapely.wkt

crs = {"init": "epsg:4326"}
df = pd.DataFrame(
    {
        #  Polygon([(4.338074,50.848677), (4.344961,50.833264), (4.366227,50.840809), (4.367945,50.852455), (4.346693,50.858306)])
        "Latitude": [
            51.13237486727857,
            51.132725423664596,
            51.13301489625002,
            51.13291397940796,
            51.1327121450621,
            51.132520932762816,
            51.13237486727857
            
        ],
        "Longitude": [
            6.692790084436616,
            6.6918971115756305,
            6.6922145189906725,
            6.6926758177672,
            6.692650425173997,
            6.692430356032901,
            6.692790084436616
        ],
    }
)
gdfPoly = gpd.GeoDataFrame(
    df, crs=crs, geometry=gpd.points_from_xy(df.Longitude, df.Latitude)
)
df2 = pd.DataFrame({"Latitude": [51.133239453736344], "Longitude": [6.692091594420977]})
gdfPoint = gpd.GeoDataFrame(
    df2, crs=crs, geometry=gpd.points_from_xy(df2.Longitude, df2.Latitude)
)

# finally distance, taking into account CRS in metres
dist = gdfPoint.to_crs(gdfPoint.estimate_utm_crs()).distance(
    gdfPoly.to_crs(gdfPoly.estimate_utm_crs())
)
print(dist)

更新-1

对于以下多边形和点

POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))
POINT(6.692091594420977 51.133239453736344)

距离应为:26.4203162027475 但我得到107.89036

更新-2

以下代码提供的距离等于107.890471。当我在联机wkt查看器中将多边形和点i视为wkt时,该点非常接近多边形 请让我知道是什么原因导致了远程阅读的错误

rs = {"init": "epsg:4326"}
df = pd.DataFrame(
    {
        #  Polygon([(4.338074,50.848677), (4.344961,50.833264), (4.366227,50.840809), (4.367945,50.852455), (4.346693,50.858306)])
        "Latitude": [
            51.13237486727857,
            51.132725423664596,
            51.13301489625002,
            51.13291397940796,
            51.1327121450621,
            51.132520932762816,
            51.13237486727857
            
        ],
        "Longitude": [
            6.692790084436616,
            6.6918971115756305,
            6.6922145189906725,
            6.6926758177672,
            6.692650425173997,
            6.692430356032901,
            6.692790084436616
        ],
    }
)
gdfPoly = gpd.GeoDataFrame(
    df, crs=crs, geometry=gpd.points_from_xy(df.Longitude, df.Latitude)
)
df2 = pd.DataFrame({"Latitude": [51.133239453736344], "Longitude": [6.692091594420977]})
gdfPoint = gpd.GeoDataFrame(
    df2, crs=crs, geometry=gpd.points_from_xy(df2.Longitude, df2.Latitude)
)

# finally distance, taking into account CRS in metres
dist = gdfPoint.to_crs(32632).distance(
    gdfPoly.to_crs(25832)
)
print(dist)

Tags: 距离dfdistnanpointdf2polygonlatitude

热门问题