# How to merge/dissolve multiple polygons into one single polygon?

Problem: I want to merge this Multipolygon into a single Polygon (without the inner lines). And I want the merged Polygon in WKT, not just a visual plot.

EDIT: Sorry I wasn't explicit enough. I had multiple individual Polygons and then I used `unary_union` to make them into the MultiPolygon blow, but the thing is I cannot get rid of the inner lines in Python and get the result in WKT.

This is the `union`ed MultiPolygon WKT:

``````MULTIPOLYGON (((-90.52695518499314 14.57694601429162, -90.52182159264571 14.57352610269226, -90.51989485961862 14.57313150462852, -90.52403141432457 14.56700724216438, -90.52462631453743 14.56712907550862, -90.52875409890652 14.56987887928577, -90.52695518499314 14.57694601429162)), ((-90.53493097342313 14.57129735428182, -90.53510599999998 14.571144, -90.52895321097442 14.56909669606283, -90.52875410413675 14.5698788587385, -90.52462632400072 14.56712905778129, -90.52403142643807 14.56700722423007, -90.52620965641566 14.56378230556998, -90.52594431895147 14.56316632507827, -90.53889737465951 14.57161762516256, -90.54722022047719 14.58403701780301, -90.55002994485503 14.59861297919247, -90.54689769119557 14.61312648282012, -90.53829936772357 14.62536760576341, -90.53772729724494 14.62573108422974, -90.53348225684952 14.62348240755173, -90.53526596410298 14.62234908761351, -90.54290897615581 14.61146821168509, -90.54569341924321 14.5985673962071, -90.54319613612486 14.58561097242886, -90.5357981866622 14.57457141172557, -90.53380139148749 14.57324120729656, -90.53493097342313 14.57129735428182)), ((-90.5277824703885 14.59114230150452, -90.52936771483645 14.59839507210988, -90.5278931688056 14.60522473916912, -90.52384676374005 14.61098494379777, -90.51784447557753 14.61479862283177, -90.51080023419193 14.61608508551137, -90.50378670628027 14.61464847291389, -90.4978718479131 14.61070757970664, -90.49395619462449 14.60486249564351, -90.4926357326702 14.59800317419219, -90.49411124577365 14.59117389101431, -90.49815789109707 14.58541425906093, -90.50415955196254 14.58160100603092, -90.51120266595787 14.58031457313634, -90.51821522679643 14.58175080185194, -90.52412984490628 14.58569112238351, -90.52456415941288 14.58633929345437, -90.52243699010062 14.59469600141536, -90.52552699656599 14.59619100939188, -90.5277824703885 14.59114230150452), (-90.50161789586878 14.59429666312536, -90.50079828497144 14.59809076099797, -90.50153201538127 14.60190149776697, -90.50370745933373 14.60514871924251, -90.50699346717164 14.6073380369471, -90.51088975791731 14.60813610881709, -90.51480309192564 14.60742140997459, -90.5181376241418 14.60530274881971, -90.52038566029393 14.60210270078383, -90.52120497332609 14.59830848247912, -90.52047089495525 14.59449775495453, -90.5182952573924 14.59125066496212, -90.51500932370823 14.58906152400964, -90.51111333144196 14.58826357062182, -90.5072003453937 14.58897826027146, -90.50386600678814 14.59109678994338, -90.50161789586878 14.59429666312536)), ((-90.50079830517592 14.59809076127704, -90.50161791499018 14.59429666932821, -90.50386602073326 14.59109680455138, -90.50720035275695 14.58897827906933, -90.51111333122633 14.58826359080655, -90.51500931594208 14.589061542645, -90.51829524313051 14.59125067926166, -90.52047087627135 14.5944977626415, -90.52120495312089 14.59830848225313, -90.52038564178571 14.6021026926837, -90.51813761019635 14.60530273421199, -90.5148030845625 14.60742139117666, -90.51088975813323 14.60813608863236, -90.50699347493816 14.60733801831188, -90.50370747359628 14.60514870494359, -90.50153203406576 14.60190149008134, -90.50079830517592 14.59809076127704), (-90.50630968862838 14.59624829350593, -90.50589992036325 14.59814535824523, -90.5062668294508 14.6000507274844, -90.50735457674512 14.60167432342716, -90.5089975730083 14.6027689612481, -90.51094568292196 14.60316798274903, -90.51290230828754 14.60281063414034, -90.51456955182675 14.60175131897233, -90.51569358032317 14.6001513154356, -90.51610327458278 14.59825421912584, -90.51573627850436 14.59634885223631, -90.51464848280746 14.59472528916447, -90.51300550508267 14.59363069553159, -90.51105746978891 14.59323170365123, -90.50910093141333 14.5935890499617, -90.5074337362769 14.59464833225893, -90.50630968862838 14.59624829350593)), ((-90.50589994056773 14.59814535852388, -90.50630970774989 14.59624829970843, -90.50743375022208 14.59464834686688, -90.50910093877658 14.59358906875958, -90.51105746957322 14.59323172383596, -90.51300549731643 14.59363071416691, -90.5146484685454 14.59472530346385, -90.5157362598203 14.59634885992293, -90.51610325437757 14.59825421889942, -90.51569356181511 14.60015130733519, -90.51456953788137 14.60175130436454, -90.51290230092435 14.60281061534243, -90.51094568313779 14.6031679625643, -90.50899758077475 14.60276894261285, -90.50735459100751 14.60167430912808, -90.50626684813514 14.60005071979843, -90.50589994056773 14.59814535852388)), ((-90.53029756805302 14.57917267690476, -90.53656807908693 14.5882032173511, -90.53855088590083 14.59849214510384, -90.53633942949614 14.60873679108308, -90.53026991151975 14.61737731439291, -90.52126624247809 14.62309799413207, -90.51069945511419 14.62502769989141, -90.50017879812779 14.62287263640751, -90.49130641901225 14.61696108038603, -90.48543317389084 14.6081932929422, -90.48345290370396 14.59790429817398, -90.48566653602228 14.58766051592812, -90.49173659457898 14.57902128113803, -90.49639116774408 14.57606392732041, -90.50823200181394 14.57957800983411, -90.5120658910698 14.57602438979509, -90.51767300480016 14.5764210087726, -90.51989484750514 14.57313152256279, -90.52182158400919 14.57352612091459, -90.52695517976288 14.57694603483895, -90.5245641660407 14.58633926741659, -90.52412985765062 14.58569110684197, -90.5182152345624 14.58175078321651, -90.51120266426983 14.58031455311379, -90.50415954459923 14.58160098723308, -90.49815787869433 14.58541424324573, -90.4941112272662 14.59117388291263, -90.49263571246458 14.59800317401511, -90.49395617536834 14.6048625014151, -90.49787183240441 14.61070759249101, -90.50378669851358 14.61464849154905, -90.5108002339759 14.6160851056961, -90.51784448465628 14.61479864075894, -90.52384677768566 14.61098495840539, -90.52789318686833 14.60522474816281, -90.52936773504169 14.5983950723352, -90.52778248001883 14.59114227994767, -90.53047720177973 14.58539039514161, -90.52896001109058 14.58140600094455, -90.53029756805302 14.57917267690476)), ((-90.53380138141996 14.57324122462137, -90.53579817174098 14.5745714252995, -90.54319611744167 14.58561098011747, -90.5456933990775 14.59856739698946, -90.54290895764697 14.61146820358631, -90.53526594948187 14.62234907371724, -90.53338523218386 14.62338649128007, -90.53509716054782 14.61802504219403, -90.53187799615986 14.61712099076672, -90.52880728001617 14.6264527379118, -90.52392776474954 14.62955305016539, -90.51062102405807 14.63198306142494, -90.49737243241094 14.62926913626162, -90.48619971799339 14.62182474321997, -90.47880401042903 14.61078366846285, -90.47631075120849 14.59782714532199, -90.47909864305903 14.58492770837649, -90.48674250844088 14.57404888149517, -90.49209375828903 14.57064888803561, -90.4966586538851 14.56991853240323, -90.49535699000185 14.57575699980864, -90.49639114232363 14.57606391977621, -90.49173657995905 14.57902126724067, -90.48566651716074 14.58766050875966, -90.48345288349951 14.59790429789349, -90.48543315520584 14.60819330062666, -90.49130640474915 14.61696109468445, -90.5001787886632 14.62287265413419, -90.51069945489805 14.62502772007614, -90.5212662515568 14.62309801205925, -90.5302699254655 14.61737732900041, -90.5363394480048 14.60873679918225, -90.53855090610607 14.59849214532836, -90.53656809834202 14.58820321157604, -90.53029757834103 14.57917265972683, -90.53380138141996 14.57324122462137)), ((-90.47909862393821 14.584927702172, -90.47631073100403 14.59782714504094, -90.47880399117248 14.61078367423318, -90.48619970373005 14.62182475751817, -90.49737242464396 14.62926915489666, -90.51062102288714 14.63198308155371, -90.52392777119992 14.62955306927105, -90.52880727015771 14.62645276787116, -90.52671214560344 14.63272982053002, -90.5255437522236 14.63347218681117, -90.51057338499506 14.63620595727526, -90.49566847570512 14.63315269480565, -90.48309911148732 14.62477760830323, -90.47477909831115 14.61235629198987, -90.47197446480253 14.59778019610385, -90.47511108539966 14.58326842594115, -90.48295029870201 14.5721117861607, -90.49209370844271 14.57064889601072, -90.4867424938211 14.57404886759768, -90.47909862393821 14.584927702172)))
``````

