maposmatic-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Maposmatic-dev] [PATCH 1/2] New /nominatim space reserved for the local


From: David Decotigny
Subject: [Maposmatic-dev] [PATCH 1/2] New /nominatim space reserved for the local gateway to nominatim
Date: Sun, 20 Dec 2009 01:11:36 +0100

We query nominatim and present the results in json format for use by a
local javascript routine.
---
 www/maposmatic/views.py                       |   49 ++++++++++++++++++++++++-
 www/templates/maposmatic/query_nominatim.html |   28 ++++++++++++++
 www/urls.py                                   |    1 +
 3 files changed, 77 insertions(+), 1 deletions(-)
 create mode 100644 www/templates/maposmatic/query_nominatim.html

diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py
index abfa448..ac6663c 100644
--- a/www/maposmatic/views.py
+++ b/www/maposmatic/views.py
@@ -28,7 +28,7 @@ from django.forms.util import ErrorList
 from django.forms import CharField, ChoiceField, FloatField, RadioSelect, \
                          ModelForm, ValidationError
 from django.shortcuts import get_object_or_404, render_to_response
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, HttpResponseBadRequest
 from django.utils.translation import ugettext_lazy as _
 from django.template import RequestContext
 
@@ -40,6 +40,16 @@ import math
 from www.maposmatic.widgets import AreaField
 from ocitysmap.coords import BoundingBox as OCMBoundingBox
 
+# Nominatim parsing + json export
+# Note: we query nominatim in XML format because otherwise we cannot
+# access the osm_id tag. Then we format it as json back to the
+# javascript routines
+from urllib import urlencode
+import urllib2
+from xml.etree.ElementTree import parse as XMLTree
+import json
+
+
 # Test if a given city has its administrative boundaries inside the
 # OpenStreetMap database. We don't go through the Django ORM but
 # directly to the database for simplicity reasons.
@@ -255,6 +265,43 @@ def all_maps(request):
                               { 'maps': maps },
                               context_instance=RequestContext(request))
 
+
+def _parse_nominatim_xml(squery,
+                         
nominatim_url="http://nominatim.openstreetmap.org/search/";,
+                         with_polygons = False):
+    """
+    Return a list of entries for the given squery (eg. "Paris"). Each entry
+    is a dictionary key -> value.
+    """
+    query_tags = dict(q=squery, format='xml')
+    if with_polygons:
+        query_tags['polygon']=1
+
+    qdata = urlencode(query_tags)
+    f = urllib2.urlopen(url="%s?%s" % (nominatim_url, qdata))
+    return [dict(place.items()) for place in 
XMLTree(f).getroot().getchildren()]
+
+
+def query_nominatim(request, format, squery):
+    if not format:
+        format = "json"
+    else:
+        format = format[:-1]
+
+    if format not in ("json",):
+        return HttpResponseBadRequest("Invalid format: %s" % format)
+
+    try:
+        contents = _parse_nominatim_xml(squery,
+                                        with_polygons=False)
+    except:
+        contents = []
+
+    if format == "json":
+        return render_to_response('maposmatic/query_nominatim.html',
+                                  { 'contents': json.dumps(contents) },
+                                  context_instance=RequestContext(request))
+
 def about(request):
     return render_to_response('maposmatic/about.html',
                               context_instance=RequestContext(request))
diff --git a/www/templates/maposmatic/query_nominatim.html 
b/www/templates/maposmatic/query_nominatim.html
new file mode 100644
index 0000000..2566b88
--- /dev/null
+++ b/www/templates/maposmatic/query_nominatim.html
@@ -0,0 +1,28 @@
+{% comment %}
+ coding: utf-8
+
+ maposmatic, the web front-end of the MapOSMatic city map generation system
+ Copyright (C) 2009  David Decotigny
+ Copyright (C) 2009  Frédéric Lehobey
+ Copyright (C) 2009  David Mentré
+ Copyright (C) 2009  Maxime Petazzoni
+ Copyright (C) 2009  Thomas Petazzoni
+ Copyright (C) 2009  Gaël Utard
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+{% endcomment %}
+
+{% block page %}
+{{ contents }}
+{% endblock %}
diff --git a/www/urls.py b/www/urls.py
index 54976f4..8af2dde 100644
--- a/www/urls.py
+++ b/www/urls.py
@@ -40,6 +40,7 @@ urlpatterns = patterns('',
     (r'^jobs/$', maposmatic.views.all_jobs),
     (r'^maps/$', maposmatic.views.all_maps),
     (r'^about/$', maposmatic.views.about),
+    (r'^nominatim/([^/]*/)?(.*)$', maposmatic.views.query_nominatim),
     (r'^i18n/', include('django.conf.urls.i18n')),
 
     (r'^results/(?P<path>.*)$',
-- 
1.6.3.3





reply via email to

[Prev in Thread] Current Thread [Next in Thread]