Top Banner
Geography 465 GIS Database Programming Getting Started with GIS Database Programming
24

Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Geography 465 GIS Database Programming

Getting Started with GIS Database Programming

Page 2: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

See Geog 465 course web site for

Overview and Schedule

Page 3: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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

Page 4: Geography 465 GIS Database Programming Getting Started with GIS Database 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

Page 5: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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 ArcGIS 9 and ESRI samples provided

Page 6: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Writing code in Python

• Writing Python code– Python command line – IDE (integrated development environment)

e.g., PythonWin, IDLE, Komodo– Michalis suggests using Komodo Edit

http://www.activestate.com/Products/komodo_ide/komodo_edit.mhtml

• IDE allows you to perform all jobs from one location– Write, Save, Run, and Debug code

Page 7: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

IDE Interface should have

• Script window– Write and save code

• Interactive window– Test lines of code– Report messages

• Menus and Toolbars– standard and debugging

Page 8: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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 classimport win32com.clientgp =

win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”)## gp.Workspace = “C:\\Python_Data\\SanDiego.mdb”Gp.Buffer_analysis (“Freeways”, “BuffFreeway”, 1000)

• Can comment and uncomment blocks of code

Page 9: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Creating Scripts in Python

• Use of ArcGIS help system to find:– Usage, command syntax, and scripting

examples of standard ArcToolbox tools,– Usage and syntax of Geoprocessor properties

and methods, which are only accessible through scripting

Page 10: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Running Python Scripts Three Modes

1) Running scripts in IDE

2) Running scripts as script tools in ArcGIS

3) Running scripts as embedded model components in ArcGIS

Page 11: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

(non) Running Scripts in Python

• Problem with the schema lock– Cannot manipulate with the same file open in

ArcGIS and in IDE at the same time

Page 12: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Running scripts in PythonWin

Example of a simple script to buffer a feature class

# Name: Tim Nyerges

# Date: January 5, 2009

# Purpose: To buffer a feature class

import win32com.client

gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1“

gp.Workspace = "C:\\Python_Data\\SanDiego.mdb”

gp.Buffer_analysis ("Freeways", "BufferedFreeways", 1000)

Page 13: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Debugging the Code

• Test code in IDE– A check for syntax errors

• Do not test a script from ArcToolbox• Test code in IDE, then add it to ArcToolbox

Page 14: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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 ArcCatalog

Page 15: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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 classimport win32com.clientgp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")gp.Workspace = "C:\\Python_Data\\SanDiego.mdb"gp.Clip_analysis ("MajorAttractions", "SDdowntown",

"SDdowntownAttractions")

Page 16: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

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 freewaysimport win32com.clientgp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")gp.Workspace = "C:\\Python_Data\\SanDiego.mdb"if gp.Exists(gp.Workspace + "\\bufferedFreeways"): gp.Delete_management(gp.Workspace + "\\bufferedFreeways")gp.Buffer_analysis ("Freeways", "bufferedFreeways", "500")gp.Clip_analysis ("bufferedFreeways", "SDdowntown", "downtownFreeways")

Page 17: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Michalis will get you started in labb session… “hands-on”

Python and Komodo edit

Page 18: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Variables in Python

• Variables are dynamically typed– No declaration required– No type assignment required fc = “C:\\ProjectData\\SanDiego.mdb\\Freeways.shp”

• Variables are case sensitivefc = “Freeways.shp” Fc = 20000

• Variables can hold different data types– numbers, strings, lists, files

Two different variables

Page 19: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Numbers

• Variables can hold numbers and expressions

num1 = 1.2 num2 = 3 + 5

Page 20: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Strings

• Variables can hold strings folder = “c:/Student”

• Strings are surrounded in double (“) or single (‘) quotes

• Pathnames use two back (\\) or one forward (/) slashOne backslash (\) is a reserved escape

character and a line continuation character

Page 21: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Strings• Strings can be combined together

gdbPath = “c:\\SanDiego.mdb”fc = “Roads”fullPath = gdbPath + “\\” + fc

• Strings are indexed– strings are zero-based from the left and one-based from

the rightfc = “Streets.shp”fc[0] ---> “S” # S is in the 0 positionfc[1:3] ---> “tr” # start at 1st, up to not including 3rdfc[:-4] ---> “Streets” # get rid of the last 4 charaters

C:\SanDiego.mdb\Roads”

Page 22: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Lists

• Variables can hold listsnumList = [1, 2, 3]

fcList = [“Roads”, “Streets”, “Parcels”, “Zipcodes”]

• Lists are indexed

fc1 = fcList[1]

fc2 = fcList[0:2] ---> “Roads”, “Streets”

fc3 = fcList[0:-1] ---> “Roads”, “Streets”, “Parcels”

fc4 = fcList[2:] ---> “Parcels”, “Zipcodes”

---> “Streets”

Page 23: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Variable naming conventions

• Upper case versus lower case– First word lower case, capitalize each successive word tableFieldName = “Street”

- Acronym at the beginning, use lower case letters gdbPath = “C:\\SanDiego.mdb”

- Acronym in the middle or at the end, use upper case lettersinputFC = “Streets.shp”

• Avoid special characters (for example / \ & % # !)

• Use descriptive variable names

Page 24: Geography 465 GIS Database Programming Getting Started with GIS Database Programming.

Line continuation• Line continuation characters

– Parentheses ( ), brackets [ ], and braces { }– Backslash \

• Indentation is automaticfcList = [“Roads”, “Climate”, “Streams”,

“Zipcodes”, “Coastlines”]

distanceValues = 100, 200, 300, 400, 500, \1000, 1500

gp.Buffer_analysis(fcList[2], BuffStreams1000, distanceValues[5])