You are on page 1of 13

GIS

Technologies
Geodjango: A Django extension Postgres/ PostGIS: GIS Database Leaflet: Javascript Library OpenStreetMap(OSM) GeoJSON

Geodjango
Geographic Web Framework Provide API to work with Geospatial data Application with MTV pattern

PostGIS
An extension of Postgres Enable spatial data storage & query Many useful buit-in functions to work with geographic data

Leaflet
User interaction library Fast, pleasant user experience. Easy to use, well-documented

OpenStretMap (OSM)
Open project contributed by GIS users around the world >400k registered users Absolutely FREE !

GeoJSON
A lightweight data-interchange format JSON (JavaScript Object Notation) Format for encoding a variety of geographic data structures

Application

Models
class Category(models.Model): name = models.CharField(max_length=50) super_cat = models.CharField(max_length=50, default="")

class Restaurant(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=100) description = models.TextField(null=True) categories = models.ManyToManyField(Category) pt = models.PointField() objects = models.GeoManager()

Database Design

Views
def index(request): t = loader.get_template('restaurant.html) main_categories = Category.objects.filter(super_cat='') categories = [{"cat": item, "sub_cats": Category.objects.filter(super_cat=item.name) } for item in main_categories] c = Context({ 'categories': categories }) return HttpResponse(t.render(c))

Views
def restaurant_list(request): bbox = request.GET['bbox'].split(',') poly = Polygon.from_bbox(bbox) cats = request.GET['cat'].split(',')[:-1] cats = [int(cat_id) for cat_id in cats] restaurants = Restaurant.objects.filter(pt__within=poly, categories__pk__in=cats) restaurants = list(set(restaurants)) geojson_dict = { "type": "FeatureCollection", "features": [restaurantToJSON(restaurant) for restaurant in restaurants] } # Return the response return HttpResponse(json.dumps(geojson_dict), content_type="application/json")

You might also like