3

What is the GeoPandas equivalent of ST_MakePolygon?

I want to convert a MULTIPOLYGON to one single polygon. Currently, using GeoPandas.boundary a MultiLineString is returned.

How can this one be converted to a regular POLYGON? As far as I understand ST_MakePolygon would be the post GIS pendant.

edit

I am aware of https://stackoverflow.com/questions/2964751/how-to-convert-a-geos-multilinestring-to-polygon-using-python, i.e. convex_hull, but this only returns a rather distorted geometry.

I.e. in case one looks at the shape of Austria this is: enter image description here

edit2

A df.explode() will convert the MULTIPOLYGON to POLYGON.

Some answers suggest to aggregate via dissolve, however:

type(df(by='column_with_constant_value', aggfunc='sum').iloc[0].geometry)

still is

shapely.geometry.multipolygon.MultiPolygon

The same for:

type(df.cascaded_union)
shapely.geometry.multipolygon.MultiPolygon

edit3

The Polygon I am looking at is the Austrian borders from GADM. https://biogeo.ucdavis.edu/data/gadm3.6/gpkg/gadm36_AUT_gpkg.zip is the download link.

  • unzip it
  • df = gp.read_file()

The distorted image looks like: enter image description here

Georg Heiler
  • 401
  • 3
  • 12
  • Try: somedf.geometry = somedf.geometry.apply(lambda x: list(x)[0]). See: How to extract Polygons from Multipolygons in shapely?. – BERA Feb 13 '19 at 06:21
  • This distorts the geoemtry. Do note that: df.explode already does this and returns a list of polygons. However, then as a next step I need to get them combined into a single polygon. – Georg Heiler Feb 13 '19 at 06:31
  • Add a screenshot of your polygon please. Or even better a reproducible example polygon. – BERA Feb 13 '19 at 06:34
  • done -see the latest edit – Georg Heiler Feb 13 '19 at 06:38
  • Your polygon is in fact a multipart polygon consisting of two parts. Your added screenshot is one of the two parts, the other part is much bigger and looks like austria and is touching by the Southern tip of your screenshot polygon. So it is not possible to convert the two non-overlapping parts into one single part polygon. – BERA Feb 13 '19 at 07:10
  • But they touch? So why can't they be combined? Or atleast their single shared boundary be used to create a new single polygon with same extent? – Georg Heiler Feb 13 '19 at 07:13

1 Answers1

1

Your polygon is in fact a multi-polygon constisting of two parts:

enter image description here

They are intersecting(/touching borders?) in one point:

part1, part2 = polys.geometry.apply(lambda x: list(x))[0]
part1.intersects(part2)
True

But it does not seem to be possible to convert them into one single part. I assume they need to overlap, not only touch borders in one point as you would create a self-intersecting polygon.

You can try creating a small buffer polygon at the intersection point to connect the two parts and then dissolve the three polygons:

polygons = polys.geometry.apply(lambda x: list(x))[0]
polygons.append(polygons[0].intersection(polygons[1]).buffer(0.0001))
polys.geometry = [unary_union(polygons)]

polys.geometry.type
0    Polygon
dtype: object

polys.plot(cmap='cool')

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161