In the earlier recipes, we saw how to read in data from different formats, such as GML and KML, but what about exporting data from a map? A user of your mapping application may want to get a copy of the vector layer data to use it somewhere else. Or perhaps, you're saving the state of a map and need to send the layer content to a server for persistent storage so that the same geometry can be retrieved later.
In this recipe, we will export the contents of a vector layer to the GeoJSON format. The source code can be found in ch03/ch03-export-geojson. Here's what we'll end up with:

We are going to enable the use of an export button that will output the vector layer features as GeoJSON in a text box. Follow these steps to achieve this goal:
<div id="js-map"></div>
<div>
<h1>Exporting GeoJSON</h1>
<form>
<button type="submit">Export layer</button>
<textarea id="js-textarea"></textarea>
</form>
</div>map, view, and background raster layers:var map = new ol.Map({
view: new ol.View({
zoom: 5,
center: [2103547, 6538117]
}),
target: 'js-map',
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'osm'})
})
]
});var getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};var features = [], numberOfFeatures = 0;
while(numberOfFeatures < 10) {
var circle = new ol.geom.Circle(
ol.proj.fromLonLat(
[getRandomInt(14, 23), getRandomInt(48, 54)]
), getRandomInt(4, 15) * 10000
);
var polygonCircle = ol.geom.Polygon.fromCircle(circle);
features.push(new ol.Feature(polygonCircle));
numberOfFeatures++;
}features to the vector source, and add this to map:var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: features
})
});
map.addLayer(vectorLayer);export button and export features as GeoJSON to the text box:document.forms[0].addEventListener('submit', function(event) {
event.preventDefault();
var format = new ol.format.GeoJSON();
var features = vectorLayer.getSource().getFeatures();
var geoJson = format.writeFeatures(features);
document.getElementById('js-textarea').value = geoJson;
});The JavaScript contains a method (getRandomInt) that picks an integer between a range at random. For more information on the inner workings of this method, visit http://stackoverflow.com/questions/1527803.
Let's focus on the new components that are utilized throughout this recipe:
var circle = new ol.geom.Circle(
ol.proj.fromLonLat(
[getRandomInt(14, 23), getRandomInt(48, 54)]
),
getRandomInt(4, 15) * 10000
);Within the while loop (that performs 10 iterations), we created a circle geometry each time. In order to programmatically create a set of differently sized circles, we've supplied a range of numbers representing longitude and latitude values that are randomly selected by getRandomInt. These ranges form an inaccurate bounding box around Poland, Europe.
The view uses the EPSG:3857 projection, so we use ol.proj.fromLonLat to convert the longitude and latitude values to this projection (which is the default if it is not specified as the second parameter).
We also create a randomly generated radius, which then gets multiplied by 10,000 so that it's clearly visible at the starting resolution.
var polygonCircle = ol.geom.Polygon.fromCircle(circle);
We convert the circle geometry to a regular polygon feature, which produces an approximated circle. This part may not be apparent, but GeoJSON (at the time of writing) doesn't support circle geometries yet, so we must convert it to a regular polygon so that it can be serialized and exported later on.
features.push(new ol.Feature(polygonCircle));
The polygon circle is then converted into an ol.Feature instance so that OpenLayers can use it on the layer source, as follows.
document.forms[0].addEventListener('submit', function(event) {
event.preventDefault();
var format = new ol.format.GeoJSON();
var features = vectorLayer.getSource().getFeatures();
var geoJson = format.writeFeatures(features);
document.getElementById('js-textarea').value = geoJson;
});We add a submit event listener to the first and only form on the page, which prevents the default form submission.
We create a new instance of the GeoJSON format and store it in the format variable. The vector features are retrieved from the layer source via the helpful getSource method from the vector layer and through the getFeatures method from the vector source.
The GeoJSON is finally serialized using the writeFeatures method from the GeoJSON format provider, which expects an array of OpenLayers features. The GeoJSON string is then displayed inside the textarea element.
Displaying it in the textbox is one idea. You may decide to perform other actions with this data, such as posting it via AJAX to a server to save it, and so on.