Select to view content in your preferred language

Creating layers for each attribute value via Arcpy script

5728
10
Jump to solution
04-28-2015 06:40 AM
deleted-user-3rTxRfVTcNm-
Deactivated User

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.

0 Kudos
1 Solution

Accepted Solutions
JeffWard
Honored Contributor

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)
Jeff Ward
Summit County, Utah

View solution in original post

10 Replies
JakeSkinner
Esri Esteemed Contributor

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 + "'")
deleted-user-3rTxRfVTcNm-
Deactivated User

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.

0 Kudos
deleted-user-3rTxRfVTcNm-
Deactivated User

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)
0 Kudos
JakeSkinner
Esri Esteemed Contributor

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 
deleted-user-3rTxRfVTcNm-
Deactivated User

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__

0 Kudos
JeffWard
Honored Contributor

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'])

Jeff Ward
Summit County, Utah
deleted-user-3rTxRfVTcNm-
Deactivated User

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,

0 Kudos
JeffWard
Honored Contributor

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)
Jeff Ward
Summit County, Utah
deleted-user-3rTxRfVTcNm-
Deactivated User

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.

0 Kudos