Other requirements:

• The final geometry must be valid according to shapely
• Ideally, the precision stays the same
• Try multipart to single part before dissolving Aug 9, 2021 at 9:32
• The title is a bit misleading. In the beginning you do not have multiple polygons but just one multipolygon. Actually I do not understand why for example PostGIS considers that the original geometry is valid. The parts of a multipolygon are allowed to touch only at one point, not along a line. Aug 9, 2021 at 10:20
• Hi @Taras, thank you for commenting. I've updated the problem description with the code.
– anon
Aug 9, 2021 at 13:18
• Hi @user30184, thanks! I've updated the question. Would you take another look?
– anon
Aug 9, 2021 at 13:19
• There is nothing new in starting with polygons compared with the multipolygon WKT that you provided first. I exploded already the multipolygon and as a result I had the polygons. Your problem is that those polygons won't dissolve because their vertices do not match. Dissolve does not do anything for your geometries so you must fix your data first. How to clean an almost matching topology of a polygon layer is worth another question. There are also other ways to get an approximate result, for example concave hull or to rasterize your polygons and then polygonize the raster. Aug 9, 2021 at 14:05

The source WKT that you privide has only one geometry and therefore cascaded_union, unary_union, or dissolve cannot be used beyond that point. You are on the right track with explode but with your geometry it will not work directly because your geometry is not topologically perfect and adjacent polygons do not share exactly the same vertices. Therefore exploded polygons cannot be merged without anomalies. "Multipart to sigle part" is another term that is used in some software instead of "explode"

