2

I am trying to plot polygons in WKT format I have in an array onto a single layer and add it to my map. I can post 1 polygon by referencing an entry in the entry (array[0]) and I have managed to plot many polygons from an array in straightforward coordinate format but I cannot manage many polygons from an array in WKT format! This is where I've got to, mainly referencing this article: https://gis.stackexchange.com/a/141792/80756 which is the closest thing I can find to what I need:

// example array
fields = [];
fields.push('POLYGON((0.37865281105041504 52.715357357381926,0.3784811496734619 52.71480491657922,0.3788459300994873 52.71480491657922,0.37882447242736816 52.715370355904795,0.37865281105041504 52.715357357381926))');
fields.push('POLYGON((0.37668943405151367 52.71364151836265,0.37785887718200684 52.71366101691244,0.37789106369018555 52.714271967057215,0.37796616554260254 52.714414954153405,0.3783094882965088 52.714382457127265,0.3783094882965088 52.71463593328936,0.377429723739624 52.714655431394654,0.3774082660675049 52.714453950552866,0.37659287452697754 52.71446694934492,0.37668943405151367 52.71364151836265))');
fields.push('POLYGON((0.37491917610168457 52.71521437337461,0.3757345676422119 52.71533136032452,0.37583112716674805 52.714876409312474,0.37500500679016113 52.714791917887844,0.37491917610168457 52.71521437337461))');
fields.push('POLYGON((0.37282705307006836 52.71657920160306,0.37366390228271484 52.7165727025222,0.37358880043029785 52.715461345456504,0.3727841377258301 52.71552633787724,0.37282705307006836 52.71657920160306))');

// Set format
var format = new ol.format.WKT();

var polyCoords = [];
for (var i in fields) {
    polyCoords.push(format.readFeature(fields[i]).getGeometry().transform('EPSG:4326', 'EPSG:3857'));
}

var feature = new ol.Feature({
    geometry: new ol.geom.Polygon([polyCoords])
})

var fields_layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [feature]
  })
});

// add field layer to map (map is globally defined and I know this part works)
map.addLayer(fields_layer);

I'm new to OL3.

1 Answer 1

1

to use geometries as WKT in OL3 you'll have to call an ol.format.WKT() and readGeometry, like this :

var wktFormat= new ol.format.WKT();
yourGeometry=wkt.readGeometry(yourWKTGeometry);
// and that's it you have a valid ol3 geometry now

in your code you are calling readFeature while you have a geometry instead

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.