geodjango in a nutshell

47
GeoDjango in a nutshell Vasyl Dizhak http://djangostars.com / Friday, August 26, 2011

Upload: django-stars

Post on 17-May-2015

8.103 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: GeoDjango in a nutshell

GeoDjango in a nutshellVasyl Dizhak

http://djangostars.com/

Friday, August 26, 2011

Page 2: GeoDjango in a nutshell

• Into

• Standards

• Django/GeoDjango

• Demo app

• Open source

Friday, August 26, 2011

Page 3: GeoDjango in a nutshell

Friday, August 26, 2011

Page 4: GeoDjango in a nutshell

“80% of projects and services include spatial

data” Cliché

Friday, August 26, 2011

Page 5: GeoDjango in a nutshell

“nere things are more related than distant

things”W.R.Tobler (1970)

Friday, August 26, 2011

Page 6: GeoDjango in a nutshell

Geographic information systems(GIS)

• Standards, spatial data, coordinate systems

• Maps are flat, world is not

Friday, August 26, 2011

Page 7: GeoDjango in a nutshell

Coordinate systemsGeodetic (coordinates are in angels latitude/

longitude)

Projected (represents world surface in a flat way, using linear units and play with various distortions)

Friday, August 26, 2011

Page 8: GeoDjango in a nutshell

Standards• OGC: Open Geospatial Consortium. Inc.

• Promulgates standards for geospatialand location based services

• SFS: Simple Feature Access for SQL

• OGC standard for spatial databases

• The most popular

• WKT: Well Known Text

• Geometries

• Coordinate systems

Friday, August 26, 2011

Page 9: GeoDjango in a nutshell

• WKB: Well Known Binary

• Binary representation of geometries

• HEX: Binary

• EWKT

• EWKB, HEXEWKB

• GML: Geometry Markup Language (XML based)

• KML: Keyhole Markup Language

• Google Earth/Maps Format

• WMS: Web Mapping Service(serves up images)

• WFS: Web Feature Service (serves up vector data)

Friday, August 26, 2011

Page 10: GeoDjango in a nutshell

• GeoJSON: Geometry JSON (not in OGC standard)

• SRS: Spatial Reference System

• CRS: Coordinate Reference System (same as SRS)

• EPSG: European Petroleum Survey Group

• SRID: Spatial Reference Indenifier (corresponds to EPSG number)

Near things

Friday, August 26, 2011

Page 11: GeoDjango in a nutshell

• Point

• LineString

• LinearRing

• Polygon

• MultiPoint

• MultiString

• MultiPolygon

• GeometryCollection

Geometry objects

Friday, August 26, 2011

Page 12: GeoDjango in a nutshell

• Python based framework

• Powerful ORM

• MTV concepts

• Support unix and windows platforms

Django

Friday, August 26, 2011

Page 13: GeoDjango in a nutshell

GeoDjango

• Based on the top of django, so include all benefits :)

• Include python interfaces for GEOS, GDAL, GeoIP thanks to ctypes support in python

Friday, August 26, 2011

Page 14: GeoDjango in a nutshell

Friday, August 26, 2011

Page 15: GeoDjango in a nutshell

http://houstoncrimemaps.com/

Friday, August 26, 2011

Page 16: GeoDjango in a nutshell

History

• Initially created by Justin Bronn

• Started in 2006

• 2008 - 1.0 release

• 2009 - 1.1 release

• 2010 - 1.2 release

• 2011 - 1.3 release

Friday, August 26, 2011

Page 17: GeoDjango in a nutshell

Real using

http://www.everyblock.com/

Friday, August 26, 2011

Page 19: GeoDjango in a nutshell

GEOS

• Geometry Engine Open SourceIn [3] from django.contrib.gis.geos import *

In [4]: point = GEOSGeometry('Point(0 10)')

In [5]: point.coords

Out[5]: (0.0, 10.0)

Friday, August 26, 2011

Page 20: GeoDjango in a nutshell

In [7]: coords = ((0, 0), (0, 10), (10, 13), (0, 0))

In [8]: polygon = Polygon(coords)

In [9]: mpolygon = MultiPolygon(polygon)

