I want to calculate the distance from each point of dataframe geosearch_crs to the polygons in the gelb_crs dataframe, returning only the minimum distance.
I have tried this code:
for i in range(len(geosearch_crs)):
point = geosearch_crs['geometry'].iloc[i]
for j in range(len(gelb_crs)):
poly = gelb_crs['geometry'].iloc[j]
print(point.distance(poly).min())
it returns this error:
AttributeError: 'float' object has no attribute 'min'
I somehow don't get how to return what i want, the points.distance(poly).min() function should work though.
This is part of the data frames (around 180000 entries): geosearch_crs:
count | geometry |
---|---|
12 | POINT (6.92334 50.91695) |
524 | POINT (6.91970 50.93167) |
5 | POINT (6.96946 50.91469) |
gelb_crs (35 entries):
name | geometry |
---|---|
Polygon 1 | POLYGON Z ((6.95712 50.92851 0.00000, 6.95772... |
Polygon 2 | POLYGON Z ((6.91896 50.92094 0.00000, 6.92211... |
I'm not sure about the 'distance' method, but maybe you could try adding the distances to a list:
distances = list()
for i in geosearch_crs:
for j in gelb_crs:
distances.append(i.distance(j))
print(min(distances))
sjoin_nearest()
is simple way to find nearest polygon and get distance. Have use UTM CRS so that distance is in meters rather than degreesimport geopandas as gpd
import pandas as pd
import shapely
import io
df = pd.read_csv(
io.StringIO(
"""count,geometry
12,POINT (6.92334 50.91695)
524,POINT (6.91970 50.93167)
5,POINT (6.96946 50.91469)"""
)
)
geosearch_crs = gpd.GeoDataFrame(
df, geometry=df["geometry"].apply(shapely.wkt.loads), crs="epsg:4326"
)
# generated as sample in question unusable
df = pd.read_csv(
io.StringIO(
'''name,geometry
Polygon 1,"POLYGON ((6.9176561 50.8949742, 6.9171649 50.8951417, 6.9156967 50.8957149, 6.9111788 50.897751, 6.9100077 50.8989409, 6.9101989 50.8991319, 6.9120049 50.9009167, 6.9190374 50.9078591, 6.9258157 50.9143227, 6.9258714 50.9143691, 6.9259546 50.9144355, 6.9273598 50.915413, 6.9325715 50.9136438, 6.9331018 50.9134553, 6.9331452 50.9134397, 6.9255391 50.9018725, 6.922309 50.8988869, 6.9176561 50.8949742))"
Polygon 2,"POLYGON ((6.9044955 50.9340428, 6.8894236 50.9344297, 6.8829359 50.9375553, 6.8862995 50.9409307, 6.889446 50.9423764, 6.9038401 50.9436598, 6.909518 50.9383374, 6.908634 50.9369064, 6.9046363 50.9340648, 6.9045721 50.9340431, 6.9044955 50.9340428))"'''
)
)
gelb_crs = gpd.GeoDataFrame(
df, geometry=df["geometry"].apply(shapely.wkt.loads), crs="epsg:4326"
)
geosearch_crs.to_crs(geosearch_crs.estimate_utm_crs()).sjoin_nearest(
gelb_crs.to_crs(geosearch_crs.estimate_utm_crs()), distance_col="distance"
)
count | geometry | index_right | name | distance | |
---|---|---|---|---|---|
0 | 12 | POINT (354028.1446652143 5642643.287732874) | 0 | Polygon 1 | 324.158 |
2 | 5 | POINT (357262.7994182631 5642301.777981625) | 0 | Polygon 1 | 2557.33 |
1 | 524 | POINT (353818.4585403281 5644287.172541857) | 1 | Polygon 2 | 971.712 |
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.