Home > OS >  convert polygon to multi polygon
convert polygon to multi polygon

Time:09-22

I have a Dataframe as below. If a FID contains more than one polygons I need to create Multipolygon for FID. In my Dataframe I have FID 978 which contains two polygons so it should be converted to Multipolygon otherwise polygon.

|FID|Geometry|
|975|POLYGON ((-94.2019149289999 32.171910245, -94.201889847 32.171799917, -94.2019145369999 32.1719110220001, -94.2019344619999 32.171974117, -94.2019149289999 32.171910245))
|976|POLYGON ((-94.204485668 32.175813341, -94.2045721649999 32.1758854190001, -94.2044856639999 32.1758124690001, -94.204358881 32.1757171630001, -94.204485668 32.175813341))
|978|POLYGON ((-94.30755277 32.402906479, -94.321881945 32.4028226820001, -94.321896361 32.4035500580001, -94.3074214489999 32.4037557020001, -94.3075504129999 32.4029064600001, -94.30755277 32.402906479))
|978|POLYGON ((-94.30755277 32.402906479, -94.307552779 32.4005399370001, -94.307558688 32.4005401040001, -94.30755277 32.402906479))

I am using the following function to convert Multipolygons

def ploygon_to_multipolygon(wkt):
    list_polygons =  [wkt.loads(poly) for poly in wkt]
    return shapely.geometry.MultiPolygon(list_polygons)

looks like polygons are not converting to Multipolygons.

CodePudding user response:

from shapely.wkt import loads
from shapely.ops import unary_union

# Convert from wkt ... 
# I think there's a better way to do this with Geopandas, this is pure pandas.
df.Geometry = df.Geometry.apply(loads)

# Use groupby and unary_union to combine Polygons.
df = df.groupby('FID', as_index=False)['Geometry'].apply(unary_union)
print(df)

# Let's print out the multi-polygon to verify
print(df.iat[2,1])

Output:

   FID                                           Geometry
0  975  POLYGON ((-94.2019149289999 32.171910245, -94....
1  976  POLYGON ((-94.204485668 32.175813341, -94.2045...
2  978  (POLYGON ((-94.30755277900001 32.4005399370001...

MULTIPOLYGON (((-94.30755277900001 32.4005399370001, -94.307558688 32.4005401040001, -94.30755277 32.402906479, -94.30755277900001 32.4005399370001)), ((-94.321881945 32.4028226820001, -94.321896361 32.4035500580001, -94.3074214489999 32.4037557020001, -94.3075504129999 32.4029064600001, -94.30755277 32.402906479, -94.321881945 32.4028226820001)))

CodePudding user response:

I edited your function to return the Multipolygon as wkt and to check whether there are more than one polygons in the group.

import pandas as pd
from shapely import wkt, geometry
df = pd.DataFrame({
    'FID': [975, 976, 978, 978],
    'Geometry': [
        'POLYGON ((-94.2019149289999 32.171910245, -94.201889847 32.171799917, -94.2019145369999 32.1719110220001, -94.2019344619999 32.171974117, -94.2019149289999 32.171910245))',
        'POLYGON ((-94.204485668 32.175813341, -94.2045721649999 32.1758854190001, -94.2044856639999 32.1758124690001, -94.204358881 32.1757171630001, -94.204485668 32.175813341))',
        'POLYGON ((-94.30755277 32.402906479, -94.321881945 32.4028226820001, -94.321896361 32.4035500580001, -94.3074214489999 32.4037557020001, -94.3075504129999 32.4029064600001, -94.30755277 32.402906479))',
        'POLYGON ((-94.30755277 32.402906479, -94.307552779 32.4005399370001, -94.307558688 32.4005401040001, -94.30755277 32.402906479))',
    ],
})
def to_multipolygon(polygons):
    return (
        geometry.MultiPolygon([wkt.loads(polygon) for polygon in polygons]).wkt
        if len(polygons) > 1        
        else polygons.iloc[0]
    )
result = df.groupby('FID')['Geometry'].apply(lambda x: to_multipolygon(x))
print(result)

Output

FID
975    POLYGON ((-94.2019149289999 32.171910245, -94....
976    POLYGON ((-94.204485668 32.175813341, -94.2045...
978    MULTIPOLYGON (((-94.30755277 32.402906479, -94...
Name: Geometry, dtype: object
  • Related