3

I have a folder full of shapefiles, I would like to read them into geopandas and then clip them all to one bounding box iteratively.

However, I'm stuck on the first part which is reading each shapefile into geopandas as its own geodataframe.

Could anyone advise?

import glob
import geopandas as gpd
for filepath in glob.iglob('E:/folder/*.shp'):
    gpd.read_file(filepath)

1 Answer 1

8

You can store each GeoDataFrame in a list or a generator to avoid any memory issue.

import glob
import geopandas as gpd

files = glob.iglob('E:/folder/*.shp')
gdfs = (gpd.read_file(file) for file in files) # generator

# A list is an option for small files
# gdfs = [gpd.read_file(file) for file in files]

for gdf in gdfs:
    # clip stuffs

Or

import glob
import geopandas as gpd

for filepath in glob.iglob('E:/folder/*.shp'):
    gdf = gpd.read_file(filepath)

    # clip stuff with gdf

    # save the clip result
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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