SlideShare a Scribd company logo
3
Most read
IntroductiontoPythoninArcGIS
JohnZhou
May,2011
Scripting or not Scripting
What do we usually do with GIS
When do we need scripting
An ugly case in point
#Python Script for putting watermark annotationon image
#John Zhou MWH Global 12/19/2006
#Fragile,not for other use
#need Python and PIL Installed
import win32com.client
import fnmatch
import string
import os
import Image,ImageDraw,ImageFont,sys
engine = win32com.client.Dispatch("DAO.DBEngine.36")
strPath="E:PWP_WaterMark"
#Determinethe path to your databaseand use the following
syntax:
#makesure the database name
db =
engine.OpenDatabase(strPath+"ConsolidatedGPS_18Dec2006.m
db")
totalNum=0
ferr=open("c:err.txt","w")
fsuccess=open("c:success.txt","w")
for fileNamein os.listdir( strPath+"FieldPhotos"):
if fnmatch.fnmatch( fileName, '*.jpg' ) :
path, name = os.path.split (fileName)
queryString="selectPoleFacilityID, Address,PhotoLocation from
Pole where PhotoLocation like '*" +name+"*'"
# print queryString
x=1
rs = db.OpenRecordset( queryString)
Anno=""
while not rs.EOF and x<2:
Anno= rs.Fields("PoleFacilityID").Value+"|"+rs.Fields("Address").Value+
"|"+name
rs.MoveNext
x=x+1
strFileName= strPath+"FieldPhotos"+fileName.upper()
totalNum=totalNum+1
if (len(Anno)>0):
fsuccess.write(strFileName+"n")
else:
ferr.write(strFileName+"n")
# print strFileName
im1=Image.open(strFileName)
im2=Image.new(im1.mode,im1.size)
font = ImageFont.truetype("arial.ttf",16)
textsize=font.getsize(Anno)
for mySize in range(18,100,2):
if textsize[0]>=im1.size[0]-im1.size[0]*0.2:
break
font = ImageFont.truetype("arial.ttf",mySize)
textsize=font.getsize(Anno)
draw2=ImageDraw.Draw(im2)
draw2.rectangle([0,im2.size[1],im2.size[0],im2.size[1]-
textsize[1]],fill=128)
im3=Image.blend(im1,im2,0.2)
draw3=ImageDraw.Draw(im3)
draw3.text(((im3.size[0]-textsize[0])/2,im3.size[1]-textsize[1]), Anno,
font=font)
im3.save(strFileName,"JPEG")
print totalNum
ferr.close()
fsuccess.close()
What about Model Builder
Model Builder is a great way to lay out a
geoprocessing workflow, but some
functionality is just not available
Models can be exported to scripts
Exported scripts can be modified to support more
complex logic and looping
Good way to learn tool usage and python syntax
Why Python
Python is an open-source object-oriented
programming language that offers two to ten
fold programmer productivity increases over
languages like C, C++, Java, C#,Visual Basic (VB),
and Perl.
excellent for beginners, yet superb for experts
highly scalable, suitable for large projects as well as
small ones
portable, cross-platform
powerful standard libs
wealth of 3rd party packages/modules (numPy, SciPy)
# Create Geoprocessing object
import arcgisscripting
gp = arcgisscripting.create(9.3)
# Set the workspace
gp.workspace = "c:/st_Johns/GISData.gdb“
# use Geoprocessing tool
gp.intersect("roads;urban_area", "urban_roads", 5, "join")
# run a tool
gp.Buffer("c:/ws/roads.shp", "c:/outws/roads10.shp", 10)
# Print the tools messages to screen
print gp.GetMessages() #0-informative, 1-warning, 2-error, blank-all messages
# Get a list of all polygon feature
# classes in the workspace
fcList = gp.ListFeatureClasses("*", "polygon")
# Print the list of feature classes one at a time
for fc in fcList:
print fc
Infeatures = gp.GetParameterAsText(0)
Outfeatures = gp.GetParameterAsText(1)
Access to all toolbox tools
Details: http://webhelp.esri.com/arcgisdesktop/9.3/body.cfm?tocVisable=1&ID=920&TopicName=Running%20a%20tool#
Anatomy of ArcGIS Python
Python gis
http://webhelp.esri.com/arcgisdesktop/9.3/pdf/Geoprocessor_93.pdf
Programming Model
Advanced Topic - ArcPy Mapping (10)
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D388EF60(arcpy.mapping sample
script tools)
http://blogs.esri.com/dev/blogs/arcgisdesktop/archive/2010/12/14/combining-data-driven-pages-with-python-and-arcpy.mapping.aspx
(Combining Data Driven Pages with Python acrpy.mapping)
For those committed
http://code.google.com/edu/languages/google-python-
class/introduction.html (PyQuick class by Google)
http://training.esri.com/acb2000/showdetl.cfm?DID=6&Pro
duct_ID=971 (Using Python in ArcGIS Desktop 10, web
course)
http://proceedings.esri.com/library/userconf/devsummit10/
tech/presumm_04.html (Getting Started with Python in
ArcGIS)
http://docs.python.org/tutorial/introduction.html (Python
Tutorial)
Books
Learning Python by Mark Lutz
Core Python by Wesley J. Chun
Dive Into Python by Mark Pilgrim

