View
224
Download
1
Embed Size (px)
Citation preview
Using GeoDjango for user participation in enriching web GIS systems
Bo ZhaoUniversity of Florida
July 15th, 2009
Requests
ID Function Item1 CGA assigned logins authentication &
Permissions2 User login ability3 User uploads shape file to server shapefile uploading4 User specifies layer symbology
spatial data customizing & management
5 User populates Layer List tab6 User defines layer searchability7 User defines attributes returned8 Files that are generated if adding new layer Spatial Crud & related
operation9 Files effected when layer is removed
So we need…
• Web Framework–PHP, ASP, Python, Java
• Geospatial libraries–Commercial Geospatial libaries, Open
Source libaries.
Why Python Matters?
• Glue Language• Dynamic script Language
Open Source Geospatial Libs
• Pros– Open source is very competitive for geospatial server
software– Reduced total cost of ownership– Possibility that you own your own software– Better preparation for computing trends.
• Cons– Limited technical support.– Adding Patches or updating might render to crash.– Possibility that you own your own software
Why Geodjango?
“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.”
Features of Django Framework
• Object-relational mapper: Define your data models entirely in Python. You get a rich, dynamic database-access API, unnecessary to write SQL for query.
• Template system: Use template language to separate design, content and Python code.
• Automatic admin interface: Django does that automatically, and it's production-ready.
• Elegant URL design: Design pretty URLs with no framework-specific limitations. Be as flexible as you like.
• Cache system: cache frameworks for super performance.• Internationalization: Django has full support for multi-language
applications.
Design Pattern
MTVModel Template View
Model
Object-relational mapping
• Object-relational mapping (ORM)– a programming technique for converting data
between incompatible type systems in relational databases and object-oriented programming languages.
– ORM creates, in effect, a "virtual object database" that can be used from within the programming language.
CREATE TABLE “hug_layer_world" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(300) NOT NULL,
"geom" geometry NOT NULL,
CONSTRAINT hug_layer_world_pkey PRIMARY KEY (hug_fid),
CONSTRAINT enforce_dims_geom CHECK (ndims(geom) = 2),
CONSTRAINT enforce_geotype_geom CHECK (geometrytype(geom) =
'MULTIPOLYGON'::text OR geom IS NULL),
CONSTRAINT enforce_srid_geom CHECK (srid(geom) = 4326)
);
Scary Quirky Language
• SQL knows no version control
• Can be dangerous
• DRY(Don’t Repeat Yourself.)
from django.contrib.gis.db import models
class LAYERS(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=20)
geom = models.MultiLineStringField(srid=4326)
objects = models.GeoManager()
SELECT"hug_layer_world"."id","hug_layer_world".“name","hug_layer_world".“geom"FROM ""hug_layer_world"WHERE "hug_layer_world"."name" = "China";
WORLD.objects.filter(title='China')
Template
DRY(Don’t repeat yourself)
index.html
<html><head><title>{% block title %}{% endblock %}</title></head><body>{% block content %}{% endblock %}</body></html>
tilecache.cfg
[{{layer.name}}]url = {{layer.mf_url}} layers = {{layer.name}}spherical_mercator = {{layer.tc_spherical_mercator}}extension = {{layer.tc_extension}}metatile = {{layer.tc_metatile}}srs = EPSG:{{layer.tc_srs}}type = {{layer.tc_type}}searchable = {{layer.tc_searchable}}… …
dataLayer.js
amTemplates.{{layer.name}} = '<ul class="featureDetailList">' +{% for item in layer.alias %} {% ifnotequal item.name 'geom' %} '<li><label>{{item.alias}}</label><span> {{layer.left}}{{item.name}}{{layer.right}}</span></li>' + {% endifnotequal %}{% endfor %}'</ul>';
View
URLs
/export.php?id=2&type=tilecache/upload.aspx?filetype=shapefile
/export/tilecache/world//upload/shapefile/
url.py
• (r'^admin/upload/', 'upload_zipped_shapefiles.upload_zipfile'),
• (r'^admin/export/$', 'export_config_files.index'),
• (r'^databrowse/(.*)', databrowse.site.root),
• (r'^map/(?P<path>.*)$', 'django.views.static.serve', {'document_root':
settings.MAPS_DIR, 'show_indexes': True}),
View functions
def index(request):all_layers = LAYER.objects.all()all_maps = MAP.objects.all()return render_to_response('export.html', {'layers': all_layers, 'maps': all_maps, 'user': request.user})
App
world/ __init__.py admin.py models.py views.py templates/ layer_javascript.html layer_tilechache.html layer_mapfile.html
Hands-on demo
Thanks, any questions?
References
• http://www.djangoproject.com/• Justin Bronn, Web Applications for (Neo)Geographers with
Deadlines, Oct. 2008• http://en.wikipedia.org/wiki/Object-relational_mapping