Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

New to Python need on advice on this script

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jim.valenza at gmail

Nov 10, 2009, 11:33 AM

Post #1 of 3 (529 views)
Permalink
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_Scripts\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/QAQC_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_Scripts\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_Basin\\Pipeline\\QAQC_Scripts\\")
log.close()
print "FINISHED"
print time.ctime()


python at mrabarnett

Nov 10, 2009, 12:09 PM

Post #2 of 3 (523 views)
Permalink
Re: New to Python need on advice on this script [In reply to]

Jim Valenza wrote:
> 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:
>
[snip]

I've spotted a bug:

P_Value = (P.strip(".mdb"))

The .strip() method DOESN'T strip off a string, it strips CHARACTERS off
BOTH ends.

P_Value will be P but with any number of the characters ".", "m", "d" or
"b" stripped off both ends, for example, if P == "my_database.mdb" then
P_Value will be "y_database".

You should do something like:

if P.endswith(".mdb"):
P_Value = P[ : -4]
else:
P_Value = P

Another point: you're recording the valid domains in a list and then
checking with:

if P_Value in domainList:

This will perform a linear search of the list, which can be slow if the
list is long.

It would be much more efficient to put the domains in a set instead.
Checking for the presence of an item in a set is fast.

--
http://mail.python.org/mailman/listinfo/python-list


VBoycheva at jonesedmunds

Nov 10, 2009, 1:21 PM

Post #3 of 3 (511 views)
Permalink
RE: New to Python need on advice on this script [In reply to]

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]
Sent: Tuesday, November 10, 2009 2:33 PM
To: python-list [at] python
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()

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.