Search code examples
openlayers

Format GeoJSON coordinates with OpenLayers by easily swapping between DD, DMS, DDM natively?


When writing GeoJSON from OpenLayers what is the default coordinate format? When writing the GeoJSON from an ExtentInteraction I get an output formatted like so:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-13614109.32469666,-1789063.2448918968],[-13614109.32469666,3379341.6847958053],[-6736155.0721122585,3379341.6847958053],[-6736155.0721122585,-1789063.2448918968],[-13614109.32469666,-1789063.2448918968]]]},"properties":null}]}

Apologies as I'm a complete tyro. Also, didn't see it on the Openlayers website API documentation.

Additionally, is there a way to write the GeoJSON so that the coordinates are returned in a specified format. I.e. DD, DMS, and DDM

This is how I'm writing the GeoJSON now:

         const extentCopy = extent.getExtent();

         new GeoJSON().writeFeatures([new Feature(fromExtent(extentCopy))]);

Thanks for the admonishment in advance.


Solution

  • Your result is in the view projection units. For degrees use

    new GeoJSON().writeFeatures(
      [new Feature(fromExtent(extentCopy))],
      {dataProjection: 'EPSG:4326', featureProjection: map.getView().getProjection()}
    );