Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two polygons.

polygon1 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing1]))
polygon2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing2]))

How can I merge the two polygons into one using OpenLayers?

share|improve this question
Please explain your question, I'm not sure what do you mean. – drnextgis Feb 8 '12 at 7:57
up vote 13 down vote accepted

For geometry manipulation on client side you can use JSTS Topology Suite. Here is my little example of solving your problem: Union example. Source code:

var reader = new;  

var a ='POLYGON((10 10, 100 10, 100 100, 10 100, 10 10))');
var b ='POLYGON((50 50, 200 50, 200 200, 50 200, 50 50))');

var union = a.union(b);

var parser = new;

union = parser.write(union);

var map = new OpenLayers.Map('map', {
  maxExtent: new OpenLayers.Bounds(0, 0, 300, 300),
  maxResolution: 100,
  units: 'm',
 controls: [new OpenLayers.Control.MousePosition(), new OpenLayers.Control.Navigation()]

var layer = new OpenLayers.Layer.Vector('test', {isBaseLayer: true});

var unionOutput = new OpenLayers.Feature.Vector(union, null, { fillColor: 'green', fillOpacity: 1});

layer.addFeatures([unionOutput ]);
share|improve this answer
(+1) Always learning something new here. JSTS looks very handy. – CaptDragon Feb 8 '12 at 14:29
Nice handy fiddle example. Kudos :) – Rob Quincey May 23 '13 at 8:49

What I understand from your question is that you want merge two polygon features. I think you have to do this on the server side with ST_Union feauture which is in PostGIS Geometry Processing Functions. Then you can get the result and add this to your app. whatever you want. To merge...

In postgis you can union lots of polygons like this:

SELECT ST_AsText(ST_Union(ST_GeomFromText('POINT(1 2)'),
    ST_GeomFromText('POINT(1 2)') ) );

First of all you have to make some openlayers request for your geo collections.

And in GeoDjango you can do this easily with GeoDjango Topological Methods..

polygon.union( secondpolygon )

Returns a GEOSGeometry representing all the points in this geometry and the other.

I hope this helps you...

share|improve this answer

Every polygon is consider as one feature. when ever you add the polygon to the vector layer, that feature is stored over there. and you can get independent features from the layer object.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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