GIS, Python and company

    Igor Pochechuev
    22-23 October, 2011
    Kyiv, Ukraine
Content
●   What is geometry?
    geometry formats
●   How to store geo data?
    spatial databases
●   Where we can get geo data?
●   Render it!
●   Demo
General provisions
     what we got
Geometry
                       formats

●   WKT, WKB
●   KML, GML (xml based)
●   GeoJSON (json based)
●   Shapefile (ESRI open specification)
●   etc.
Geometry
                     common types

●   Point
    (x, y)
●   Line
    (<point>, <point1>, <point2>, …)
●   Polygon
    (<line>, <line1>, <line2>, …)
MultiGeometry
                 additional types of geometry

●   MultiPoint
    (<point>, <point1>, <point2>, ...)
●   MultiLine
    (<line>, <line1>, <line2>, ...)
●   MultiPolygon
    (<polygon>, <polygon1>, <polygon2>, ...)
Database layer
  storing spatial data
Storing data
                     spatial databases
●   SpatiaLite (SQLite)
●   MySQL
●   Oracle
●   PostGIS (PostgreSQL)
Storing data
   right choice




PostGIS
How to get geo data?
      vector data
Getting data
     vector data


OpenStreetMap
OpenStreetMap
                              stats

●   < 400 000 registered users
●   ~ 1 100 000 000 nodes (points)
●   100 000 000 ways
●   ~ 800 — 3 000 nodes added each day
●   ~ 100 000 — 300 000 ways added each day
●   More statistics:
    http://wiki.openstreetmap.org/wiki/Stats
OpenStreetMap
                              downloading ...

●   Download planet.osm
    http://wiki.openstreetmap.org/wiki/Planet.osm
●   OSM API
    http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145

●   JOSM (download through desktop app)
    http://wiki.openstreetmap.org/wiki/JOSM
●   Third part services (GIS LAB)
    http://gis-lab.info/projects/osm-export.html
Loading data to database
>wget http://wiki.openstreetmap.org/wiki/Planet.osm
OSM2PgSQL
                                    loading data

●   osm2pgsql
    -d <db_name>
    -H <db_host>
    -U <db_user>
    -P <db_port>
    -c # create tables if needed
    -s # store tmp data in database
    -S osm.import.style # mapping style
    -k # add tags without column to an additional hstore
    -z extra_data # hstore column containing all tags that start with 'extra_data'
    RU-KRS.osm # path to OSM data file
Rendering ...
Rendering
             tool choosing




MapServer                    Mapnik
Mapnik
                       description

●   Written in C++
●   Python interface
●   OpenStreetMap, Flickr, CloudMade use mapnik
●   PostGIS, Shapefiles, GeoTIFF, OSM XML, ...
●   PNG, JPG, SVG, PDF, ...
Mapnik
map example
Layers
 intro
Mapnik
                                  code example

import mapnik

m = mapnik.Map(600,300,"+proj=latlong +datum=WGS84")
m.background = mapnik.Color('steelblue')

r = mapnik.Rule()
r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')))
r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1))

s = mapnik.Style()
s.rules.append(r)
m.append_style('World Style',s)
Mapnik
                                  code example

import mapnik

m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84")
m.background = mapnik.Color('steelblue')

r = mapnik.Rule()
r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')))
r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1))

s = mapnik.Style()
s.rules.append(r)
m.append_style('World Style',s)
Mapnik
                                  code example

import mapnik

m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84")
m.background = mapnik.Color('steelblue')

r = mapnik.Rule()
r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')))
r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'), 0.1))

s = mapnik.Style()
s.rules.append(r)
m.append_style('World Style',s)
Mapnik
                                  code example

import mapnik

m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84")
m.background = mapnik.Color('steelblue')

r = mapnik.Rule()
r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')))
r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'), 0.1))

s = mapnik.Style()
s.rules.append(r)
m.append_style('World Style',s)
Mapnik
                             code example 2

lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84")

lyr.datasource = mapnik.Shapefile(file='world_borders.shp')

lyr.styles.append('World Style')

m.layers.append(lyr)

m.zoom_to_box(lyr.envelope())

mapnik.render_to_file(m,'world.png', 'png')
Mapnik
                             code example 2

lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84")

lyr.datasource = mapnik.Shapefile(file='world_borders.shp')

lyr.styles.append('World Style')

m.layers.append(lyr)

m.zoom_to_box(lyr.envelope())

