I'm having some trouble starting this script. Not sure how to start, as I have to create/output layers for each attribute value from a certain column named that has integers. They are all unique values.
My first thought was to somehow use MakeFeatureLayer but then again, how do you create the layers without making countless of local variables with layer name.
Another idea would be to use the SearchCursor to iterate each value, and place a layer for each but then again how would I do that.
Looking forward to some suggestions.
Solved! Go to Solution.
The SearchCursor help reference has a sample for getting unique values from a field:
import arcpy fc = 'c:/data/base.gdb/well' field = 'Diameter' # Use SearchCursor with list comprehension to return a # unique set of values in the specified field values = [row[0] for row in arcpy.da.SearchCursor(fc, field)] uniqueValues = set(values) print(uniqueValues)
Hi Florin-Daniel,
You can iterate through the feature class's field and append the values to a list, remove the duplicate values, and then iterate through the list to create a feature layer for each unique value. Ex:
list = [] with arcpy.SearchCursor('Soils', ['LandUse']) as cursor: for row in cursor: list.append(row[0]) del cursor #remove duplicates from list list = dict.fromkeys(list) list = list.keys() for landuse in list: arcpy.MakeFeatureLayer_management('Soils', 'Soils_' + landuse, "LandUse = '" + landuse + "'")
Hi Jake,
Thank you for dropping by. Sounds like an interesting solution, I'll try that and come back with a verdict. At the moment, I was using a different approach but it does not work.
I can't seem to make it work, getting a Runtime Error for my SearchCursor line.
import arcpy from arcpy import env #variables locales in_features = "D:/M1 Geomatique/Programmation II/Dossier/ELYTR_TR_BUREAU_VOTE_2015_polygon.shp" list = [] with arcpy.SearchCursor(in_features, "CODE_SECTE") as cursor: for row in cursor: list.append(row[0]) del cursor #effacer les doubles list = dict.fromkeys(list) list = list.keys() for val in list: arcpy.MakeFeatureLayer_management(in_features, val)
For the SearchCursor, you will need to pass the field as a list. Ex:
with arcpy.SearchCursor(in_features, ["CODE_SECTE"]) as cursor: for row in cursor: list.append(row[0]) del cursor
Thanks again, made the correction but I am still getting a RunTime Error. I guess it has to be something with the loop?
Runtime error
Traceback (most recent call last):
File "<string>", line 14, in <module>
AttributeError: __exit__
with arcpy.SearchCursor(in_features, ["CODE_SECTE"]) as cursor: for row in cursor: list.append(ro
Have you tried dropping the "with" statement? That error has something to do with how the with statement catches errors and cleans up objects and it's not finding the __exit__ function for SearchCursor.
Maybe try:
cursor = arcpy.SearchCursor(infeatures, ['CODE_SECTE'])
That would make perfect sense, I thought it had to do something with it. I've seen "with" being used mostly with da.SearchCursor not the other one,
The SearchCursor help reference has a sample for getting unique values from a field:
import arcpy fc = 'c:/data/base.gdb/well' field = 'Diameter' # Use SearchCursor with list comprehension to return a # unique set of values in the specified field values = [row[0] for row in arcpy.da.SearchCursor(fc, field)] uniqueValues = set(values) print(uniqueValues)
Thanks for the reference Jeff, that was really helpful. Initially, I tried it, but it didn't worked out well, perhaps the rest of my code was wrong.
Still, I would like to know how the other solution works out. I must be doing something wrong, for sure.