0

My data consists of a directory of shp files. I'm trying to accomplish the following: Go through each shp file and perform a function on each feature. The way my script is working now, it is preforming the function on every feature in the feature class at once, so the fields I'm writing to has the field values of every feature in the feature class in it rather than the values of each feature seperately. Here is my script as of now:

import arcpy, time, datetime, os
from datetime import datetime
from arcpy import env
arcpy.env.workspace = r"C:\Data_State\MT\temp\gpx"
def PopulateGPX():
 GPXFile = r"C:\Data_State\MT\temp\gpx\GPXtest.gpx"
 writeGPX = open(GPXFile, 'a+')
 writeGPX.writelines('''
 <wpt lat="''' + str(Latitude) + '''" lon="''' + str(Longitude) + '''">
 <name>''' + str(Name) + '''</name>
 <cmt></cmt>
 <extensions>
 <gpxx:WaypointExtension>
 <gpxx:Address>
 <gpxx:StreetAddress>''' + str(Address) + '''</gpxx:StreetAddress>
 <gpxx:City>''' + str(City) + '''</gpxx:City>
 <gpxx:State>''' + str(State) + '''</gpxx:State>
 <gpxx:PostalCode></gpxx:PostalCode>
 </gpxx:Address>
 </gpxx:WaypointExtension>
 </gpxx:extensions>
 </wpt>
 '''+ '\n')
 writeGPX.flush()
for fc in arcpy.ListFeatureClasses(): 
 for feature in fc:
 Latitude = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "Latitude")]))
 Longitude = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "Longitude")]))
 Name = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "Name")]))
 Address = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "ADDRESS_TA")]))
 City = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "CITY_TAX")]))
 State = sorted(set([r[0] for r in arcpy.da.SearchCursor(fc, "STATE_TAX")]))
 PopulateGPX()

I hope I explained what I'm trying to accomplish well enough. Any ideas of where I'm going wrong? Using 10.3

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 14, 2016 at 16:28
1
  • Please be sure to always include the version(s) of software in use with each question. Commented Jun 14, 2016 at 17:05

1 Answer 1

2

I'm not sure what you're trying to accomplish with the sorting, but this should go through every feature in each featureclass and then call the PopulateGPX function.

fields = ['Latitude','Longitude','Name','ADDRESS_TA','CITY_TAX','STATE_TAX']
for fc in arcpy.ListFeatureClasses():
 with arcpy.da.SearchCursor(fc, fields) as cursor:
 for row in cursor:
 Latitude = row[0]
 Longitude = row[1]
 Name = row[2]
 Address = row[3]
 City = row[4]
 State = row[5]
 PopulateGPX()
 del cursor
answered Jun 14, 2016 at 16:54
4
  • That was from another script, I should have seen that it would cause issues. I just re-wrote the script and got it working, then saw your answer, which is cleaner than what I wrote. Thank you Commented Jun 14, 2016 at 17:01
  • Where possible, all "old" cursor applications should be transitioned to DA Cursors (an order of magnitude faster) Commented Jun 14, 2016 at 17:04
  • Good point @Vince I've updated the code in the answer with the new DA cursor. Commented Jun 14, 2016 at 17:20
  • The del cursor is not needed with a with statement (the automatic cleanup is part of the benefit of using the with construct) Commented Jun 14, 2016 at 20:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.