Home arrow GIS Data & Resources arrow Scripts and Code arrow Python arrow Python Code from UGIC '08 Workshop!
Python Code from UGIC '08 Workshop! PDF Print E-mail

Written by Kevin Bell,


Here are the python code samples that I used in Friday's Introduction to Python workshop at the UGIC conference in Cedar City.

#----------------------------------------------------------------------------------------------------------------------- 

#1gpExists.py
#gp METHOD

import arcgisscripting
gp = arcgisscripting.create()

if gp.exists(r"C:\ArcGIS\ArcTutor\BuildingaGeodatabase\Montgomery.gdb\Parcels"):
    print "USstates.shp exists"
else:
    print "no shp named USstates.shp"
   
del gp
print "DONE"

#ooooooooooooooooooooo OR oooooooooooooooooooooooo

#import os

#if os.path.isfile(r"C:\ArcGIS\ArcTutor\Using_ArcGIS_Desktop\USstates.shp"):
    #print "USstates.shp exists"
#else:
    #print "no shp named USstates.shp"

#-------------------------------------------------------------------------------------------------------------------

 #2gpCreateDir
#gp TOOL

import arcgisscripting
gp = arcgisscripting.create()

if gp.exists("C:/temp/ugic"): #---gp method
    print "the directory is already there"
else:
    gp.CreateFolder_management("C:/temp", "ugic") #---tool
    print "made the directory"

del gp

#----------------------------------------------------------------------------------------------------------------------------

#1 & 2 combined

import arcgisscripting
gp = arcgisscripting.create()

theShp = r"C:\ArcGIS\ArcTutor\Using_ArcGIS_Desktop\USstates.shp"
thePath = r"C:\temp\ugic"

if gp.exists(theShp):
    print "USstates.shp exists,"
   
    if gp.exists(thePath):
        print "...and the directory exists"
    else:
        gp.CreateFolder_management(r"C:\temp", "ugic")
        print "...and I made the directory"
   
    gp.Copy_management(theShp, thePath + "\\myUSstates.shp")
    print "successfully copied"
else:
    print "no shp named USstates.shp"
   
del gp

#----------------------------------------------------------------------------------------------------------------------

import arcgisscripting
gp = arcgisscripting.create()
fc = r"C:\temp\Lights8.shp"

dsc = gp.describe(fc)
 
print "Spatial Index: " + str(dsc.HasSpatialIndex) #must convert boolean to str
print "Shape Type: " + dsc.ShapeType #already a str (see object model)
print dsc.CatalogPath
print dsc.DataType #Shapefile, FeatureClass
print dsc.SpatialReference.Name

del gp

#-------------------------------------------------------------------------------------------------------------------------

import arcgisscripting
gp = arcgisscripting.create()
gp.workspace = r"P:\StreetLights\MonthlyDataFromCLS\200709"

fields = []

myList = gp.ListFields("Lights.shp")
field = myList.Next()
while field:
    fields.append(field.name)
    print field.name + " is a " + field.Type
    field = myList.Next()

del gp
print fields

#---------------------------------------------------------------------------------------------------------------------------

print "start"

import arcgisscripting
gp = arcgisscripting.create()
gp.workspace = r"C:\ArcGIS\ArcTutor\BuildingaGeodatabase\Montgomery.gdb\Landbase"

fc = "Parcels"

#SEARCH CURSOR xxxxxxxxxxxxxxxxxxxxxx

#rows = gp.SearchCursor(fc)
#row = rows.Next()
#l = []
#while row:
    #print row.STATE_NAME
    #l.append(row.STATE_NAME)
    #row = rows.Next()
 
#l.sort()
#print l

#INSERT CURSOR xxxxxxxxxxxxxxxxxxxxxxxx
try:
    rows = gp.InsertCursor(fc)
    row = rows.NewRow()
    row.Res = "KevinLand"
    rows.InsertRow(row)
    print "insert sucess"
except:
    print "failed"


#UPDATE CURSOR xxxxxxxxxxxxxxxxxxxxxxx
#myAge = 12

#rows = gp.UpdateCursor(fc)
#row = rows.Next()

#while row:
    #row.POP90_SQMI = row.POP90_SQMI * myAge
    #print row.POP90_SQMI * myAge
    #rows.UpdateRow(row)
    #row = rows.Next()

del rows
del gp

#----------------------------------------------------------------------------------------------------

import arcgisscripting
gp = arcgisscripting.create()
gp.workspace = "c:\\temp\\ugic"
fc = "myUSstates.shp"

'''ArcGIS toolbox parameters:
DisplayName: Enter your age
DataType: String'''

myAge = int(gp.GetParameterAsText(0)) #convert str to int

rows = gp.UpdateCursor(fc)
row = rows.Next()

while row:
    row.POP90_SQMI = row.POP90_SQMI - myAge
    print row.POP90_SQMI - myAge
    rows.UpdateRow(row)
    row = rows.Next()

del rows
del gp

#--------------------------------------------------------------------------------------------------

#call Model

import arcgisscripting
gp = arcgisscripting.create()
gp.workspace = "c:\\temp\\ugic"

gp.AddToolbox(r"P:\Transportation\00_SLCDOT.tbx")

try:
    gp.getFreqofMStates()
    print "success"
except:
    print "bombed"

#---------------------------------------------------------------------------------------------------

 #file named TEN_functions.py

def getSurroundingStates(stateName):
    '''given a state name, creates a shp with those surrounding it '''
    import arcgisscripting
    gp = arcgisscripting.create()
    gp.overwriteoutput = 1
    gp.workspace = (r"C:\ArcGIS\ArcTutor\Using_ArcGIS_Desktop")
   
    try:
        gp.MakeFeatureLayer("USstates.shp","States")
        gp.SelectLayerByAttribute("States", "NEW_SELECTION", '"STATE_NAME"' + " =  " + "'" + stateName + "'")
        gp.SelectLayerByLocation("States", "BOUNDARY_TOUCHES", "States")
        gp.CopyFeatures("States", "c:/temp/statesSurrounding" + stateName + ".shp")
        print "success: getSurroundingStates of " + stateName

    except:
        print gp.GetMessages()
   
    del gp

#-----------------------------------------------------------------------------------------------------

import os

#you must tell python were to look for your code
os.sys.path.append(r"C:\Documents and Settings\Desktop\pyClass\zMisc")

import TEN_functions

TEN_functions.getSurroundingStates("California")

#------- you can prompt for input...
#functions.getSurroundingStates(raw_input("Enter a State Name: "))

#-------"map" is a python built-in function will execute a function for each item in a list...
#stateList = ["Mississippi", "Oregon", "New York"]
#map(TEN_functions.getSurroundingStates, stateList)

print "done calling imported function"

 



Users' Comments  
 

No comment posted

Add your comment

29, Apr. 2008
Last Updated ( 29, Apr. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for