mapnik.render_to_file(m,'world.png', 'png')
Mapnik
                             code example 2

lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84")

lyr.datasource = mapnik.Shapefile(file='world_borders.shp')

lyr.styles.append('World Style')

m.layers.append(lyr)

m.zoom_to_box(lyr.envelope())

mapnik.render_to_file(m,'world.png', 'png')
Map in browser
Mapnik
tile description
Mapnik
                       map services

●   Tile Map Service
●   Web Map Service
Mapnik
                    TMS

http: //website.com/<zoom>/<x>/<y>.png

../tiles/12/2451/1360.png
Mapnik
                             using osm scripts

●   svn export http://svn.openstreetmap.org/applications/rendering/...
●   ./generate_xml.py --password 'value' --host 'value' --port 'value'
    --user 'value' --dbname 'value'
●   MAPNIK_MAP_FILE='osm.xml' MAPNIK_TILE_DIR='tiles/'
    ./generate_tiles.py
Mapnik XML
<Map bgcolor="#b5d0d0">
<Style name="map_style">
   <Rule>
       <MaxScaleDenominator>[scale]</MaxScaleDenominator>
       <MinScaleDenominator>[scale]</MinScaleDenominator>
       <Filter>[field]='value'</Filter>
       <PointSymbolizer/>
       <LineSymbolizer/>
       <PolygonSymbolizer/>
       …
   </Rule>
</Style>
Mapnik XML
<Layer name="misc_boundaries">

<StyleName>map_style</StyleName>
<Datasource>
   <Parameter name="table">
       (select way,way_area,name,boundary from polygon
       where boundary='national_park') as boundary
   </Parameter>
   <Parameter name='dbname'>[dbname]</Parameter>
   <Parameter name='host'>[host]</Parameter>
   <Parameter name='user'>[user]</Parameter>
   …
</Datasource>
</Layer>
Tile generating



./generate_tiles.py
Got tiles, what's next?
Frontend maps
                    choosing tool

●   OpenLayers
●   GeoExt
●   Leaflet
OpenLayers
●   Most powerfull
●   Most used
●   Large community
●   OSM supported
●   OSM, Google, Yahoo maps in one string
●   TMS, WMS, WFS
●   GML, GeoJSON, GeoRSS
OpenLayers
Show time!
Thanks!
Questions?