Friday, August 26, 2011

Page 21: GeoDjango in a nutshell

In [33]: polygon.wkt

Out[33]: 'POLYGON ((0.0000000000000000 0.0000000000000000, 0.0000000000000000 10.0000000000000000, 10.0000000000000000 13.0000000000000000, 0.0000000000000000 0.0000000000000000))'

In [34]: polygon.contains(Point(11, 12))

Out[34]: False

In [35]: polygon.json

Out[35]: '{ "type": "Polygon", "coordinates": [ [ [ 0.000000, 0.000000 ], [ 0.000000, 10.000000 ], [ 10.000000, 13.000000 ], [ 0.000000, 0.000000 ] ] ] }'

In [36]: polygon.kml

Out[36]: '<Polygon><outerBoundaryIs><LinearRing><coordinates>0.0,0.0,0 0.0,10.0,0 10.0,13.0,0 0.0,0.0,0</coordinates></LinearRing></outerBoundaryIs></Polygon>'

Friday, August 26, 2011

Page 22: GeoDjango in a nutshell

GDALGeospatial Data Abstraction Library

In [2]: from django.contrib.gis.gdal import *...In [28]: k = DataSource('data/ukraine_administrative.shp')

In [29]: layer = k[0]

In [30]: layer

Out[30]: <django.contrib.gis.gdal.layer.Layer object at 0x10325f990>

In [31]: len(layer)

Out[31]: 9976

In [32]: layer.fields

Out[32]: ['NAME', 'ADMIN_LEVE']

Friday, August 26, 2011

Page 23: GeoDjango in a nutshell

You can transform coordinates from one projection to another

In [37]: s = SpatialReference(4326)

In [39]: k = GEOSGeometry('Point(10 10)', s)

In [40]: k.transform(900913)

Friday, August 26, 2011

Page 24: GeoDjango in a nutshell

GeoIPIP-based Geolocation binding to MaxMind

In [1]: from django.contrib.gis.utils import GeoIP

In [2]: g = GeoIP()

In [6]: g.country('it-jam.ciklum.net')

Out[6]: {'country_name': 'Ukraine', 'country_code': 'UA'}

In [7]: g.city('it-jam.ciklum.net')

Out[7]: {'city': None, 'region': None, 'area_code': 0, 'longitude': 32.0, 'country_code3': 'UKR', 'latitude': 49.0, 'postal_code': None, 'dma_code': 0, 'country_code': 'UA', 'country_name': 'Ukraine'}

In [8]: g.geos('it-jam.ciklum.net')

Out[8]: <Point object at 0x103482fd0>

In [9]: g.geos('it-jam.ciklum.net').coords

Out[9]: (32.0, 49.0)

Friday, August 26, 2011

Page 25: GeoDjango in a nutshell

Geometry Fields• GeometryField

• PointField

• LineStringField

• PolygonField

• MultiPointField

• MultiLineStringField

• MultiPolygonField

• GeometryCollectionField

Friday, August 26, 2011

Page 26: GeoDjango in a nutshell

Models example

from django.contrib.gis.db import models

class Checkin(models.Model): type = models.CharField(max_length=1, choices=CHECKIN_TYPE) title = models.CharField(max_length=255, blank=True, null=True) geodata = models.PointField() updated = models.DateTimeField(auto_now=True)

objects = models.GeoManager()

Friday, August 26, 2011

Page 27: GeoDjango in a nutshell

PostgreSQL SQL

Friday, August 26, 2011

Page 28: GeoDjango in a nutshell

Geometry Field Options

• srid (Spatial Reference System Identity, corresponding to spatial_ref_sys db table, default is 4326)

• spatial_index (Spatial indexes are created by default)

• dim (Set dimension if backend supports this, default is 2, postgis only)

• geography

Friday, August 26, 2011

Page 29: GeoDjango in a nutshell

Spatial Lookups

Friday, August 26, 2011

Page 30: GeoDjango in a nutshell

Lookup types

https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#spatial-lookup-compatibility

PostGIS is a rock star :)

As parameters lookups can be any data that can be converted into GEOS

