Edit

Geographic Editing

Editing geometries with geographic coordinates.

Calling the useGeographic function in the 'ol/proj' module makes it so the map view uses geographic coordinates (even if the view projection is not geographic).

index.html<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Geographic Editing</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <select id="mode">
      <option value="modify">select a feature to modify</option>
      <option value="draw">draw new features</option>
    </select>
    <script src="index.js"></script>
  </body>
</html>
index.jsimport 'ol/ol.css';
import {Map, View} from 'ol/index';
import GeoJSON from 'ol/format/GeoJSON';
import {Modify, Select, Draw, Snap} from 'ol/interaction';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';
import {useGeographic} from 'ol/proj';

useGeographic();

var source = new VectorSource({
  url: 'data/geojson/countries.geojson',
  format: new GeoJSON()
});

var map = new Map({
  target: 'map',
  layers: [
    new TileLayer({
      source: new OSM()
    }),
    new VectorLayer({
      source: source
    })
  ],
  view: new View({
    center: [0, 0],
    zoom: 2
  })
});

var select = new Select();

var modify = new Modify({
  features: select.getFeatures()
});

var draw = new Draw({
  type: 'Polygon',
  source: source
});

var snap = new Snap({
  source: source
});

function removeInteractions() {
  map.removeInteraction(modify);
  map.removeInteraction(select);
  map.removeInteraction(draw);
  map.removeInteraction(select);
}

var mode = document.getElementById('mode');
function onChange() {
  removeInteractions();
  switch (mode.value) {
    case 'draw': {
      map.addInteraction(draw);
      map.addInteraction(snap);
      break;
    }
    case 'modify': {
      map.addInteraction(select);
      map.addInteraction(modify);
      map.addInteraction(snap);
      break;
    }
    default: {
      // pass
    }
  }
}
mode.addEventListener('change', onChange);
onChange();
package.json{
  "name": "edit-geographic",
  "dependencies": {
    "ol": "6.1.1"
  },
  "devDependencies": {
    "parcel": "1.11.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"
  }
}