i = 1 number_of_points = int(arcpy.GetCount_management("route_one_points").getOutput(0)) print number_of_points while i <= number_of_points: print "testing" print i pointname = "route_one_point" + str(i) arcpy.MakeFeatureLayer_management("route_one_points", pointname) arcpy.SelectLayerByAttribute_management(pointname, "NEW_SELECTION", "OBJECTID = %s" % i) arcpy.CopyFeatures_management("route_one_point" + str(i), "route_one_point" + str(i)) print pointname outCostPath = arcpy.sa.CostPath(pointname, "route_one_cost", "route_one_backlink") outCostPath.save("route_one_point" + str(i) + "_path") i += 1
# cost paths tests import sys, string, os, arcgisscripting, arcpy, datetime, time, math arcpy.CheckOutExtension("Spatial") #CostPath (in_destination_data, in_cost_distance_raster, in_cost_backlink_raster, {path_type}, {destination_field}) #route_one arcpy.env.workspace = "C:/Users/Ryan/Documents/arcgis_folder/sitetwoworkarea.gdb" #arcpy.env.workspace = "C:\Users\Ryan\Desktop\GIS\python\project_test\project_test_gdb.gdb" #script will overwrite existing data arcpy.env.overwriteOutput = 1 begin = datetime.datetime.now () start = time.clock () loopcounter = 0 raster_list = arcpy.ListRasters() print raster_list #def create_cost_paths(): #print "I'm making the route cost." #i = 1 #number_of_points = int(arcpy.GetCount_management("route_one_points").getOutput(0)) #print number_of_points #loopcounter = 0 #while i <= number_of_points: #print "testing" #print i #pointname = "route_one_point" + str(i) #arcpy.MakeFeatureLayer_management("route_one_points", pointname) #arcpy.SelectLayerByAttribute_management(pointname, "NEW_SELECTION", "OBJECTID = %s" % i) #arcpy.CopyFeatures_management("route_one_point" + str(i), "route_one_point" + str(i)) #print pointname ##outCostPath = arcpy.sa.CostPath(pointname, "route_one_cost", "route_one_backlink") ##outCostPath.save("route_one_point" + str(i) + "_path") #i += 1 def create_cost_paths(): print "I'm making the route cost." i = 1 number_of_points = int(arcpy.GetCount_management("route_one_points").getOutput(0)) print number_of_points loopcounter = 0 while i <= number_of_points: print "testing" print i pointname = "route_one_point" + str(i) #arcpy.MakeFeatureLayer_management("route_one_points", pointname) #arcpy.SelectLayerByAttribute_management(pointname, "NEW_SELECTION", "OBJECTID = %s" % i) #arcpy.CopyFeatures_management("route_one_point" + str(i), "route_one_point" + str(i)) print pointname outCostPath = arcpy.sa.CostPath(pointname, "route_one_cost", "route_one_backlink") outCostPath.save("route_one_point" + str(i) + "_path") i += 1 i = 1 def test(): outCostPath = arcpy.sa.CostPath("route_one_point1", "route_one_cost", "route_one_backlink") outCostPath.save("route_one_point1_path") create_cost_paths() print "I started at " + str(begin.hour) + ":" + str(begin.minute) print "I took " + str(int((time.clock() - start) / 3600)) + " hours and " + str((int(time.clock() - start) / 60 ) % 60) + " minutes to finish"
Sorry for the long response. A lot came up with the reserves this last month.
I set up a count so I can set up a try/except later. I use it in a similar loop and it works out pretty well. If I don't use a separate counter, it can't keep track of both the iteration and which point is being run. At least I don't know enough about running cursors to do so.
If I don't include the MakeFeatureLayer it throws this error
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
The help for the Select Layer By Attribute scripting gives this as the example for performing this process
# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
# Import system modules
import arcpy
# Set the workspace
env.workspace = "c:/data/mexico.gdb"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr")
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")
# Within selected features, further select only those cities which have a population > 10,000
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")
I'm mostly using their example as a startpoint for this. I'm perfectly willing to try something else, but again keep in mind I'm pretty new to this. I'm not sure if that's what you meant when you asked why I go through that. And I'm not sure how to pass the where clause straight to the costpath, but I also need to keep each point individually, because each point is going to be used later.
The Help for Cost Path says it takes an input feature layer, not a feature class.
The thing is, it will accept the output if I run the cost path in another script. I'm not sure if that matters.
I don't see a post by Dallas. Mark Denil's post is the last I can see. Are there other posts?
Hi Ryan,
Just looking over the Bulk of the scipt and have questions:
arcpy.env.workspace = "SET TO A GDB"
You have your environment set to a GDB.
raster_list = arcpy.ListRasters()
Listing Rasters from the environment.
I don't think you going to get a list of Rasters, they are stored in Raster Datasets within a GDB.
When i Print the list it is NONE.
The environment should be set to a folder where the Rasters are stored.
def create_cost_paths(**ARGUMENTS**):
Your create_cost_paths() function has no Arguments()
So when you set variables referenced to "route_one_cost" (RASTER) it doesn't reference anything. It doesn't find anything.
This is because you carry nothing into in the function from the environment or list from the environment.
You can fix this be either taking your arcpy.env.workspace and moving it into the function, or you can add the LIST from the environment as argument for the function that uses it.
I don't think you going to get a list of Rasters, they are stored in Raster Datasets within a GDB.
When i Print the list it is NONE.
The environment should be set to a folder where the Rasters are stored.
Again, I'm new to python, but wouldn't it return none for you no matter what since you don't have the rasters? When I print the list it does return the rasters, even when I place print code inside the function.
You are correct, but I have raster datasets inside another GDB that i am using to test with.
What are you setting for your env?
I feel like your env, is not set to a GDB.