Search code examples
openlayers-3geojsonprojection

Drag and drop a GeoJSON with linked CRS


I have a GeoJSON file that looks like this:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "link",
    "properties": {
      "href": "http://spatialreference.org/ref/epsg/32198/proj4/",
      "type": "proj4"
    }
  },
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [200000, 20000]
      },
      "properties": {
        "id": 1,
        "name": "foo"
      }
    }
  ]
}

As you can see, the crs definition uses the link type, which is documented here: http://geojson.org/geojson-spec.html#linked-crs

I'm dropping the file in an OL3 map that has the ol.interaction.DragDrop interaction enabled, but it fails to load it. OpenLayers 3 doesn't currently support this type of crs definition, thus the reason it fails to load it. It supports those of type name and EPSG, see: https://github.com/openlayers/ol3/blob/master/src/ol/format/geojsonformat.js#L484 (snippet below):

if (crs.type == 'name') {
  return ol.proj.get(crs.properties.name);
} else if (crs.type == 'EPSG') {
  // 'EPSG' is not part of the GeoJSON specification, but is generated by
  // GeoServer.
  // TODO: remove this when http://jira.codehaus.org/browse/GEOS-5996
  // is fixed and widely deployed.
  return ol.proj.get('EPSG:' + crs.properties.code);
} else {
  goog.asserts.fail('Unknown crs.type: ' + crs.type);
  return null;
}

Looking at it, I don't know if it would be possible to natively support the link type inside OpenLayers directly, as it would require to do an asynchronous request to fetch the projection definition within code that's synchronous. I suspect that I'm stuck with this problem.

I'm looking for an alternative to approach the problem, or maybe I'm just wrong about the fact that it could be possible to support this (with a proper patch) natively in OL3.

Any hint ?


Solution

  • Alexandre, your best bet is to avoid using the old GeoJSON linked CRS (which is very poorly supported by software) and either 1) convert your data to GeoJSON's default WGS84 long/lat – this is the best option by far or 2) use a CRS name like "urn:ogc:def:crs:EPSG::32198".