I am trying to write a bit of code that combines a bunch of DEMs that match up perfectly and convert the elevation values of the pixel cells from meters to feet. This is all using Pyscripter and Python 2.7.
I found some code on stack exchange (Add two rasters in python using map algebra) that I tried to translate using my own variables, but cannot get it to work. I can create the mosaic fine, but once it gets to the Map Algebra part it crashes on the expression.
Here is my code. I pretty much took what the post had and translated using my own variables. I also tried putting the expression in eval() and it still crashed. Putting '"' + .... + '"' didn't work either.
#Convert DEM values to feet.
dem_input = str(mos_wrk) + "\\" + str(mos_name)
demft = dem_input*3.28084
demft.save(str(mos_wrk)+"\\"+str(demft)+"_ft")
-
Demft is raster. Instead of str use "demft" in last line.FelixIP– FelixIP2018年09月19日 22:13:01 +00:00Commented Sep 19, 2018 at 22:13
-
2Also use arcpy.Raster(dem_input) in second line.FelixIP– FelixIP2018年09月19日 22:15:51 +00:00Commented Sep 19, 2018 at 22:15
1 Answer 1
Thank you @FelixIP for the help. I managed to correct my mistake. By using the arcpy.Raster() function I, then, had to check out a Spatial Analyst license in the script. Below is my final draft of the code. It is converting Meters to Feet
print "Starting Process 2: Converting Units to Feet..."
#Checkout License
class LicenseError(Exception):
pass
try:
if arcpy.CheckExtension("Spatial") == "Available":
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = wrkspc
dem_input = mos_wrk + "\\" + mos_name
demft = arcpy.Raster(dem_input)*3.28084
demft.save(str(mos_wrk)+"\\"+ str(mos_name) +"_ft")
else:
# raise a custom exception
raise LicenseError
except LicenseError:
print("Spatial Analyst license is unavailable")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
print "Units were converted."
Explore related questions
See similar questions with these tags.