import arcpy arcpy.env.workspace = "D:/GIS_data/Topo_Water" arcpy.CheckOutExtension("Spatial") cellSize = 30 #adjust this value to your data #assume there's 519 sections in workspace(you can test script with smaller value, eg. 3) for i in range(1, 520): myTopoContour = arcpy.sa.TopoContour([['topo_ras_input_topoSection'+str(i) + '.shp','contour']]) myTopoLake = arcpy.sa.TopoLake(['topo_ras_input_watersecSection'+str(i) + '.shp']) outTopoToRaster = arcpy.sa.TopoToRaster([myTopoContour, myTopoLake], cellSize) outTopoToRaster.save("D:/GIS_data/Raster/twraster" + str(i).zfill(3))
# --------------------------------------------------------------------------- # Listdata.py # # Description: # Lists data sets in a workspace and then runs Topo to Raster on each feature # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Check out any necessary licenses arcpy.CheckOutExtension ("Spatial") # Set the workspace for the ListFeatureClass function arcpy.env.workspace = "D:/GIS_data/Topo_Water" # Use the ListFeatureClasses function to return a list of # all shapefiles and feature classes. fcList = arcpy.ListFeatureClasses() # Process each file with loop in topo to raster for fc in fcList: fcBase = fc.rstrip(".shp") #strips off the '.shp.' fcNUM = fcBase .lstrip("topo_ras_input_topoSection") # should leave just the digits rasIN = 'topo_ras_input_watersecSection%s' % (str(fcNUM)) # output for this iteration myTopoContour = TopoContour([[fc,'contour']]) myTopoLake = TopoLake([rasIN]) # does not check if this exists.... # Execute TopoToRaster outTopoToRaster = TopoToRaster([myTopoContour, myTopoLake]) # Save the output outTopoToRaster.save("D:/GIS_data/Raster/twraster%s" % (fcNUM)) # saves each one
Assuming that all your shapefiles are in one catalog and the follow naming convention "topo_ras_input_topoSection1","topo_ras_input_topoSection2", ... , "topo_ras_input_topoSection519", the following script should work:import arcpy arcpy.env.workspace = "D:/GIS_data/Topo_Water" arcpy.CheckOutExtension("Spatial") cellSize = 30 #adjust this value to your data #assume there's 519 sections in workspace(you can test script with smaller value, eg. 3) for i in range(1, 520): myTopoContour = arcpy.sa.TopoContour([['topo_ras_input_topoSection'+str(i) + '.shp','contour']]) myTopoLake = arcpy.sa.TopoLake(['topo_ras_input_watersecSection'+str(i) + '.shp']) outTopoToRaster = arcpy.sa.TopoToRaster([myTopoContour, myTopoLake], cellSize) outTopoToRaster.save("D:/GIS_data/Raster/twraster" + str(i).zfill(3))
The output raster name follow the rule: twraster001, twraster002, ...
You can try running the script for testing for few shapefiles - adjust number in range(1, x) part.
You may need to test to find optimal cell size of output raster.
Can you please explain why there was no need to list features and loop? I thought that was the proper way to approach the problem? Is your approach something that can be used in other cases where I have many files in the same directory?
Try being explict with setting the names.
Even if inputing the bald wildcards worked, you would get one output from all 519 input pairs, not 519 outputs...
This is not tested, but may point you in the right direction.
It assumes there are fc and raster pairs numbered the same.
It itterates through the feature classes, assuming there is a matching raster.
It uses the same 'contour' fc input for each loop.
It outputs a twraster<number> where <number> matches the input number.# --------------------------------------------------------------------------- # Listdata.py # # Description: # Lists data sets in a workspace and then runs Topo to Raster on each feature # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Check out any necessary licenses arcpy.CheckOutExtension ("Spatial") # Set the workspace for the ListFeatureClass function arcpy.env.workspace = "D:/GIS_data/Topo_Water" # Use the ListFeatureClasses function to return a list of # all shapefiles and feature classes. fcList = arcpy.ListFeatureClasses() # Process each file with loop in topo to raster for fc in fcList: fcBase = fc.rstrip(".shp") #strips off the '.shp.' fcNUM = fcBase .lstrip("topo_ras_input_topoSection") # should leave just the digits rasIN = 'topo_ras_input_watersecSection%s' % (str(fcNUM)) # output for this iteration myTopoContour = TopoContour([[fc,'contour']]) myTopoLake = TopoLake([rasIN]) # does not check if this exists.... # Execute TopoToRaster outTopoToRaster = TopoToRaster([myTopoContour, myTopoLake]) # Save the output outTopoToRaster.save("D:/GIS_data/Raster/twraster%s" % (fcNUM)) # saves each one
... and I see that as I was typing, someone else answered too....
myTopoLake= 'topo_ras_input_watersecSection%s' % (str(fcNUM)) waterBool = arcpy.Exists(myTopoLake)
if waterBool: arcpy.TopoToRaster_sa([myTopoContour, myTopoLake], 2) else: arcpy.TopoToRaster_sa([myTopoContour], 2)
It sounds like you have two 'optional if present' inputs to the process.
You want to run the process on ALL the topos present, although some of the tiles may be missing (so there would be nothing to run)
If there is a water layer present, you would want to include it.
I recall that the topos are features and the waters are rasters
The code I suggested already cycles through the vector features, so it naturally weeds out missing tiles.
I would test for the existance of the water raster immidietly after the name is setmyTopoLake= 'topo_ras_input_watersecSection%s' % (str(fcNUM)) waterBool = arcpy.Exists(myTopoLake)
Then use the existance boolean to decide which version of the command you need, with or without water.if waterBool: arcpy.TopoToRaster_sa([myTopoContour, myTopoLake], 2) else: arcpy.TopoToRaster_sa([myTopoContour], 2)
if waterBool: print "%s %s" % (myTopoContour, myTopoLake) else: print "%s" % (myTopoContour)
The only issue is that you are looping through a list of all the feature classes.
If that list includes both the topo and the water, then things might get confused.
Use a wildcard on ListFeatureClasses so you only get the contours in the list.
fcList = arcpy.ListFeatureClasses("topo_ras_input_topoSection*")
Otherwise, there shouldn't be an issue with finding the right pairs of inputs.
I would try a version of the process that does not do the actual TopoToRaster,
but just prints out the names of the inputs instead.
You can see if you are getting the right files for each itteration.
something like:
if waterBool: print "%s %s" % (myTopoContour, myTopoLake) else: print "%s" % (myTopoContour)