Run Particle tracking tool for a set of points

2354
4
Jump to solution
12-20-2014 02:04 PM
GabrielBacca-Cortes
New Contributor II

Hi All -

I started using Python for a couple of weeks now, but I am stuck with the subject problem.  I want to run either a CSV or a point FC providing a list of 'SourcePoints' required by the Particle Tracking tool. I've tried to set the code up in both ways, but don't know yet if it is accepted by the tool's parameters because I am getting another error regarding the outputTrack file, which is the TXT output file.  The name of this output file changes with respect to the point currently run through the tool. The error I am getting reads:

Failed to execute. Parameters are not valid.

ERROR 000885: Output particle track file:

C:\GIS\Track1\1_Orig\Track1.gdb\Tracklines\xy_1.txt's does not have a file extension.

Failed to execute (ParticleTrack).

When I print 'outTrackFile', the path and filename look ok, but the extension is not recognized though.

Any help trying to solve this issue is appreciate it !  

thanks

Gabriel

import arcpy, os, csv

from arcpy import env

from arcpy.sa import *

env.workspace = "C:/GIS/Track1/1_Orig/Track1.gdb/"

# Set local variables

outWorkspace = "C:/GIS/Track1/1_Orig/Track1.gdb/Tracklines/"

inDirectionRaster = "ragw_dir2"

inMagnitudeRaster = "ragw_mag2"

stepLength = 10

trackingTime = 10000000

# Check out the ArcGIS Spatial Analyst extension license

arcpy.CheckOutExtension("Spatial")

infile = open(r'C:\2_UWaterloo\10_ArcGIS\GWtoolset\LandBlocks\RA\input\SourcePts10.csv','r')

lines = []

for line in infile:

        lines.append(line.split(','))

infile.close()

#print lines

#field_names = ['point_x','point_y']

#coords = arcpy.da.SearchCursor(lines, field_names)

try:

        for line in lines[1:]:

                x = line[0]

                y = line[1]

                id = line[2]

                sourcePoint = arcpy.Point(x,y)

                #print sourcePoint

                # Output files in TXT and SHP formats for each x,y particle

                #outTrackFile = os.path.join(outWorkspace,"xy_" + str(id) + ".txt")

                outTrackFile = outWorkspace + "xy_" + id + ".txt"

                #print outTrackFile

                #outTrackPolylineFeatures = os.path.join(outWorkspace, "xy_" + str(id) + ".shp")

                outTrackPolylineFeatures = outWorkspace + "xy_" + id + ".shp"

                #print outTrackPolylineFeatures

                # Execute ParticleTrack

                ParticleTrack(inDirectionRaster, inMagnitudeRaster, sourcePoint,

                              outTrackFile, stepLength, trackingTime, outTrackPolylineFeatures)

except:

    print arcpy.GetMessages(2)

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You are trying to create a text file in this location:

"C:/GIS/Track1/1_Orig/Track1.gdb/Tracklines/"

This is a dataset within your file geodatabase and that won't work. The other thing is you trying to create a shapefile inside you file geodatabase, which won't work either. It would be better to define the "outTrackFile" and "outTrackPolylineFeatures" as follows:

outTrackFile = os.path.join(folder, "xy_{0}.txt".format(id))
outTrackPolylineFeatures = os.path.join(ws, ds_name, "xy_{0}".format(id))

This will require defining another variable called folder that points to an existing folder where the text file should be created. I haven't looked closer to your code and obviously haven't tested it, but the code below might work for you:

import arcpy, os
from arcpy import env
from arcpy.sa import *


# Set local variables
folder = r"C:\GIS\Track1\1_Orig"
ws = r"C:\GIS\Track1\1_Orig\Track1.gdb"
ds_name = "Tracklines"
inDirectionRaster = "ragw_dir2"
inMagnitudeRaster = "ragw_mag2"
stepLength = 10
trackingTime = 10000000
csv_file = r"C:\2_UWaterloo\10_ArcGIS\GWtoolset\LandBlocks\RA\input\SourcePts10.csv"
env.workspace = ws

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

i = 0
try:
    with open(csv_file ,'r') as infile:
        for line in infile:
            i += 1
            if i != 1:
                x = line.split(',')[0]
                y = line.split(',')[1]
                id = line.split(',')[2]
                sourcePoint = arcpy.Point(float(x),float(y))

                outTrackFile = os.path.join(folder, "xy_{0}.txt".format(id))
                outTrackPolylineFeatures = os.path.join(ws, ds_name, "xy_{0}".format(id))

                # Execute ParticleTrack
                ParticleTrack(inDirectionRaster, inMagnitudeRaster, sourcePoint,
                              outTrackFile, stepLength, trackingTime, outTrackPolylineFeatures)

    arcpy.CheckInExtension("Spatial")

except:
    print arcpy.GetMessages(2)

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

did you forget the str(id) like you did in the commented out line?

0 Kudos
XanderBakker
Esri Esteemed Contributor

You are trying to create a text file in this location:

"C:/GIS/Track1/1_Orig/Track1.gdb/Tracklines/"

This is a dataset within your file geodatabase and that won't work. The other thing is you trying to create a shapefile inside you file geodatabase, which won't work either. It would be better to define the "outTrackFile" and "outTrackPolylineFeatures" as follows:

outTrackFile = os.path.join(folder, "xy_{0}.txt".format(id))
outTrackPolylineFeatures = os.path.join(ws, ds_name, "xy_{0}".format(id))

This will require defining another variable called folder that points to an existing folder where the text file should be created. I haven't looked closer to your code and obviously haven't tested it, but the code below might work for you:

import arcpy, os
from arcpy import env
from arcpy.sa import *


# Set local variables
folder = r"C:\GIS\Track1\1_Orig"
ws = r"C:\GIS\Track1\1_Orig\Track1.gdb"
ds_name = "Tracklines"
inDirectionRaster = "ragw_dir2"
inMagnitudeRaster = "ragw_mag2"
stepLength = 10
trackingTime = 10000000
csv_file = r"C:\2_UWaterloo\10_ArcGIS\GWtoolset\LandBlocks\RA\input\SourcePts10.csv"
env.workspace = ws

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

i = 0
try:
    with open(csv_file ,'r') as infile:
        for line in infile:
            i += 1
            if i != 1:
                x = line.split(',')[0]
                y = line.split(',')[1]
                id = line.split(',')[2]
                sourcePoint = arcpy.Point(float(x),float(y))

                outTrackFile = os.path.join(folder, "xy_{0}.txt".format(id))
                outTrackPolylineFeatures = os.path.join(ws, ds_name, "xy_{0}".format(id))

                # Execute ParticleTrack
                ParticleTrack(inDirectionRaster, inMagnitudeRaster, sourcePoint,
                              outTrackFile, stepLength, trackingTime, outTrackPolylineFeatures)

    arcpy.CheckInExtension("Spatial")

except:
    print arcpy.GetMessages(2)
GabrielBacca-Cortes
New Contributor II

Thanks Xander Bakker !

I incorporated your suggestions into my code, and decided to send the shapefile outside of the GDB as well, and work out nicely.  I tried your code and works as well... it is more 'elegant'

Thanks again, now I can have a beer !!

Gabriel

0 Kudos
XanderBakker
Esri Esteemed Contributor

Cheers! I'll have on too....

Could you mark the post as answered (there should be a button "Correct Answer" at each post, just click on the one corresponding to the post that answered your question).

0 Kudos