From a6334a26d1138cdc2771edd548264e63028b44fa Mon Sep 17 00:00:00 2001 From: Sanj Date: Mon, 29 Aug 2011 05:52:27 +0530 Subject: [PATCH] geojson --- gazetteer/places/models.py | 16 ++++++++++++++++ gazetteer/places/views.py | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index b001034..402fcdd 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -1,6 +1,10 @@ from django.db import connection from django.contrib.gis.db import models from django.contrib.gis.geos import Polygon +try: + import json +except: + import simplejson as json # Create your models here. @@ -53,6 +57,18 @@ class Feature(models.Model): return "%s: %s" % (self.feature_type.code, self.feature_type.name,) feature_type_name.short_description = "Feature Type" + def get_geojson(self): + geom = json.loads(self.geojson) + properties = { + 'id': self.id, + 'preferred_name': self.preferred_name + } + return { + 'type': 'Feature', + 'properties': properties, + 'geometry': geom + } + def time_start(self): tf = self.time_frame if tf is not None: diff --git a/gazetteer/places/views.py b/gazetteer/places/views.py index f39f25f..c39f495 100644 --- a/gazetteer/places/views.py +++ b/gazetteer/places/views.py @@ -17,8 +17,12 @@ def search_json(request): bs = bbox.split(",") bs_param = (float(bs[0]), float(bs[1]), float(bs[2]), float(bs[3]),) - features = Feature.search.overlaps(bs_param, text=search_term, srid=3785) - d = {'c': features.count()} + features_qset = Feature.search.overlaps(bs_param, text=search_term, srid=3785) + features = [f.get_geojson() for f in features_qset] + d = { + 'type': 'FeatureCollection', + 'features': features + } return render_to_json_response(d) def search_related(request):