根据名称组合不同的 shapefile 要素类

问题描述 投票:0回答:1

我有一个 shapefile,其中包含我可以读取的各种要素类

import geopandas as gpd

vec_data = gpd.read_file("map.shp")
vec_data.head()
vec_data['DESCR_ENG'].unique()

我得到了以下结果:

array(['Meadow (Half Sheed Tara 20%)', 'Meadow (permanent meadow)',
       'Forest', 'Infrastructures', 'Meadow special area',
       'Meadow special area/S28-1 poor meadows and fen meadows',
       'Meadow (permanent meadow)/S28-1 poor meadows and fen meadows',
       'Pasture', 'Hedges',
       'Meadow special area/S28-2 species-rich mountain meadows',
       'Waterbody', 'Other Areas',
       'Meadow (permanent meadow)/S28-2 species-rich mountain meadows',
       'Grain',
       'Meadow (half-sheared)/S28-1 poor meadows and fen meadows',
       'Berry fruit (without strawberry)', 'Alpe (without tare)', 'Apple',
       'Alpe (stocked 20%)', 'Alpe (stocked 50%)',
       'Field vegetable cultivation', 'Meadow (half-sheared)',
       'Willow (Tare 20%)', 'Viticulture', 'Corn', 'Meadow (Permanent Meadow Tara 20%)', 'Alpe (tare 70%)',     
       'Pasture (tare 20%)/S28-6 Wooded pastures',
       'Pasture (tare 50%)/S28-6 Wooded pastures',
       'Lawn special area (tare 20%)', 'Apricot',
       'Meadow special area (tare 20%)/S28-4 Meadows rich in wooded species',
       'Meadow special area/S28-4 Species-rich meadows with trees']

例如,我想将所有“Meadow*”要素类折叠到一个名为“Agriculture”的独特类中,并将所有“Pasture*”类折叠到另一个名为“Cultivated”的类中。 完成此步骤后,我想将结果保存为新的形状文件。 有什么建议吗?

python shapefile
1个回答
0
投票

您可以在 GeoDataFrame 上使用标准 pandas 功能。

示例:

import geopandas as gpd
from shapely import box

# vec_data = gpd.read_file("map.shp")
vec_data = gpd.GeoDataFrame(
    data={"DESCR_ENG": [
        "Meadow (Half Sheed Tara 20%)",
        "Meadow (permanent meadow)",
        "Pasture (tare 20%)/S28-6 Wooded pastures",
        'Grain',
    ]},
    geometry=[box(0, 0, 5, 5), box(5, 0, 10, 5), box(10, 0, 15, 5), box(15, 0, 20, 5)],
    crs=31370,
)

# Add new "Classes" column based on DESCR_ENG column
vec_data["Classes"] = None
vec_data.loc[vec_data["DESCR_ENG"].str.startswith("Meadow"), "Classes"] = "Agriculture"
vec_data.loc[vec_data["DESCR_ENG"].str.startswith("Pasture"), "Classes"] = "Cultivated"

print(vec_data.head())

结果:

                                  DESCR_ENG  ...      Classes
0              Meadow (Half Sheed Tara 20%)  ...  Agriculture
1                 Meadow (permanent meadow)  ...  Agriculture
2  Pasture (tare 20%)/S28-6 Wooded pastures  ...   Cultivated
3                                     Grain  ...         None
© www.soinside.com 2019 - 2024. All rights reserved.