gpt4 book ai didi

amazon-s3 - 如何将 zip 文件(包含 shp)从 s3 存储桶加载到 Geopandas?

转载 作者:行者123 更新时间:2023-12-03 08:53:10 24 4
gpt4 key购买 nike

我压缩了 name.shp、name.shx、name.dbf 文件并将它们上传到 AWS s3 存储桶中。所以现在,我想加载这个 zip 文件并将其中包含的 shapefile 转换为 geopandas 的 GeoDataFrame。

如果文件是压缩的 geojson 而不是压缩的 shapefile,我可以完美地做到这一点。

import io
import boto3
import geopandas as gpd
import zipfile

cliente = boto3.client("s3", aws_access_key_id=ak, aws_secret_access_key=sk)

bucket_name = 'bucketname'
object_key = 'myfolder/locations.zip'

bytes_buffer = io.BytesIO()
cliente.download_fileobj(Bucket=bucket_name, Key=object_key, Fileobj=bytes_buffer)
geojson = bytes_buffer.getvalue()

with zipfile.ZipFile(bytes_buffer) as zi:
with zi.open("locations.shp") as file:
print(gpd.read_file(file.read().decode('ISO-8859-9')))

我收到此错误:

ç­¤íEÀ¡ËÆ3À: No such file or directory

最佳答案

基本上,geopandas 包允许直接从 S3 读取文件。正如上面的答案中提到的,它还允许读取 zip 文件。所以下面你可以看到从 s3 读取 zip 文件而不下载它的代码。您需要在开头输入zip+s3://,然后添加S3中的路径。

geopandas.read_file(f'zip+s3://bucket-name/file.zip')

关于amazon-s3 - 如何将 zip 文件(包含 shp)从 s3 存储桶加载到 Geopandas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57638545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com