4

I am curious if my geoJSON created with the geoJSON python module has a valid CRS.

The manual on github specifies a feature collection like so:

from geojson import Feature, Point, FeatureCollection    
my_feature = Feature(geometry=Point((1.6432, -19.123)))    
my_other_feature = Feature(geometry=Point((-80.234, -22.532)))    
FeatureCollection([my_feature, my_other_feature]) 

When I try and specify a crs and dump to json, the crs is at the beginning of the geojson string:

myFeat=FeatureCollection([my_feature, my_other_feature],crs="EPSG:3857")
dump = geojson.dumps(myFeat)
dump
'{"crs": "EPSG:3857", "type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [1.6432, -19.123]}, "type": "Feature", "properties": {}, "id": null}, {"geometry": {"type": "Point", "coordinates": [-80.234, -22.532]}, "type": "Feature", "properties": {}, "id": null}]}'

The geojson validates with geoJSONlint:

import requests
val = 'http://geojsonlint.com/validate'
req = requests.post(val, data=dump)
req.json()
{u'status': u'ok'}

But when inserted into QGIS the CRS is not recognized:

QGIS screengrab

Is this a problem with QGIS or with the geoJSON Python module?

2
  • 2
    For comparison, here is a point converted into GeoJSON with ogr2ogr { "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } }, "features": [ { "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 2617169.220924682, 8733876.365854254 ] } } ] }
    – user30184
    Jun 11 '14 at 12:16
  • Note that explicit specification of coordinate system in GeoJSON was removed in RFC 7946 to avoid interoperability issues. All coordinates are interpreted as coordinates specified using WGS84 datum with longitude and latitude units of decimal degrees. Sep 2 at 13:49
10

In the geojson specifcation there are two options for a CRS object: a named CRS, and a linked CRS. These are the two formats of CRS that QGIS (through OGR) will recognise. Note that the geojson format is flexible, so as long as what you supplied to geojsonlint was valid JSON, and satisfies the two "must" requirements then it will validate:

  • The GeoJSON object must have a member with the name "type". This member's value is a string that determines the type of the GeoJSON object.
  • The value of the type member must be one of: "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection", "Feature", or "FeatureCollection". The case of the type member values must be as shown here.

The simplest way to specify a CRS that will be recognised by QGIS is the named CRS, which specifies the CRS object to appear as:

"crs": {
  "type": "name",
    "properties": {
      "name": "EPSG:3857"
    }
  }

You can specify this in your code with:

crs = {
    "type": "name",
    "properties": {
        "name": "EPSG:3857"
    }
}
myFeat=FeatureCollection([my_feature, my_other_feature],crs=crs)

Note that this does not project your data for you. Geojson assumes your data is projected in the format specified in the CRS. Instead you would have to use a library like pyproj to project the coordinate values.

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.