Beyond arcpy, Python for GIS, Grant Miller-Francisco

  • 2,594 views
Uploaded on

Beyond arcpy | …

Beyond arcpy |
python for GIS |
Grant Miller-Francisco |
Presentation for GIS in Action conference |
Mar 29, 2011

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,594
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
81
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Beyond arcpy GIS in Action March 29-30, 2011 Grant Miller-Francisco GIS Analyst Sky Research Ashland, OR
  • 2. arcpy ArcGIS Python
  • 3. arcpy ArcGIS Python
  • 4.  
  • 5. {‘H3574’: ‘C:pathH3574.jpg’, ‘ H3575’: ‘C:pathH3575.jpg’, ‘ H3623’: ‘C:pathH3623.jpg’, . . . }
  • 6. {‘H3574’: ‘C:pathH3574.jpg’, ‘ H3575’: ‘C:pathH3575.jpg’, ‘ H3623’: ‘C:pathH3623.jpg’, . . . }
  • 7.
    • Yak shaving
  • 8.
    • IDEs:
    • pyscripter (http://code.google.com/p/pyscripter)
    • Wingware ( https://wingware.com/)
  • 9. The Standard Library
  • 10.
    • # handling paths with os.path
    • import os
    • f = r‘c:datasomefile.txt’
    • base = os.path.basename(f)
    • > ‘somefile.txt’
    • directory = os.path.dirname(f)
    • > ‘c:data’
    • full_path = os.path.join(directory, base)
    • > ‘c:datasomefile.txt’
  • 11.
    • # get list of files with glob
    • from glob import glob
    • shps = glob(r‘c:somedir*.shp’)
    • > [‘C:somedirshp1.shp’, ‘C:somedirshp2.shp’, …]
  • 12.
    • Other useful standard library modules:
    • csv
    • shutil
    • datetime
    • math
    • decimal
    • re
    • itertools
  • 13. Python Module of the Week http://www.doughellmann.com/PyMOTW/
  • 14.
    • Installing 3 rd Party
    • Python Packages
  • 15. The Python Package Index (PyPI) http://pypi.python.org/pypi
  • 16.  
  • 17.  
  • 18.  
  • 19.
    • setuptools / easy_install
    • >c:Python26Scripts> easy_install pip
  • 20.
    • pip
    • >pip install antigravity
    • >pip install antigravity==1.0.4
    • # use ==, >=, >, <, <=
    • >pip install ––upgrade antigravity
    • >pip uninstall antigravity
    • >pip search “kml”
    • >pip freeze > c:requirements.txt
    • >pip install –r c:requirements.txt
    • virtualenv – creates isolated Python environments
  • 21.
    • Yak shaving
    That wasn’t so bad!
  • 22.
    • Public Art near PSU ( http://www.civicapps.org/datasets)
  • 23.
    • tablib
  • 24.
    • import arcpy, csv
    • src = r'C:GIS_in_Action.gdbpublic_art_selected'
    • dest = r'C:datapublic_art_selected.csv‘
    • with open(dest, 'w') as f:
    • w = csv.writer(f, quoting=1, lineterminator='n')
    • fieldlist = arcpy.ListFields(src)
    • ignorefields = ['OBJECTID', 'Shape', 'detail_url']
    • fieldnames = [field.name for field in fieldlist if field.name not in ignorefields]
    • w.writerow(fieldnames)
    • rows = arcpy.SearchCursor(src)
    • for row in rows:
    • outputrow = []
    • for fieldname in fieldnames:
    • outputrow.append(str(row.getValue(fieldname)))
    • w.writerow(outputrow)
  • 25.
    • import tablib
    • src = r'C:datapublic_art_selected.csv'
    • contents = []
    • with open(src, 'r') as f:
    • for line in f:
    • contents.append(tuple(line.rstrip('n').split(',')))
    • data = tablib.Dataset(*contents[1:], headers=tuple(contents[0]))
  • 26.
    • import tablib
    • src = r'C:datapublic_art_selected.csv'
    • contents = []
    • with open(src, 'r') as f:
    • for line in f:
    • contents.append(tuple(line.rstrip('n').split(',')))
    • data = tablib.Dataset(*contents[1:], headers=tuple(contents[0]))
  • 27. print data.csv
    • artist,title,street,lat,lng,image_url,record_id
    • Steven Gillman,Peace Chant,SW Park Ave. and SW Columbia St.,45.514946,-122.683125,http://data.racc.org/pa_inventory/0657/0657.jpg,789
    • Paul Sutinen,In the Shadow of the Elm,SW Park Ave. and SW Market St.,45.513751,122.684352,http://data.racc.org/pa_inventory/0658/06 . . .
    • Oliver Barrett,Rebecca at the Well (Shemanski Fountain),SW Park Ave. and SW Main St.,45.516939,-122.682039,http://data.racc.org/pa_in. . .
    • James Carpenter,Spectral Light Dome,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0898/0898.jpg,966
    • Edo Period,Kano School Japanese Screen,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0906/0906.jpg,972
    • Linda Ethier,Muse Maze Mirror Gaze,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0989/0989.jpg,1065
    • Henk Pander,Portland Town,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0990/0990.jpg,1066
    • Tom Hardy,Running Horses,SW 6th Ave. and SW Madison St.,45.515729,-122.680413,http://data.racc.org/pa_inventory/1071/1071.jpg,1185
    • Alexander Phimister Proctor,Theodore Roosevelt Rough Rider,SW Park Ave. and SW Jefferson St.,45.515738,-122.683275,http://data.r . . .
    • George Fite Waters,Abraham Lincoln,SW Park Ave. and SW Madison St.,45.516406,-122.682923,http://data.racc.org/pa_inventory/0660/066 . . .
    • Juan Alonso,Diva,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1261/1261.jpg,1674
    • Robert Calvo,Rose City Labyrinth,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1264/1264.jpg,1701
    • Zhao Suikang,Portland Pamphlets,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1266/1266.jpg,1705
    • Alejandro Colunga,Mago Hermano (Brother Wizard or Magician),1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_invento . . .
    • Donald Wilson,Holon,1525 SW Park Ave.,45.514043,-122.684217,http://data.racc.org/pa_inventory/1492/1492.jpg,2176
    • Cynthia Lahti,Station 4,511 SW College Street,45.509734,-122.682872,http://data.racc.org/pa_inventory/1550/1550.jpg,2278
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262b/1262b.jpg,2518
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262c/1262c.jpg,2519
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262d/1262d.jpg,2520
    • Patti Warashina,City Reflections,SW 6th Ave. and SW Main St.,45.516409,-122.680049,http://data.racc.org/pa_inventory/1872/1872.jpg,2881
    • Jack Archibald,Icarus Triumphant,1510 SW 13th Avenue,45.515271,-122.688128,http://data.racc.org/pa_inventory/1837/1837.jpg,2937
    • Keiko Hara,Verses - Reflected and Reflecting,1800 SW 6th Ave Suite 550,45.511733,-122.682559,http://data.racc.org/pa_inventory/18 . . .
  • 28.
    • artist title street lat lng
    • Steven Gillman Peace Chant SW Park Ave. and SW Columbia St. 45.514946 -122.683125
    • Paul Sutinen In the Shadow of the Elm SW Park Ave. and SW Market St. 45.513751 -122.684352
    • Oliver Barrett Rebecca at the Well (Shemanski Fountain) SW Park Ave. and SW Main St. 45.516939 -122.682039
    • James Carpenter Spectral Light Dome 1111 SW Broadway 45.516623 -122.681327
    • Edo Period Kano School Japanese Screen 1111 SW Broadway 45.516623 -122.681327
    • Linda Ethier Muse Maze Mirror Gaze 1111 SW Broadway 45.516623 -122.681327
    • Henk Pander Portland Town 1111 SW Broadway 45.516623 -122.681327
    • Tom Hardy Running Horses SW 6th Ave. and SW Madison St. 45.515729 -122.680413
    • Alexander Phimister Proctor Theodore Roosevelt Rough Rider SW Park Ave. and SW Jefferson St. 45.515738 -122.683275
    • George Fite Waters Abraham Lincoln SW Park Ave. and SW Madison St. 45.516406 -122.682923
    • Juan Alonso Diva 1900 SW 4th Ave 45.509748 -122.68105
    • Robert Calvo Rose City Labyrinth 1900 SW 4th Ave 45.509748 -122.68105
    • Zhao Suikang Portland Pamphlets 1900 SW 4th Ave 45.509748 -122.68105
    • Alejandro Colunga Mago Hermano (Brother Wizard or Magician) 1111 SW Broadway 45.516623 -122.681327
    • Donald Wilson Holon 1525 SW Park Ave. 45.514043 -122.684217
    • Cynthia Lahti Station 4 511 SW College Street 45.509734 -122.682872
    • Robert Yoder Sweet Air Bend 1900 SW 4th Ave 45.509748 -122.68105
    • Robert Yoder Sweet Air Bend 1900 SW 4th Ave 45.509748 -122.68105
    • Robert Yoder Sweet Air Bend 1900 SW 4th Ave 45.509748 -122.68105
    • Patti Warashina City Reflections SW 6th Ave. and SW Main St. 45.516409 -122.680049
    • Jack Archibald Icarus Triumphant 1510 SW 13th Avenue 45.515271 -122.688128
    • Keiko Hara Verses - Reflected and Reflecting 1800 SW 6th Ave Suite 550 45.511733 -122.682559
    print data.tsv
  • 29. print data.json
    • [{&quot;artist&quot;: &quot;Steven Gillman&quot;, &quot;title&quot;: &quot;Peace Chant&quot;, &quot;street&quot;: &quot;SW Park Ave. and SW Columbia St.&quot;, &quot;lat&quot;: &quot;45.514946&quot;, &quot;lng&quot;: &quot;-122.683125&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0657/0657.jpg&quot;, &quot;record_id&quot;: &quot;789&quot;}, {&quot;artist&quot;: &quot;Paul Sutinen&quot;, &quot;title&quot;: &quot;In the Shadow of the Elm&quot;, &quot;street&quot;: &quot;SW Park Ave. and SW Market St.&quot;, &quot;lat&quot;: &quot;45.513751&quot;, &quot;lng&quot;: &quot;-122.684352&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0658/0658.jpg&quot;, &quot;record_id&quot;: &quot;790&quot;}, {&quot;artist&quot;: &quot;Oliver Barrett&quot;, &quot;title&quot;: &quot;Rebecca at the Well (Shemanski Fountain)&quot;, &quot;street&quot;: &quot;SW Park Ave. and SW Main St.&quot;, &quot;lat&quot;: &quot;45.516939&quot;, &quot;lng&quot;: &quot;-122.682039&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0661/0661.jpg&quot;, &quot;record_id&quot;: &quot;791&quot;}, {&quot;artist&quot;: &quot;James Carpenter&quot;, &quot;title&quot;: &quot;Spectral Light Dome&quot;, &quot;street&quot;: &quot;1111 SW Broadway&quot;, &quot;lat&quot;: &quot;45.516623&quot;, &quot;lng&quot;: &quot;-122.681327&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0898/0898.jpg&quot;, &quot;record_id&quot;: &quot;966&quot;}, {&quot;artist&quot;: &quot;Edo Period&quot;, &quot;title&quot;: &quot;Kano School Japanese Screen&quot;, &quot;street&quot;: &quot;1111 SW Broadway&quot;, &quot;lat&quot;: &quot;45.516623&quot;, &quot;lng&quot;: &quot;-122.681327&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0906/0906.jpg&quot;, &quot;record_id&quot;: &quot;972&quot;}, {&quot;artist&quot;: &quot;Linda Ethier&quot;, &quot;title&quot;: &quot;Muse Maze Mirror Gaze&quot;, &quot;street&quot;: &quot;1111 SW Broadway&quot;, &quot;lat&quot;: &quot;45.516623&quot;, &quot;lng&quot;: &quot;-122.681327&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0989/0989.jpg&quot;, &quot;record_id&quot;: &quot;1065&quot;}, {&quot;artist&quot;: &quot;Henk Pander&quot;, &quot;title&quot;: &quot;Portland Town&quot;, &quot;street&quot;: &quot;1111 SW Broadway&quot;, &quot;lat&quot;: &quot;45.516623&quot;, &quot;lng&quot;: &quot;-122.681327&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0990/0990.jpg&quot;, &quot;record_id&quot;: &quot;1066&quot;}, {&quot;artist&quot;: &quot;Tom Hardy&quot;, &quot;title&quot;: &quot;Running Horses&quot;, &quot;street&quot;: &quot;SW 6th Ave. and SW Madison St.&quot;, &quot;lat&quot;: &quot;45.515729&quot;, &quot;lng&quot;: &quot;-122.680413&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1071/1071.jpg&quot;, &quot;record_id&quot;: &quot;1185&quot;}, {&quot;artist&quot;: &quot;Alexander Phimister Proctor&quot;, &quot;title&quot;: &quot;Theodore Roosevelt Rough Rider&quot;, &quot;street&quot;: &quot;SW Park Ave. and SW Jefferson St.&quot;, &quot;lat&quot;: &quot;45.515738&quot;, &quot;lng&quot;: &quot;-122.683275&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0659/0659.jpg&quot;, &quot;record_id&quot;: &quot;1192&quot;}, {&quot;artist&quot;: &quot;George Fite Waters&quot;, &quot;title&quot;: &quot;Abraham Lincoln&quot;, &quot;street&quot;: &quot;SW Park Ave. and SW Madison St.&quot;, &quot;lat&quot;: &quot;45.516406&quot;, &quot;lng&quot;: &quot;-122.682923&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/0660/0660.jpg&quot;, &quot;record_id&quot;: &quot;1323&quot;}, {&quot;artist&quot;: &quot;Juan Alonso&quot;, &quot;title&quot;: &quot;Diva&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1261/1261.jpg&quot;, &quot;record_id&quot;: &quot;1674&quot;}, {&quot;artist&quot;: &quot;Robert Calvo&quot;, &quot;title&quot;: &quot;Rose City Labyrinth&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1264/1264.jpg&quot;, &quot;record_id&quot;: &quot;1701&quot;}, {&quot;artist&quot;: &quot;Zhao Suikang&quot;, &quot;title&quot;: &quot;Portland Pamphlets&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1266/1266.jpg&quot;, &quot;record_id&quot;: &quot;1705&quot;}, {&quot;artist&quot;: &quot;Alejandro Colunga&quot;, &quot;title&quot;: &quot;Mago Hermano (Brother Wizard or Magician)&quot;, &quot;street&quot;: &quot;1111 SW Broadway&quot;, &quot;lat&quot;: &quot;45.516623&quot;, &quot;lng&quot;: &quot;-122.681327&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1481/1481.jpg&quot;, &quot;record_id&quot;: &quot;2152&quot;}, {&quot;artist&quot;: &quot;Donald Wilson&quot;, &quot;title&quot;: &quot;Holon&quot;, &quot;street&quot;: &quot;1525 SW Park Ave.&quot;, &quot;lat&quot;: &quot;45.514043&quot;, &quot;lng&quot;: &quot;-122.684217&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1492/1492.jpg&quot;, &quot;record_id&quot;: &quot;2176&quot;}, {&quot;artist&quot;: &quot;Cynthia Lahti&quot;, &quot;title&quot;: &quot;Station 4&quot;, &quot;street&quot;: &quot;511 SW College Street&quot;, &quot;lat&quot;: &quot;45.509734&quot;, &quot;lng&quot;: &quot;-122.682872&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1550/1550.jpg&quot;, &quot;record_id&quot;: &quot;2278&quot;}, {&quot;artist&quot;: &quot;Robert Yoder&quot;, &quot;title&quot;: &quot;Sweet Air Bend&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1262b/1262b.jpg&quot;, &quot;record_id&quot;: &quot;2518&quot;}, {&quot;artist&quot;: &quot;Robert Yoder&quot;, &quot;title&quot;: &quot;Sweet Air Bend&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1262c/1262c.jpg&quot;, &quot;record_id&quot;: &quot;2519&quot;}, {&quot;artist&quot;: &quot;Robert Yoder&quot;, &quot;title&quot;: &quot;Sweet Air Bend&quot;, &quot;street&quot;: &quot;1900 SW 4th Ave&quot;, &quot;lat&quot;: &quot;45.509748&quot;, &quot;lng&quot;: &quot;-122.68105&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1262d/1262d.jpg&quot;, &quot;record_id&quot;: &quot;2520&quot;}, {&quot;artist&quot;: &quot;Patti Warashina&quot;, &quot;title&quot;: &quot;City Reflections&quot;, &quot;street&quot;: &quot;SW 6th Ave. and SW Main St.&quot;, &quot;lat&quot;: &quot;45.516409&quot;, &quot;lng&quot;: &quot;-122.680049&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1872/1872.jpg&quot;, &quot;record_id&quot;: &quot;2881&quot;}, {&quot;artist&quot;: &quot;Jack Archibald&quot;, &quot;title&quot;: &quot;Icarus Triumphant&quot;, &quot;street&quot;: &quot;1510 SW 13th Avenue&quot;, &quot;lat&quot;: &quot;45.515271&quot;, &quot;lng&quot;: &quot;-122.688128&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1837/1837.jpg&quot;, &quot;record_id&quot;: &quot;2937&quot;}, {&quot;artist&quot;: &quot;Keiko Hara&quot;, &quot;title&quot;: &quot;Verses - Reflected and Reflecting&quot;, &quot;street&quot;: &quot;1800 SW 6th Ave Suite 550&quot;, &quot;lat&quot;: &quot;45.511733&quot;, &quot;lng&quot;: &quot;-122.682559&quot;, &quot;image_url&quot;: &quot;http://data.racc.org/pa_inventory/1838/1838.jpg&quot;, &quot;record_id&quot;: &quot;3018&quot;}]
  • 30. open(r'C:datapublic_art_selected.xls', 'wb').write(data.xls)
  • 31.
    • tabular
  • 32.
    • import tabular
    • src = r'C:datapublic_art_selected.csv‘
    • table = tabular.tabarray(SVfile=src)
    • table.sort(order=['street'])
    • print table[table['artist'] == 'Robert Yoder']
    • pivot_table = table.pivot('street', 'record_id')['artist']
    • pivot_table.saveSV(r'C:datapublic_art_selected_pivot.csv')
  • 33.  
  • 34.
    • Shapely
  • 35. “ set-theoretic analysis and manipulation of planar features” Create: Point, LineString, LinearRing, Polygons MultiPoint, MultiLineString, MultiPolygon General Attributes/Methods .area, .length, .bounds, geom_type, .distance (to another object) Binary Predicates contains, crosses, disjoint, equals, intersects, touches, within set-theoretic methods .boundary, .centroid, .difference, .intersection, .symmetric_difference, .union constructive method .buffer, .convex_hull, .envelope, .simplify other merging, cascading union Interoperation WKT, numpy arrays, geo interface (GeoJSON)
  • 36. import arcpy from shapely.geometry import MultiPoint, asShape, mapping fc = r'C:GIS_in_Action.gdbpublic_art_selected‘ g = arcpy.Geometry() geometryList = arcpy.CopyFeatures_management(fc, g) mp = [] for item in geometryList: mp.append(asShape(item.__geo_interface__)) points = MultiPoint(mp) out_fc = r'C:GIS_in_Action.gdbpublic_art_selected_convex_hull‘ ch = points.convex_hull buffered_ch = ch.buffer(100) points_convex_hull = arcpy.AsShape(mapping(buffered_ch)) arcpy.CopyFeatures_management(points_convex_hull, ch_fc) prjFile = ‘path_to_prj_file’ spatialRef = arcpy.SpatialReference(prjFile) arcpy.DefineProjection_management(out_fc, spatialRef)
  • 37.
    • Use Shapely in conjunction with
    • pyproj for projected coordinates
  • 38.
    • gdal/ogr
  • 39.
    • from osgeo import gdal
    • dataset = gdal.Open(r'C:datarastersLiDAR_be.tif')
    • cols, rows = dataset.RasterXSize, dataset.RasterYSize
    • bands = dataset.RasterCount
    • print 'Columns: %i, Rows: %i, Bands: %i' % (cols, rows, bands)
    • Columns: 1914, Rows: 1858, Bands: 1
    • band = dataset.GetRasterBand(1)
    • print 'Band Type =',gdal.GetDataTypeName(band.DataType)
    • min, max = band.GetMinimum(), band.GetMaximum()
    • print 'Min=%.3f, Max=%.3f' % (min, max)
    • Band Type = Float32
    • Min=10.094, Max=1000.430
  • 40.
    • from osgeo import ogr
    • shapefile = ogr.Open(r'C:datashpspublic_art_selected.shp')
    • layer = shapefile.GetLayer()
    • for i in range(layer.GetFeatureCount()):
    • feature = layer.GetFeature(i)
    • title = feature.GetField('title')
    • geometry = feature.GetGeometryRef()
    • print i, title, geometry.GetGeometryName()
    0, Peace Chant ,POINT 1, In the Shadow of the Elm ,POINT 2, Rebecca at the Well (Shemanski Fountain) ,POINT 3, Spectral Light Dome ,POINT 4, Kano School Japanese Screen ,POINT 5, Muse, Maze, Mirror, Gaze ,POINT 6, Portland Town ,POINT 7, Running Horses ,POINT 0, Peace Chant ,POINT 1, In the Shadow of the Elm ,POINT 2, Rebecca at the Well (Shemanski Fountain) ,POINT 3, Spectral Light Dome ,POINT 4, Kano School Japanese Screen ,POINT 5, Muse, Maze, Mirror, Gaze ,POINT 6, Portland Town ,POINT 7, Running Horses ,POINT . . .
  • 41.
    • reportlab
  • 42. public_art.csv tablib for row in data: # put in PDF
  • 43.
    • # Setting up the PDF
    • from reportlab.pdfgen.canvas import Canvas
    • from reportlab.lib.pagesizes import letter
    • from reportlab.lib.styles import getSampleStyleSheet
    • pdfFile = r'somedir/somefile.pdf'
    • pdf = Canvas(pdfFile, pagesize=letter)
    • pdf.showPage()
    • pdf.save()
    • styles = getSampleStyleSheet()
    • styleMRA = styles['Normal']
    • pdf.setFont(&quot;Helvetica&quot;,24)
  • 44.
    • # Drawing boxes, images, and text
    • from reportlab.lib.units import inch
    • from reportlab.lib.utils import ImageReader
    • pdf.rect(x, y, width, height, stroke=1, fill=0)
    • # measurements are from lower left corner
    • # use dist*inch
    • pdf.drawAlignedString(x, y, text)
    • pdf.drawCentredString(x, y, text) #note British spelling
    • photo = ImageReader(url)
    • pdf.drawImage(image, x, y, width, height, mask, preserveAspectRatio)
  • 45.
    • # Flowables
    • from reportlab.platypus import Paragraph, Frame
    • from reportlab.platypus.flowables import Spacer
    • text = []
    • text.append(Paragraph(text, styleMRA))
    • text.append(Spacer(0.1*inch, 0.1*inch))
    • f = Frame(x, y, width, height, leftPadding, bottomPadding, rightPadding, topPadding)
    • f.addFromList(text, pdf)
  • 46. print data.csv
    • artist,title,street,lat,lng,image_url,record_id
    • Steven Gillman,Peace Chant,SW Park Ave. and SW Columbia St.,45.514946,-122.683125,http://data.racc.org/pa_inventory/0657/0657.jpg,789
    • Paul Sutinen,In the Shadow of the Elm,SW Park Ave. and SW Market St.,45.513751,122.684352,http://data.racc.org/pa_inventory/0658/06 . . .
    • Oliver Barrett,Rebecca at the Well (Shemanski Fountain),SW Park Ave. and SW Main St.,45.516939,-122.682039,http://data.racc.org/pa_in. . .
    • James Carpenter,Spectral Light Dome,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0898/0898.jpg,966
    • Edo Period,Kano School Japanese Screen,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0906/0906.jpg,972
    • Linda Ethier,Muse Maze Mirror Gaze,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0989/0989.jpg,1065
    • Henk Pander,Portland Town,1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_inventory/0990/0990.jpg,1066
    • Tom Hardy,Running Horses,SW 6th Ave. and SW Madison St.,45.515729,-122.680413,http://data.racc.org/pa_inventory/1071/1071.jpg,1185
    • Alexander Phimister Proctor,Theodore Roosevelt Rough Rider,SW Park Ave. and SW Jefferson St.,45.515738,-122.683275,http://data.r . . .
    • George Fite Waters,Abraham Lincoln,SW Park Ave. and SW Madison St.,45.516406,-122.682923,http://data.racc.org/pa_inventory/0660/066 . . .
    • Juan Alonso,Diva,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1261/1261.jpg,1674
    • Robert Calvo,Rose City Labyrinth,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1264/1264.jpg,1701
    • Zhao Suikang,Portland Pamphlets,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1266/1266.jpg,1705
    • Alejandro Colunga,Mago Hermano (Brother Wizard or Magician),1111 SW Broadway,45.516623,-122.681327,http://data.racc.org/pa_invento . . .
    • Donald Wilson,Holon,1525 SW Park Ave.,45.514043,-122.684217,http://data.racc.org/pa_inventory/1492/1492.jpg,2176
    • Cynthia Lahti,Station 4,511 SW College Street,45.509734,-122.682872,http://data.racc.org/pa_inventory/1550/1550.jpg,2278
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262b/1262b.jpg,2518
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262c/1262c.jpg,2519
    • Robert Yoder,Sweet Air Bend,1900 SW 4th Ave,45.509748,-122.68105,http://data.racc.org/pa_inventory/1262d/1262d.jpg,2520
    • Patti Warashina,City Reflections,SW 6th Ave. and SW Main St.,45.516409,-122.680049,http://data.racc.org/pa_inventory/1872/1872.jpg,2881
    • Jack Archibald,Icarus Triumphant,1510 SW 13th Avenue,45.515271,-122.688128,http://data.racc.org/pa_inventory/1837/1837.jpg,2937
    • Keiko Hara,Verses - Reflected and Reflecting,1800 SW 6th Ave Suite 550,45.511733,-122.682559,http://data.racc.org/pa_inventory/18 . . .
  • 47. ‘ image_url’ ‘ artist’, ‘ title’, ‘ location’
  • 48.
    • mapnik
  • 49. mapnik makes pretty maps
  • 50. The Map consists of one or more layers Each Layer has a datasource A Style groups and organizes rules A Rule contains symbolizers and filters Filter (optional) Symbolizer
  • 51.
    • import mapnik
    • # Ortho
    • Ortho_style = mapnik.Style()
    • Ortho_rule = mapnik.Rule()
    • Ortho_symbol = mapnik.RasterSymbolizer()
    • Ortho_rule.symbols.append(Ortho_symbol)
    • Ortho_style.rules.append(Ortho_rule)
    • # LineSymbolizer
    • Polygon_style = mapnik.Style()
    • Polygon_rule = mapnik.Rule()
    • stroke = mapnik.Stroke()
    • stroke.color = mapnik.Color(&quot;#000000&quot;)
    • stroke.width = 8.0
    • Polygon_outline_symbol = mapnik.LineSymbolizer(stroke)
    • Polygon_rule.symbols.append(Polygon_outline_symbol)
    • Polygon_style.rules.append(Polygon_rule)
  • 52.
    • # Points
    • Points_style = mapnik.Style()
    • Points_rule = mapnik.Rule()
    • Points_symbol = mapnik.PointSymbolizer(r'C:markersred_pushpin.png', &quot;png&quot;, 64, 64)
    • Points_symbol.allow_overlap = True
    • Points_rule.symbols.append(Points_symbol)
    • Points_style.rules.append(Points_rule)
  • 53.
    • # Ortho
    • Ortho_file = r'C:dataortho_mosaic.tif'
    • Ortho_datasource = mapnik.Gdal(file=Ortho_file)
    • Ortho_layer = mapnik.Layer(&quot;Ortho&quot;)
    • Ortho_layer.datasource = Ortho_datasource
    • Ortho_layer.styles.append(&quot;Ortho_style&quot;)
    • # Polygon
    • Polygon_file = r'C:datapublic_art_selected_ch_buffer.shp'
    • Polygon_datasource = mapnik.Shapefile(file=Polygon_file)
    • Polygon_layer = mapnik.Layer(&quot;Polygon&quot;)
    • Polygon_layer.datasource = Polygon_datasource
    • Polygon_layer.styles.append(&quot;Polygon_style&quot;)
    • # Points
    • Points_file = r'C:datapublic_art_selected.shp'
    • Points_datasource = mapnik.Shapefile(file=Points_file)
    • Points_layer = mapnik.Layer(&quot;Points&quot;)
    • Points_layer.datasource = Points_datasource
    • Points_layer.styles.append(&quot;Points_style&quot;)
  • 54.
    • # Map
    • map = mapnik.Map(1800, 1800)
    • map.background = mapnik.Color(“white&quot;)
    • map.aspect_fix_mode = mapnik._mapnik.aspect_fix_mode.GROW_CANVAS
    • map.append_style(&quot;Ortho_style&quot;, Ortho_style)
    • map.append_style(&quot;Polygon_style&quot;, Polygon_style)
    • map.append_style(&quot;Points_style&quot;, Points_style)
    • map.layers.append(Ortho_layer)
    • map.layers.append(Polygon_layer)
    • map.layers.append(Points_layer)
    • # map.zoom_all()
    • map.zoom_to_box(mapnik.Envelope(524100, 5039373, 525240, 5040680))
    • mapnik.render_to_file(map, &quot;C:mapnik_outputmap.png&quot;, &quot;png&quot;)
  • 55. Alternative: Map Definition File (XML) Cascadenik
  • 56.  
  • 57.
    • Being Pythonic
    • Mark Pilgrim, Dive Into Python
    • http://diveintopython.org
    • David Goodger, “Code Like a Pythonista”
    • http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
  • 58.
    • # with
    • # must be first import statement
    • from __future__ import with_statement
    • with open(r’c:somedirfile.txt’) as f:
    • for line in f:
    • processline()
  • 59.
    • # iterating over records
    • import arcgisscripting
    • gp = arcgisscripting.create()
    • shp = r’c:someshp.shp’
    • rows = gp.searchcursor(shp)
    • for row in iter(rows.next, None):
    • doSomething()
  • 60.
    • # list comprehensions
    • a_list = [ expression for item in iterable if condition ]
    • fieldlist = arcpy.ListFields(src)
    • ignorefields = ['OBJECTID', 'Shape', 'detail_url']
    • fieldnames = [field.name for field in fieldlist if field.name not in ignorefields]
    • [‘artist’, ‘title’, ‘street’, ‘lat’, ‘lng’, ‘image_url’, ‘record_id’
    • ]
  • 61.
    • Documentation:
    • tablib http://tablib.org/
    • tabular http://www.parsemydata.com/tabular/
    • xlrd/xlwt http://www.python-excel.org/
    • Shapely http://gispython.org/shapely/docs/1.2/
    • pyproj http://code.google.com/p/pyproj/
    • gdal/ogr http://trac.osgeo.org/gdal/wiki/GdalOgrInPython
    • reportlab http://www.reportlab.com/software/opensource/rl- toolkit/guide/
    • mapnik http://trac.mapnik.org/wiki/WindowsInstallation
  • 62. arcpy ArcGIS Python
  • 63. arcpy ArcGIS Python
  • 64. Credits
    • Images
    • import antigravity: http://xkcd.com/353/
    • Columbia library reading room: Wurts Bros., N.Y., &quot;Business Library Reading Room,&quot; in CU Libraries Exhibitions , Item #529, https://ldpd.lamp.columbia.edu/omeka/items/show/529 (accessed February 5, 2011).
    • Stockholm public library: architect’s concept | http:// features.cgsociety.org/stories/2009_05/2009_05_stockholmlibrary/18-final-image.jpg
    • yak: Brian Dearth | http://www.flickr.com/photos/bdearth/4881180471
    • mapnik diagram: Erik Westra, Python Geospatial Development , p. 264 (slightly modified)
    • Datasets
    • Portland Public art dataset: http://www.civicapps.org/datasets
    • LiDAR: http://pugetsoundlidar.ess.washington.edu/lidardata/restricted/projects/2005lowercolumbiariver.html | http://www.oregongeology.org/sub/lidar/background.htm
    • Ortho: The National Map Viewer ( http://viewer.nationalmap.gov/viewer/)