What I meant by using ArcMap tools, was not actually using the raster calculator, but the spatial analys "Con" tool.What I would do is open ArcMap, open the catalog window, right-click on Toolbox (or My Toolboxes) folder and choose "New" Model.
env.workspace = FD_addr # DEM/DSM raster locations raster_list = arcpy.ListRasters("*", "ALL") # adds DEM/DSM raster
arcpy.gp.RasterCalculator_sa("""elevation = Con("Surface_RAS"-"Ground_RAS">=0,"Surface_RAS"-"Ground_RAS",0)""","C:/Summer_13/Geo.gdb/elevation")
DEM = arcpy.Raster(raster_list[0]) DSM = arcpy.Raster(raster_list[1]) expression = """elevation = Con({}-{}>=0,{}-{},0)""".format(DSM,DEM,DSM,DEM) raster_out = os.path.join(FD_addr,"elevation") arcpy.gp.RasterCalculator_sa(expression,raster_out)
elevation = Con("Surface_RAS"-"Ground_RAS">=0,"Surface_RAS"-"Ground_RAS",0)
Not really sure how to see Arcmessages as I do everything in the command line, and not really familiar with the Arc command window/python tools.How are you running this? In the command window, or making a script tool or what?If running in console window, after it runs, if you type: print elevation print raster0 print raster1 do you get any results?also, can you create the elevation raster that you are after within arcmap using the tools? I am wondering if it is creating an empty output, so is not saving it.I would make sure that it is possible to do what you are trying using arcmap tools/model and get the desired output. then work on the python code (export from the model builder and modify).Might also look into the DEM format. I have tried to use this in the past for certain tasks, and found that it does not support state plane coordinates. Perhaps one of the input rasters is not supported or valid for this operation? If you can do the raster calculations in ArcMap, then that would rule this out as well.R_
print elevation print raster0 print raster1
import arcpy import os from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("Spatial") # for arcpy.sa env.workspace = FD_addr # DEM/DSM raster locations raster_list = arcpy.ListRasters("*", "ALL") # adds DEM/DSM raster size = len(raster_list) #### Make some exception here if != 2 raster0 = arcpy.Raster(raster_list[0]) raster1 = arcpy.Raster(raster_list[1]) elevation = arcpy.sa.Con(raster1-raster0 >= 0, raster1-raster0,0) elevation.save("elevation") # saved in FD_addr
Interesting. Are you sure you hard-coded it to an "existing" FGDB? If so, what do you mean it didn't work? Didn't create the raster with your hard-coded name, created a blank raster? Error messages? What does it report if make the modifications in red? import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) print "temporary: ",elevation.isTemporary elevation.save(FD_addr) print "temporary: ",elevation.isTemporary R_ Also, it looks like elevation.save(FD_addr) is telling it to save with the filename of the workspace. Workspace is normally a folder or FGDB, not a file. It's my understanding that elevation.save() would save it in the current workspace as raster dataset "elevation". Now that I think about it, before trying the above code, what happens if you try this (of course, make sure the FD_addr is set to a FGDB that the current user has write access to): import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) elevation.save()
import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) print "temporary: ",elevation.isTemporary elevation.save(FD_addr) print "temporary: ",elevation.isTemporary
import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) elevation.save()
Still didn't work 😕 I'm setting the workspace here: env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") (not sure how you make it so that code appears in gray?) I feel that has to be working because the rasters being inserted into list are the rasters in that location (I double checked.) FD_addr is set way in the beginning of the code.
(not sure how you make it so that code appears in gray?)
I don't see in the code anywhere that you are setting the variable 'FD_addr' (maybe it is above the imports somewhere?) to actually set the env.workspace.Could try somehting like: elevation.save("C:/output/file_gdb.gdb/elevation") # or whatever the path/filename you want for your out raster If this works, at least then you know it's an issue with the workspace/path being set properly.R_
elevation.save("C:/output/file_gdb.gdb/elevation") # or whatever the path/filename you want for your out raster
Yep - on the last line try:elevation.save("my_output")The raster "my_output" should then be saved to the 'FD_addr' workspace.Question though: Does 'FD_addr' point to a feature dataset within a geodatabase? Last I checked, I thought that you can't write rasters to a feature dataset (has to be directly to the GDB).
elevation.save("my_output")
The inputs need to be "raster objects" to work in teh new map algebra. Try this:import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) elevation.save(FD_addr)
import arcpy, os arcpy.CheckOutExtension('Spatial') arcpy.AddMessage("Creating Elevation Raster") arcpy.env.workspace = FD_addr list = arcpy.ListRasters("*", "ALL") raster0 = arcpy.Raster(list[0]) raster1 = arcpy.Raster(list[1]) elevation = arcpy.sa.Con(raster1 - raster0 >= 0, raster1 - rasters0, 0) elevation.save(FD_addr)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.