简体   繁体   中英

How to find distance between two geo data frames

i would like to find the distance between two geodata-frames as shown below in the code. the code posted below provide different distances values then those i get from postgis database.by visualising distances on qgis, the distances from the database postgis is more realistic and reliable than the ones i get from the below posted code. please let me know how to correctly find the distance between two geodata-frames.

code

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

updated results :

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

update-1

for the below posted code, i get the error message:

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

Geopandas version is '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)

update-1

for the following polygon and point

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)

the distance should be:26.4203162027475 but i get 107.89036

update-2

the following code provide distance equal to 107.890471.when i visualised both of the polygon and the point i as wkt in online wkt viewer, the point is so close to the polygon please let me know what causes this error in distance reading.

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)
  • if you want a distance in metres or km, you need to use a UTM CRS
  • see output 6.4 km. Have also plotted point and polygon for sensibility check

output

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

在此处输入图片说明

this following was missing in the code:

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

code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM