Hello
I am new to Python and I am trying to use the particle tracking with a CSV file of coordinates. So I want to run the particle tracking tool for several points listed in the file. I found an old post where someone made it work (Solved: Run Particle tracking tool for a set of points - Esri Community), and I tried to adapt the code to my project, but I keep running into one of these errors:
Parsing error SyntaxError: invalid syntax
Parsing error IndentationError: unexpected indent
I am not sure what the issue is, I tried to go over it several times, but I am not very experienced with python.
Any help is greatly appreciated!
-Louise
import arcpy
import os
from arcpy import env
from arcpy.sa import *
#set local variables
folder = r"C:\GIS\P8\Pythontest"
ws = r"C:\GIS\P8\Pythontest\python.gdb"
ds_name = "Tracklines"
inDirectionRaster = "curdir"
inMagnitudeRaster = "curmag"
stepLength = 10
trackingTime = 10000000
csv_file = r"C:\GIS\P8\Pythontest\pol_points_test.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(',')[1]
y = line.split(',')[2]
id = line.split(',')[0]
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)
Code formatting ... the Community Version - Esri Community
It is hard to read your code to check for improper indentation.
Any good python IDE should point out indentation and other errors, so check your .... try .... except block first and format your code for posting here so that there are line numbers to refer to
Sorry, I just saw the link you added with the code formatting, I have tried to show it here:
>>> import arcpy
>>> import os
>>> from arcpy import env
>>> from arcpy.sa import *
>>> #set local variables
>>> folder = r"C:\GIS\P8\Pythontest"
>>> ws = r"C:\GIS\P8\Pythontest\python.gdb"
>>> ds_name = "Tracklines"
>>> inDirectionRaster = "curdir"
>>> inMagnitudeRaster = "curmag"
>>> stepLength = 10
>>> trackingTime = 10000000
>>> csv_file = r"C:\GIS\P8\Pythontest\pol_points_test.csv"
>>> env.workspace = ws
>>> # Check out the ArcGIS Spatial Analyst extension license
>>> arcpy.CheckOutExtension("Spatial")
u'CheckedOut'
>>> i=0
>>> try:
... with open(csv_file ,'r') as infile:
... for line in infile:
... i += 1
... if i != 1:
... x = line.split(',')[1]
... y = line.split(',')[2]
... id = line.split(',')[0]
... sourcePoint = arcpy.Point(float(x),float(y))
...
Parsing error SyntaxError: invalid syntax (line 10)
>>> try:
... with open(csv_file ,'r') as infile:
... for line in infile:
... i += 1
... if i != 1:
... x = line.split(',')[1]
... y = line.split(',')[2]
... id = line.split(',')[0]
... 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)
...
Parsing error SyntaxError: invalid syntax (line 15)
I am using the python window in ArcMap, I am not sure when I should execute it or stop with the ...
It is your try-except block, it isn't indented properly,
try:
with.... and the rest
except:
print(...
you need to use a python IDE aka, editor not the python window to load and create python scripts