Select to view content in your preferred language

ArcMap10.1 Loop through a unique list to create new layers for each unique name.

2077
13
Jump to solution
04-10-2014 07:50 AM
StewartChritton
Emerging Contributor
I have only taken one GIS class that used python so this is messy!

I was able to get the unique list from the attributes table under the column 'Layer'. Next I need the program to use each unique name from that list, select by attributes and create a new layer file for each unique name. I have the count in there because there are 33 different unique names in that list. I know there is a better way to do this, I just don't remember how.

I realize my code is far from perfect so if you have other questions or tips please don't hesitate to ask.

import arcpy, sys, os, traceback

fc = r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\dshline_Project"
field = ["Layer"]
valueList = []

#Use search cursor to aquire a list of unique values
    with arcpy.da.SearchCursor(fc, field) as cursor:
        for row in cursor:
            valueList.append(row[0])

    uniqueSet = set(valueList)
    uniqueList = list(uniqueSet)
    uniqueList.sort()

    del cursor

    count = 0
    name = uniqueList[count]
    outputname = r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\"" + name
    if count < 34:
        arcpy.MakeFeatureLayer_management (r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\dshline_Project", "layer")
        arcpy.SelectLayerByAttribute_management("layer","NEW_SELECTION", """Layer" = """ + name)


##        arcpy.SelectLayerByAttribute_management("dshline_Project","NEW_SELECTION",""""Layer" = 'ANNOT'""")

        arcpy.CopyFeatures_management(fc,outputname,"#","0","0","0")
        count += 1
    else:
        print "Done"

Please Help!
Tags (2)
0 Kudos
13 Replies
T__WayneWhitley
Honored Contributor
lol, I am sorry, I am mistaken...
Actually, you only need one 'name = uniqueList[count]' line and you can put that just after your loop begins, as such:

count = 0
while count < lencount:
     name = uniqueList[count]
     arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = '" + name + "'")
     # etc., etc.
     count += 1

print 'done!'


...or, maybe simpler:
for count in range(lencount):
     name = uniqueList[count]
     arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = '" + name + "'")
     # etc., etc.

print 'done!'



Sorry about that (and yes, 'while count <= lencount - 1' would work too).

Wayne


PS - Incidentally, you may end with a 'done!' statement outside of your loop, as shown.
0 Kudos
StewartChritton
Emerging Contributor
Thanks again, this time I actually put it in and tested it  and everything ran smoothly.

Appreciate it.
0 Kudos
T__WayneWhitley
Honored Contributor
I thank you Stewart, but I'm following up late, guess just for a little 'quality assurance' as this was something I have struggled with before too - you should credit Jake.

Have fun with Python!

Wayne
0 Kudos
StewartChritton
Emerging Contributor
Thought I could check both because you both contributed, didn't realize only one could receive credit haha.

Good on you for pointing that out and thanks again for that last bit of help.
0 Kudos