Q:

Appending linear ring to multipolygons in OpenLayers

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.. 

A:

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.

