I am trying to do a union across thousands of different geometries. The geometries can be very complex so a naive union can take many hours. I then want to dump these geometries so each polygon becomes its own shape rather than one very large multipolygon.

However, I have extra information about the geometries. The total area covered by the geometries is split up into squares. Each geometry is contained within a square and thus can only touch the geometries of the 8 adjacent squares. Is there some way to use this extra information about which polygons could possible intersect in order to reduce the time of the union?

I can do this either in PostGIS or in Python.

  • 2
    This is a description which screams for a graphic. How can the polygons be both complex and squares? In addition, you are asking two questions by asking for two environments. The rule here is, "One question per Question." Please edit the question.
    – Vince
    Sep 10, 2017 at 18:48
  • 1
    Sorry. They can be complicated but the whole complicated geometry will be within a square.
    – TSW
    Sep 10, 2017 at 18:49

1 Answer 1


As long as you are using PostGIS or the Python shapely module, your union operation is being computed by libgeos. GEOS already optimizes the union operation by taking advantage of the relative locations of geometries, using an algorithm called "cascaded union," which is well described in a blog post by its author.

From my experience, your best options for accelerating the computation are to (a) perform the union using multiple threads, or (b) use a specialized algorithm if your polygons are fully topological (no line segments cross, and all nodes are shared by geometries that touch them).

  • 1
    thanks for your help. Is there a way to specify the union to run on multiple threads?
    – TSW
    Sep 10, 2017 at 22:23
  • 1
    Not directly, no. Easiest is to manually split your data into one subset per thread, run multiple queries (or Python processes) in parallel, then combine the results. Not terribly elegant, I know.
    – dbaston
    Sep 10, 2017 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.