user8918738
user8918738

Reputation:

Merge multiple features into one

I have multiple openlayer features/vectors which get highlighted by clicking on one.

I want to merge all the vectors I have into a single big one that kind of acts like an outline. Imagine a big square (which would be the merged vektor) and in this big square are multiple smaller ones (the individial vectors).

I tried to merge them using the polygon.union() function but that just highlights all of them individually.

features is my array where all the vectors are in.

var test = null;
for(var j = 0; i < features.length; i++){
                test.union(features[i]);
            }
test.layer.drawFeature(test, 'default');

As I allready said, I want to create a shape off of my existing vectors that "contains" all of them. What I get is every vector highlighted at once.

Upvotes: 2

Views: 2742

Answers (1)

Philip Attisano
Philip Attisano

Reputation: 223

You want to use the ol.geom.geometryCollection class. Understand that this is only a collection of geometries exposed as a single feature not a collection of features. This seems to solve your question though.

var myarrayofgeoms = [
    new ol.geom.Polygon(somepolycords),
    new ol.geom.Point(somepointcoords)
];

var feature = new ol.Feature({
    geometry: new ol.geom.GeometryCollection(myarrayofgeoms),
    name: 'My Polygon'
});

https://openlayers.org/en/latest/apidoc/module-ol_geom_GeometryCollection-GeometryCollection.html

Upvotes: 2

Related Questions