Ok, I'm stumped (again!)... If I comment out my output parameter this works great and I have point features in my FC, but I need set the output symbology with a lyr file. If I keep the code the way it is the layer is added to the TOC, but no features are inserted to the FC.
You can really ignore most of this and just know that after the insert cursor I have the expected data (gps points).
Any ideas?!!
You can really ignore most of this and just know that after the insert cursor I have the expected data (gps points).
Any ideas?!!
Code:
import arcpy
import re
import os
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "GPS Isolator"
self.alias = "GPS Isolator"
# List of tool classes associated with this toolbox
self.tools = [isolateGPS]
class isolateGPS(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Get a track"
self.description = "this will pull out one officers gps track..."
self.canRunInBackground = True
def getParameterInfo(self):
"""Define parameter definitions"""
unitid = arcpy.Parameter(
displayName="Unit ID",
name="unitId",
datatype="String",
parameterType="Required",
direction="Input")
theDate = arcpy.Parameter(
displayName="Date",
name="date",
datatype="Date",
parameterType="Required",
direction="Input")
#startTime = arcpy.Parameter(
#displayName="Start Time",
#name="starttime",
#datatype="Date",
#parameterType="Optional",
#direction="Input")
#endTime = arcpy.Parameter(
#displayName="End Time",
#name="endtime",
#datatype="Date",
#parameterType="Optional",
#direction="Input")
outdir = arcpy.Parameter(
displayName="Output Directory",
name="outDir",
datatype="Folder",
parameterType="Required",
direction="Input")
output = arcpy.Parameter(
displayName = "AVL",
name = "avl",
datatype = "Feature Class",
parameterType = "Derived",
direction = "Output")
output.symbology= os.path.join(os.path.dirname(__file__), 'AVL.lyr')
params = [unitid, theDate, outdir, output]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
def reformatDateTime(dt):
s = str(dt)
dtList = re.findall('..', s)
return dtList[1] + '/' +dtList[2] + '/20' + dtList[0] + ' ' + dtList[3] +':' + dtList[4] +':' + dtList[5]
arcpy.AddMessage('\n')
arcpy.AddMessage('the unit id is: ' + parameters[0].valueAsText)
arcpy.AddMessage('the Date is: ' + parameters[1].valueAsText)
arcpy.AddMessage('the output directory is: ' + parameters[2].valueAsText)
arcpy.AddMessage('\n')
unitID = parameters[0].valueAsText
theDate = parameters[1].valueAsText
outdir = parameters[2].valueAsText
#-------------CREATE THE FILE GEODATABASE-----------------------------
gpspath = r'E:\gis\rawgps'
m, d, y = theDate.split('/')
if len(m) == 1:
m = '0' + m
if len(d) == 1:
d = '0' + d
goodDate = y + m + d
gps = gpspath + '\\' + goodDate + '.gps'
arcpy.AddMessage(gps)
arcpy.env.workspace = outdir + '\\' + 'avl_' + goodDate + '_' + unitID + '.gdb'
arcpy.CreateFileGDB_management (outdir, 'avl_' + goodDate + '_' + unitID + '.gdb')
arcpy.CreateFeatureclass_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb', unitID, 'POINT')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'unitid', 'TEXT')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'gpstime', 'DATE')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'xcoord', 'LONG')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'ycoord', 'LONG')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'hspeed', 'DOUBLE')
arcpy.AddField_management(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID, 'heading', 'DOUBLE')
c = arcpy.da.InsertCursor(outdir + '\\' +'avl_' + goodDate + '_' + unitID + '.gdb\\' + unitID,
('unitid', 'gpstime', 'xcoord', 'ycoord', 'hspeed', 'heading', 'SHAPE@XY'))
i = 0
f = open(gps, 'r')
for line in f.readlines():
i+=1
if len(line) > 40:
#arcpy.AddMessage(line)
if str(i).endswith('000'):
arcpy.AddMessage('.....processed ' + str(i) + ' records.')
sp = line.split('|')
try:
if sp[0] == unitID:
c.insertRow((sp[0], reformatDateTime(sp[2]), int(sp[4]) +211, int(sp[5]),
float(sp[7]), float(sp[9]), (float(sp[4]) +211, float(sp[5]))))
except:
pass
f.close()
del f, c
parameters[3].value = outdir + '\\' + 'avl_' + goodDate + '_' + unitID + '.gdb' + '\\' + unitID
return