utils.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. from flask import current_app
  3. from collections import OrderedDict
  4. from datetime import datetime
  5. import pytz
  6. import json
  7. def dict_to_geojson(d_in):
  8. """
  9. Encode a dict representing a GeoJSON object into a JSON string.
  10. This is needed because spatialite's GeoJSON parser is not really
  11. JSON-compliant and it fails when keys are not in the right order.
  12. """
  13. d=OrderedDict()
  14. d['type']=d_in['type']
  15. if 'crs' in d_in:
  16. d['crs']=d_in['crs']
  17. # our spatialite geo column is defined with EPSG SRID 4326 (WGS 84)
  18. d['crs'] = {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:EPSG:4326'}}
  19. if 'bbox' in d_in:
  20. d['bbox']=d_in['bbox']
  21. d['coordinates']=d_in['coordinates']
  22. return json.dumps(d)
  23. def utcnow():
  24. """
  25. Return the current UTC date and time as a datetime object with proper tzinfo.
  26. """
  27. return datetime.utcnow().replace(tzinfo=pytz.utc)
  28. def tosystemtz(d):
  29. """
  30. Convert the UTC datetime ``d`` to the system time zone defined in the settings
  31. """
  32. return d.astimezone(pytz.timezone(current_app.config['SYSTEM_TIME_ZONE']))