New to Python need on advice on this script

Valentina Boycheva VBoycheva at jonesedmunds.com
Tue Nov 10 16:21:18 EST 2009


Jim, 

 

In Python 2.5 under ArcMap 9.3 domains can be only listed. To get to the
domain values, they need to be converted to tables. Also, the default
value cannot be obtained from the existing GP API. Very annoying! The
workaround is to use VBA ArcObjects or connect to the database directly,
if possible, and extract the information from the GDB. This latter will
work for PGDB, not sure about FGDB.

 

Regards,

Valentina Boycheva



 

From: Jim Valenza [mailto:jim.valenza at gmail.com] 
Sent: Tuesday, November 10, 2009 2:33 PM
To: python-list at python.org
Subject: New to Python need on advice on this script

 

Hello all - I'm new to the world of Python as I am a GIS guy who would
like to broaden his horizons. I have a question about a script that
we've been working on: The purpose of the code is to Syncronize SDE
DropBox with Regulatory Project working files. 

 

I am looking for where the script dumps the domain list to a table and
loops through the table? Is It necessary? Seems like you should be able
to simply loop through the domain list. I was just trying to see if
there was a more straitforward way. Thanks

The script is as follows:

 

# Create the Geoprocessor
import sys, string, os, arcgisscripting, shutil, time
gp = arcgisscripting.create()

# Jim V testing
# Allow output to overwrite
gp.OverwriteOutput = 1

gp.AddToolbox("C://Program Files//ArcGIS//ArcToolbox//Toolboxes//Data
Management Tools.tbx")

#---create a logging file named "YYYYMMDD HH_MM_QualityControlLog.txt" 
theTime = time.strftime('%Y%m%d %H_%M')
#logfilename = "H:\\Development\\Python\\" + theTime +
"_PODsQAQCLog.txt"
logfilename =
"L:\\SharedData\\Denver\\Regulatory\\GIS\\Powder_River_Basin\\Pipeline\\
QAQC_Scripts\\" + theTime + "_PODsQAQCLog.txt"
log = open(logfilename,'w')
log.write(time.ctime() + "\n")
print time.ctime()

THE_WORKSPACE =
os.path.normpath("L:/SharedData/Denver/Regulatory/GIS/Powder_River_Basin
") + "\\"  #REAL MODE LINE
#THE_WORKSPACE = os.path.normpath("H:/Development/testdata") + "\\"
#TEST MODE LINE
log.write("THE_WORKSPACE IS " + THE_WORKSPACE + "\n")
print "THE_WORKSPACE IS " + THE_WORKSPACE

P_CODE = "P_CODE"

#Create table of valid P_CODEs and convert to List
gp.DomainToTable_management(r"L:\SharedData\Denver\Regulatory\GIS\Powder
_River_Basin\GIS_Data\RMP_Current_Geodatabase\rmp_2_5.mdb", \
                            "regulatory_p_code",
r"L:\SharedData\Denver\Regulatory\GIS\Powder_River_Basin\Pipeline\QAQC_S
cripts\test.mdb\code_table", \
                            "pcode", "pdesc", "")
#searchRows =
gp.searchCursor(r"H:\Development\temp\test.mdb\code_table")
searchRows =
gp.searchCursor(r"L:\SharedData\Denver\Regulatory\GIS\Powder_River_Basin
\Pipeline\QAQC_Scripts\test.mdb\code_table")
searchRow = searchRows.next()
domainList = []
while searchRow:
    domainList.append(searchRow.pcode)
    searchRow = searchRows.next()
#print domainList

try:

    #Get Workspaces from text file
    #for line in open("H:/Development/testdata/PRB_POD_Paths.txt", 'r'):
#REAL MODE LINE
    for line in
open("L:/SharedData/Denver/Regulatory/GIS/Powder_River_Basin/Pipeline/QA
QC_Scripts/PRB_POD_Paths.txt", 'r'):   #REAL MODE LINE
    #for line in open("H:/Development/testdata/workspaces.txt", 'r'):
#TEST MODE LINE
        if not line: break
        line = line.strip()
        if not line.startswith("#"): # skip lines that start with #
            ws, P = line.split("RMP_2_5_",1) #parse line to get path and
P_CODE
            log.write("  " + "\n")
            log.write("  " + "\n")
            log.write(P + "\n")
            print "  "
            print P
            src = THE_WORKSPACE + line  #defines each workspace path
            
            #If the Geodatabase exists, perform the operations
            if os.path.exists(src):
                #Archive Geodatabase before running script
                log.write("The Geodatabase exists " + src + "\n")
                print "The Geodatabase exists " + src
                archiveFolder = THE_WORKSPACE + ws + "Archive" + "\\" +
"RMP_2_5_" + P
                log.write("archiveFolder is " + archiveFolder + "\n")
                print "archiveFolder is " + archiveFolder
                shutil.copy(src, archiveFolder)    #Archive the
Geodatabase

                #Set workspace and variables
                gp.workspace = src
                log.write(gp.workspace + "\n")
                print gp.workspace
                P_Value = (P.strip(".mdb"))

                #Valitate P_Code value against Domain list
                if P_Value in domainList:
                    log.write("P_Code is valid:  " + P_Value + "\n")
                    print "P_Code is valid:  " + P_Value

                    # List all feature classes in feature datasets
                    fdss = gp.ListDatasets("","")
                    fds = fdss.Next()
                    while fds:
                        fcs = gp.ListFeatureClasses("*","",fds)
                        fc = fcs.Next()
                        while fc:
                            gp.CalculateField_management(fc, P_CODE, "'"
+ P_Value + "'", "PYTHON")    #Calc P_Code
                            gp.RepairGeometry(fc,
r"L:\SharedData\Denver\Regulatory\GIS\Powder_River_Basin\Pipeline\QAQC_S
cripts\outy.shp")
                            fc = fcs.Next()
                            print fc
                        fds = fdss.Next()

                    #Copy the Geodatabase to the SDE InBox
                    sdeInbox = "S:/Production/DropBox/InBox"   #REAL
MODE LINE
                    #sdeInbox = "H:/Development/testInbox"   #TEST MODE
LINE
                    log.write("sdeInbox is " + sdeInbox + "\n")
                    print "sdeInbox is " + sdeInbox
                    shutil.copy(src, sdeInbox)     #Copy Geodatabase to
SDE Inbox
                else:
                    log.write("P_Code is NOT valid:  " + P_Value + "\n")
                    print "P_Code is NOT valid:  " + P_Value
            else:
                log.write("The Geodatabase does not exist " + src +
"\n")
                print "The Geodatabase does not exist " + src

except:
    # If an error occurred, print the messages returned by the tool
    log.write(gp.GetMessages() + "\n")
    print gp.GetMessages()

log.write(time.ctime() + "\n")
log.write("FINISHED" + "\n")
os.startfile("L:\\SharedData\\Denver\\Regulatory\\GIS\\Powder_River_Basi
n\\Pipeline\\QAQC_Scripts\\")
log.close()
print "FINISHED"
print time.ctime()

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091110/f1989244/attachment-0001.html>


More information about the Python-list mailing list