Introduction to Python in ArcGIS
John Zhou
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 water mark annotation on image#John Zhou MWH Global 12/19/2006#Fragile, not for other use#need Python and PIL Installed
import win32com.clientimport fnmatchimport stringimport osimport Image,ImageDraw,ImageFont,sysengine = win32com.client.Dispatch("DAO.DBEngine.36")
strPath="E:\\PWP_WaterMark\\"#Determine the path to your database and use the following syntax:#make sure the database namedb = engine.OpenDatabase(strPath+"ConsolidatedGPS_18Dec2006.mdb")totalNum=0ferr=open("c:\\err.txt","w")fsuccess=open("c:\\success.txt","w")for fileName in os.listdir ( strPath+"Field Photos"):if fnmatch.fnmatch ( fileName, '*.jpg' ) :path, name = os.path.split (fileName)queryString="select PoleFacilityID, Address, PhotoLocation from Pole where PhotoLocation like '*" +name+"*'"# print queryStringx=1rs = db.OpenRecordset( queryString )Anno=""
� while not rs.EOF and x<2:Anno= rs.Fields("PoleFacilityID").Value+"|"+rs.Fields("Address").Value+ "|"+namers.MoveNextx=x+1strFileName = strPath+"Field Photos\\"+fileName.upper()totalNum=totalNum+1if (len(Anno)>0):fsuccess.write(strFileName+"\n")else:ferr.write(strFileName+"\n")# print strFileNameim1=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:breakfont = 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 totalNumferr.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 tenfold 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
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&Product_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
Top Related