0

I'm using the particle track tool 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. This is what I have so far but I get an error. It works fine if I remove the section: for row in cursor: but then it won't loop.

import os
import arcpy
from arcpy import*
from arcpy.sa import*
arcpy.CheckOutExtension ('Spatial')
arcpy.env.overwriteOutput= True

arcpy.env.workspace= r'C:/Time_slices/January_newrun/Magnitude2/Rectified/Stripped'
mag= arcpy.ListRasters('*.tif')
arcpy.env.workspace= r'C:/Time_slices/January_newrun/Direction360'
direction= arcpy.ListRasters('*.tif')

sourcePoint = arcpy.Point(418350.56, 84472.21)
outTrackFile = "C:/Time_slices/January_newrun/trackparticle/trackfileMod10.txt"
trackingTime = 86400
OutputFolder = r'C:/Time_slices/January_newrun/trackparticle'

for idx in range(0, len(mag)):
    inMagnitudeRaster= mag [idx]
    inDirectionRaster= direction [idx]
    basename= inDirectionRaster.split(".tif")[0]
    outTrackPolylineFeatures = os.path.join (OutputFolder, basename + '.shp')
    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")
    cursor= arcpy.SearchCursor(outTrackPolylineFeatures)
    for row in cursor:
           endx= row.getValue("LastX")
           endy= row.getValue("LastY")
    sourcePoint= arcpy.Point(endx, endy) 
    inMagnitudeRaster= mag[idx+1]
    inDirectionRaster= direction[idx+1]
    basename= inDirectionRaster.split(".tif")[0]
    outTrackPolylineFeatures = os.path.join (OutputFolder, basename + '.shp')
    ParticleTrack (Raster(inDirectionRaster), os.path.join(r'C:/Time_slices/January_newrun/Magnitude2/Rectified/Stripped',inMagnitudeRaster), sourcePoint,outTrackFile, "#", trackingTime,outTrackPolylineFeatures)

The error message I get is:

Traceback (most recent call last):
  File "C:/particle_cursor_15_02.py", line 40, in <module>
    ParticleTrack (Raster(inDirectionRaster), os.path.join(r'C:/Time_slices/January_newrun/Magnitude2/Rectified/Stripped',inMagnitudeRaster), sourcePoint,outTrackFile, "#", trackingTime,outTrackPolylineFeatures)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 2149, in ParticleTrack
    out_track_polyline_features)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 2140, in Wrapper
    out_track_polyline_features)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (ParticleTrack).
  • It would be helpful to add comments to your code. – Aaron Mar 18 at 1:43
0

first you should change to the data access module cursor and look if it work with them, also they are faster. (For more information on them: https://pro.arcgis.com/de/pro-app/arcpy/data-access/what-is-the-data-access-module-.htm)

cursor= arcpy.da.SearchCursor(outTrackPolylineFeatures, ["LastX","LastY"])
for row in cursor:
       endx= row[0]
       endy= row[1]
       sourcePoint= arcpy.Point(endx, endy) 
       inMagnitudeRaster= mag[idx+1]
       inDirectionRaster= direction[idx+1]
       basename= inDirectionRaster.split(".tif")[0]
       outTrackPolylineFeatures = os.path.join (OutputFolder, basename + '.shp')
       ParticleTrack (Raster(inDirectionRaster), os.path.join(r'C:/Time_slices/January_newrun/Magnitude2/Rectified/Stripped',inMagnitudeRaster), sourcePoint,outTrackFile, "#", trackingTime,outTrackPolylineFeatures)

Hope this helps.

  • That helped, thanks. I noticed though that the problem seems to be with the sourcePoint. The endx and endy are being retrieved. Not sure if arcpy.point does not take a variable as input. – Krys01 Feb 15 at 15:55
0

I made it work by specifying the StepLength of the Particle track and editing the code

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, 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.