Particle Track arcpy.point failing in a loop

561
4
02-18-2019 04:19 AM
Kris01
by
New Contributor

I'm using the particle track in python and limiting it to a specific trackingtime. For each tool run, I want the rasters input (magnitude and direction) to change to the next rasters in the lists, and the starting point of the tracking (x,y) to be derived from the last polyline point of the previous run. Attached is what I have so far but I get Error999999. It works when arcpy.point is not in a loop but fails within it. 

Any help would be very appreciated. 

 

for idx in range(0, len(mag)):
    print "working on index "+str(idx)
    inMagnitudeRaster= mag [idx]
    print inMagnitudeRaster
    inDirectionRaster= direction [idx]
    print inDirectionRaster
    basename= inDirectionRaster.split(".tif")[0]
    outTrackPolylineFeatures = os.path.join (OutputFolder, basename + '.shp')
    outTrackFile= os.path.join(OutputFolder, basename +'.txt')
    ParticleTrack (Raster(inDirectionRaster), os.path.join(r'C:/Time_slices/January_newrun/Magnitude2/Rectified/Stripped',inMagnitudeRaster), sourcePoint,outTrackFile, "#", trackingTime,outTrackPolylineFeatures)
    arcpy.AddField_management (outTrackPolylineFeatures, "LastX", "DOUBLE")
    arcpy.AddField_management (outTrackPolylineFeatures, "LastY", "DOUBLE")
    arcpy.CalculateField_management(outTrackPolylineFeatures, "LastX", "(!SHAPE.lastpoint.X!)", "PYTHON_9.3")
    arcpy.CalculateField_management(outTrackPolylineFeatures, "LastY", "(!SHAPE.lastpoint.Y!)", "PYTHON_9.3")
    endx, endy = [i for i in arcpy.da.SearchCursor(outTrackPolylineFeatures,['LastX','LastY'])][-1]
    sourcePoint= arcpy.Point(float(endx),float(endy),None,None)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

have you tried throwing in a print statement in between lines 15 and 16?

I am wondering if endx, endy are scalars and not something else

0 Kudos
Kris01
by
New Contributor

I did add a print statement and the correct x, y coordinates are printed. When I print the type, it's a float.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It helps to provide the error and traceback.  I doubt the issue is with your ArcPy Point line.  Looking at this line:

endx, endy = [i for i in arcpy.da.SearchCursor(outTrackPolylineFeatures,['LastX','LastY'])][-1]

What exactly are you trying to achieve, are you trying to grab the last record of the search cursor?  If so, a couple of thoughts, if your cursor only has 1 record you will run into a TypeError with your tuple unpacking.  With multiple records but no SQL ORDER BY clause, you can't guarantee the last record will be the "last." 

0 Kudos
Kris01
by
New Contributor

Thanks Joshua. 

That might have been the problem. I edited the loop as I realised I only need one time step in the shapefile. So all but the first row were deleted and now the code works. 

for idx in range(0, len(mag)):
    print "working on index "+str(idx)
    inMagnitudeRaster= mag [idx]
    print inMagnitudeRaster
    inDirectionRaster= direction [idx]
    print inDirectionRaster
    basename= inDirectionRaster.split(".tif")[0]
    outTrackPolylineFeatures = os.path.join (OutputFolder, basename + '.shp')
    outTrackFile= os.path.join(OutputFolder, basename +'.txt')
    ParticleTrack (Raster(inDirectionRaster), os.path.join(r'C:/Leverhulme/Time_slices/January_newrun/Magnitude2/Rectified/Stripped',inMagnitudeRaster), sourcePoint,outTrackFile, stepLength, trackingTime,outTrackPolylineFeatures)
    with arcpy.da.UpdateCursor(outTrackPolylineFeatures, "FID") as cursor:
        for row in cursor:
            if row[0] > 0:
                cursor.deleteRow()    
    arcpy.AddField_management (outTrackPolylineFeatures, "LastX", "DOUBLE",20 )
    arcpy.AddField_management (outTrackPolylineFeatures, "LastY", "DOUBLE", 20)
    arcpy.CalculateField_management(outTrackPolylineFeatures, "LastX", "(!SHAPE.lastpoint.X!)", "PYTHON_9.3")
    arcpy.CalculateField_management(outTrackPolylineFeatures, "LastY", "(!SHAPE.lastpoint.Y!)", "PYTHON_9.3")
    endx, endy = [i for i in arcpy.da.SearchCursor(outTrackPolylineFeatures,['LastX','LastY'])][-1]
    sourcePoint= arcpy.Point(float(endx),float(endy),None,None)
0 Kudos