0

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.

import arcpy
from arcpy import env

#environnement de travail
env.workspace = "D:/M1 Geomatique/Programmation II/Dossier"

#variables locales
in_features = "ELYTR_TR_BUREAU_VOTE_2015_polygon.shp"
field = "CODE_SECTE"

with arcpy.da.SearchCursor(in_features, field) as cursor:
    for row in cursor:
        name = row
        to_output = arcpy.SelectLayerByAttribute_management(row)
        arcpy.MakeFeatureLayer_management(to_output, name)

Getting a RuntimeError: Error in executing tool, but this most likely due to not using SelectLayer and MakeFeatureLayer properly, I guess the order is not good, but then again the input for the MakeFeatureLayer is not good.

  • 1
    Can you please describe what exactly you are trying to achieve? A screenshot of a sample table would be helpful and how do you want the output to look like. – Alex Tereshenkov Apr 28 '15 at 15:08
3

No need to write any code, a very simple model as shown below could do all this. You set the group by field to be your integer field and the iterator will create a sub-set which you save out using the select tool.

Model

  • 1
    Indeed, that would be the best approach when not using code, perhaps copying the Python snippet after model would work just as well. – Geosphere Apr 28 '15 at 17:37
1

I have been working on something similar - at the moment I have it to the point where is a script that can be run from IDLE.

What happens is that you make a temporary feature layer and then you need to save to have your layer.

The second link below, save_to_layer_file has a python code example.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Save_To_Layer_File/001700000070000000/

import arcpy
from arcpy import env
## This is where the file that is to be processed is saved  
arcpy.env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

# The file to be processed inside the arcp.env that has been set 
Source_File = "SDE_COMPANY_LIST.shp"

# The attribute that is being looked at in this case is "COMPANY"

feild_to_be_used = "COMPANY"
# The search cursor , goes through the given feild and extracts all the values 
all_values_in_field = [row[0] for row in   arcpy.da.SearchCursor(Source_File,feild_to_be_used)]

Unique_values = set(all_values_in_field)

print UniqueCompany

# for loop to loop through each unique_value in feild. Takes this value and applies it   within a defination query/ where clause
# to only show those unique values

for a_item in Unique_values:


    temp_name = str(a_item)+"lyr"

    out_layer = temp_name +"lyr"

    #MakeFeatureLayer variables
    Source_File = "SDE_COMPANY_LIST.shp"
    # The attribute that is being looked at is repeated here 
    where_clause = '"'+ feild_to_be_used + '"' '=' + "'" + company_query + "'"
    print where_clause
    workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

try:
    # Execute MakeFeatureLayer
    arcpy.MakeFeatureLayer_management(Source_File, out_layer0, where_clause)
    # change the output environment 
    arcpy.env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

    # Execute SaveToLayerFile
    arcpy.SaveToLayerFile_management(temp_name, out_layer, "ABSOLUTE")
except:
    print arcpy.GetMessages()

My code might be a bit buggy, but if you change where you need to, you can run that from IDLE - no need to be in ArcGIS. Things to watch out for are the formatting of the where clause and the making sure your environments are right, for the correct part of the process.

  • Thank you, Jack. Actually, I saw your code earlier today but I thought it was rather long and too complicated for my needs. – Geosphere Apr 28 '15 at 17:55
  • yeah its probably longer that its needs to be . I have been looking into this a fair bit recently. Feel free to shorten the code lol. What would be incorprated it into a tool. That would allow you to tick the values that you want a layer for. Maybe this is possible in model building, Not much experience of creating tools or model builder though . – Jack Walker Apr 28 '15 at 18:12
0

In any case, I solved the problem eventually with some help. You might as well check the Geonet community on ESRI for my solution if someone is curious.

It involves using the SearchCursor to get the unique values, followed by a loop to get each value from a list that is used as an input in MakeFeatureLayer tool.

  • Hi @geosphere, can you add you code here instead of pretty general SearchCursor and MakeFeatureLayer helps? This would be very helpful. – maycca Aug 26 at 6:40
0

This answer is based on answer provided by @BERA: Save a selected row into a new shapefile with arcpy

The scripts reads the feature class, and exports each of the rows as unique shapefile in three steps:

  1. use ``arcpy.da.SearchCursorto loop throught each row in a feature class to get uniqueOBJECTID`
  2. use OBJECTID to create a new query to subset the row
  3. save new shapefile using arcpy.Select_analysis tool.

No need for intermediate MakeFeatureLayer or SaveToLayer or CopyFeatureClass tools.

import arcpy,os

input_fc=r'C:\TEST\Shape.shp'
outfolder=r'C:\folder'

# get unique ID by OID@ token
with arcpy.da.SearchCursor(input_fc,'OID@') as cursor:

    # loop throught shapefile to save the unique row ID and save it in a correct format for query   
    for row in cursor:
        # Create query, row[0] = OID@
        sql="""{0} = {1}""".format(arcpy.AddFieldDelimiters(input_fc, arcpy.Describe(
            input_fc).OIDFieldName),row[0])  
        arcpy.Select_analysis(in_features = input_fc,
                              out_feature_class = os.path.join(outfolder,
                                                  'Shapefile_{0}.shp'.format(row[0])),
                                                  where_clause = sql)

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.