如何在地瓦丹斯出口一系列功能集?

发布于 2025-01-22 14:58:03 字数 547 浏览 2 评论 0原文

我正在尝试使用地geopandas将特征收集的数组导出到Shapefiles。阵列结构如下:

“特征集合的数组”

我希望每个功能集合都以自己的shapefile导出,我是我通过在geopandas中进行以下操作(通过使用简单的Geojson文件)来完成:

gdf.to_file(filePath)

上面的任何Geojson(或在这种情况下,在这种情况下是单个功能集合),我将其交给它&创建一个shapefile。问题是,我希望能够一次(迭代)一次导出多个功能集。

我还能够同时导出任何数量的Shapefiles:

for num, row in gdf.iterrows():
gdf.iloc[num:num+1,:].to_file(path)

问题是,上述代码仅适用于功能集合(不在功能集合中)。 Geopandas根本不认识一系列特征集合的格式。有人找到了解决方法吗?

I'm attempting to use geopandas to export an array of feature collections to shapefiles. The array structure is as follows:

array of feature collections

I want each feature collection to be exported as it's own shapefile, which I have been able to accomplish by doing the following in geopandas (by using a simple geojson file):

gdf.to_file(filePath)

The above takes any geojson (or in this case a single feature collection) that I hand it & creates a shapefile. The issue is, I want to be able to export multiple feature collections at once (iteratively).

I have also been able to export any number of shapefiles at once with the following:

for num, row in gdf.iterrows():
gdf.iloc[num:num+1,:].to_file(path)

The problem is, the above code only works on a feature collection of features (not on an array of feature collections). Geopandas does not recognize the format of an array of feature collections at all. Has anyone found a workaround for this?

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

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

发布评论

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

评论(1

氛圍 2025-01-29 14:58:03
  • 在答案中,数据作为图像不可用。已经构造了从中构建了一系列功能集合,
  • 可以简单地在此数组上迭代,每个功能集合创建一个形状文件
# create a shapefile for each feature collection in array
for fn, feature in enumerate(fca):
    f = Path.cwd().joinpath(f"{fn}/{fn}.shp")
    if not f.parent.exists(): f.parent.mkdir()
    gpd.GeoDataFrame.from_features(feature, crs="epsg:4386").to_file(f)

完整代码

import numpy as np
import geopandas as gpd
from pathlib import Path

tb = (
    gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    .loc[lambda d: d.iso_a3.eq("BEL")]
    .total_bounds
)
x, y = np.linspace(*tb[[0, 2]], 100), np.linspace(*tb[[1, 3]], 100)

# construct an array of feature collections
fca = [
    {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {"identifier": f"{i}_{j}"},
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        np.random.choice(x, 1)[0],
                        np.random.choice(y, 1)[0],
                    ],
                },
            }
            for j in range(5)
        ],
    }
    for i in range(3)
]

# create a shapefile for each feature collection in array
for fn, feature in enumerate(fca):
    f = Path.cwd().joinpath(f"{fn}/{fn}.shp")
    if not f.parent.exists(): f.parent.mkdir()
    gpd.GeoDataFrame.from_features(feature, crs="epsg:4386").to_file(f)

  • data as an image is not usable in an answer. Have constructed an array of feature collections
  • from this it is simple to iterate over this array, creating a shape file per feature collection
# create a shapefile for each feature collection in array
for fn, feature in enumerate(fca):
    f = Path.cwd().joinpath(f"{fn}/{fn}.shp")
    if not f.parent.exists(): f.parent.mkdir()
    gpd.GeoDataFrame.from_features(feature, crs="epsg:4386").to_file(f)

full code

import numpy as np
import geopandas as gpd
from pathlib import Path

tb = (
    gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    .loc[lambda d: d.iso_a3.eq("BEL")]
    .total_bounds
)
x, y = np.linspace(*tb[[0, 2]], 100), np.linspace(*tb[[1, 3]], 100)

# construct an array of feature collections
fca = [
    {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {"identifier": f"{i}_{j}"},
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        np.random.choice(x, 1)[0],
                        np.random.choice(y, 1)[0],
                    ],
                },
            }
            for j in range(5)
        ],
    }
    for i in range(3)
]

# create a shapefile for each feature collection in array
for fn, feature in enumerate(fca):
    f = Path.cwd().joinpath(f"{fn}/{fn}.shp")
    if not f.parent.exists(): f.parent.mkdir()
    gpd.GeoDataFrame.from_features(feature, crs="epsg:4386").to_file(f)

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