I have a project where I need to save polygons to a database. I found that you can get the coordinates array from a feature using: feature.getGeometry().getCoordinates(). This returns an array of all the coordinate points. Is this the bare minimum I have to save to a database in order to redraw this feature from scratch again later?

I don't need a PostGIS database to save all of the other feature data. The other information is not needed for my application. All I need is too redraw the exact same feature that was saved into the database.

I also only use EPSG:3857. But I would also save this with a feature just to be sure.

  • 1
    If you have only polygons and you don't care about the style and your CRS is always EPSG:3857, then coordinates are enough. See polygon geometry creation method: openlayers.org/en/latest/apidoc/…
    – TomazicM
    Jun 9 at 9:30
  • @TomazicM Thanks for the answer!
    – Stephen
    Jun 9 at 10:42

You can easily dump Openlayers Layer to GeoJSON saving not only geometry but all the attributes as well. GeoJSON can be store anywhere, as a file or in a database.

import GeoJSON from 'ol/format/GeoJSON.js'

const format = new GeoJSON({ featureProjection: 'EPSG:3857' });
const layers = map.getLayers(); // map is 'ol/Map' object
const source = layers.item(0).getSource(); // grab first layer
const features = source.getFeatures();
const json = format.writeFeatures(features);

// dump json to file or save in database

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.