I have quite a few hundred rasters that I am looking to generate contour lines off. I have successfully run the Contour tool on individual rasters located in the 3D Analyst Tools -> Raster Surface -> toolkit but can anyone advise if there is a script or other tool that I can point to a series of directories to process quite a few hundred sets of contour lines off?
I've seen the script in the help file which is below but is there a fileobject algorithm that can be run over a directory with to extract filenames with many files in it?
Contour (3D Analyst)
# Name: Contour_3d_Ex_02.py# Description: Creates contours or isolines from a raster surface.# Requirements: 3D Analyst Extension# Import system modulesimport arcpyfrom arcpy import env# Set environment settingsenv.workspace = "C:/data"# Set local variablesinRaster = "elevation"contourInterval = 200baseContour = 0outContours = "C:/sapyexamples/output/outcontours02.shp"# Check out the ArcGIS 3D Analyst extension licensearcpy.CheckOutExtension("3D")# Execute Contourarcpy.Contour_3d(inRaster, outContours, contourInterval, baseContour)
Put all rasters in one (workspace/Raster dataset). Then use ModelBuilder and put Iterate Rasters.
Thankyou Abdullah, I'm having trouble creating a raster dataset and using model builder so I'll stick with the coding.
Phillip, it would help if you formatted your code using the syntax highlighter for python ( follow the dots ... in thread tools)
To expand on Dan's suggestion, here's how to format the code in GeoNet (a process which is not obvious). Since spacing is important for Python, getting the code formatted can aid in troubleshooting by GeoNet responders.
I know you stated you tried the syntax highlighter, but just wanted to be sure the complete instructions were available.
Posting code with Syntax Highlighting on GeoNet
Chris Donohue, GISP
Thanks I've used the syntax highlighter for python and keep getting syntax errors on line 4. It's a pretty long directory name and file name but I've used the syntax call from the help guide which is repeated below.
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "input file directory and filename" Contour("elevation", "D:\test\contour.shp", 5, 0)
I've also tried copying the code from the background process syntax and getting the same error.
import arcpy
from arcpy import env
from arcpy.sa import *
Contour "input file directory and filename" "D:\test\contour.shp" 5 0 1
Can anyone please tell me if there is a glaring issue in the code?
Yes... there is a limit on path lengths... and if there are any spaces in them, many Spatial Analyst tools will fail.
Move your stuff to a simple folder like...
c:\test\Data
Thanks Dan, the file names and folder names are huge and renaming 17000 rasters would be a nightmare. Upon a second look I noted the python coding uses forward slashes rather than back slashes for directory paths. I also thought that the "elevation" was a variable call to the contour function but it is actually the name of the raster file you are trying to process. Below is the working code with comments for anyone it can help.
This is basically from the help guide with brief commentary that tripped me up.
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "enter the directory path where your files are"
inRaster = "enter your file name with .asc at the end"
contourInterval = 5
baseContour = 0
outContours = "D:/map/dembula/really long nested folder structure with spaces in the folder name/testcountours.shp"
arcpy.CheckOutExtension("Spatial")
Contour(inRaster, outContours, contourInterval, baseContour)
Not to get too far off subject, but Python will handle forward and back slashes. But forward slashes are easier to implement.
Also, from an ESRI blog:
Notice that in calling the clip tool, we used forward slashes (/). Back slashes () and forward slashes work equally well in ArcGIS on ALL platforms, so you don’t need to worry about switching back and forth when you change to Windows. You can even use back slashes on Linux and Solaris. The problem is that in Python, back slashes are a special character, so to use them, you have to treat them differently than most other characters.
If you stick to forward slashes on all platforms, you improve the look of your code
and reduce the effort it takes to type it.
Source: Tips and tricks – Unix, Python, and Geoprocessing | ArcGIS Blog
Chris Donohue, GISP
And this missive on paths.... /blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python
spaces are the killer when working with the spatial analyst... simply moving folder contents goes a long way.