Select to view content in your preferred language

PYT:  how to set symbology on output feature class?

1431
13
Jump to solution
01-17-2013 07:50 AM
KevinBell
Deactivated User
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?!!


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
Tags (2)
0 Kudos
13 Replies
T__WayneWhitley
Honored Contributor
No good clue.... however, forgive me if I'm just guessing, but I'm suspicious about this:

self.canRunInBackground = True


Even though you deleted your cursor obj, that doesn't necessarily mean 'garbage collection' is complete - not that I understand all of that, but I can't help thinking somehow communication (or at least 'awareness', for lack of a better term) is disrupted between the lyr object (file on disk) and your lyr object (in the map)...

Would it make sense to run it in the 'foreground'?


I'm probably grasping.....interesting problem though.  I'd like to know the answer as I've only begun looking into feature and record sets.

EDIT:  Reading the help I noticed parameterDependencies is geared for 'Derived' output, as you have set your 'Output' parameter.  Not sure if this applies here.
0 Kudos
KevinBell
Deactivated User
Duh...  I was trying to set the parameter[3].value to a feature class, but all I need to do was pass it a layer.

arcpy.MakeFeatureLayer_management(outdir + '\\' + 'avl_' + goodDate + '_' + unitID + '.gdb' + '\\' + unitID, 'avllyr')
parameters[3].value = 'avllyr'

Now this works great!
0 Kudos
T__WayneWhitley
Honored Contributor
Wow, great, I'm glad to hear you've fixed it!
Fascinating, would you mind filling me in - does the tool also execute a script file?...or did you need to insert all the code in the script tool classes for some other purpose?

Thanks for the problem, very interesting...
-Wayne
0 Kudos
T__WayneWhitley
Honored Contributor
Forget that line of questioning, silly of me - I didn't know what a Python toolbox (pyt) was!! (versus a custom toolbox with a separate associated script file)  Must be new at 10.1.

For those "just waking up" (like me), see the webhelp comparison:
http://resources.arcgis.com/en/help/main/10.1/index.html#/Comparing_custom_and_Python_toolboxes/0015...

Haven't upgraded to 10.1 yet, but I'm anxious now to try out this pyt toolbox type.

Thanks,
Wayne
0 Kudos