Search code examples
openlayersopenlayers-3

Save feature as WKT


I need to save feature after they are drawn without clicking a "save" button...

I've chosen to use the "drawend" listener to do this...

This is part of my "drawend" code...

draw.on('drawend', function (e) {

                        if (webMapValues.drawType == 'Polygon') {
                            //var writer = new ol.format.GeoJSON();
                            //var geojsonStr = writer.writeFeatures(e.feature.getGeometry().getCoordinates());

                            var format = new ol.format.WKT();
                            var feature = format.readFeature(e.feature);

                            var justNowFeature = e.feature;
                            var featureGeom = justNowFeature.getGeometry();

                            })
                        }

As you can see I'm trying many things, I don't want to just get the coordinates of the feature I want to write out the actual feature being passed in "e" to WKT

Nothing I've tried thus far has worked...short of grabbing ALL the features, which doesn't help me.

This is how I get ALL features...how can I do this for e.feature in 'drawend'?

var drawLayer = rcisMapService.getLayer("Draw");

                        var drawSource = drawLayer.getSource();

                        //Getsource
                        var features = drawSource.getFeatures();

                        var writer = new ol.format.GeoJSON();
                        var geojsonStr = writer.writeFeatures(drawSource.getFeatures());

Any help is greatly appreciated!!


Solution

  • WKT is a text markup language for representing vector geometry and not features. So try the following snip

    var format = new ol.format.WKT(); var wktfeaturegeom = format.writeGeometry(e.feature.getGeometry());