Select to view content in your preferred language

Raster Calculator Expression used during iteration loop

1167
2
Jump to solution
04-22-2014 01:53 PM
NoahHuntington
Deactivated User
I was wondering if someone can help with the following Raster Calculator Expression.  I am trying to calculate the output least cost path raster to "null" where the end points overlap full least cost path raster.  Additionally I am trying to use in -line variable substitution in the expression as the following snippet iterates 3 times.

I hope I have made sense.  I appreciate any suggestions?

Entire function:
# Process: Cost Distance           arcpy.gp.CostDistance_sa(destValue, weighted_Raster, "distance_Raster", "", 'backlink_Raster')      # Process: Cost Path     Least_Cost_Path = arcpy.gp.CostPath_sa(originValue,"distance_Raster", "backlink_Raster", os.path.join(savepath + "\\Results.gdb" + "\\lcp_" + str(count)), "EACH_CELL", "OBJECTID")      global count      while count < 3:          # Local variables:         End_Pts = 'End_Pts'         Output_polyline_features = 'Output_polyline_features'         End_Pts_Raster = 'End_Pts_Raster'         Least_Cost_Route = 'Least_Cost_Route'         Reclass_rast1 = 'Reclass_rast1'          # Process: Raster to Polyline         arcpy.RasterToPolyline_conversion(Least_Cost_Path, Output_polyline_features, "ZERO", "0", "SIMPLIFY", "")          # Process: Feature Vertices To Points         arcpy.FeatureVerticesToPoints_management(Output_polyline_features, End_Pts, "BOTH_ENDS")          # Process: Point to Raster         arcpy.PointToRaster_conversion(End_Pts, "", End_Pts_Raster, "MOST_FREQUENT", "NONE", Least_Cost_Path)          # Process: Raster Calculator         arcpy.gp.RasterCalculator_sa("SetNull(~IsNull("%End_Pts_Raster%"), \"%Least_Cost_Path%\")", Least_Cost_Route)

Set null statement:
       arcpy.gp.RasterCalculator_sa("SetNull(~IsNull("%End_Pts_Raster%"), \"%Least_Cost_Path%\")", Least_Cost_Route)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
Are you using ModelBuilder and just exporting the script for us to see?

If you're working Python, arcpy map algebra is a lot easier to work with.

For example instead of
arcpy.gp.RasterCalculator_sa("SetNull(~IsNull("%End_Pts_Raster%"), \"%Least_Cost_Path%\")", Least_Cost_Route)


the arcpy map algebra equivalent is something like this. Note how easy it is to save your rasters using the count variable to uniquely named grids with the .save() method.

from arcpy.sa import * while ....     ...     eptras = Raster(End_Pts_Raster)     LCostPth = Raster(Least_Cost_Path)     LCR = SetNull( ~ IsNull(eptras), LCostPth)     LCR.save("lcr{0}".format(count)     count -= 1


Desktop 10.1 Help: What is Map Algebra

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Alum
Are you using ModelBuilder and just exporting the script for us to see?

If you're working Python, arcpy map algebra is a lot easier to work with.

For example instead of
arcpy.gp.RasterCalculator_sa("SetNull(~IsNull("%End_Pts_Raster%"), \"%Least_Cost_Path%\")", Least_Cost_Route)


the arcpy map algebra equivalent is something like this. Note how easy it is to save your rasters using the count variable to uniquely named grids with the .save() method.

from arcpy.sa import * while ....     ...     eptras = Raster(End_Pts_Raster)     LCostPth = Raster(Least_Cost_Path)     LCR = SetNull( ~ IsNull(eptras), LCostPth)     LCR.save("lcr{0}".format(count)     count -= 1


Desktop 10.1 Help: What is Map Algebra
0 Kudos
NoahHuntington
Deactivated User
Map algebra tools were used in my solution.  Thanks Curtis! Here is what worked for me...

        # Returns a value of 1 where End_Raster is NoData and 0 End_Raster points.
        isNull1 = IsNull(End_Raster)
        isNull1.save("isNull1_{0}".format(count))

        #Returns LCP minus the end points
        #
        setNull1 = SetNull(isNull1,get_LCP(count),"Value = 0")
        setNull1.save("setNull1_{0}".format(count))
0 Kudos