Error in executing tool (Focal Statistics)

735
3
02-24-2018 03:19 AM
NinaKrašovec
New Contributor

Hi, I'm really new at using python and I'm trying to create a script for terrain ruggedness index but I'm using 4 shapefiles. I want to convert them to rasters and then merge them and do the TRI on this one raster. My script keeps failing because of the RuntimeError: Object: Error in executing tool. I would be really grateful if anybody helps me.

I got this:

Traceback (most recent call last):
File "F:\GIS_mag\py_nmg\test.py", line 60, in <module>
arcpy.gp.FocalStatistics_sa(dmr, v3x3max, "Rectangle 3 3 CELL", "MAXIMUM", "DATA")
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

This is my script:

# Import arcpy module
import arcpy

# Script arguments
dmr1 = arcpy.GetParameterAsText(0)
if dmr1 == '#' or not dmr1:
    dmr1 = "dmr1" # provide a default value if unspecified

dmr2 = arcpy.GetParameterAsText(1)
if dmr2 == '#' or not dmr2:
    dmr2 = "dmr2" # provide a default value if unspecified

dmr3 = arcpy.GetParameterAsText(2)
if dmr3 == '#' or not dmr3:
    dmr3 = "dmr3" # provide a default value if unspecified

dmr4 = arcpy.GetParameterAsText(3)
if dmr4 == '#' or not dmr4:
    dmr4 = "dmr4" # provide a default value if unspecified

TRI = arcpy.GetParameterAsText(4)
if TRI == '#' or not TRI:
    TRI = "F:\\GIS_mag\\py_nmg\\tri" # provide a default value if unspecified

# Local variables:
dmr1_ras = "F:\\GIS_mag\\py_nmg\\dmr1_ras"
dmr2_ras = "F:\\GIS_mag\\py_nmg\\dmr2_ras"
dmr3_ras = "F:\\GIS_mag\\py_nmg\\dmr3_ras"
dmr4_ras = "F:\\GIS_mag\\py_nmg\\dmr4_ras"
dmr = dmr1_ras, dmr2_ras, dmr3_ras, dmr4_ras
py_nmg = "F:\\GIS_mag\\py_nmg"
v3x3max = "F:\\GIS_mag\\py_nmg\\3x3max"
v3x3min = "F:\\GIS_mag\\py_nmg\\3x3min"

# Process: Point to Raster
arcpy.PointToRaster_conversion(dmr1, "Field3", dmr1_ras, "MOST_FREQUENT", "NONE", "1")

# Process: Point to Raster (2)
arcpy.PointToRaster_conversion(dmr2, "Field3", dmr2_ras, "MOST_FREQUENT", "NONE", "1")

# Process: Point to Raster (3)
arcpy.PointToRaster_conversion(dmr3, "Field3", dmr3_ras, "MOST_FREQUENT", "NONE", "1")

# Process: Point to Raster (4)
arcpy.PointToRaster_conversion(dmr4, "Field3", dmr4_ras, "MOST_FREQUENT", "NONE", "1")

# Process: Mosaic To New Raster
arcpy.MosaicToNewRaster_management("F:\\GIS_mag\\py_nmg\\dmr1_ras;F:\\GIS_mag\\py_nmg\\dmr2_ras;F:\\GIS_mag\\py_nmg\\dmr3_ras;F:\\GIS_mag\\py_nmg\\dmr4_ras", py_nmg, "dmr", "", "8_BIT_UNSIGNED", "", "1", "LAST", "FIRST")

# Process: Focal Statistics (2)
arcpy.gp.FocalStatistics_sa(dmr, v3x3max, "Rectangle 3 3 CELL", "MAXIMUM", "DATA")

# Process: Focal Statistics
arcpy.gp.FocalStatistics_sa(dmr, v3x3min, "Rectangle 3 3 CELL", "MINIMUM", "DATA")

# Process: Raster Calculator
arcpy.gp.RasterCalculator_sa("SquareRoot(Abs(Square(\"3x3max\") - Square(\"3x3min\")))", TRI)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I would suspect that there is a disconnect between the script and the parameters unless you are running this as a tool within arcmap/PRO.

Throw in some print statements to print out your parameters at each stage before you get to the processing stage

NinaKrašovec
New Contributor

The problem was the 'dmr' in lines 51 and 54, because at local variables it's defined as tuple made of 4 strings but the focal statistics method can only work with one, therefore I just changed the 'dmr' in lines 51 and 54 for "F:\\GIS_mag\\py_nmg\\dmr" and now works perfectly fine.
Thanks for your help!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You should mark this closed then Nina