簡體   English   中英

如何找到兩個地理數據框之間的距離

[英]How to find distance between two geo data frames

我想在代碼中找到兩個地理數據框之間的距離,如下所示。 下面發布的代碼提供了不同的距離值,然后我從 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)
  • 如果您想要以米或公里為單位的距離,則需要使用 UTM CRS
  • 輸出6.4 公里。 還繪制了點和多邊形以進行敏感性檢查

輸出

0    6439.754875
dtype: float64
import pandas as pd
import geopandas as gpd
import shapely.geometry, json
import plotly.express as px

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)
)

# reconstruct polygon...
gdfPoly2 = gpd.GeoSeries([shapely.geometry.Polygon(gdfPoly.geometry)], crs=crs)


# what does it look like?
px.scatter_mapbox(gdfPoint, lat="Latitude", lon="Longitude").update_layout(
    mapbox={
        "style": "carto-positron",
        "layers":[{"source": json.loads(gdfPoly2.to_json()), "type":"fill"}]
    }
).show()

# finally distance, taking into account CRS in metres
gdfPoint.to_crs(gdfPoint.estimate_utm_crs()).distance(
    gdfPoly2.to_crs(gdfPoly2.estimate_utm_crs())
)

在此處輸入圖片說明

代碼中缺少以下內容:

    gdfPoly2 = gpd.GeoSeries([shapely.geometry.Polygon(gdfPoly.geometry)], crs=crs)

代碼

crs = {"init": "epsg:25832"}
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": [
            1003060.89945681,
            1003170.95198635,
            1003781.73122161,
            1003694.55868486,
            1003060.89945681
        ],
        "Longitude": [
            5622486.93624152,
            5622079.52632924,
            5622126.00418918,
            5622444.73987453,
            5622486.93624152
        ],
    }
)
gdfPoly = gpd.GeoDataFrame(
    df, crs=crs, geometry=gpd.points_from_xy(df.Longitude, df.Latitude)
)
df2 = pd.DataFrame({"Latitude": [1003010.44704936], "Longitude": [5622524.15909579]})
gdfPoint = gpd.GeoDataFrame(
    df2, crs=crs, geometry=gpd.points_from_xy(df2.Longitude, df2.Latitude)
)

gdfPoly2 = gpd.GeoSeries([shapely.geometry.Polygon(gdfPoly.geometry)], crs=crs)

dist = gdfPoint.to_crs(25832).distance(
    gdfPoly2.to_crs(25832)
)
print("dist: {0}".format(dist))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM