如何将GeodataFrame下载到我的本地驱动器上,从Google Colab下载?

发布于 2025-01-20 15:11:31 字数 279 浏览 5 评论 0原文

我对 SHAPEFiles 和 GeoDataFrames 相当陌生,所以如果这是一个非常基本的问题,我深表歉意。

我在 Mac 上的 Google Chrome 中使用 Google Colab。 Python 版本 3.xx

我有一个现有的 Esri SHAPEFile,其中包含多边形和多多边形的 CRS 点(纬度-经度)。

我的目标是找到这些多边形和多多边形的质心,并将质心添加为现有 SHAPE 文件中的新列,然后在本地保存文件。

如何将文件保存到本地?我使用什么格式? 谢谢。

I am fairly new to SHAPEFiles and GeoDataFrames so apologies if this is a very basic question.

I am using Google Colab in Google Chrome on a Mac. Python version 3.xx

I have an existing Esri SHAPEFile that has CRS points (latitiude-longitude) of Polygons and MultiPolygons.

My objective was to find the centroid of these Polygons and MultiPolygons and add the centroids as a new column in the existing SHAPEfile and then save the file locally.

How do I save the file locally? What format do I use?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2025-01-27 15:11:31
import geopandas as gpd

df = gpd.read_file("polygons.shp")

df["geometry"] = df["geometry"].centroid  # assuming a projection CRS is used

df.to_file("centroids.shp", driver="Shapefile")

您需要刷新 Google Colabs 中的工作目录才能查看 shapefile。确保您也下载了所有关联的文件。

注意:您不能在一个 shapefile 中存储多个几何图形。

该脚本使用几何形状的质心创建一个新的 shapefile,但也保留所有其他列。这样您就能够关联两个数据集。


以下是一个可能的工作流程:

  1. 创建一个新的 Jupyter 笔记本或打开现有的笔记本
  2. 创建一个包含形状文件的 zip 文件包括所有关联文件
  3. 上传将其复制到您的 Colabs 工作目录
    • 我在示例中将文件命名为 polygons.zip
  4. 在单元格中运行以下代码:
!pip install geopandas

import os
import tempfile
import zipfile
import glob

import geopandas as gpd

OUTPUT_SHAPE_FILE = "centroids.shp"
SHAPE_FILE_ZIP = "polygons.zip"

with tempfile.TemporaryDirectory() as wd:

    with zipfile.ZipFile(SHAPE_FILE_ZIP, "r") as shape_file_zip:
        shape_file_zip.extractall(wd)

    shape_file = os.path.join(wd, SHAPE_FILE_ZIP.replace(".zip", ".shp"))

    df = gpd.read_file(shape_file)

    df["geometry"] = df["geometry"].centroid  # assuming a projection CRS is used

    df.to_file(os.path.join(wd, OUTPUT_SHAPE_FILE), driver="Shapefile")

    shape_file_name = OUTPUT_SHAPE_FILE.replace(".shp", "")

    with zipfile.ZipFile(f"{shape_file_name}.zip", "w") as output_file:

        for file in glob.iglob(f"{os.path.join(wd, shape_file_name)}.*"):
            output_file.write(file, os.path.basename(file))
  1. 刷新 Google Colabs 上的工作目录,您将看到 centroids.zip
  2. 下载文件 (将鼠标悬停在文件上时单击三个点)

PS。对于这种情况,我建议使用本地 Jupyter 笔记本和 Python 环境。

import geopandas as gpd

df = gpd.read_file("polygons.shp")

df["geometry"] = df["geometry"].centroid  # assuming a projection CRS is used

df.to_file("centroids.shp", driver="Shapefile")

You'll need to refresh your working directory in Google Colabs to see the shapefile. Make sure you download all associated files as well.

Note: You cannot store more than one geometry in a shapefile.

The script creates a new shapefile using the centroids for the geometries but also keeping all other columns. That way you will be able to relate both datasets.


Here is a possible workflow:

  1. Create a new Jupyter notebook or open an existing one
  2. Create a zip file containing your shape file including all associated files
  3. Upload it to your Colabs working directory
    • I call the file polygons.zip in my example
  4. Run following code in a cell:
!pip install geopandas

import os
import tempfile
import zipfile
import glob

import geopandas as gpd

OUTPUT_SHAPE_FILE = "centroids.shp"
SHAPE_FILE_ZIP = "polygons.zip"

with tempfile.TemporaryDirectory() as wd:

    with zipfile.ZipFile(SHAPE_FILE_ZIP, "r") as shape_file_zip:
        shape_file_zip.extractall(wd)

    shape_file = os.path.join(wd, SHAPE_FILE_ZIP.replace(".zip", ".shp"))

    df = gpd.read_file(shape_file)

    df["geometry"] = df["geometry"].centroid  # assuming a projection CRS is used

    df.to_file(os.path.join(wd, OUTPUT_SHAPE_FILE), driver="Shapefile")

    shape_file_name = OUTPUT_SHAPE_FILE.replace(".shp", "")

    with zipfile.ZipFile(f"{shape_file_name}.zip", "w") as output_file:

        for file in glob.iglob(f"{os.path.join(wd, shape_file_name)}.*"):
            output_file.write(file, os.path.basename(file))
  1. Refresh your working directory on Google Colabs, you'll see centroids.zip
  2. Download the file (click on the three dots when hovering over the file)

PS. I would recommend to use a local Jupyter notebook and Python environment for such situations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文