Pycon2011

  • 1.
    GIS, Python andcompany Igor Pochechuev 22-23 October, 2011 Kyiv, Ukraine
  • 3.
    Content ● What is geometry? geometry formats ● How to store geo data? spatial databases ● Where we can get geo data? ● Render it! ● Demo
  • 4.
  • 5.
    Geometry formats ● WKT, WKB ● KML, GML (xml based) ● GeoJSON (json based) ● Shapefile (ESRI open specification) ● etc.
  • 6.
    Geometry common types ● Point (x, y) ● Line (<point>, <point1>, <point2>, …) ● Polygon (<line>, <line1>, <line2>, …)
  • 7.
    MultiGeometry additional types of geometry ● MultiPoint (<point>, <point1>, <point2>, ...) ● MultiLine (<line>, <line1>, <line2>, ...) ● MultiPolygon (<polygon>, <polygon1>, <polygon2>, ...)
  • 8.
    Database layer storing spatial data
  • 9.
    Storing data spatial databases ● SpatiaLite (SQLite) ● MySQL ● Oracle ● PostGIS (PostgreSQL)
  • 10.
    Storing data right choice PostGIS
  • 11.
    How to getgeo data? vector data
  • 12.
    Getting data vector data OpenStreetMap
  • 13.
    OpenStreetMap stats ● < 400 000 registered users ● ~ 1 100 000 000 nodes (points) ● 100 000 000 ways ● ~ 800 — 3 000 nodes added each day ● ~ 100 000 — 300 000 ways added each day ● More statistics: http://wiki.openstreetmap.org/wiki/Stats
  • 14.
    OpenStreetMap downloading ... ● Download planet.osm http://wiki.openstreetmap.org/wiki/Planet.osm ● OSM API http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145 ● JOSM (download through desktop app) http://wiki.openstreetmap.org/wiki/JOSM ● Third part services (GIS LAB) http://gis-lab.info/projects/osm-export.html
  • 15.
  • 16.
  • 17.
    OSM2PgSQL loading data ● osm2pgsql -d <db_name> -H <db_host> -U <db_user> -P <db_port> -c # create tables if needed -s # store tmp data in database -S osm.import.style # mapping style -k # add tags without column to an additional hstore -z extra_data # hstore column containing all tags that start with 'extra_data' RU-KRS.osm # path to OSM data file
  • 18.
  • 19.
    Rendering tool choosing MapServer Mapnik
  • 20.
    Mapnik description ● Written in C++ ● Python interface ● OpenStreetMap, Flickr, CloudMade use mapnik ● PostGIS, Shapefiles, GeoTIFF, OSM XML, ... ● PNG, JPG, SVG, PDF, ...
  • 21.
  • 22.
  • 23.
    Mapnik code example import mapnik m = mapnik.Map(600,300,"+proj=latlong +datum=WGS84") m.background = mapnik.Color('steelblue') r = mapnik.Rule() r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9'))) r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1)) s = mapnik.Style() s.rules.append(r) m.append_style('World Style',s)
  • 24.
    Mapnik code example import mapnik m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84") m.background = mapnik.Color('steelblue') r = mapnik.Rule() r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9'))) r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1)) s = mapnik.Style() s.rules.append(r) m.append_style('World Style',s)
  • 25.
    Mapnik code example import mapnik m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84") m.background = mapnik.Color('steelblue') r = mapnik.Rule() r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9'))) r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'), 0.1)) s = mapnik.Style() s.rules.append(r) m.append_style('World Style',s)
  • 26.
    Mapnik code example import mapnik m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84") m.background = mapnik.Color('steelblue') r = mapnik.Rule() r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9'))) r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'), 0.1)) s = mapnik.Style() s.rules.append(r) m.append_style('World Style',s)
  • 27.
    Mapnik code example 2 lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84") lyr.datasource = mapnik.Shapefile(file='world_borders.shp') lyr.styles.append('World Style') m.layers.append(lyr) m.zoom_to_box(lyr.envelope()) mapnik.render_to_file(m,'world.png', 'png')
  • 28.
    Mapnik code example 2 lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84") lyr.datasource = mapnik.Shapefile(file='world_borders.shp') lyr.styles.append('World Style') m.layers.append(lyr) m.zoom_to_box(lyr.envelope()) mapnik.render_to_file(m,'world.png', 'png')
  • 29.
    Mapnik code example 2 lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84") lyr.datasource = mapnik.Shapefile(file='world_borders.shp') lyr.styles.append('World Style') m.layers.append(lyr) m.zoom_to_box(lyr.envelope()) mapnik.render_to_file(m,'world.png', 'png')
  • 30.
  • 31.
  • 32.
    Mapnik map services ● Tile Map Service ● Web Map Service
  • 33.
    Mapnik TMS http: //website.com/<zoom>/<x>/<y>.png ../tiles/12/2451/1360.png
  • 34.
    Mapnik using osm scripts ● svn export http://svn.openstreetmap.org/applications/rendering/... ● ./generate_xml.py --password 'value' --host 'value' --port 'value' --user 'value' --dbname 'value' ● MAPNIK_MAP_FILE='osm.xml' MAPNIK_TILE_DIR='tiles/' ./generate_tiles.py
  • 35.
    Mapnik XML <Map bgcolor="#b5d0d0"> <Stylename="map_style"> <Rule> <MaxScaleDenominator>[scale]</MaxScaleDenominator> <MinScaleDenominator>[scale]</MinScaleDenominator> <Filter>[field]='value'</Filter> <PointSymbolizer/> <LineSymbolizer/> <PolygonSymbolizer/> … </Rule> </Style>
  • 36.
    Mapnik XML <Layer name="misc_boundaries"> <StyleName>map_style</StyleName> <Datasource> <Parameter name="table"> (select way,way_area,name,boundary from polygon where boundary='national_park') as boundary </Parameter> <Parameter name='dbname'>[dbname]</Parameter> <Parameter name='host'>[host]</Parameter> <Parameter name='user'>[user]</Parameter> … </Datasource> </Layer>
  • 37.
  • 38.
  • 39.
    Frontend maps choosing tool ● OpenLayers ● GeoExt ● Leaflet
  • 40.
    OpenLayers ● Most powerfull ● Most used ● Large community ● OSM supported ● OSM, Google, Yahoo maps in one string ● TMS, WMS, WFS ● GML, GeoJSON, GeoRSS
  • 41.
  • 42.
  • 43.