Select to view content in your preferred language

Issue with Copy Features in Python

2030
13
06-21-2014 04:38 PM
ryank
by
Deactivated User
I'm fairly new to writing scripts in python.  I'm creating one to take a feature class of about 100 points and turn it into individual points then run a cost path analysis on each of them.  The cost rasters and backlink rasters are already created.

The problem with the script is that it will create the individual points, but they won't work with the rest of the script that creates the cost paths while it's running.  If the script stops running and restarts with the section of code commented out to create the points, it will run the cost paths on the points it created the last time it ran.

     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


I'm pretty sure the problem is in Copy Features.  The script will recognize the features exist, but gives this error message:

arcgisscripting.ExecuteError: ERROR 010045: COSTPATH: The number of FROM cells is 0.
ERROR 010067: Error in executing grid expression.


Again, if I run it with the costpath lines commented out, it will run fine.  If I rerun it with the lines making the points commented out, it will run fine and create the costpaths.

The whole thing has to run in one go, so I can't just create 2 separate scripts and run them one at a time unfortunately.

Any suggestions on how to solve this problem?
Tags (2)
0 Kudos
13 Replies
IanMurray
Honored Contributor

Both your original feature layer you create and your feature class have the same name, pointname ( or "route_one_point" + str(i)).

I'm assuming you want to use your feature class you created, so perhaps give your feature class a different name than your feature layer and run again(say pointname + "fc").  It might be trying to run the tool off feature layer you made first, instead of the feature class with only one point.

Also, since you already made a variable for pointname = "route_one_point" + str(i), hardcoding in "route_one_point" + str(i) again and again is a bit wasteful.

XanderBakker
Esri Esteemed Contributor

There is still a potential risk in this code. If you loop through the points to create a where clause with your Object ID, you assume that the values of the ObjectID is a range without gaps in its values. This might be the case, but if you edit the featureclass and delete a feature, it will no longer be the case.

To create a more stable way you could use something like:

fc_pnts = "C:/Users/Ryan/Documents/arcgis_folder/sitetwoworkarea.gdb/route_one_points"

# determine the ObjectID field name

fld_oid = arcpy.Describe(fc_pnts).OIDFieldName

# make a list of all the ObjectID's in the featureclass

lst_oid = [row[0] for row in arcpy.da.SeachCursor(fc_pnts, ("OID@"))]

# loop through oid's and create the where clause

for oid in lst_oid:

    where = "{0} = {1}".format(arcpy.AddFieldDelimiters(fc_pnts, fld_oid), oid)

0 Kudos
ryank
by
Deactivated User

That worked, Ian.  I can't believe it, it seems kinda obvious, but I've been looking at this off and on for 2 months, two people I go to school with have been looking at it, and 4 people on here did.   And like almost every huge problem, it turns out to be something obvious. 

0 Kudos
IanMurray
Honored Contributor

Another reason to use variables more effectively.  If you had used your variable in ever place you spelled out its value, I bet it would have been caught a little sooner, since it would have been more apparent ot the eye that you were using the same string for each.

Glad you got it working properly.

0 Kudos