2

I am trying to append linear ring to multipolygon. When I try to do same for polygon it works.

case 'Polygon':

linearRing=new ol.geom.LinearRing(...);

select.getFeatures().getArray()[0].getGeometry().appendLinearRing(linearRing);

case 'MultiPolygon':

linearRing=new ol.geom.LinearRing(...);
select.getFeatures().getArray()[0].getGeometry().getPolygon(0).appendLinearRing(linearRing);

it's not throwing error, but still coordinates of created linearRing are not appended to selected feature..

1
  • The polygon with linearRing is created but it's not updated on map , I should remove existing polygon and append newly created, is there other and simpler way to do this ?
    – Goran
    Commented Aug 19, 2019 at 13:23

1 Answer 1

2

A multipolygon is defined by an array of polygon coordinates, it isn't a collection of polygons so what is returned by getPolygon() or getPolygons() should be regarded as clones.

You could either push the linear ring coordinates into the appropriate polygon coordinates and build a new multipolygon

var feature = select.getFeatures().getArray()[0];
var coordinates = feature.getGeometry().getCoordinates();
coordinates[0].push(linearRing.getCoordinates());
feature.setGeometry(new ol.geom.MultiPolygon(coordinates));

or append the linear ring to the polygon then append each polygon clone to a new empty multipolygon

var feature = select.getFeatures().getArray()[0];
var polygons = feature.getGeometry().getPolygons();
polygons[0].append(linearRing);
var multiPolygon = new ol.geom.MultiPolygon([]);
polygons.forEach(function(polygon){
  multiPolygon.appendPolygon(polygon);
});
feature.setGeometry(multiPolygon);

The same applies to line strings returned by getLineString()/getLineStrings() from a multilinestring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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