Package madrona :: Package common :: Module jsonutils
[hide private]

Source Code for Module madrona.common.jsonutils

 1  """ 
 2  GeoJSON helper functions 
 3  """ 
 4  from django.core import serializers 
 5  import json 
 6   
7 -def get_properties_json(i):
8 json_orig = serializers.serialize('json', [i,]) 9 # use_natural_keys=True is not reliable; can fail w/ content_type error: 10 # 'NoneType' object has no attribute 'natural_key' 11 obj = json.loads(json_orig) 12 props = obj[0]['fields'] 13 unwanted_properties = [ 14 'geometry_final', 15 'geometry_orig', 16 'content_type', 17 'object_id', 18 ] 19 for uwp in unwanted_properties: 20 try: 21 props.pop(uwp) 22 except: 23 pass 24 # Add uid 25 props['uid'] = i.uid 26 return props
27
28 -def get_feature_json(geom_json, prop_json):
29 return """{ 30 "type": "Feature", 31 "geometry": %s, 32 "properties": %s 33 }""" % (geom_json, prop_json)
34
35 -def srid_to_urn(srid):
36 """ 37 Take a postgis srid and make it into a OGC CRS URN 38 As suggested by http://www.geojson.org/geojson-spec.html#named-crs 39 This is pretty dumb right now and just assumes EPSG as the authority 40 except for 4326 which uses: 41 4326 -> urn:ogc:def:crs:OGC:1.3:CRS84 42 """ 43 if int(srid) == 4326: 44 return "urn:ogc:def:crs:OGC:1.3:CRS84" 45 46 return "urn:ogc:def:crs:EPSG::%d" % srid
47
48 -def srid_to_proj(srid):
49 """ 50 Take a postgis srid and return the proj4 string 51 Useful for custom projections with no authority 52 """ 53 from django.contrib.gis.gdal import SpatialReference 54 srs = SpatialReference(srid) 55 return srs.proj.strip()
56