# How to perform union on polygons in pygplates

**URL:** https://discourse.gplates.org/t/how-to-perform-union-on-polygons-in-pygplates/200
**Category:** Q&A
**Created:** [18 October 2020 02:28 UTC](https://discourse.gplates.org/t/how-to-perform-union-on-polygons-in-pygplates/200 "2020-10-18T02:28:06Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![john.cannon](https://discourse.gplates.org/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@john.cannon](https://discourse.gplates.org/u/john.cannon)
#### Post date: [19 October 2020 12:18 UTC](https://discourse.gplates.org/t/how-to-perform-union-on-polygons-in-pygplates/200/2 "2020-10-19T12:18:29Z")

</div>

Hi Lavie,

The [pygplates.PolygonOnSphere.partition](https://www.gplates.org/docs/pygplates/generated/pygplates.PolygonOnSphere.html#pygplates.PolygonOnSphere.partition) function is really just meant for intersections (as opposed to unions). One way to use it to find the union of two polygons is to create a uniform, dense multi-point (MultipointOnSphere) in the area covered by the two polygons and find which points are inside either (or both) polygons (using pyGPlates to partition the multi-point). And then use some kind of outline algorithm to find the boundary of those points and create a polygon (possibly with interior holes) from that. However this approach is a bit cumbersome and has the problem that the final union polygon would look a bit gridded (depending on the density of the initial points).

Alternatively, what you want may be possible with the Boost geometry library - I’m not sure - they mention a [union](https://www.boost.org/doc/libs/1_74_0/libs/geometry/doc/html/geometry/reference/algorithms/union_/union__3.html) operation. There’s mention of supporting [geographical coordinate systems](https://www.boost.org/doc/libs/1_74_0/libs/geometry/doc/html/geometry/quickstart.html#geometry.quickstart.non_cartesian) (ie, points on a _sphere_) but they mention that that is an _extension_ where they say:

> Note that the library extensions are not distributed in the official Boost releases, but only available in the Boost.Geometry (develop branch) and that they are subject to change.

> [@LavieNguyen](#):
>
> I have tried to apply different modules such as pyproj and shapely in Python scientific package to perform union. However, I ended up with different artifacts because some geometries crossing the dateline. From what I understand, most GIS modules do not recognize that -180 and +180 longitude is the same line which causes unusual behavior.

I would assume that the result you should get from these packages would be equivalent to the final union polygon (calculated without any dateline clipping) followed by dateline clipping that final intersection polygon. Because presumably they would start with the two initial polygons (but that are each implicitly dateline clipped because they are in latitude/longitude coordinates) and then perform the union of those which I think should give the same result. But maybe that’s not happening.

On a side note: Currently `pygplates.PolygonOnSphere.partition` does not properly partition a polygon (in other words when you intersect a polygon with another _polygon_ you only get the clipped polyline pieces rather than the actual intersected polyon - there’s a warning in the docs [here](https://www.gplates.org/docs/pygplates/generated/pygplates.PolygonOnSphere.html#pygplates.PolygonOnSphere.partition) that goes into more detail). In a future release soon, proper polygon-polygon intersection (with interior holes) will be implemented, actually it’s currently implemented in the [dateline wrapper](https://www.gplates.org/docs/pygplates/generated/pygplates.DateLineWrapper.html) but that’s a simpler case. Also intersection is just one of the outputs of doing this properly, other boolean operations like union and difference can be made available with minor modifications.

Regards,  
John

---

_[View the full topic](https://discourse.gplates.org/t/how-to-perform-union-on-polygons-in-pygplates/200)._
