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
-
Please be sure to always include the version(s) of software in use with each question.Vince– Vince2016年06月14日 17:05:24 +00:00Commented Jun 14, 2016 at 17:05
1 Answer 1
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
-
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 youmorgan– morgan2016年06月14日 17:01:09 +00:00Commented Jun 14, 2016 at 17:01
-
Where possible, all "old" cursor applications should be transitioned to DA Cursors (an order of magnitude faster)Vince– Vince2016年06月14日 17:04:35 +00:00Commented Jun 14, 2016 at 17:04
-
Good point @Vince I've updated the code in the answer with the new DA cursor.Jacob F– Jacob F2016年06月14日 17:20:18 +00:00Commented Jun 14, 2016 at 17:20
-
The
del cursor
is not needed with awith
statement (the automatic cleanup is part of the benefit of using thewith
construct)Vince– Vince2016年06月14日 20:17:19 +00:00Commented Jun 14, 2016 at 20:17