Concat multiple shapefiles via geopandas

后端 未结 2 713
悲&欢浪女
悲&欢浪女 2021-02-08 23:04

I\'m trying to combine multiple shapefiles by implementing the follwing:

import geopandas as gpd
import pandas as pd

for i in range(10,56):
    interesting_file         


        
相关标签:
2条回答
  • 2021-02-08 23:31

    If using pandas.concat like the answer of @Paul H, some geographical imformation such as coordinate reference system(crs) does not get preserved by default. But it worked when using the way like below:

    import os
    import geopandas as gpd
    
    file = os.listdir("Your folder")
    path = [os.path.join("Your folder", i) for i in file if ".shp" in i]
    
    gdf = gpd.GeoDataFrame(pd.concat([gpd.read_file(i) for i in path], 
                            ignore_index=True), crs=gpd.read_file(path[0]).crs)
    

    In this way, the geodataframe will have CRS as your need

    0 讨论(0)
  • 2021-02-08 23:39

    I can't test this since I don't have your data, but you want something like this (assuming python 3):

    from pathlib import Path
    import pandas
    import geopandas
    
    folder = Path("/Users/m3105/Downloads/area")
    shapefiles = folder.glob("tl_2015_*_arealm.shp")
    gdf = pandas.concat([
        geopandas.read_file(shp)
        for shp in shapefiles
    ]).pipe(geopandas.GeoDataFrame)
    gdf.to_file(folder / 'compiled.shp')
    
    0 讨论(0)
提交回复
热议问题