2

I have found a draw tool in OpenLayers-3 which allows us to draw on a map (see http://openlayers.org/en/v3.2.0/examples/draw-features.html). Is there an API in openlayers to store the drawn feature in geojson format?

  • Where do you want to store it? Local storage in the browser? On the server using WFS-T? – bartvde May 20 '15 at 6:43
  • @bartvde i just want to get the drawn feature in geojson format and store it in a text file on the server (say) – ishan3243 May 20 '15 at 12:04
1

You can use ol.format.GeoJSON to write out your feature as GeoJSON, and then use any AJAX / XHR library to get it back to your server.

http://openlayers.org/en/v3.5.0/apidoc/ol.format.GeoJSON.html#writeFeature

See also this example which uses EsriJSON but the concepts are the same:

http://openlayers.org/en/v3.5.0/examples/vector-esri-edit.html

  • but the problem is how do I store a drawn "feature" into a variable? Please see the link I have posted in the question above. It just draws the feature on the map and doesn't store it anywhere. – ishan3243 May 20 '15 at 15:29
  • please check the vector-esri-edit example it should tell you all you need – bartvde May 20 '15 at 17:15
  • I am right now on openlayers 3.4.0, which doesn't seem to have this – ishan3243 May 20 '15 at 17:18
  • Then check out 3.5.0. Or just view the online example for the code. – bartvde May 21 '15 at 6:33
3

Further to the answer above, this is how I'm grabbing the draw features before storing them in an indexed db for offline storage:

// declare vars to populate later
var routetitle, routeDescription, routeFeatures, 
routeScreenshot, routePNG, tags;



// THIS IS THE IMPORTANT BIT
var allFeatures = drawLayer.getSource().getFeatures();
var format = new ol.format.GeoJSON();
var routeFeatures = format.writeFeatures(allFeatures);

If you then console log(routeFeatures it should give you a JSON Feature Collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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