Cerin
Cerin

Reputation: 64739

Efficiently merging and reducing polygons with Python

What's the most efficient way, preferably in Python, to merge and simplify several polygons into the equivalent shape but using much fewer polygons?

polygon reduction

I'm attempting to graph zip code regions, as defined by US Census data on a Google Map. Using Django's excellent GeoDjango framework and a hack I made to the django-census-places app, I now have thousands of zips and lat/lng vectors describing their region in my database.

I've written some basic Python to export the vectors and render the Javascript to draw selected zips on a Google Map. However, the problem I've run into is that it's horribly slow, as even a single zip code can contain thousands of polygons. I can preprocess this output to JSON and cache it, but even then it takes the browser a few minutes to load all the triangles, most of which are redundant since I'm only interested in the overall outline of several zip codes.

Note, this question is similar to this, but not the same. I've not concerned with overlapping polygons, since I believe there are no overlaps.

Edit: This question also appears very similar.

Upvotes: 2

Views: 1999

Answers (2)

Cerin
Cerin

Reputation: 64739

It turns out one of the several features in GeoDjango is a full range of boolean polygon operators. All I have to do is call union() on each of my MultiPolygon objects to get the MultiPolygon describing the entire region. The reduction in number of points is an order of magnitude.

Upvotes: 1

Angus Johnson
Angus Johnson

Reputation: 4643

To merge polygons I suggest you need to perform a boolean union operation. I'm not sure how you'd do this in Python, but since you've also mentioned JavaScript, there's an excellent JavaScript translation of my Clipper library (written in Delphi, C++ and C#) here.

There's also an online demo here which very nicely shows the capabilities of the clipping library.

Upvotes: 0

Related Questions