0

The final save raster file errors without message with Does not exist or is not support. But it does exist. If you can find anything wrong with this script I would be very greatfull. I had a vast array of errors with this bit of code. But much of it is already working in other parts of the script.

path ="C:/gisdata/india/imagery/"
parentdir ="off"

print "===== using Not isfile and listing folder names only ======="
for fname in os.listdir(path + parentdir):
  if not os.path.isfile(os.path.join(path + parentdir, fname)):
#   pathfolders.append(os.path.join(path, fname))
    dirname.append(fname)

print dirname

#Loop through image directories and list the tif raster files within each and
#process to reflectance values
for dir in dirname:
    print "Setting workspace"
    #Set workspace to current working directory
    env.workspace = path + parentdir + "/" + dir
    print "Workspace set!"



print "Creating local variables for input bands to Dark Pixel Subtraction"
#Local variable = Raster("LE71460402010085ASN00_B1.TIF")
Rt_B1dp = Raster(path + parentdir + "/" + dir + "/" + "Rt_b1.TIF")
Rt_B2dp = Raster(path + parentdir + "/" + dir + "/" + "Rt_b2.TIF")
Rt_B3dp = Raster(path + parentdir + "/" + dir + "/" + "Rt_b3.TIF")
Rt_B4dp = Raster(path + parentdir + "/" + dir + "/" + "Rt_b4.TIF")

sample = "C:/gisdata/india/imagery/sample"
bands = ("Rt_B1.TIF", "Rt_B2.TIF", "Rt_B3.TIF", "Rt_B4.TIF")
dpRasters = (Rt_B1dp, Rt_B2dp, Rt_B3dp, Rt_B4dp)
fieldNames = ("Rt_b1", "Rt_b2", "Rt_b3", "Rt_b4")

for dpRaster in dpRasters:
    for band in bands:
        for fieldName in fieldNames:
            #Sampling raster values using select points from Delhi_dp.shp
            print "Sampling raster values using selected points"
            arcpy.sa.Sample( band,"C:/gisdata/india/imagery/Delhi_dp.shp","sample","NEAREST")

            queryString = '"{0}" IS NOT NULL AND "{0}" > 0'.format(fieldName)
            #Use SearchCursor to query non NULL values for creating a new variable
            with arcpy.da.SearchCursor("sample", (fieldName),queryString) as cursor:
                for row in cursor:
                    dpValue = row[0]

                    print "Starting dpRaster subtraction"
                    if arcpy.Exists(dpRaster):
                        arcpy.Delete_management(dpRaster)
                    print "Subtracting " + str(dpValue) + " from " + str(dpRaster)
                    raster_dp = dpRaster - dpValue
                    print "Saving " + str(dpRaster) + " as image file"
                    raster_dp.save(path + parentdir + "/" + dir + "/" + dpRaster + ".TIF")
                    print str(dpRaster) + " has been created"
7
  • 2
    Why don't you use os.path.join() for all of your path-building needs? Also, I'm not sure if it's a formatting error, but your indentation doesn't seem correct.
    – Paul
    Commented Oct 3, 2013 at 4:30
  • Thanks, the indentation is correct but they didn't paste into the box very well. Can you elaborate on how I might use os.path.join()? I m a beginner remember :-)
    – Simon
    Commented Oct 3, 2013 at 6:51
  • Try putting an "r" in front of all the path strings, for example, path = r"C:/gisdata/india/imagery/"
    – Hornbydd
    Commented Oct 3, 2013 at 12:55
  • 1
    In your raster_dp.save() line, replace what you have in the brackets with os.path.join(path + parentdir,dir,str(dpRaster) + ".TIF"). It seems like you might be getting numeric values for dpRaster, in which case you cannot use string concatenation as you currently have it, the number needs to be cast to a string. Commented Oct 4, 2013 at 5:03
  • 1
    Can you determine what line the code is hanging up on?
    – Barbarossa
    Commented Oct 7, 2013 at 14:59

1 Answer 1

1

I think it may be due to single and double slashes. I might also try defining input and output shapefiles/rasters as local variables. Try this:

path = r"C:/gisdata/india/imagery"
parentdir ="off"

print "===== using Not isfile and listing folder names only ======="
for fname in os.listdir(path + parentdir):
    if not os.path.isfile(os.path.join(path + parentdir, fname)):
    # pathfolders.append(os.path.join(path, fname))
    dirname.append(fname)

print dirname

# Loop through image directories and list the tif raster files within each and
# process to reflectance values
for dir in dirname:
    print "Setting workspace"
#Set workspace to current working directory
env.workspace = path + "//" + parentdir + "//" + dir
print "Workspace set!"

print "Creating local variables for input bands to Dark Pixel Subtraction"
# Local variable = Raster("LE71460402010085ASN00_B1.TIF")
Rt_B1dp = Raster(path + "//" + parentdir + "//" + dir + "//" + "Rt_b1.TIF")
Rt_B2dp = Raster(path + "//" + parentdir + "//" + dir + "//" + "Rt_b2.TIF")
Rt_B3dp = Raster(path + "//" + parentdir + "//" + dir + "//" + "Rt_b3.TIF")
Rt_B4dp = Raster(path + "//" + parentdir + "//" + dir + "//" + "Rt_b4.TIF")

outSample = r"C:/gisdata/india/imagery/sample"
inSample = r"C:/gisdata/india/imagery/Delhi_dp.shp"
bands = ("Rt_B1.TIF", "Rt_B2.TIF", "Rt_B3.TIF", "Rt_B4.TIF")
dpRasters = (Rt_B1dp, Rt_B2dp, Rt_B3dp, Rt_B4dp)
fieldNames = ("Rt_b1", "Rt_b2", "Rt_b3", "Rt_b4")

for dpRaster in dpRasters:
    for band in bands:
        for fieldName in fieldNames:
            # Sampling raster values using select points from Delhi_dp.shp
            print "Sampling raster values using selected points"
            arcpy.sa.Sample(band, inSample, outSample, "NEAREST")

        queryString = '"{0}" IS NOT NULL AND "{0}" > 0'.format(fieldName)
        # Use SearchCursor to query non NULL values for creating a new variable
        with arcpy.da.SearchCursor("outSample", (fieldName),queryString) as cursor:
            for row in cursor:
                dpValue = row[0]

                print "Starting dpRaster subtraction"
                if arcpy.Exists(dpRaster):
                    arcpy.Delete_management(dpRaster)
                print "Subtracting " + str(dpValue) + " from " + str(dpRaster)
                raster_dp = dpRaster - dpValue
                print "Saving " + str(dpRaster) + " as image file"
                raster_dp.save(path + "//" + parentdir + "//" + dir + "//" + dpRaster + ".TIF")
                print str(dpRaster) + " has been created"
3
  • Shouldn't that be double backslashes? At any rate, you're opening up the the output to errors by concatenating paths that way. The os module has a function already designed for this...
    – Paul
    Commented Oct 4, 2013 at 15:51
  • @Paul Thanks for the correction, and you are correct about the os module. I was simply pointing out to Simon that the simplest fix may be adding double backslashes, as he has already pointed out that he lacks an extensive knowledge of python.
    – Barbarossa
    Commented Oct 4, 2013 at 16:01
  • Thanks @Barbarossa, I've tried changing the path to double backslashes but the error persists.
    – Simon
    Commented Oct 6, 2013 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.