问题
I have a shapefile of London with over 4000 unique polygons representing lsoa areas. I have also created a geodataframe of points representing sensors in the city. I need to work out which lsoa area (ie which polygon) each sensor belongs to/is within. So ideally I would have a list of each sesnsor_id_location and their respective lsoa number (LSOA11DC).
My points look like this:
pod_id_location Borough Latitude Longitude geometry
0 1245 Barnet 51.604486 -0.206551 POINT (-0.20655 51.60449)
1 2245 Camden 51.521880 -0.120434 POINT (-0.12043 51.52188)
2 3245 Camden 51.555485 -0.152338 POINT (-0.15234 51.55548)
3 5245 Wandsworth 51.440399 -0.186775 POINT (-0.18677 51.44040)
4 6245 Hounslow 51.468625 -0.359770 POINT (-0.35977 51.46863)
and my london shape file looks like this:
LSOA11CD LSOA11NM geometry
0 E01000001 City of London 001A POLYGON ((-0.09729 51.52158, -0.09652 51.52027...
1 E01000002 City of London 001B POLYGON ((-0.08813 51.51941, -0.08929 51.51752...
2 E01000003 City of London 001C POLYGON ((-0.09679 51.52325, -0.09647 51.52282...
3 E01000005 City of London 001E POLYGON ((-0.07323 51.51000, -0.07553 51.50974...
4 E01000006 Barking and Dagenham 016A POLYGON ((0.09115 51.53909, 0.09326 51.53787, ...
Visually my data looks like this:
Thanks for the help.
回答1:
I have found a very simple solution using a spatial join as follows:
merge = gpd.sjoin(map_df, sensors, how="right", op='contains')
index_left LSOA11CD LSOA11NM pod_id_location Borough Latitude Longitude geometry
index_right
0 199 E01000204 Barnet 025C 1245 Barnet 51.604486 -0.206551 POINT (-0.20655 51.60449)
1 897 E01000915 Camden 027A 2245 Camden 51.521880 -0.120434 POINT (-0.12043 51.52188)
来源:https://stackoverflow.com/questions/60136349/return-list-of-points-within-polygon-geopandas