More Related Content

PDF
BASTA 2020 VS Code Data Visualisation
PDF
Endofday: A Container Workflow Engine for Scalable, Reproducible Computation
PDF
Libtcc and gwan
PDF
Ce di l_1800_0
PDF
How to measure everything - a million metrics per second with minimal develop...
ODP
Daniel Sikar: Hadoop MapReduce - 06/09/2010
PDF
Web 2 . 0 .Zero Coding Services
BASTA 2020 VS Code Data Visualisation
Endofday: A Container Workflow Engine for Scalable, Reproducible Computation
Libtcc and gwan
Ce di l_1800_0
How to measure everything - a million metrics per second with minimal develop...
Daniel Sikar: Hadoop MapReduce - 06/09/2010
Web 2 . 0 .Zero Coding Services

What's hot (19)

PDF
Don't turn on/off your Photoshop yet
PDF
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
PDF
The power of streams in node js
PPTX
2 19-2018-mean of all runs
ODP
Aws Quick Dirty Hadoop Mapreduce Ec2 S3
PDF
1606015 m1 yamamoto
PPTX
我在 Mac 上的常用开发工具
PDF
201801 CSE240 Lecture 12
PDF
Marble Virtual Globe for Developers - Factsheet
PDF
C-- Sample Programs and Screenshots
PDF
Web 2 . 0 .0 Programming Companies
PPT
Handcrafting Webassembly
PDF
Quick 入門 | iOS RDD テストフレームワーク for Swift/Objective-C
PDF
Collaborative Coding: Git + Github (NSI Tech Talks)
PDF
Unleash your build with nuke
PDF
Altitude San Francisco 2018: WebAssembly Tools & Applications
PPTX
HItchhickers Guide to TypeScript
DOCX
R Data Visualization-Spatial data and Maps in R: Using R as a GIS
PDF
Three Functional Programming Technologies for Big Data
Don't turn on/off your Photoshop yet
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
The power of streams in node js
2 19-2018-mean of all runs
Aws Quick Dirty Hadoop Mapreduce Ec2 S3
1606015 m1 yamamoto
我在 Mac 上的常用开发工具
201801 CSE240 Lecture 12
Marble Virtual Globe for Developers - Factsheet
C-- Sample Programs and Screenshots
Web 2 . 0 .0 Programming Companies
Handcrafting Webassembly
Quick 入門 | iOS RDD テストフレームワーク for Swift/Objective-C
Collaborative Coding: Git + Github (NSI Tech Talks)
Unleash your build with nuke
Altitude San Francisco 2018: WebAssembly Tools & Applications
HItchhickers Guide to TypeScript
R Data Visualization-Spatial data and Maps in R: Using R as a GIS
Three Functional Programming Technologies for Big Data
Ad

Similar to Python gis (20)

PPTX
Python and GIS: Improving Your Workflow
PPTX
GIS: The Future's So Bright, I Gotta Wear Shades
PPTX
ArcGIS Python Programming (3Nov11)
PDF
Esri International User Conference 2011: Python: Integrating Standard and Thi...
PDF
UC2023 ArcGIS API for Python Intro_1688158152584001jdnT .pdf
PPT
Python 4 Arc
PPTX
APPLICATION OF PYTHON IN GEOSCIENCE
PPTX
Esri South Africa Python for Everyone
PDF
Pycon 2012 Taiwan
PDF
那些年 Python 攻佔了 GIS / The Year Python Takes Over GIS
PPTX
Python in geospatial analysis
PDF
Python Programming and GIS
PPT
Python first day
PPT
Python first day
PPTX
Analytics with arcpy package detailing of Mapping Module Properties and Data ...
PDF
Python Programming for ArcGIS: Part I
PPTX
Geoprocessing
PDF
Reading and writing spatial data for the non-spatial programmer
PPT
01 ESRI software arcgis article history.ppt
PPTX
Python en la Plataforma ArcGIS
Python and GIS: Improving Your Workflow
GIS: The Future's So Bright, I Gotta Wear Shades
ArcGIS Python Programming (3Nov11)
Esri International User Conference 2011: Python: Integrating Standard and Thi...
UC2023 ArcGIS API for Python Intro_1688158152584001jdnT .pdf
Python 4 Arc
APPLICATION OF PYTHON IN GEOSCIENCE
Esri South Africa Python for Everyone
Pycon 2012 Taiwan
那些年 Python 攻佔了 GIS / The Year Python Takes Over GIS
Python in geospatial analysis
Python Programming and GIS
Python first day
Python first day
Analytics with arcpy package detailing of Mapping Module Properties and Data ...
Python Programming for ArcGIS: Part I
Geoprocessing
Reading and writing spatial data for the non-spatial programmer
01 ESRI software arcgis article history.ppt
Python en la Plataforma ArcGIS
Ad

