I have a script I have exported from ModelBuilder and am trying to incorporate at the beginning a script written by @BERA. The process is to first convert a polygon shapefile to a raster and do a cost distance analysis, set null values in the resultant accumulated cost distance and then summarise the accumulated cost distance inside a polygon of interest. At the beginning, I need to iterate through the fields in an attribute table to provide the values for the cells in the cost surface raster that goes into the cost distance analysis, and the whole process to repeat for each raster. I am having difficulty with providing inline variables to give names for the input and output names for the cost analysis and the set null values and summarize zones. @BERA provided the means for the raster outputs to be named after the field names – which is perfect, but being completely new to Python I can’t get the two scripts to flow together because of the input and output names. I have provided the two scripts without my attempts!
This is written by @BERA:`
Import arcpy,os
polygons=r'C:\folder\polygons.shp'
sources=r'C:\folder\sources.shp'
fieldlist=['fodyexp1','fodyexp2','fodyexp3','fodyexp4'] #extend/edit to include all your fields
for item in fieldlist: #For each field specified in fieldlist perform all below:
FeatureToRaster_conversion(in_features=polygons, field=item, out_raster=r'in_memory\tempraster')
#Dont know what you want to do next, cost distance?:
outCostDist = CostDistance(sources, r'in_memory\tempraster')
outCostDist.save(os.path.join(r'C:\outputfolder','Raster_'+item)) #The outputs will be named after field, for example Raster_fodyexp1
#and so on...`
This is my cost distance script:
# Import arcpy module
import arcpy
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "C:\\rod.gdb"
arcpy.env.snapRaster = "scores_fody_m"
arcpy.env.extent = "534630.586094391 7813776.96082858 552599.503136767 7825252.24043838"
arcpy.env.workspace = "C:\\rod.gdb"
# Local variables:
habitat = "habitat"
fodyrange_current = "fodyrange_current"
AQ = "AQ"
hab_sn_1 =
"D:\\gis_data\\rodrigues\\Rod_analysis\\iterate_model\\hab_ras\\hab_sn1"
dist_sn_1 =
"D:\\gis_data\\rodrigues\\Rod_analysis\\iterate_model\\dis_raster\\dist_sn_1"
backlink_fmaq = ""
nullcost_sn_1 =
"D:\\gis_data\\rodrigues\\Rod_analysis\\iterate_model\\setnull_cost_r\\nullcost_sn_1"
zone_sn_1 = "D:\\gis_data\\rodrigues\\Rod_analysis\\iterate_model\\zonal_tables\\zone_sn_1"
# Process: Polygon to Raster
arcpy.PolygonToRaster_conversion(habitat, "warb_mean", hab_sn_1, "CELL_CENTER", "NONE", "20")
# Process: Cost Distance
arcpy.gp.CostDistance_sa(fodyrange_current, hab_sn_1, dist_sn_1, "",
backlink_fmaq)
# Process: Set Null
arcpy.gp.SetNull_sa(dist_sn_1, dist_sn_1, nullcost_sn_1, "\"value\" = 0")
# Process: Zonal Statistics as Table
arcpy.gp.ZonalStatisticsAsTable_sa(AQ, "id", nullcost_sn_1, zone_sn_1, "DATA", "ALL")