将Geopandas数据框直接导出为压缩的shapefile文件

10

我试图将Geopandas数据帧保存到直接写入压缩文件夹的shapefile中。

正如任何shapefile用户所知,shapefile不是单个文件,而是一组文件,应该一起读取。因此,调用 myGDF.to_file(filename='myshapefile.shp', driver='ESRI Shapefile') 不仅会创建 myshapefile.shp 文件,还会创建 myshapefile.prjmyshapefile.dbfmyshapefile.shxmyshapefile.cpg 这些文件。这可能是我在这里无法正确使用语法的原因。

例如,考虑一个虚拟的Geopandas数据帧:

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

data = pd.DataFrame({'name': ['a', 'b', 'c'],
    'property': ['foo', 'bar', 'foo'],
        'x': [173994.1578792833, 173974.1578792833, 173910.1578792833],
        'y': [444135.6032947102, 444186.6032947102, 444111.6032947102]})
geometry = [Point(xy) for xy in zip(data['x'], data['y'])]
myGDF = gpd.GeoDataFrame(data, geometry=geometry)

我看到人们在使用gzip,所以我也试了一下:


import geopandas as gpd
myGDF.to_file(filename='myshapefile.shp.gz', driver='ESRI Shapefile',compression='gzip')

但是它并没有起作用。

接着我在Google Colab环境中尝试了以下方法:

import zipfile
pathname = '/content/'
filename = 'myshapefile.shp'
zip_file = 'myshapefile.zip'
with zipfile.ZipFile(zip_file, 'w') as zipf:
   zipf.write(myGDF.to_file(filename = '/content/myshapefile.shp', driver='ESRI Shapefile'))

但它只会将.shp文件保存在一个zip文件夹中,而其余部分则写在zip文件夹旁边。

我该如何将Geopandas DataFrame直接写成压缩的shapefile?


你的 myGDF.to_file(... 方法将返回 None,而 zipf.write 的输入必须是一个字符串,所以那样永远不会起作用。我也怀疑你在那段代码中是否有任何文件被写入到 zip 文件中。 - José L. Patiño
4个回答

8

只需将文件扩展名更改为.zip,并保留驱动程序的名称:

myGDF.to_file(filename='myshapefile.shp.zip', driver='ESRI Shapefile')

这应该适用于 GDAL 3.1 或更新版本。


9
我收到了一个名为 myshapefile.zip 的(非压缩的)文件夹。GDAL 3.3.1。 - jsta
这很接近了 - 扩展名必须是.shz或.shp.zip,例如myGDF.to_file(filename='myshapefile.shp.zip')https://gdal.org/drivers/vector/shapefile.html#compressed-files - Ari
谢谢,已更新答案。 - GreatEmerald

6

对于您来说,类似这样的方法可以起作用——将shapefile(S)转储到一个新的临时目录中,然后压缩该临时目录中的所有内容。

import tempfile
import zipfile
from pathlib import Path

with tempfile.TemporaryDirectory() as temp_dir:

    temp_dir = Path(temp_dir)

    # geodataframe.to_file(str(d / "myshapefile.shp"))
    with open(temp_dir / "a.shp", "w") as _f:
        _f.write("blah")
    with open(temp_dir / "a.prj", "w") as _f:
        _f.write("blah")

    with zipfile.ZipFile('myshapefile.zip', 'w') as zipf:
        for f in temp_dir.glob("*"):
            zipf.write(f, arcname=f.name)

1
从Geopandas数据框创建一个压缩的shapefile:
import shutil
import tempfile
from pathlib import Path
#gdf = some geopandas dataframe

with tempfile.TemporaryDirectory() as temp_dir:
  temp_dir = Path(temp_dir)
  localFile = 'myshapefile'

  gdf.to_file(filename=temp_dir, driver='ESRI Shapefile')

  archiveFile = shutil.make_archive(localFile, 'zip', temp_dir)
  shutil.rmtree(temp_dir)

0

我认为我用了两行代码就让它工作了。 假设gdf是您的地理Pandas数据框。

import shutil
#this first line creates a new folder "gdf-folder" with the shape-files (unfortunatly also with the -folder in the name): gdf-folder.shp, gdf-folder.dbf...).
gdf.to_file("gdf-folder", driver="ESRI Shapefile")

#this second line compresses the files from the folder to a .zip file. named: gdf-zipfile.zip. Inside the .zip file the filenames are gdf-folder.shp; gdf-folder.dbf, ...)
shutil.make_archive("gdf-zipfile", "zip", "gdf-folder")

  • 你可能想要在代码文件名中删除-folder-zipfile。我只是试图清楚地表明每行代码中与彼此相关的内容。

  • 新文件夹gdf-folder里有gdf-folder.shp文件,仍然在你的目录中,你可以在Windows资源管理器中找到它。

  • 我认为,默认情况下,新文件夹被创建在与你的Python脚本文件相同的目录中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接