summaryrefslogtreecommitdiff
path: root/app/projects/shortcuts.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/projects/shortcuts.py')
-rw-r--r--app/projects/shortcuts.py46
1 files changed, 23 insertions, 23 deletions
diff --git a/app/projects/shortcuts.py b/app/projects/shortcuts.py
index 600e56d..7753d18 100644
--- a/app/projects/shortcuts.py
+++ b/app/projects/shortcuts.py
@@ -28,22 +28,22 @@ def a_shapes(request):
mimetype=mimetype,
proj_transform=900913,
pretty_print=pretty_print
- )
-
+ )
+
def responses(qs,type_='countries',pretty_print=True,mimetype='text/plain'):
if type_ == 'locations':
qs = qs.geolocations()
- return render_to_geojson(qs,
- excluded_fields=['json'],
+ return render_to_geojson(qs,
+ excluded_fields=['json'],
geom_field='point',
proj_transform=900913,
mimetype=mimetype,
pretty_print=pretty_print
)
- elif type_ == 'affiliates':
+ elif type_ == 'affiliates':
qs = qs.exclude(geokeywords='').attach_locations()
return render_to_geojson(qs,
- included_fields=['id','_geokeywords_cache'],
+ included_fields=['id','_geokeywords_cache'],
geom_attribute='point',
extra_attributes=['name'],
proj_transform=900913,
@@ -57,11 +57,11 @@ def responses(qs,type_='countries',pretty_print=True,mimetype='text/plain'):
#geom_attribute='point',
mimetype=mimetype,
pretty_print=pretty_print
- )
+ )
else:# type_ == 'countries' or type is None:
if len(qs) > 10:
# this is a limit, weird huh?
- # requires another all() otherwise it
+ # requires another all() otherwise it
# returns a list!
qs = qs.all()[:10]
return render_to_geojson(qs,
@@ -77,10 +77,10 @@ def responses(qs,type_='countries',pretty_print=True,mimetype='text/plain'):
def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_attributes=[],mimetype='text/plain', pretty_print=False, excluded_fields=[],included_fields=[],proj_transform=None):
'''
-
+
Shortcut to render a GeoJson FeatureCollection from a Django QuerySet.
Currently computes a bbox and adds a crs member as a sr.org link
-
+
'''
excluded_fields.append('_state')
collection = {}
@@ -89,7 +89,7 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
query_set = [query_set]
else:
fields = query_set.model._meta.fields
-
+
if geom_attribute:
geometry_name = geom_attribute
geo_field = None
@@ -112,7 +112,7 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
else:
geo_fields = [f for f in fields if isinstance(f, GeometryField)]
-
+
#attempt to assign geom_field that was passed in
if geom_field:
#import pdb;pdb.set_trace()
@@ -126,15 +126,15 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
raise Exception('There appears to be no valid geometry on this model')
geo_field = geo_fields[0] # no support yet for multiple geometry fields
-
- #remove other geom fields from showing up in attributes
+
+ #remove other geom fields from showing up in attributes
if len(geo_fields) > 1:
for field in geo_fields:
if field.name not in excluded_fields:
excluded_fields.append(field.name)
geometry_name = geo_field.name
-
+
srid = geo_field.srid
@@ -148,10 +148,10 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
crs_properties = {}
crs_properties['href'] = 'http://spatialreference.org/ref/epsg/%s/' % to_srid
crs_properties['type'] = 'proj4'
- crs['properties'] = crs_properties
+ crs['properties'] = crs_properties
collection['crs'] = crs
collection['srid'] = to_srid
-
+
# Build list of features
features = []
if query_set.distinct():
@@ -199,14 +199,14 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
pass #features.append({'type':'Feature','geometry': {},'properties':{}})
# Label as FeatureCollection and add Features
- collection['type'] = "FeatureCollection"
+ collection['type'] = "FeatureCollection"
collection['features'] = features
-
+
# Attach extent of all features
if query_set:
ex = None
query_set.query.distinct = False
- if hasattr(query_set,'agg_extent'):
+ if hasattr(query_set,'agg_extent'):
ex = [x for x in query_set.agg_extent.tuple]
elif '.' in geometry_name:
prop, meth = geometry_name.split('.')
@@ -223,13 +223,13 @@ def render_to_geojson(query_set, geom_field=None, geom_attribute=None, extra_att
poly.transform(proj_transform)
ex = poly.extent
collection['bbox'] = ex
-
+
# Return response
response = HttpResponse()
if pretty_print:
response.write('%s' % simplejson.dumps(collection, indent=1))
else:
- response.write('%s' % simplejson.dumps(collection))
+ response.write('%s' % simplejson.dumps(collection))
response['Content-length'] = str(len(response.content))
response['Content-Type'] = mimetype
- return response \ No newline at end of file
+ return response