1

I want to create a separate layer in Openlayers 4 with multipolygon geometry. I thought, it's enough to know a few coordinates, put it in an array and create the geometry. After reading documentations, I've no idea to create a multipolygon in Version 4.

Any ideas? Examples? Please let me know.

I solved the Problem myself. Here are some Details: I wanted to Show a polygon with hole. So i need a multipolygon. First, I draw a rectangle as polygon with coordinates and show them in the map. Second, I edit the polygon to multipolygon and added a second polygon as hole. I didn't find a simple examples, how to do this. Here it is...

//area from database in ESPG:4258
var gebiet = '10.59 53.36, 10.73 53.42, 11.07 53.14, 12.11 52.87, 11.90 53.24, 12.04 53.37, 12.33 53.31, 12.51 53.95, 11.92 53.98, 11.47 53.75, 10.77 53.78, 10.95 53.64, 10.59 53.36';
var style=
                new ol.style.Style({
                  stroke: new ol.style.Stroke({
                    color: 'gray',
                    width: 1
                  }),
                  fill: new ol.style.Fill({
                    color: 'rgba(80, 80, 80, 0.2)'
                  })
                });

//import data and parse to array            
            function parseData(inputData, rowSeparator, colSeparator){
                var outputData = inputData.split(rowSeparator);
                parsedData =  new Array(outputData.length);
                for (var i = 0; i < parsedData.length; ++i) {
                    parsedData[i] = outputData[i].split(colSeparator);
                }

                return parsedData;
            }

//create layer
            function createLayer(features, id, style){
            //console.log('in func createLinestringLayer');
                return new ol.layer.Vector({
                    source: new ol.source.Vector({
                        features: [new ol.Feature({
                            geometry: features,
                            id: id,
                            name: 'Line'
                        })]
                    }),
                    style: style
                })
            }

//set features 
            function setFeatures(inputData, simplify){  
                var parsedData = parseData(inputData, ', ', ' ');               
                var lineString = new ol.geom.LineString();              

                for (var i = 0; i < parsedData.length; ++i) {                   
                    var coordinates = ol.proj.fromLonLat([parseFloat(parsedData[i][0]), parseFloat(parsedData[i][1])]);
                    lineString.appendCoordinate(coordinates);
                }

                if(simplify > 0){
                    lineString = lineString.simplify(simplify);
                }

                return lineString;
            }

var  geomPoly = new ol.geom.MultiPolygon([
                [
                    setFeatures(gebiet, 500).getCoordinates(),
                    [[0.5e6, 7.52e6], [2e6, 7.52e6], [2e6, 6.5e6], [0.5e6, 6.5e6], [0.5e6, 7.52e6]]
                ]
            ]);

map.addLayer(createLayer(geomPoly, 123, style));

closed as off-topic by Evil Genius, Vince, Dan C, xunilk, Andre Silva Jun 20 '18 at 2:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Evil Genius, Vince, Dan C, xunilk, Andre Silva
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You've expressed your requirements but not without enough details: you want to draw them from a draw tool, from coordinates, if yes, which ones? Provide a minimal sample, please Something like a codepen would really help – ThomasG77 Jun 19 '18 at 10:57
  • Solved, look at the main text – ben_GI89 Jun 20 '18 at 13:46