GeoDjango in a nutshell
                                 Vasyl Dizhak
                           http://djangostars.com/




Friday, August 26, 2011
• Into
                     • Standards
                     • Django/GeoDjango
                     • Demo app
                     • Open source

Friday, August 26, 2011
Friday, August 26, 2011
“80% of projects and
                     services include spatial
                            data” Cliché



Friday, August 26, 2011
“nere things are more
                   related than distant
                         things”
                          W.R.Tobler (1970)




Friday, August 26, 2011
Geographic information
                     systems(GIS)
                     • Standards, spatial
                          data, coordinate
                          systems

                     • Maps are flat, world is
                          not




Friday, August 26, 2011
Coordinate systems
             Geodetic (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
Standards
                     •    OGC: Open Geospatial Consortium. Inc.

                          •   Promulgates standards for geospatial
                              and 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
•    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
Near things
                     •    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)



Friday, August 26, 2011
Geometry objects

         •        Point
         •        LineString
         •        LinearRing
         •        Polygon

         •        MultiPoint

         •        MultiString

         •        MultiPolygon

         •        GeometryCollection


Friday, August 26, 2011
Django

                     • Python based framework
                     • Powerful ORM
                     • MTV concepts
                     • Support unix and windows platforms

Friday, August 26, 2011
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
Friday, August 26, 2011
http://houstoncrimemaps.com/


Friday, August 26, 2011
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
Real using




                          http://www.everyblock.com/

Friday, August 26, 2011
• http://www.nytimes.com/
                     • http://projects.latimes.com/mapping-la/
                       neiborhoods/

                     • http://marinemap.org/
                     • http://earth.burningman.com/
                     • http://about.nsw.gov.au/
                     • and others ...

Friday, August 26, 2011
GEOS
                          • Geometry Engine Open Source
                            In [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
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
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
GDAL
                          Geospatial 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
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
GeoIP
                          IP-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
Geometry Fields
                          •   GeometryField

                          •   PointField

                          •   LineStringField

                          •   PolygonField

                          •   MultiPointField

                          •   MultiLineStringField

                          •   MultiPolygonField

                          •   GeometryCollectionField



Friday, August 26, 2011
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
PostgreSQL SQL




Friday, August 26, 2011
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
Spatial Lookups




Friday, August 26, 2011
Lookup types
   PostGIS is a rock star :)
  As parameters lookups
 can be any data that can
 be converted into GEOS
      geometry like
   GeoJSON, WKT etc.

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




Friday, August 26, 2011
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
Measurement objects

                          Conversation between measurement units




