Hello all,
I want to do a raster calculation as done here: https://gis.stackexchange.com/questions/332925/looping-through-two-raster-folders-to-perform-raster-... and here: https://gis.stackexchange.com/questions/333082/looping-through-two-raster-folders-to-perform-raster-...
It is more or less running correctly, but the output is written into the first env.workspace or second env.workspace. I simply cannot get the output rasters into my dedicated output directory.
Any clues?
Here my code snipped:
import arcpy
from arcpy import env
from arcpy.sa import *#Spatial Analyst module
import os
from datetime import datetime
import subprocess
import glob
arcpy.env.overwriteOutput = True
arcpy.env.matchMultidimensionalVariable = False
arcpy.CheckOutExtension("Spatial")
#-----------------------------
# Divide LPAWC*100 by LAPWC
# get first set of rasters
LPAWC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWCDate.gdb" #'source 1'
arcpy.env.workspace = LPAWC
rasters_LPAWC = arcpy.ListRasters()
rasters_LPAWC = [os.path.join(LPAWC, r) for r in rasters_LPAWC] # necessary for zip, but permits folder2 to get to work...
# get second set of rasters
LPAWFC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWFC.gdb" #'source 2'
arcpy.env.workspace = LPAWFC
rasters_LPAWFC = arcpy.ListRasters()
rasters_LPAWFC = [os.path.join(LPAWFC, rs) for rs in rasters_LPAWFC] # necessary for zip, but permits folder2 to get to work...
# folder path2
folder2 = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPWCfinal.gdb"+ "\\" # not working!!!
#loop for multiply by 100 and divide by LPAWC to get percent nFK
for LPAWC_ras, LPAWFC_ras in zip(rasters_LPAWC, rasters_LPAWFC):
r1 = Raster(LPAWC_ras)
r2 = Raster(LPAWFC_ras)
output_raster2 = 100*r1/r2
out_raster2 = folder2 + arcpy.Describe(r1).baseName + "_prznFK" #'{}{}'.format(r1,"_prznFK")
output_raster2.save(out_raster2)
print("{} Processed Successfully!".format(out_raster2))
Cheers for any help!
Ingo
Solved! Go to Solution.
Ok, cheers for the advice with
arcpy.ClearEnvironment("workspace")
It seemed not working though, until I just created a new output path! It sometimes is so odd, why some output .gdbs are easily accepted within ArcCatalog, but not in a stand alone script!
However, this is my script now working, despite long path names which I need for my working routine...
import arcpy
from arcpy import env
from arcpy.sa import *#Spatial Analyst module
import os
from datetime import datetime
import subprocess
import glob
arcpy.env.overwriteOutput = True
arcpy.env.matchMultidimensionalVariable = False
arcpy.CheckOutExtension("Spatial")
start_time = datetime.now()
#-----------------------------
# Divide LPAWC*100 by LAPWC
# get first set of rasters
LPAWC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWCDate.gdb" #'source 1'
arcpy.env.workspace = LPAWC
rasters_LPAWC = arcpy.ListRasters()
rasters_LPAWC = [os.path.join(LPAWC, r) for r in rasters_LPAWC] # necessary for zip, but permits folder2 to get to work...
arcpy.ClearEnvironment("workspace")
# prints None
print(arcpy.env.workspace)
# get second set of rasters
LPAWFC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWFC.gdb" #'source 2'
arcpy.env.workspace = LPAWFC
rasters_LPAWFC = arcpy.ListRasters()
rasters_LPAWFC = [os.path.join(LPAWFC, rs) for rs in rasters_LPAWFC] # necessary for zip
arcpy.ClearEnvironment("workspace")
# prints None
print(arcpy.env.workspace)
# folder path
folder2 = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPWC_nfk.gdb" + "\\"
#loop for multiply by 100 and divide by LPAWC to get percent nFK
for LPAWC_ras, LPAWFC_ras in zip(rasters_LPAWC, rasters_LPAWFC):
r1 = Raster(LPAWC_ras)
r2 = Raster(LPAWFC_ras)
output_raster2 = 100*r1/r2
out_raster2 = folder2 + arcpy.Describe(r1).baseName + "_prznFK"
output_raster2.save(out_raster2)
print("{} Processed Successfully!".format(out_raster2))
end_time = datetime.now()
print('Prozessdauer: {}'.format(end_time - start_time))
Cheers!
Ingo
P.S. German and efficiency is like oil and water, seriously!
indentation and line numbers would help
Code formatting ... the Community Version - Esri Community
Apologies! I'm not too often posting scripts...
import arcpy
from arcpy import env
from arcpy.sa import *#Spatial Analyst module
import os
from datetime import datetime
import subprocess
import glob
arcpy.env.overwriteOutput = True
arcpy.env.matchMultidimensionalVariable = False
arcpy.CheckOutExtension("Spatial")
start_time = datetime.now()
# Divide LPAWC*100 by LAPWC
# get first set of rasters
LPAWC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWCDate.gdb" #'source 1'
arcpy.env.workspace = LPAWC
rasters_LPAWC = arcpy.ListRasters()
rasters_LPAWC = [os.path.join(LPAWC, r) for r in rasters_LPAWC] # necessary for zip, but permits folder2 to get to work...
# get second set of rasters
LPAWFC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWFC.gdb" #'source 2'
arcpy.env.workspace = LPAWFC
rasters_LPAWFC = arcpy.ListRasters()
rasters_LPAWFC = [os.path.join(LPAWFC, rs) for rs in rasters_LPAWFC] # necessary for zip
# folder path2
folder2 = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPWCfinal.gdb"+ "\\"
#loop for multiply by 100 and divide by LPAWC to get percent nFK
for LPAWC_ras, LPAWFC_ras in zip(rasters_LPAWC, rasters_LPAWFC):
r1 = Raster(LPAWC_ras)
r2 = Raster(LPAWFC_ras)
output_raster2 = 100*r1/r2
out_raster2 = folder2 + arcpy.Describe(r1).baseName + "_prznFK" #'{}{}'.format(r1,"_prznFK")
output_raster2.save(out_raster2)
print("{} Processed Successfully!".format(out_raster2))
end_time = datetime.now()
print('Prozessdauer: {}'.format(end_time - start_time))
between line 41 and 42, try throwing in a print statement to print to make sure the preferred outputs are correct
Hi Dan,
ok, the print statement gives the correct output workspace ans anticipated rastername...but only if i comment the .save - command from line 42 (then 43) out.
Cheers!
Ingo
Can you share the output of your prints? I think it may be something to do with the raw strings then how you're concatenating them, it misreads the string and puts it into the workspace, I much prefer to use os.path.join
perhaps try https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/clearenvironment.htm and see if it fails with a helpful message when trying to write to that location.
Hi David,
cheers for tipping me to
arcpy.ClearEnvironment("my_workspace")
However, it es not working.
The error message is as follows:
Running script: "K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\Zentrale_Scripte\NFK_mGROWA.py"
Traceback (most recent call last):
File "K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\Zentrale_Scripte\NFK_mGROWA.py", line 163, in <module>
arcpy.ClearEnvironment(LPAWC)
File "C:\Program Files\ESRI\ArcGISPro\Resources\ArcPy\arcpy\__init__.py", line 275, in ClearEnvironment
return gp.clearEnvironment(environment_name)
File "C:\Program Files\ESRI\ArcGISPro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 126, in clearEnvironment
self._gp.ClearEnvironment(*gp_fixargs(args, True)))
AttributeError: Object: Cannot clear environment <K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWCDate.gdb>
I just added the command for bothe einvironment workspace below my output directory "folder2".
When I comment both "ClearEnvironment" commands out, the cript says
Running script: "K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\Zentrale_Scripte\NFK_mGROWA.py"
Traceback (most recent call last):
File "K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\Zentrale_Scripte\NFK_mGROWA.py", line 172, in <module>
output_raster2.save(out_raster2)
RuntimeError: ERROR 000875: Output raster: Der Workspace von K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPWCfinal.gdb\L1pawc20230222_prznFK ist ein ungültiger Ausgabe-Workspace.
the output-wokspace is not valid (I have no clue why ths error message comes in German...)
Cheers
Ingo
It's in German as its more efficient.
I think you just want to clear the workspace environment like the sample, try it exactly as below:
arcpy.ClearEnvironment("workspace")
# prints None
print(arcpy.env.workspace)
Is there any way you can try the process in a simpler folder without spaces and not beginning with numbers?
Ok, cheers for the advice with
arcpy.ClearEnvironment("workspace")
It seemed not working though, until I just created a new output path! It sometimes is so odd, why some output .gdbs are easily accepted within ArcCatalog, but not in a stand alone script!
However, this is my script now working, despite long path names which I need for my working routine...
import arcpy
from arcpy import env
from arcpy.sa import *#Spatial Analyst module
import os
from datetime import datetime
import subprocess
import glob
arcpy.env.overwriteOutput = True
arcpy.env.matchMultidimensionalVariable = False
arcpy.CheckOutExtension("Spatial")
start_time = datetime.now()
#-----------------------------
# Divide LPAWC*100 by LAPWC
# get first set of rasters
LPAWC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWCDate.gdb" #'source 1'
arcpy.env.workspace = LPAWC
rasters_LPAWC = arcpy.ListRasters()
rasters_LPAWC = [os.path.join(LPAWC, r) for r in rasters_LPAWC] # necessary for zip, but permits folder2 to get to work...
arcpy.ClearEnvironment("workspace")
# prints None
print(arcpy.env.workspace)
# get second set of rasters
LPAWFC = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPAWFC.gdb" #'source 2'
arcpy.env.workspace = LPAWFC
rasters_LPAWFC = arcpy.ListRasters()
rasters_LPAWFC = [os.path.join(LPAWFC, rs) for rs in rasters_LPAWFC] # necessary for zip
arcpy.ClearEnvironment("workspace")
# prints None
print(arcpy.env.workspace)
# folder path
folder2 = r"K:\6202_Klimawandel_Folgen_Anpassung\620203_Werkbank\mGROWA\WHM_Klimaatlas_Neuentwicklungen\LPWC_nfk.gdb" + "\\"
#loop for multiply by 100 and divide by LPAWC to get percent nFK
for LPAWC_ras, LPAWFC_ras in zip(rasters_LPAWC, rasters_LPAWFC):
r1 = Raster(LPAWC_ras)
r2 = Raster(LPAWFC_ras)
output_raster2 = 100*r1/r2
out_raster2 = folder2 + arcpy.Describe(r1).baseName + "_prznFK"
output_raster2.save(out_raster2)
print("{} Processed Successfully!".format(out_raster2))
end_time = datetime.now()
print('Prozessdauer: {}'.format(end_time - start_time))
Cheers!
Ingo
P.S. German and efficiency is like oil and water, seriously!