pycon 2012 taiwan
TRANSCRIPT
那些年Python攻佔GIS The Year Python Takes Over GIS
鄧東波 Dongpo Deng中央研究院資訊科學研究所
﹠胡崇偉 marr, Tsung Wei Hu中央研究院人文社會科學中心
PyCon 2012
Saturday, June 9, 2012
Content
• Introduction - GIS and why GIS uses python
• Python-blinding core geospatial libraries
• the use of python in Desktop GISs
• Web application framework for geospatial
Saturday, June 9, 2012
What is GIS?• GIS is stand for Geographic
Information System
• integrates hardware, software, and data for capturing, managing, analyzing, and displaying geospatial data.
• allows people to use methods for understanding, interpreting, and visualizing relationships and patterns of geospatial data
Saturday, June 9, 2012
Why Geospatial domain uses Python
• Easy to learn
• Code is readable
• Large community
• Easy interaction with C and Java libraries
• Many existing modules and packages
• core geospatial libraries
• map rendering
• database
• web server
Picture from http://pypi.python.org/pypi/collective.geo.bundle
Saturday, June 9, 2012
Why Geospatial domain uses Python
• Easy to learn
• Code is readable
• Large community
• Easy interaction with C and Java libraries
• Many existing modules and packages
• core geospatial libraries
• map rendering
• database
• web server
Picture from http://pypi.python.org/pypi/collective.geo.bundle
Saturday, June 9, 2012
Geospatial development tasks
• Visualizing geospatial data
Saturday, June 9, 2012
Geospatial development tasks
• Analyzing geospatial data
• e.g. How many people should escape as Kuosheng nuclear power plant (核二廠) explodes?
• Create a geospatial mashup
Saturday, June 9, 2012
The geospatial development tasks involve
• Math- analytic geometry
• e.g.Euclidean geometry
• Computer graphics (rendering)
• e.g. rending, visualizing
• Database
• General Search Tree (GiST)
• open geospatial standards,
• e.g. GML, WKT
Saturday, June 9, 2012
Python libraries for geospatial development
• Reading/ Writing geospatial data
• GDAL/OGR
• Dealing with Projections
• pyproj
• Analyzing and manipulating geospatial data
• Shapely
• Visualizing geospatial data
• Mapnik
Saturday, June 9, 2012
GDAL (Geospatial Data Abstraction Library)
from osgeo import gdal,gdalconstimport structdataset = gdal.Open("DEM.dat")band = dataset.GetRasterBand(1)fmt = "<" + ("h" * band.XSize)totHeight = 0for y in range(band.YSize): scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1,band.DataType) values = struct.unpack(fmt, scanline) for value in values: totHeight = totHeight + valueaverage = totHeight / (band.XSize * band.YSize)print "Average height =", average
• read through it one scanline at a time from file
• use the struct standard Python library module to read the individual values out of the scanline.
• Each value corresponds to the height of that point, in meters
Source from: Westra, 2010, Python Geospatial DevelopmentSaturday, June 9, 2012
OGR(OpenGIS Simple Features Reference Implementation)
• uses OGR to read through the contents of a Shapefile,
• printing out the value of the NAME attribute for each feature, along with the geometry type
from osgeo import ogr
shapefile = ogr.Open("TM_WORLD.shp")
layer = shapefile.GetLayer(0)
for i in range(layer.GetFeatureCount()):
feature = layer.GetFeature(i)
name = feature.GetField("NAME")
geometry = feature.GetGeometryRef()
print i, name, geometry.GetGeometryName()
Source from: Westra, 2010, Python Geospatial DevelopmentSaturday, June 9, 2012
PyProj• a location specified using UTM zone 17 coordinates.
• Using two Proj objects to define the UTM Zone 17 and lat/long projections
• translates this location's coordinates into latitude and longitude values
import pyproj
UTM_X = 565718.523517
UTM_Y = 3980998.9244
srcProj = pyproj.Proj(proj="utm", zone="11",
ellps="clrk66", units="m")
dstProj = pyproj.Proj(proj='longlat', ellps='WGS84',
datum='WGS84')
long,lat = pyproj.transform(srcProj, dstProj, UTM_X, UTM_Y)
print "UTM zone 17 coordinate (%0.4f, %0.4f) = %0.4f, %0.4f" %
(UTM_X, UTM_Y, lat, long)Source from: Westra, 2010, Python Geospatial Development
Saturday, June 9, 2012
Shapely GEOS
import shapely.geometrypt = shapely.geometry.Point(0, 0)circle = pt.buffer(1.0)square = shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])intersect = circle.intersection(square)for x,y in intersect.exterior.coords: print x,y
Source from: Westra, 2010, Python Geospatial DevelopmentSaturday, June 9, 2012
Mapnik
• Mapnik is an open source mapping toolkit for desktop- and server-based map rendering, written in C++.
• there are Python bindings to facilitate fast-paced agile development.
• OpenStreetMap project (OSM) uses Mapnik in combination with an Apache Web Server module (mod_tile) to render tiles that make up the OSM 'Slippy Map' Layer.
Source from: Westra, 2010, Python Geospatial Development
Saturday, June 9, 2012
Mapnikimport mapnik
symbolizer = mapnik.PolygonSymbolizer(
mapnik.Color("darkgreen"))
rule = mapnik.Rule()
rule.symbols.append(symbolizer)
style = mapnik.Style()
style.rules.append(rule)
layer = mapnik.Layer("mapLayer")
layer.datasource = mapnik.Shapefile(
file="TW_village.shp")
layer.styles.append("mapStyle")
map = mapnik.Map(800, 400)
map.background = mapnik.Color("steelblue")
map.append_style("mapStyle", style)
map.layers.append(layer)
map.zoom_all()
mapnik.render_to_file(map, "map.png", "png")
Saturday, June 9, 2012
Desktop GIS
Pic from http://www.pressebox.de/attachments/details/39739
Saturday, June 9, 2012
Script Languages in ESRI family
Saturday, June 9, 2012
ArcPy• ArcPy is a package for performing geographic data
analysis, data conversion, data management, and map automation in ArcGIS with Python.
• ArcPy includes three modules:
• mapping module (arcpy.mapping)
• Spatial analyst module (arcpy.sa)
• Geostatistical analyst module (arcpy.ga)
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/What_is_ArcPy/
ArcGIS ArcPy Python
Saturday, June 9, 2012
arcpy in ArcGIS 10
Saturday, June 9, 2012
Saturday, June 9, 2012
Saturday, June 9, 2012
Python in QGIS
• There’s a number of ways to access the layers in QGIS.
• Each way starts by first referencing the QgisInterface class which is called iface in the Python bindings.
Saturday, June 9, 2012
Example
Saturday, June 9, 2012
GeoDjango
Saturday, June 9, 2012
http://140.109.160.129:8000/admin/world/worldborders/Saturday, June 9, 2012
Remarks
• There are many Python libraries or applications for geospatial purposes
• Python is increasing its value in geospatial domain
• Will Python take over GIS? .....Let’s see!
Saturday, June 9, 2012