2

I have three adjacent polygons that together make up a larger shape. I need an algorithm that will dissolve the internal borders leaving only the outside border. An example would be a group of adjacent counties. I want to merge together all the county polygons and dissolve the internal borders so I'm left with just the outline of the state.

  • Welcome to GIS SE! Would you be able to use the edit button beneath your Question to revise it with details of any algorithms that you have found and rejected so far, please? Questions here should demonstrate that you have researched them before posting. – PolyGeo Feb 3 '14 at 21:26
  • Sure. I've look through stackexchange and stackoverflow for such an algorithm without success. I latched onto the idea of a convex hull algorithm, but it seems as though that is quite complicated and perhaps unneeded since I have more than a collection of points - I have a collection of polygons. Understanding that the operation is called "union" is a big help in and of itself. – Timothy Barmann Feb 3 '14 at 23:26
  • Instead of using Comments to respond did you know that you can use the edit button beneath your Question to revise it with the additional details? This makes it much easier for readers to understand exactly what you are asking and what progress you have made towards your question being answered. – PolyGeo Feb 3 '14 at 23:44
  • @TimothyBarmann, +1 for perfect title. I stumbled onto your thread here researching a StackOverflow question. Sorry I'm so late to the party, but if you're still trying to get the JavaScript/Dissolve operation working, I posted an example using the JSTS library klewis cited. It's actually quite simple. Cheers. – elrobis Jun 6 '14 at 17:27
4

This is a pure Javascript api that performs Dissolve, also commonly called Union, and other spatial operations. It is based on Java Topology Suite. jsts

  • Thanks for the link to this library. I think I found the appropriate class - jsts.operation.union.CascadedPolygonUnion - though I'm not quite sure how to use it. Could you provide an example of how to use it if each polygon was in an array of x,y coordinates , such as var poly1 = [[10,10],[10,20],[20,20],[10,10]]; var poly2 = [[x,y],.....] – Timothy Barmann Feb 3 '14 at 22:53
  • 2
    This has a Union example. You should be able to step into the code. gis.ibbeck.de/ginfo/apps/OLExamples/OL210/JSTS_Example/… – klewis Feb 3 '14 at 23:41
  • @klewis, +1 for recommending JSTS. I'd never heard of it before and so wanted to give it a try. I managed to get the Union/Dissolve operation working (tested in a Google Map of all things) and posted a step-by-step example below. – elrobis Jun 6 '14 at 17:24
2

Stumbled across this and thought I'd provide an example. In addition to my explanation, below, here's a JSFiddle demo with a couple extras.

[3/18/2016: The structure of JSTS github has changed and this no longer works, however I discuss another way you can get JSTS in this post.]

1) First download the JSTS library, unzip it, browse into the lib folder, and include the two lib files (javascript.util.js, and jsts.js) in your project. I put mine in a separate jsts folder and referenced them like so..

<script type="text/javascript" src="jsts/javascript.util.js"></script>
<script type="text/javascript" src="jsts/jsts.js"></script>

2) Assuming you're comfortable with Well Known Text (WKT), as that's essential to the exercise, you can perform a Union (JSTS, SQL-proper terminology) a.k.a. "Dissolve" (GIS-centric terminology) as follows.

Using JavaScript Topology Suite to Dissolve Two Geometries

function DissolveTwoGeomsWithJSTS(wkt1, wkt2)
{
    // Instantiate JSTS WKTReader and get two JSTS geometry objects
    var wktReader = new jsts.io.WKTReader();
    var geom1 = wktReader.read(wkt1);
    var geom2 = wktReader.read(wkt2);

    // In JSTS, "union" is synonymous with "dissolve"
    var dissolvedGeometry = geom1.union(geom2);

    /* Since 'union()' is a method of a JSTS geometry object, you
       could easily modify this method to iterate over an array
       of JSTS geometry objects, calling a 'union()' on each 
       sequential object. */

    // Instantiate JSTS WKTWriter and get new geometry's WKT
    var wktWriter = new jsts.io.WKTWriter();
    var wkt = wktWriter.write(dissolvedGeometry);

    return wkt;
}
1

You can (now) use the turf-union function of turf.js

turf.union(poly1, poly2)

Takes two polygons and returns a combined polygon. If the input polygons are not contiguous, this function returns a MultiPolygon feature.

However it's worth noting that this is currently just a convenient wrapper around the JavaScript Topology Suite, as can be seen here, but it's convenient if you're already using turf, and it might just save you a few lines of code.

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.