Geography 465 GIS Database Programming Getting Started with

Geography 465 GIS Database Programming Getting Started with GIS Database Programming

See Geog 465 course web site for Overview and Schedule

What is GIS Database Programming? • Geoprocessing - application of GIS operations on a set of data inputs for generating information. • Database (DB) programming – the part of geoprocessing focusing on DB manipulation; not quite spatial analysis but almost. • Use scripting to automate DB programming

Why write scripts? • Scripts supply the added benefit of decision making logic and looping functionality • Automate a work flow, for example: – Copy all incoming data into a geodatabase – Perform a project, clip, buffer operation on multiple data sets (iterate) • Easily distribute code – A script is a self-contained, single file

Why use Python for Scripting? • An open-source, object-oriented, scripting language • Offers IDE (integrated development environment) with debugging tools • Modular, can be broken apart • Ability to compile scripts • Installed with Arc. GIS 9 and ESRI samples provided

Writing code in Python • Writing Python code – Python command line – IDE (integrated development environment) e. g. , Python. Win, IDLE, Komodo – Michalis suggests using Komodo Edit http: //www. activestate. com/Products/komodo_id e/komodo_edit. mhtml • IDE allows you to perform all jobs from one location – Write, Save, Run, and Debug code

IDE Interface should have • Script window – Write and save code • Interactive window – Test lines of code – Report messages • Menus and Toolbars – standard and debugging

Basics of Python • Comment: A non-executable line of code – One number sign (#) for green and italicized – Two number signs (##) for gray # Name: Michalis Avraam # Date: January 5, 2009 # Purpose: To buffer a feature class import win 32 com. client gp = win 32 com. client. Dispatch(“esri. Geoprocessing. Gp. Dispatch. 1”) ## gp. Workspace = “C: \Python_Data\San. Diego. mdb” Gp. Buffer_analysis (“Freeways”, “Buff. Freeway”, 1000) • Can comment and uncomment blocks of code

Creating Scripts in Python • Use of Arc. GIS help system to find: – Usage, command syntax, and scripting examples of standard Arc. Toolbox tools, – Usage and syntax of Geoprocessor properties and methods, which are only accessible through scripting

Running Python Scripts Three Modes 1) Running scripts in IDE 2) Running scripts as script tools in Arc. GIS 3) Running scripts as embedded model components in Arc. GIS

(non) Running Scripts in Python • Problem with the schema lock – Cannot manipulate with the same file open in Arc. GIS and in IDE at the same time

Running scripts in Python. Win Example of a simple script to buffer a feature class # Name: Tim Nyerges # Date: January 5, 2009 # Purpose: To buffer a feature class import win 32 com. client gp = win 32 com. client. Dispatch("esri. Geoprocessing. Gp. Dispatch. 1“ gp. Workspace = "C: \Python_Data\San. Diego. mdb” gp. Buffer_analysis ("Freeways", "Buffered. Freeways", 1000)

Debugging the Code • Test code in IDE – A check for syntax errors • Do not test a script from Arc. Toolbox • Test code in IDE, then add it to Arc. Toolbox

Running scripts in IDE • Run the script • Check the messages that return: – While the script is running you will see the message “running script <…. >. py” - The indication of the successful run is the message “Script <…. > returned the exit code 0” • Display the result in Arc. Catalog

Running scripts in IDE Example of a simple script to clip a feature class # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To clip a feature class import win 32 com. client gp =win 32 com. client. Dispatch("esri. Geoprocessing. Gp. Dispatch. 1") gp. Workspace = "C: \Python_Data\San. Diego. mdb" gp. Clip_analysis ("Major. Attractions", "SDdowntown. Attractions")

Running scripts in IDE Example of a simple script to buffer and clip a feature class # Name Tim Nyerges # Date: January 5, 2009 # Purpose: To buffer SD freeways first and then to clip the downtown section # of freeways import win 32 com. client gp = win 32 com. client. Dispatch("esri. Geoprocessing. Gp. Dispatch. 1") gp. Workspace = "C: \Python_Data\San. Diego. mdb" if gp. Exists(gp. Workspace + "\buffered. Freeways"): gp. Delete_management(gp. Workspace + "\buffered. Freeways") gp. Buffer_analysis ("Freeways", "buffered. Freeways", "500") gp. Clip_analysis ("buffered. Freeways", "SDdowntown", "downtown. Freeways")

Michalis will get you started in labb session… “hands-on” Python and Komodo edit

Variables in Python • Variables are dynamically typed – No declaration required – No type assignment required fc = “C: \Project. Data\San. Diego. mdb\Freeways. shp” • Variables are case sensitive fc = “Freeways. shp” Fc = 20000 Two different variables • Variables can hold different data types – numbers, strings, lists, files

Numbers • Variables can hold numbers and expressions num 1 = 1. 2 num 2 = 3 + 5

Strings • Variables can hold strings folder = “c: /Student” • Strings are surrounded in double (“) or single (‘) quotes • Pathnames use two back (\) or one forward (/) slash One backslash () is a reserved escape character and a line continuation character

Strings • Strings can be combined together gdb. Path = “c: \San. Diego. mdb” fc = “Roads” full. Path = gdb. Path + “\” + fc • Strings are indexed C: San. Diego. mdbRoads” – strings are zero-based from the left and one-based from the right fc = “Streets. shp” fc[0] ---> “S” # S is in the 0 position fc[1: 3] ---> “tr” # start at 1 st, up to not including 3 rd fc[: -4] ---> “Streets” # get rid of the last 4 charaters
![Lists • Variables can hold lists num. List = [1, 2, 3] fc. List Lists • Variables can hold lists num. List = [1, 2, 3] fc. List](http://slidetodoc.com/presentation_image_h2/74f8cb0c5e035cd697cf41cd83ea9f3d/image-22.jpg)
Lists • Variables can hold lists num. List = [1, 2, 3] fc. List = [“Roads”, “Streets”, “Parcels”, “Zipcodes”] • Lists are indexed fc 1 = fc. List[1] fc 2 = fc. List[0: 2] fc 3 = fc. List[0: -1] fc 4 = fc. List[2: ] ---> “Streets” ---> “Roads”, “Streets”, “Parcels” ---> “Parcels”, “Zipcodes”

Variable naming conventions • Upper case versus lower case – First word lower case, capitalize each successive word table. Field. Name = “Street” - Acronym at the beginning, use lower case letters gdb. Path = “C: \San. Diego. mdb” - Acronym in the middle or at the end, use upper case letters input. FC = “Streets. shp” • Avoid special characters (for example / & % # !) • Use descriptive variable names
![Line continuation • Line continuation characters – Parentheses ( ), brackets [ ], and Line continuation • Line continuation characters – Parentheses ( ), brackets [ ], and](http://slidetodoc.com/presentation_image_h2/74f8cb0c5e035cd697cf41cd83ea9f3d/image-24.jpg)
Line continuation • Line continuation characters – Parentheses ( ), brackets [ ], and braces { } – Backslash • Indentation is automatic fc. List = [“Roads”, “Climate”, “Streams”, “Zipcodes”, “Coastlines”] distance. Values = 100, 200, 300, 400, 500, 1000, 1500 gp. Buffer_analysis(fc. List[2], Buff. Streams 1000, distance. Values[5])
- Slides: 24