4

I need to find union of polygons represented as javascript arrays of geocoordinates as in the code bellow:

           var polygon1 = [[51.49151151647854, -0.20633749635419463],
                [51.491517887745886, -0.20663465247037038],
                [51.49125377257769, -0.20707803457401042], 
                [51.49125377257769, -0.20707803457401042],
                [51.49090541057069, -0.2071046417369189],
                [51.490605858341326, -0.20637621141179352],
                [51.49081102624578, -0.20598780005047956],
                [51.490859930095496, -0.20592173450688733]]


            var polygon2 = [[51.492223312188386, -0.20784254067885688],
                [51.49226262897666, -0.20812681455816695],
                [51.49162516066342, -0.2090258514344801],
                [51.491538051645385, -0.2089717941050469],
                [51.49200661100099, -0.20759325088262348]]

I would draw that resulting polygon on Google map with Google maps Javascript API v3, so they can be structured like in the code bellow, too:

   var googleMapPolygon1 = [
        new google.maps.LatLng(51.49151151647854, -0.20633749635419463),
        new google.maps.LatLng(51.491517887745886, -0.20663465247037038),
        new google.maps.LatLng(51.49125377257769, -0.20707803457401042),
        new google.maps.LatLng(51.49090541057069, -0.2071046417369189),
        new google.maps.LatLng(51.490605858341326, -0.20637621141179352),
        new google.maps.LatLng(51.49081102624578, -0.20598780005047956),
        new google.maps.LatLng(51.490859930095496, -0.20592173450688733),
        new google.maps.LatLng(51.49151151647854, -0.20633749635419463)
  ];

  var googleMapPolygon2 = [
        new google.maps.LatLng(51.492223312188386, -0.20784254067885688),
        new google.maps.LatLng(51.49226262897666, -0.20812681455816695),
        new google.maps.LatLng(51.49162516066342, -0.2090258514344801),
        new google.maps.LatLng(51.491538051645385, -0.2089717941050469),
        new google.maps.LatLng(51.49200661100099, -0.20759325088262348),
        new google.maps.LatLng(51.492223312188386, -0.20784254067885688)
  ]

So, what could I use for finding the union of polygons? Thanks in advance.

EDIT: This is what I do. I have array of Voronoi cells structured as polygons (left side of the picture bellow). And what I am trying to get is boundary around them (green polygon on the right side of the picture bellow). enter image description here

So, I think I can get it with union operation. Can anyone help?

karakulj
  • 123
  • 4
  • 11
  • 2
    Your picture doesn't [match the data you provided](http://jsfiddle.net/c35wo7rh/), the two polygons are completely separate, have no vertices in common. Have you looked at [JSTS](https://github.com/bjornharrtell/jsts)? – geocodezip Mar 06 '15 at 14:58
  • Can your provide a more complete example? Preferably a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that shows the problem. – geocodezip Mar 06 '15 at 22:34
  • JSTS helped. I used this as reference: http://stackoverflow.com/questions/24064125/googlemaps-api-v3-build-polygon-containing-multiple-zipcodes – karakulj Mar 10 '15 at 11:41
  • This [example from JSTS](http://www.geocodezip.com/scripts/jsts-0.15.0/examples/triangulation.html) might get you a better answer. Is your data what it calls "delaunay-triangulation" (red) or "voronoi diagram generation" (green)? – geocodezip Mar 11 '15 at 10:13
  • Did you found any solution for this? if yes can you help me? I am also having same problem with two adjacent geographical regions want to merge them in one. – th3g3ntl3m3n Sep 10 '19 at 09:53

1 Answers1

-3

You could just do:

var polygon1 = [[51.49151151647854, -0.20633749635419463],
                [51.491517887745886, -0.20663465247037038],
                [51.49125377257769, -0.20707803457401042], 
                [51.49125377257769, -0.20707803457401042],
                [51.49090541057069, -0.2071046417369189],
                [51.490605858341326, -0.20637621141179352],
                [51.49081102624578, -0.20598780005047956],
                [51.490859930095496, -0.20592173450688733]];

var polygon2 = [[51.492223312188386, -0.20784254067885688],
                [51.49226262897666, -0.20812681455816695],
                [51.49162516066342, -0.2090258514344801],
                [51.491538051645385, -0.2089717941050469],
                [51.49200661100099, -0.20759325088262348]];

var union = new google.maps.Polygon({
    paths: polygon1.concat(polygon2)
});
duncan
  • 31,401
  • 13
  • 78
  • 99
  • I edited the explanation of the question, please tell me if you need more details. – karakulj Mar 06 '15 at 12:02
  • It doesn't give me expecting result, I still do not have boundary. I still have lines between and some new lines that connect concatenated polygons. I need a mathematical model which will delete those lines between and give me the boundary. – karakulj Mar 06 '15 at 14:33
  • I'm trying to find boundary around Voronoi cells which are red cells on this picture: http://prntscr.com/6deoau and the blue line is what I get with this concatenation of polygons. – karakulj Mar 06 '15 at 14:41
  • oh dear, what a mess! ;-) I was going to say the polygon doesn't look like a problem to me: http://imgur.com/0Zwl3hs – duncan Mar 06 '15 at 14:45
  • 1
    I think you'll need to use the [geometry library](https://developers.google.com/maps/documentation/javascript/reference#poly), containsLocation and/or isLocationOnEdge – duncan Mar 06 '15 at 14:48