Friday, August 26, 2011
Distance, D
                In [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
Area, A
                 In [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
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
GeoQuerySet Methods

  MySQL does not
support any of these




                          more ...


Friday, August 26, 2011
Aggregate Methods

                     • collect
                     • extend
                     • extend3d
                     • make_line
                     • unionagg

Friday, August 26, 2011
Output methods
          Done more on db level

                     • kml()
                     • gml()
                     • svg()
                     • geojson()
                     • geohash()

Friday, August 26, 2011
Measurement methods

                     • area()(attachs the area to each geometry
                          field in the queryset as Area object )
                     • distance(geom)
                     • length()
                     • perimeter()

Friday, August 26, 2011
Geometry operations

                     • difference(geom)
                     • intersection(geom)
                     • union(geom)
                     • sym_difference(geom)

Friday, August 26, 2011
Geometry methods
                     • centroid()
                     • envelope()
                     • point_on_surface()
                     • scale(x, y, z=0)
                     • translate(x, y, z=0)
                     • transform(srid)
Friday, August 26, 2011
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
Show time



Friday, August 26, 2011
Related projects

                     • FeatureServer http://featureserver.org
                     • TileCache http://tilecache.org
                     • TileStache http://tilestache.org/
                     • Mapproxy http://mapproxy.org/



Friday, August 26, 2011
Cartography

                     • Mapnik http://mapnik.org
                     • MapServer http://mapserver.org




Friday, August 26, 2011
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
Many thanks
              Vasyl Dizhak
                 http://twitter.com/rootart
                http://twitter.com/djangostars
                      http://geojam.djangostars.com
                      http://github.com/rootart/geojam
             Questions?




Friday, August 26, 2011

GeoDjango in a nutshell

  • 1.
    GeoDjango in anutshell Vasyl Dizhak http://djangostars.com/ Friday, August 26, 2011
  • 2.
    • Into • Standards • Django/GeoDjango • Demo app • Open source Friday, August 26, 2011
  • 3.
  • 4.
    “80% of projectsand services include spatial data” Cliché Friday, August 26, 2011
  • 5.
    “nere things aremore related than distant things” W.R.Tobler (1970) Friday, August 26, 2011
  • 6.
    Geographic information systems(GIS) • Standards, spatial data, coordinate systems • Maps are flat, world is not Friday, August 26, 2011
  • 7.
    Coordinate systems Geodetic (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
  • 8.
    Standards • OGC: Open Geospatial Consortium. Inc. • Promulgates standards for geospatial and 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
  • 9.
    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
  • 10.
    Near things • 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) Friday, August 26, 2011
  • 11.
    Geometry objects • Point • LineString • LinearRing • Polygon • MultiPoint • MultiString • MultiPolygon • GeometryCollection Friday, August 26, 2011
  • 12.
    Django • Python based framework • Powerful ORM • MTV concepts • Support unix and windows platforms Friday, August 26, 2011
  • 13.
    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
  • 14.
  • 15.
  • 16.
    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
  • 17.
    Real using http://www.everyblock.com/ Friday, August 26, 2011
  • 18.
    • http://www.nytimes.com/ • http://projects.latimes.com/mapping-la/ neiborhoods/ • http://marinemap.org/ • http://earth.burningman.com/ • http://about.nsw.gov.au/ • and others ... Friday, August 26, 2011
  • 19.
    GEOS • Geometry Engine Open Source In [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
  • 20.
    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
  • 21.
    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
  • 22.
    GDAL Geospatial 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
  • 23.
    You can transformcoordinates 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
  • 24.
    GeoIP IP-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
  • 25.
    Geometry Fields • GeometryField • PointField • LineStringField • PolygonField • MultiPointField • MultiLineStringField • MultiPolygonField • GeometryCollectionField Friday, August 26, 2011
  • 26.
    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
  • 27.
  • 28.
    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
  • 29.
  • 30.
    Lookup types PostGIS is a rock star :) As parameters lookups can be any data that can be converted into GEOS geometry like GeoJSON, WKT etc. https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#spatial-lookup-compatibility Friday, August 26, 2011
  • 31.
    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
  • 32.
    Measurement objects Conversation between measurement units Friday, August 26, 2011
  • 33.
    Distance, D In [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
  • 34.
    Area, A In [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
  • 35.
    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
  • 36.
    GeoQuerySet Methods MySQL does not support any of these more ... Friday, August 26, 2011
  • 37.
    Aggregate Methods • collect • extend • extend3d • make_line • unionagg Friday, August 26, 2011
  • 38.
    Output methods Done more on db level • kml() • gml() • svg() • geojson() • geohash() Friday, August 26, 2011
  • 39.
    Measurement methods • area()(attachs the area to each geometry field in the queryset as Area object ) • distance(geom) • length() • perimeter() Friday, August 26, 2011
  • 40.
    Geometry operations • difference(geom) • intersection(geom) • union(geom) • sym_difference(geom) Friday, August 26, 2011
  • 41.
    Geometry methods • centroid() • envelope() • point_on_surface() • scale(x, y, z=0) • translate(x, y, z=0) • transform(srid) Friday, August 26, 2011
  • 42.
    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
  • 43.
  • 44.
    Related projects • FeatureServer http://featureserver.org • TileCache http://tilecache.org • TileStache http://tilestache.org/ • Mapproxy http://mapproxy.org/ Friday, August 26, 2011
  • 45.
    Cartography • Mapnik http://mapnik.org • MapServer http://mapserver.org Friday, August 26, 2011
  • 46.
    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
  • 47.
    Many thanks Vasyl Dizhak http://twitter.com/rootart http://twitter.com/djangostars http://geojam.djangostars.com http://github.com/rootart/geojam Questions? Friday, August 26, 2011