geometry like GeoJSON, WKT etc.

Friday, August 26, 2011

Page 31: GeoDjango in a nutshell

Example

In[8]: pnt = Point(40.187953, 49.9658546)

In[9]: Administrative.objects.get(geodata__contains=pnt)

Out[10]: <Administrative: 9976>

Friday, August 26, 2011

Page 32: GeoDjango in a nutshell

Measurement objects

Conversation between measurement units

Friday, August 26, 2011

Page 33: GeoDjango in a nutshell

Distance, DIn [1]: from django.contrib.gis.measure import D

In [2]: dist = D(km=500)

In [3]: dist.mi

Out[3]: 310.68559611866698

In [4]: dist.rod

Out[4]: 99419.390757973422

Friday, August 26, 2011

Page 34: GeoDjango in a nutshell

Area, AIn [1]: from django.contrib.gis.measure import A

In [2]: a = A(sq_m = 10)

In [3]: a.sq_km

Out[3]: 1.0000000000000001e-05

In [4]: a.sq_rod

Out[4]: 0.39536861034746451

Friday, August 26, 2011

Page 35: GeoDjango in a nutshell

Distance queries*In [10]: Checkin.objects.filter(geodata__distance_gte=(pnt, 7000))

Out[10]: [<Checkin: Ресторан «Домашня Кухня»>, <Checkin: Великий Вiз>, <Checkin: Ресторан «Домашня Кухня»>, <Checkin: Станція швидкісного трамвая "Польова">, <Checkin: НТУУ "КПИ", корпус 7>]

Friday, August 26, 2011

Page 36: GeoDjango in a nutshell

GeoQuerySet Methods

more ...

MySQL does not support any of these

Friday, August 26, 2011

Page 37: GeoDjango in a nutshell

Aggregate Methods

• collect

• extend

• extend3d

• make_line

• unionagg

Friday, August 26, 2011

Page 38: GeoDjango in a nutshell

Output methods

• kml()

• gml()

• svg()

• geojson()

• geohash()

Done more on db level

Friday, August 26, 2011

Page 39: GeoDjango in a nutshell

Measurement methods

• area()(attachs the area to each geometry field in the queryset as Area object )

• distance(geom)

• length()

• perimeter()

Friday, August 26, 2011

Page 40: GeoDjango in a nutshell

Geometry operations

• difference(geom)

• intersection(geom)

• union(geom)

• sym_difference(geom)

Friday, August 26, 2011

Page 41: GeoDjango in a nutshell

Geometry methods

• centroid()

• envelope()

• point_on_surface()

• scale(x, y, z=0)

• translate(x, y, z=0)

• transform(srid)

Friday, August 26, 2011

Page 42: GeoDjango in a nutshell

In [10]: k = Administrative.objects.kml().svg().perimeter().point_on_surface().get(id=10)

In [11]: k.point_on_surface.coords

Out[11]: (22.153155999999999, 48.512821000000002)

In [12]: k.kml

Out[12]: u'<LineString><coordinates>22.166018,48.5771299

Friday, August 26, 2011

Page 43: GeoDjango in a nutshell

Show time

Friday, August 26, 2011

Page 44: GeoDjango in a nutshell

• FeatureServer http://featureserver.org

• TileCache http://tilecache.org

• TileStache http://tilestache.org/

• Mapproxy http://mapproxy.org/

Related projects

Friday, August 26, 2011

Page 45: GeoDjango in a nutshell

Cartography

• Mapnik http://mapnik.org

• MapServer http://mapserver.org

Friday, August 26, 2011

Page 46: GeoDjango in a nutshell

Frontend interaction

• OpenLayers http://openlayer.org

• Google maps js api

• Polymaps http://polymaps.org/

• Wax http://mapbox.github.com/wax/manual/

• TileMill http://tilemill.com/pages/index.html

Friday, August 26, 2011

Page 47: GeoDjango in a nutshell

Questions?

Many thanksVasyl Dizhak

http://twitter.com/rootarthttp://twitter.com/djangostars

http://geojam.djangostars.comhttp://github.com/rootart/geojam

Friday, August 26, 2011