A proof-of-concept solution with OpenJUMP has a few steps.

1. Make the vertices to match. I used simple coordinate precision reducer. It gave an error message telling that the reduced geometry is invalid but I could use it in next steps anyway.

1. Select the reduced geometry and explode it with the mouse right click option. Multipolygon will be split into 8 polygons.

1. Dissolve the exploded layer.

This is the result as WKT

``````POLYGON (( -90.51989 14.57313, -90.52403 14.56701, -90.52621 14.56378, -90.52594 14.56317, -90.5389 14.57162, -90.54722 14.58404, -90.55003 14.59861, -90.5469 14.61313, -90.5383 14.62537, -90.53773 14.62573, -90.53348 14.62348, -90.53527 14.62235, -90.53339 14.62339, -90.5351 14.61803, -90.53188 14.61712, -90.52881 14.62645, -90.52671 14.63273, -90.52554 14.63347, -90.51057 14.63621, -90.49567 14.63315, -90.4831 14.62478, -90.47478 14.61236, -90.47197 14.59778, -90.47511 14.58327, -90.48295 14.57211, -90.49209 14.57065, -90.49666 14.56992, -90.49536 14.57576, -90.49639 14.57606, -90.50823 14.57958, -90.51207 14.57602, -90.51767 14.57642, -90.51989 14.57313 ), ( -90.5338 14.57324, -90.53493 14.5713, -90.53511 14.57114, -90.52895 14.5691, -90.52875 14.56988, -90.52696 14.57695, -90.52456 14.58634, -90.52244 14.5947, -90.52553 14.59619, -90.52778 14.59114, -90.53048 14.58539, -90.52896 14.58141, -90.5303 14.57917, -90.5338 14.57324 ))
``````

It is certainly possible do the same with Python and you may find a better solution to fix the topology with some snapping function.

• Hi, thank you for your answer. Sorry I wasn't explicit enough. I had multiple individual Polygons and then I used `unary_union` to make them into the MultiPolygon above, but the thing is I cannot get rid of the inner lines in Python and get the result in WKT.
– anon
Aug 9, 2021 at 10:19
• So you do not need to explode. Other steps should be relevant. Aug 9, 2021 at 10:22

If you have a resultant polygon with holes, you can get just the outer polygon using exterior property in shapely, i.e. polygon.exterior.coords will give you coordinates for the outer boundary. Outer polygon could be Polygon(polygon.exterior.coords).