2

I'm trying to vertically concatenate multiple shapefiles that reside in one directory, into one geodataframe. They all have the same table schema and same CRS. I am trying to use the code from this Stack Overflow post, but am getting an error 'WindowsPath' object has no attribute 'split'.

from pathlib import Path   
import geopandas as gpd
import pandas as pd

folder = Path("shp")

gdf = pd.concat([
    gpd.read_file(shp)
    for shp in folder.glob("*.shp") ## error message points to this line ###
]).pipe(gpd.GeoDataFrame)

I'm new to programming and have spent more time than I care to admit trying to figure this one out. I'm open to using alternative Python modules as long as it ends with a geodataframe.

13
  • That's my answer on SO. Try print(list(shapefiles))
    – Paul H
    Apr 18, 2019 at 21:59
  • Note that folder.glob("*.shp") returns and iterator and not an iterable. The differences is that the iterator loads each element lazily and can only be looped through once. So if you're playing around in an interactive session and already consumed the iteration, I'd full expect the error that you're seeing
    – Paul H
    Apr 18, 2019 at 22:01
  • @PaulH, print(list(shapefiles)) does indeed return an empty list
    – pdavis
    Apr 18, 2019 at 22:05
  • So that means either: you've consumed the iteration already or your folder path is specified incorrectly
    – Paul H
    Apr 18, 2019 at 22:06
  • If you're in an interactive session, it might be "safer" to do: gdf = pd.concat([gpd.read_file(shp) for shp in folder.glob("*.shp")]).pipe(gpd.GeoDataFrame). That way, you'll refresh the iterator each time you run the concat command
    – Paul H
    Apr 18, 2019 at 22:08

1 Answer 1

0

I dug a little deeper into the error message and it mentioned something about . I updated the package and now my script runs. Thanks to @PaulH for his original code I found on Stack Overflow.

Your Answer

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

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