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

  • Please be sure to always include the version(s) of software in use with each question. – Vince Jun 14 '16 at 17:05
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
  • 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 – morgan Jun 14 '16 at 17:01
  • Where possible, all "old" cursor applications should be transitioned to DA Cursors (an order of magnitude faster) – Vince Jun 14 '16 at 17:04
  • Good point @Vince I've updated the code in the answer with the new DA cursor. – Jacob F Jun 14 '16 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) – Vince Jun 14 '16 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.