-1

I have a shapefile of Indian districts:

full_state_df = gpd.read_file("kerala/shapefiles/district.shp")
print (full_state_df.head())

Result:

    DISTRICT  ...                                           geometry
0  Alappuzha  ...  POLYGON ((76.37334 9.83565, 76.37955 9.82888, ...
1  Ernakulam  ...  POLYGON ((76.68924 10.26721, 76.68724 10.26170...
2     Idukki  ...  POLYGON ((77.28895 10.22973, 77.29462 10.21643...
3     Kannur  ...  POLYGON ((75.46997 12.30049, 75.48558 12.29131...
4  Kasaragod  ...  POLYGON ((75.41667 12.50166, 75.42240 12.48463...

I have a csv file which lists cellphone towers.

radio,mcc,net,area,cell,unit,lon,lat,range,samples,changeable,created,updated,averageSignal
GSM,405,67,5021,21106,0,87.192306518555,22.334518432617,1000,1,1,1459748781,1459748781,0
GSM,405,752,7302,57151,0,86.663589477539,22.370223999023,1000,1,1,1459748770,1459748770,0
GSM,405,852,51,3066,0,77.056045532227,11.272659301758,1000,1,1,1459682666,1459682666,0
...

I want to filter out lines for points that lie within a specified district. Say, for example, I select Alappuzha:

patch_df = full_state_df.loc[full_state_df['DISTRICT'] == 'Alappuzha']

...and get:

    DISTRICT  ...                                           geometry
0  Alappuzha  ...  POLYGON ((76.37334 9.83565, 76.37955 9.82888, ...

How do I 1) extract the POLYGON and test if a lat-long lies in that?

1 Answers1

3

IIUC, you can do this if you use geodataframes for your points and polygons. The points_in_polygon_gdf will contain the points that are within each district.

points_in_polygons_gdf = gpd.sjoin(points_gdf, polygons_gdf, op='within', how='inner')
Matthew Borish
  • 3,016
  • 2
  • 13
  • 25