通过geopandas连接多个shapefile [英] Concat multiple shapefiles via geopandas

查看:185
本文介绍了通过geopandas连接多个shapefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过实现以下组合来合并多个shapefile:

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_files = "/Users/m3105/Downloads/area/tl_2015_{}_arealm.shp".format(i)
    gdf_list = []
    for filename in sorted(interesting_files):
        gdf_list.append(gpd.read_file((filename)))
        full_gdf = pd.concat(gdf_list)

,其中目录/Users/m3105/Downloads/area具有多个shapefile,例如tl_2015_01_arealm.shptl_2015_02_arealm.shp一直到tl_2015_56_arealm.shp.我想合并所有这些shapefile,并避免重复其头文件.但是,每当尝试使用上面的代码创建文件时,都会出现以下错误:

in which the directory /Users/m3105/Downloads/area has several shapefiles with such as tl_2015_01_arealm.shp, tl_2015_02_arealm.shp all the way up to tl_2015_56_arealm.shp. I'd like to combine all of these shapefiles and avoid repeating their headers. However, whenever I try concating the files using the code above, I get the following error:

ValueError: Null layer: u''

通常,我会知道如何将csv文件合并在一起,但是我要确保确定如何合并shapefile.非常感谢您的帮助

Normally, I'd know how to concat csv files together but I'm note sure how to concat shapefiles. I'd greatly appreciate any help

推荐答案

由于我没有您的数据,因此我无法对此进行测试,但是您想要这样的东西(假设python 3):

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')

这篇关于通过geopandas连接多个shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