troubleshooting Question

Python script no longer works - only minor change made

Avatar of mgjust
mgjust asked on
PythonGIS/GPS Programming
11 Comments2 Solutions737 ViewsLast Modified:
Hello,

I used the script from https://www.experts-exchange.com/Q_23068228.html with great success. Only now it doesn't work and I am unsure why.  The only thing that has changed is the length of the digit after the diretory names from 2 to 3 (i.e. 01 is now 001) I believe I have updated the code accordingly to account for this change. I also changed the back slashes to forwards slashes. The original code is in the snippet box.

I get this error message when trying to execute the script:

C:\Documents and Settings\user>c:\python24\python "c:\new_donut.py"
Traceback (most recent call last):
  File "c:\new_donut.py", line 51, in ?
    for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
  File "c:\new_donut.py", line 40, in allLandcoverDonutAndIdSuffix
    assert os.path.isdir(DnumDir)
AssertionError

----

The only output I get in my "tester.txt" is "3"
My Directory has

C:\Giswork\landcoverprep\Donut
C:\Giswork\landcoverprep\LandcoverI
C:\Giswork\landcoverprep\LandcoverL
C:\Giswork\landcoverprep\Results

Donut has folders named "donut_001 - donut_407, donut_431, donut_435, donut_440, donut_447, donut_448

LandcoverI has the same numbering (i.e. LandcoverI_001 - 407, 431, 435, 440, 447)

LandcoverL (is missing about 10)

I also tried not including LandcoverL to the same error message.

Please advise,
Thanks,
MJ


# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'c:\\Giswork\\landcoverprep'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'Donut')
    assert os.path.isdir(donutDir)
    log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'landcover*'))
    log(mask)            
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct
            # the related donut numbered directory name.
            numSuffix = LnumDir[-3:]   # last 3 chars
            idSuffix = LnumDir[-5:]    # last 5 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            log("4/n")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)432
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = 'CON(ISNULL(%s), %s, %s * -1)'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())
# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import pdb
import arcgisscripting
import glob
import os
 
gisworkDir = 'c:\Giswork'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'landcoverprep', 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'Landcoverprep\landcover*'))
 
    ##log(mask)             
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
#        pdb.set_trace()
        for LnumDir in glob.glob(mask2):
 
            # Separate the counter part, the idSuffix and construct 
            # the related donut numbered directory name.
            numSuffix = LnumDir[-2:]   # last 2 chars
            idSuffix = LnumDir[-4:]    # last 4 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            ##log("4")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = 'CON(ISNULL(%s), %s, %s * -1)'
log("\n===== Output below: ====\n")
 
#pdb.set_trace()
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    print (landcover,donut,idSuffix)
    pdb.set_trace()
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'landcoverprep', 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())
ASKER CERTIFIED SOLUTION
pepr

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros