Skip to content

Instantly share code, notes, and snippets.

@iamgeoknight
Created April 5, 2021 06:48
Show Gist options
  • Save iamgeoknight/e31ccca381744c73a2525917a92efb12 to your computer and use it in GitHub Desktop.
Save iamgeoknight/e31ccca381744c73a2525917a92efb12 to your computer and use it in GitHub Desktop.
let mergePolygon = (e) => {
/*
This function is applicable to merge only two polygons
This function will merge or perform union on two adjacent polygons. For the merge function to work, the polygons should atleast intersect each other.
*/
//Create jsts parser to read openlayers geometry
let parser = new jsts.io.OL3Parser();
//Parse Polygons geometry to jsts type
let a = parser.read(vector_layer.getSource().getFeatures()[0].getGeometry());
let b = parser.read(vector_layer.getSource().getFeatures()[1].getGeometry());
//Perform union of Polygons. The union function below will merge two polygon together
let union = a.union(b);
let merged_polygon = new ol.Feature({
geometry: new ol.geom.Polygon(parser.write(union).getCoordinates())
});
vector_layer.getSource().clear();
vector_layer.getSource().addFeature(merged_polygon);
vector_layer.setStyle(highlightStyle);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment