Объединять различные классы объектов шейп-файлов на основе их имен и геометрииPython

Программы на Python
Ответить
Anonymous
 Объединять различные классы объектов шейп-файлов на основе их имен и геометрии

Сообщение Anonymous »

У меня есть шейп-файл, содержащий различные классы объектов, которые я могу прочитать.
Я улучшил свой код благодаря предложениям @Pieter

Код: Выделить всё

import geopandas as gpd
from shapely import box
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon

shapefile = "LAFIS.shp"
vect_data = gpd.read_file(shapefile)

vec_data.head()
vec_data['DESCR_ENG'].unique()
geometry_arr = (vec_data['geometry'])
descr_name_arr = (vec_data['DESCR_ENG'])
d = {
"BW_name": descr_name_arr,
"geometry": geometry_arr,
}
gdf = gpd.GeoDataFrame(d, crs="EPSG:25832")
gdf

test_vector_data = gpd.GeoDataFrame(
data={"DESCR_ENG":  [
"Cultivated Grassland,Alpe (without tare)",
"Cultivated Grassland,Biennial cut meadow (tare 20%)/S28-1 Dry meadows and low bog meadows",
"Cultivated Grassland,Biennial cut meadow (tare 20%)/S28-2 Species-rich mountain meadows",
"Cultivated Grassland,Biennial cut meadow/S28-2 Species-rich mountain meadows",
"Cultivated Grassland,Lawn special area (tare 20%)",
"Cultivated Grassland,Lawn special area (tare 50%)",
"Cultivated Grassland,Meadow (Half Sheared Tara 20%)",
"Cultivated Grassland,Meadow (half-sheared)",
"Cultivated Grassland,Meadow (half-sheared)/S28-1 poor meadows and fen meadows",
"Cultivated Grassland,Mixed Alternate Meadow",
"Cultivated Grassland,Pasture",
"Cultivated Grassland,Pasture (rock 20%)",
"Cultivated Grassland,Potential pasture (tare 20%)",
"Fallow,Arable land fallow -  E F A & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , A l p e   ( s t o c k e d   2 0 % ) & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , A l p e   ( s t o c k e d   5 0 % ) & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , A l p e g g i o   ( w i t h o u t   t a r e s ) / S 2 8 - 6   W o o d e d   p a s t u r e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , B i e n n i a l   c u t   m e a d o w   ( t a r e   2 0 % ) / S 2 8 - 4   M e a d o w s   r i c h   i n   t r e e   s p e c i e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , B i e n n i a l   c u t   m e a d o w / S 2 8 - 4   S p e c i e s - r i c h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , B i e n n i a l   c u t   m e a d o w / S 2 8 - 5   L u s h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a   ( t a r e   2 0 % ) / S 2 8 - 4   M e a d o w s   r i c h   i n   w o o d e d   s p e c i e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a   ( t a r e   2 0 % ) / S 2 8 - 5   L u s h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a   ( t a r e   5 0 % ) / S 2 8 - 4   M e a d o w s   r i c h   i n   w o o d e d   s p e c i e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a   ( t a r e   5 0 % ) / S 2 8 - 5   L u s h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a / S 2 8 - 4   S p e c i e s - r i c h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , M e a d o w   s p e c i a l   a r e a / S 2 8 - 5   L u s h   m e a d o w s   w i t h   t r e e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , P a s t u r e   ( r o c k   2 0 % ) / S 2 8 - 6   W o o d e d   p a s t u r e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , P a s t u r e   ( r o c k   5 0 % ) / S 2 8 - 6   W o o d e d   p a s t u r e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , P a s t u r e   ( t a r e   2 0 % ) / S 2 8 - 6   W o o d e d   p a s t u r e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , P a s t u r e   ( t a r e   5 0 % ) / S 2 8 - 6   W o o d e d   p a s t u r e s & q u o t ; , < b r   / >                 & q u o t ; F o r e s t   T r e e s   /   S R F , P a s t u r e   ( t r e e s   2 0 % ) / S 2 8 - 6   Wooded pastures",
"Forest Trees / SRF,Pasture/S28-6 Wooded pastures",
"Forest Trees / SRF,Stable meadow (tare 20%)/S28-4 Meadows rich in wooded species",
"Forest Trees / SRF,Stable meadow (tare 20%)/S28-5 Lush meadows with trees",
"Forest Trees / SRF,Stable meadow/S28-4 Species-rich meadows with trees",
"Forest Trees / SRF,Stable meadow/S28-5 Lush meadows with trees",
"Forest Trees / SRF,Willow (Tara 50%)",
"Forest Trees / SRF,Willow (Tare 20%)",
"Legumes,Alfalfa",
"Legumes,Clover",
"Maize,Corn",
"Miscellaneous,Industrial Medicinal Plants",
"Miscellaneous,Plant cultivation",
"No Agriculture,Bosco/S28-8 Peat bogs and alders",
"No Agriculture,Forest",
"No Agriculture,Greenhouses",
"No Agriculture,Hedges",
"No Agriculture,Hedges/S28-9 Hedges",
"No Agriculture,Infrastructures",
"No Agriculture,Other Areas",
"No Agriculture,Other crops/S28-3 Reedbeds",
"No Agriculture,Other crops/S28-8 Peat and alder bogs",
"No Agriculture,Water",
"Orchards and Berries,Apple",
"Orchards and Berries,Apricot",
"Orchards and Berries,Astoni plants fruit",
"Orchards and Berries,Berry fruit (without strawberry)",
"Orchards and Berries,Biennial cut meadow (tare 20%)/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Castagneto/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Cherry",
"Orchards and Berries,Chestnut",
"Orchards and Berries,Currants",
"Orchards and Berries,Meadow special area (tare 20%)/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Meadow special area/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Olive",
"Orchards and Berries,Orchard being planted",
"Orchards and Berries,Other fruit",
"Orchards and Berries,Pear",
"Orchards and Berries,Plums",
"Orchards and Berries,Stable meadow (tare 20%)/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Stable meadow/S28-7 Chestnut groves and meadows with sparse fruit trees",
"Orchards and Berries,Strawberry",
"Orchards and Berries,Table grapes",
"Orchards and Berries,Vineyard under planting",
"Orchards and Berries,Viticulture",
"Other Cereals,Grain",
"Permanent Grassland,Alpe (tare 70%)",
"Permanent Grassland,Meadow (Permanent Meadow Tara 20%)",
"Permanent Grassland,Meadow (permanent meadow)",
"Permanent Grassland,Meadow (permanent meadow)/S28-1 poor meadows and fen meadows",
"Permanent Grassland,Meadow (permanent meadow)/S28-2 species-rich mountain meadows",
"Permanent Grassland,Meadow special area",
"Permanent Grassland,Meadow special area (tare 20%)/S28-1 Dry meadows and low bog meadows",
"Permanent Grassland,Meadow special area (tare 20%)/S28-2 Species-rich mountain meadows",
"Permanent Grassland,Meadow special area (tare 50%)/S28-1 Dry meadows and low bog meadows",
"Permanent Grassland,Meadow special area (tare 50%)/S28-2 Species-rich mountain meadows",
"Permanent Grassland,Meadow special area/S28-1 poor meadows and fen meadows",
"Permanent Grassland,Meadow special area/S28-2 species-rich mountain meadows",
"Permanent Grassland,Potential pasture (50% tare)",
"Permanent Grassland,Stable meadow (tare 20%)/S28-1 Dry meadows and meadows with low bog",
"Permanent Grassland,Stable meadow (tare 20%)/S28-2 Species-rich mountain meadows",
"Vegetables,Asparagus",
"Vegetables,Cabbage",
"Vegetables,Cauliflower",
"Vegetables,Field vegetable cultivation",
"Vegetables,Radish",
"Vegetables,Salads",
]},
geometry = [box(0,  0, 5, 5), box(5, 0, 10, 5), box(10, 0, 15, 5), box(15, 0, 20, 5)],
crs=25832,
)
# Add new "Classes" column based on DESCR_ENG column
test_vector_data["Classes"] = None
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Cultivated"), "Classes"] = "Cultivated Grassland"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Fallow"), "Classes"] = "Fallow"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Forest"), "Classes"] = "Forest Trees"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Legumes"), "Classes"] = "Legumes"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Maize"), "Classes"] = "Maize"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Miscellaneous"), "Classes"] = "Miscellaneous"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("No Agriculture"), "Classes"] = "No Agriculture"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Orchards and Berries"), "Classes"] = "Orchards and Berries"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Other Cereals"), "Classes"] = "Other Cereals"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Permanent Grassland"), "Classes"] = "Permanent Grassland"
test_vector_data.loc[test_vector_data["DESCR_ENG"].str.startswith("Vegetables"), "Classes"] = "Vegetables"

# Print result
print(test_vector_data.head())

test_vector_data["geometry"] = [MultiPolygon([feature]) if isinstance(feature, Polygon) \
else feature for feature in test_vector_data["geometry"]]

Каждый объект имеет свой полигон в столбце геометрии.
Я хотел бы, например, свернуть все классы объектов в один уникальный класс, указанный в списке. в первой части имени перед запятой, т. е. все имена, начинающиеся с «Культивируемые луга» в Культивируемые луга; «Лесные деревья/SRF» в Лесные деревья/SRF
Геометрия каждого объекта также должна соответствовать этому критерию, чтобы иметь один уникальный полигон для каждого сгруппированного объекта
Здесь вы можете найти файл, который я использую shapefile
Есть предложения?

Подробнее здесь: https://stackoverflow.com/questions/791 ... d-geometry
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»