Select to view content in your preferred language

Cant get ArcGIS 10.3.1 “topo to raster” to work in python script

3115
10
08-15-2016 10:18 AM
GeoffreyRawling
Deactivated User

I copied the sample python script from this webpage and modified for my own data and paths.

Topo to Raster—Help | ArcGIS for Desktop 

# Name: TopoToRaster_Ex_02.py
# Description: Interpolates a hydrologically correct surface 
#    from point, line, and polygon data.
# Requirements: Spatial Analyst Extension

# testing topo to raster gcr

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "D:/clovis_portales/cp_spatial/hydrology/water_level_analysis_v7_py"

# Set local variables
##inPointElevations = TopoPointElevation(['wells_in.shp', 'usgs_wl'])

inContours = TopoContour(['rwl_krg_cntr_smth.shp', 'CONTOUR'])

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute TopoToRaster
outTTR = TopoToRaster(inContours)

### Save the output 
##outTTR.save("D:/clovis_portales/cp_spatial/hydrology/water_level_analysis_v7_py/ttrout03")

I keep getting the following errors:

Traceback (most recent call last):
  File "D:\clovis_portales\cp_spatial\hydrology\water_level_analysis_v7_py\ord_krige_2010_2015_wls_v2_for_question_post.py", line 25, in <module>
    outTTR = TopoToRaster(inContours)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 2777, in TopoToRaster
    out_contour_error_feature)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 2753, in Wrapper
    out_contour_error_feature)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing\_base.py", line 504, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

It doesnt work with point or contour data. Topo to raster works fine with same data and paths when using the GUI in arc toolbox

thanks in advance for any help

Geoff

Tags (2)
0 Kudos
10 Replies
XanderBakker
Esri Esteemed Contributor

Apart from the tip to use Code formatting to make your code more readable (as Joshua suggested), there are some things I see to start with:

  • You use Spatial Analyst functionality before checking out the extension. If this script is executed as standalone script, it will yield an error (although different from the one you have)
  • Your workspace is pretty long and has a lot of underscores. Some functionality of Spatial Analyst will not "like" that. You could try it with a shorter path
  • Most importantly, first try to do this manually (using the tools in ArcMap). The results window will provide access to Python Snippets that you may copy to examine the way the tools should be called to make this work.

See: Using the Results window—Help | ArcGIS for Desktop 

Look at the option "Copy As Python Snippet"

XanderBakker
Esri Esteemed Contributor

And since this may well be data related I would be could to provide some data you are using to see if we can reproduce the error.

0 Kudos
XanderBakker
Esri Esteemed Contributor

When you copy the Python Snippet you will get something like this:

arcpy.gp.TopoToRaster_sa("contours_v2 Elevation Contour", "C:/Users/Xander/Documents/ArcGIS/Default.gdb/DEM10m", "10", "824700 1173050 826900 1175000", "20", "", "", "ENFORCE", "CONTOUR", "20", "", "1", "0", "2,5", "100", "", "", "", "", "", "", "", "")

The first parameter is a string and consists of 3 items:

  • contours_v2 (the featureclass with the contourlines)
  • Elevation (the field with the height values
  • Contour (the way you want to use the lines to create the topo raster)

The second parameter is the output raster and the other parameters are optional.

 

In this case the tool was executed from ArcMap and the featureclass is present in my TOC. If you want to execute the tool from a standalone script, you will either need to have the featureclass available in a previously defined workspace or use a complete path to access the data.

Also note the for some strange reason the python snippet does not use arcpy.sa.TopoToRaster, but arcpy.gp.TopoToRaster_sa. Since the Interpolation tools are also available in 3D Analyst you also have "arcpy.TopoToRaster_3d" and "arcpy.ddd.TopoToRaster" which should all do the same thing.

GeoffreyRawling
Deactivated User

Thank you for replies. This bit of code modified from the help page on the 3d analyst works. I had tried all sorts of versions using the spatial analysts topo to raster tool, including exporting the code snippet from arctoolbox and they did not work, kept getting the same error. Gui interface in arc toolbox always worked fine.

Geoff

# V3  - modified by gcr

# Description: trying to recreate workflow of v7 analysis w py scripts
# ---------------------------------------------------------------------------

# import necessary modules etc.
import arceditor
import arcpy
from arcpy import env
from arcpy.sa import *
from arcpy.cartography import *


# set workspace
env.workspace = "D:\\clovis_portales\\cp_spatial\\hydrology\\water_level_analysis_v7_py"

# check out license
arcpy.CheckOutExtension("Spatial")
arcpy.CheckOutExtension("3d")

# set to overwrite outputs
arcpy.env.overwriteOutput = True

# Process: Topo to Raster using original data points and the smoothed kriged contours, 3d version, works

inPointElevations = "usgs_CR_wells_w_good_2010_2015_wls_matching_T_08_wells_33_rise_removed.shp usgs_wl Pointelevation"
inContours = "rwl_krg_cntr_smth.shp CONTOUR Contour"
inFeatures = (inPointElevations + ";" + inContours)
outRaster =  "wl_surf_smth_kgr_cntrs_and_points.tif"

# Execute TopoToRaster
arcpy.TopoToRaster_3d(inFeatures, outRaster)
0 Kudos
DanPatterson_Retired
MVP Emeritus

I formatted the original code.

The last line, where you saved the raster, is commented out.... if you uncomment it, does it work?  In a standalone script you need to save it... to a much shorter path, it is almost too long

GeoffreyRawling
Deactivated User

I uncommented the saving of the raster and moved the "check out extension" line up. Still does not work, same errors. Paths can be 128 chars for a grid but longer for other file types correct?

0 Kudos
DanPatterson_Retired
MVP Emeritus

can't see anything obvious so it is time for print statements particularly the output filename... I know, you set the env, but stranger things have happened... try c:/temp/x.tif

The print statements will narrow down where it failed since your inputs 'look' good, but not confirmed yet.

0 Kudos