I am trying to combine two operations (Feature to raster and Conditional operation) using python script in ArcGIS to create a customized tool. I would like this tool to be transferable so my friends can feed in their inputs - polygons and conditions, to get a raster output.
I have a polygon shapefile with me. Firstly, I would like to convert the polygons into a raster. Secondly, the rasterized polygons need to get values like 10% or 20% (user-specified values) the rest (outside polygons) gets a value of 1.
To do this I have attempted with a script, then got stuck. My python knowledge is minimal so please excuse me for silly mistakes 🙂 I guess the error is where I don't know how to define the output from the feature to raster conversion. The script is here:
I am trying to combine two operations (Feature to raster and Conditional operation) using python script in ArcGIS to create a customized tool. I would like this tool to be transferable so my friends can feed in their inputs.
I have a polygon shapefile with me. Firstly, I would like to convert the polygons into a raster. Secondly, the rasterized polygons need to get values like 10% or 20% (user-specified values) the rest (outside polygons) gets a value of 1.
To do this I have attempted with a script, then got stuck. It would be great if you could guide me through it. My python knowledge is minimal so please excuse me for silly mistakes 🙂 I guess the error is where I don't know how to define the output from the feature to raster conversion. The script is here:
import os
import sys
from arcpy import env
from arcpy.sa import * #Import all modules in arcpy.sa path
arcpy.CheckOutExtension("Spatial")
#The environments for the work is set up here
arcpy.env.cellSize = 25
arcpy.env.overwriteOutput = True
in_features = arcpy.GetParameterAsText(0) # In tool,Datatype is feature class
folder = arcpy.GetParameterAsText(1) # In tool, Location of output folder
raster = arcpy.GetParameterAsText(2) #In tool, output raster name is defined as string in the tool
# newValue
newValue = float(arcpy.GetParameterAsText(3)) # use float or int and assigned as strings in the tool
#The formatable folder location and name for output
arcpy.AddMessage("Folder: {}".format(folder))
arcpy.AddMessage("Raster: {}".format(raster))
save_raster = os.path.join(folder, raster)
arcpy.AddMessage("Save Location: {}".format(save_raster))
#Two tools combining here
#Feature to raster conversion - Not sure how to write the output raster from this step.
arcpy.FeatureToRaster_conversion (in_features, "", out_raster, 25)
#In the next line, the condition i would like to apply is if there is value within the raster layer, it gets newValue(user defined), otherwise 1
OutRas = Con(out_raster, "", newValue, 1)
OutRas.save(save_raster)