Skip to content
Browse files

basic implementation of '/map/contains'

  • Loading branch information...
1 parent a8b1ea2 commit 5f8476e6724ef4a0bdce558a944938e4de48b895 @JoeGermuska JoeGermuska committed
Showing with 27 additions and 2 deletions.
  1. +1 −0 censusweb/api/urls.py
  2. +26 −2 censusweb/api/views.py
View
1 censusweb/api/urls.py
@@ -19,6 +19,7 @@
url(r'^data/(?P<geoids>[,\d]+)\.html$', views.generic_view, { "template": "data.html" }, name="data"),
url(r'^data/bulkdata.html$', views.generic_view, { "template": "bulkdata.html" }, name="bulkdata"),
url(r'^map/(?P<geoids>[,\d]+)\.html$', views.generic_view, { "template": "map.html" }, name="map"),
+ url(r'^map/contains$', views.map_contains, name="map_contains"),
url(r'^docs/json.html$', views.generic_view, { "template": "docs/json.html" }, name="json-doc"),
url(r'^docs/boundary.html$', views.generic_view, { "template": "docs/boundary.html" }, name="boundary-documentation"),
url(r'^util/create_table/(?P<aggregate>(all_files|all_tables))\.sql$', views.generate_sql, name="generate_sql"), # order matters. keep this first to catch only numbers before tables
View
28 censusweb/api/views.py
@@ -1,13 +1,15 @@
import simplejson
from django.conf import settings
-from django.http import HttpResponse, HttpResponseNotFound
+from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz
+from django.contrib.gis.geos import Point
from django.template import RequestContext, Template, Context
+from django.core.urlresolvers import reverse
-from boundaryservice.models import Boundary
+from boundaryservice.models import Boundary,BoundarySet
import csv
import help_text
@@ -17,6 +19,8 @@
DATA_ALTERNATIVES = ['2000', '2010', 'delta', 'pct_change']
+BOUNDARY_TYPES = [x[0] for x in BoundarySet.objects.values_list('slug').distinct()]
+
def homepage(request):
return render_to_response('homepage.html', {
'help_text': help_text,
@@ -251,3 +255,23 @@ def generate_sql(request, file_ids=None, table_ids=None, aggregate=None):
return HttpResponseNotFound()
+def map_contains(request):
+ point = request.REQUEST.get('point',None)
+ try:
+ lat,lng = point.split(',',1)
+ point = Point(float(lat),float(lng))
+ except:
+ raise TypeError("A point must be provided as a comma-separated string, 'lat,lng'")
+
+ types = request.REQUEST.get('types',[])
+ if types:
+ types = [x for x in types.split(',') if x in BOUNDARY_TYPES]
+ if not types: raise ValueError("None of the specified types are valid. Use one or more of (%s) separated by commas." % ','.join(BOUNDARY_TYPES))
+ else:
+ types = BOUNDARY_TYPES
+
+ boundaries = Boundary.objects.filter(shape__contains=point,set__slug__in=types)
+ geoids = sorted(x[0] for x in boundaries.values_list('external_id'))
+ geoids = ','.join(geoids)
+ return HttpResponseRedirect(reverse('map',kwargs={'geoids': geoids}))
+

0 comments on commit 5f8476e

Please sign in to comment.
Something went wrong with that request. Please try again.