Select to view content in your preferred language

Customised tool in ArcGIS using ArcPy

1572
3
Jump to solution
08-10-2021 02:02 PM
Deepa
by
Occasional Contributor

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)
0 Kudos
2 Solutions

Accepted Solutions
DavinWalker2
Esri Contributor

Looking at your code I think it should be something like this:

 

#Two tools combining here
#Feature to raster conversion - Not sure how to write the output raster from this step. 
arcpy.FeatureToRaster_conversion (in_features, "", save_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(IsNull(save_raster), 1, newValue)

OutRas.save(save_raster)

 

 

View solution in original post

Deepa
by
Occasional Contributor

The script worked when i changed the datatype as work space in tool parameters and saved output in geodatabase. Also made a minor change in script like this.

field = "OBJECTID"
arcpy.FeatureToRaster_conversion (in_features, field, "out1", 25)
OutRas = Con(IsNull("out1"), 1, newValue)
OutRas.save(save_raster)

 

Thanks for your helpful inputs 🙂 

 

View solution in original post

0 Kudos
3 Replies
DavinWalker2
Esri Contributor

Looking at your code I think it should be something like this:

 

#Two tools combining here
#Feature to raster conversion - Not sure how to write the output raster from this step. 
arcpy.FeatureToRaster_conversion (in_features, "", save_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(IsNull(save_raster), 1, newValue)

OutRas.save(save_raster)

 

 

Deepa
by
Occasional Contributor

Hi,

Thanks for the reply 🙂 

I defined OBJECTID as the field and the script is here:

#Two tools combining here
field = "OBJECTID"
arcpy.FeatureToRaster_conversion (in_features, field, save_raster, 25)
OutRas = Con(IsNull(save_raster), 1, newValue)
OutRas.save(save_raster)

However when i try to run the script tool with the changes as you suggested i get a final raster map with wrong  values like in the image attached(The expected result is a raster map with values 0.01 and 1) . Also ArcMap crashes after this.

Deepa_0-1628687885362.png

 

 

0 Kudos
Deepa
by
Occasional Contributor

The script worked when i changed the datatype as work space in tool parameters and saved output in geodatabase. Also made a minor change in script like this.

field = "OBJECTID"
arcpy.FeatureToRaster_conversion (in_features, field, "out1", 25)
OutRas = Con(IsNull("out1"), 1, newValue)
OutRas.save(save_raster)

 

Thanks for your helpful inputs 🙂 

 

0 Kudos