I have 2 shape files one "country.shp" (containing the tiles geometry covering the whole country) & other "district.shp"(containing one particular district). Now I want to get the tiles information getting intersected with the district polygon (similar to search by location in ArcGIS) and the final intersected geodataframe should contain the attributes of "country.shp" (containing only intersected rows from "country.shp"). Up till now I have used the following code but it is working fine only for some district shape files (projections are same for both the shapefiles). Can someone help me what is wrong with the following code or suggest some other alternatives?

import geopandas as gpd

f1 = "/home/geo/country.shp"
f2 = "/home/geo/district.shp"
data1 = gpd.read_file(f1)
data2 = gpd.read_file(f2)

import shapely.speedups

pip_mask = data1.within(data2.loc[0, 'geometry']) 

pip_data= data1.loc[pip_mask]

  • Hi! I have just posted a solution but it is more a guess. Could you update your question with the error messages you are getting? – ramiroaznar Jun 12 '20 at 10:40

Try using the squeeze method in order to extract the Shapely geometry:

pip_mask = data1.within(data2.loc[0, 'geometry'].squeeze())
  • I tried your suggested solution but I got an Attribute error: 'Polygon object has no attribute squeeze'. Basically my query is I have a geodataframe of country containing multiple polygon geometries (say 30 in nos.) and other geodataframe containing multiple point geometries (say 250000 in nos. some of which would lie outside my country boundary) and now I want to know how many point geometries are lying within the country shapefile (containing multiple polygon geometries) in a faster way (similar to search by location feature as in ArcGIS). – RRSC NGP Jun 12 '20 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.