utils.py 706 B

12345678910111213141516171819202122232425262728
  1. # -*- coding: utf-8 -*-
  2. from collections import OrderedDict
  3. import json
  4. def dict_to_geojson(d_in):
  5. """
  6. Encode a dict representing a GeoJSON object into a JSON string.
  7. This is needed because spatialite's GeoJSON parser is not really
  8. JSON-compliant and it fails when keys are not in the right order.
  9. """
  10. d=OrderedDict()
  11. d['type']=d_in['type']
  12. if 'crs' in d_in:
  13. d['crs']=d_in['crs']
  14. # our spatialite geo column is defined with EPSG SRID 4326 (WGS 84)
  15. d['crs'] = {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:EPSG:4326'}}
  16. if 'bbox' in d_in:
  17. d['bbox']=d_in['bbox']
  18. d['coordinates']=d_in['coordinates']
  19. return json.dumps(d)