Recently uploaded (20)

PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
A Presentation on Artificial Intelligence
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
A Presentation on Artificial Intelligence
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation

Python gis

  • 2. What do we usually do with GIS
  • 3. When do we need scripting
  • 4. An ugly case in point #Python Script for putting watermark annotationon image #John Zhou MWH Global 12/19/2006 #Fragile,not for other use #need Python and PIL Installed import win32com.client import fnmatch import string import os import Image,ImageDraw,ImageFont,sys engine = win32com.client.Dispatch("DAO.DBEngine.36") strPath="E:PWP_WaterMark" #Determinethe path to your databaseand use the following syntax: #makesure the database name db = engine.OpenDatabase(strPath+"ConsolidatedGPS_18Dec2006.m db") totalNum=0 ferr=open("c:err.txt","w") fsuccess=open("c:success.txt","w") for fileNamein os.listdir( strPath+"FieldPhotos"): if fnmatch.fnmatch( fileName, '*.jpg' ) : path, name = os.path.split (fileName) queryString="selectPoleFacilityID, Address,PhotoLocation from Pole where PhotoLocation like '*" +name+"*'" # print queryString x=1 rs = db.OpenRecordset( queryString) Anno="" while not rs.EOF and x<2: Anno= rs.Fields("PoleFacilityID").Value+"|"+rs.Fields("Address").Value+ "|"+name rs.MoveNext x=x+1 strFileName= strPath+"FieldPhotos"+fileName.upper() totalNum=totalNum+1 if (len(Anno)>0): fsuccess.write(strFileName+"n") else: ferr.write(strFileName+"n") # print strFileName im1=Image.open(strFileName) im2=Image.new(im1.mode,im1.size) font = ImageFont.truetype("arial.ttf",16) textsize=font.getsize(Anno) for mySize in range(18,100,2): if textsize[0]>=im1.size[0]-im1.size[0]*0.2: break font = ImageFont.truetype("arial.ttf",mySize) textsize=font.getsize(Anno) draw2=ImageDraw.Draw(im2) draw2.rectangle([0,im2.size[1],im2.size[0],im2.size[1]- textsize[1]],fill=128) im3=Image.blend(im1,im2,0.2) draw3=ImageDraw.Draw(im3) draw3.text(((im3.size[0]-textsize[0])/2,im3.size[1]-textsize[1]), Anno, font=font) im3.save(strFileName,"JPEG") print totalNum ferr.close() fsuccess.close()
  • 5. What about Model Builder Model Builder is a great way to lay out a geoprocessing workflow, but some functionality is just not available Models can be exported to scripts Exported scripts can be modified to support more complex logic and looping Good way to learn tool usage and python syntax
  • 6. Why Python Python is an open-source object-oriented programming language that offers two to ten fold programmer productivity increases over languages like C, C++, Java, C#,Visual Basic (VB), and Perl. excellent for beginners, yet superb for experts highly scalable, suitable for large projects as well as small ones portable, cross-platform powerful standard libs wealth of 3rd party packages/modules (numPy, SciPy)
  • 7. # Create Geoprocessing object import arcgisscripting gp = arcgisscripting.create(9.3) # Set the workspace gp.workspace = "c:/st_Johns/GISData.gdb“ # use Geoprocessing tool gp.intersect("roads;urban_area", "urban_roads", 5, "join") # run a tool gp.Buffer("c:/ws/roads.shp", "c:/outws/roads10.shp", 10) # Print the tools messages to screen print gp.GetMessages() #0-informative, 1-warning, 2-error, blank-all messages # Get a list of all polygon feature # classes in the workspace fcList = gp.ListFeatureClasses("*", "polygon") # Print the list of feature classes one at a time for fc in fcList: print fc Infeatures = gp.GetParameterAsText(0) Outfeatures = gp.GetParameterAsText(1) Access to all toolbox tools Details: http://webhelp.esri.com/arcgisdesktop/9.3/body.cfm?tocVisable=1&ID=920&TopicName=Running%20a%20tool# Anatomy of ArcGIS Python
  • 10. Advanced Topic - ArcPy Mapping (10) http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D388EF60(arcpy.mapping sample script tools) http://blogs.esri.com/dev/blogs/arcgisdesktop/archive/2010/12/14/combining-data-driven-pages-with-python-and-arcpy.mapping.aspx (Combining Data Driven Pages with Python acrpy.mapping)
  • 11. For those committed http://code.google.com/edu/languages/google-python- class/introduction.html (PyQuick class by Google) http://training.esri.com/acb2000/showdetl.cfm?DID=6&Pro duct_ID=971 (Using Python in ArcGIS Desktop 10, web course) http://proceedings.esri.com/library/userconf/devsummit10/ tech/presumm_04.html (Getting Started with Python in ArcGIS) http://docs.python.org/tutorial/introduction.html (Python Tutorial) Books Learning Python by Mark Lutz Core Python by Wesley J. Chun Dive Into Python by Mark Pilgrim