ArcGIS PRO: Geoprocessing - Model Builder: Custom Name Output Feature Classes after Performing Split By Attributes / Iteration

650
5
Jump to solution
12-16-2020 08:40 AM
by Anonymous User
Not applicable

 

I have a FC that is comprised of a polygons (parcels) that have been DISSOLVED & SPLIT BY ATTRIBUTE using the zoning class for both GP tools. 

The problem is that after I perform the SPLIT BY ATTRIBUTE, I need to have all 20 of the new FCs from the split be renamed after the zoning class they represent; right now they all have the same name. Is there a way to rename each FC based on an attribute value using model builder?

This is all part of two models that I am attempting to merge together to automate the updating of zoning class per parcel and further creating districts that are made out of the aggregation of dissolved parcels that are then split by the zoning class attribute field and then merged together into a single FC that has single part zoning districts. 

 

MODEL 1:

BrianAcheff_0-1608135996483.png

 

MODEL 2:

 

BrianAcheff_1-1608136048567.png

 

MODEL 2 (zoomed):

BrianAcheff_2-1608136130696.png

The goal is to be able to rename each FC created from the split by their corresponding zoning class then, set a precondition by attaching MODEL 2 to the finals output after the SPLIT BY ATTRIBUTES is performed.

 

Another thought would be to use an iterator to loop through the FCs created from the split and perform the aggregation with an iterator, still in that case the renaming of the FCs by zoning class would need to be accomplished.

Both models run and the desired output can achieved through manually changing each name, but the goal is to obviously have a model and python script that can be used for automation and to reduce any human error that may occur when changing field names or redirecting database paths.

0 Kudos
2 Solutions

Accepted Solutions
DuncanHornby
MVP Frequent Contributor

I've too have come across this behaviour, I think its a bug, the layers all have the same name as shown in your image. You'll find the actual dataset, the Feature Class, will have the unique name with the split value tagged on the end. Look in the workspace where you have stored them. It's just that the output when added to the Table of Contents keeps taking on the name of the variable, the text you see in the green bubble .

View solution in original post

by Anonymous User
Not applicable

import arcpy

# Set environment settings
arcpy.env.workspace = "B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb"

# Set local variables
inFeatures = "B_1_Aggregate"
fieldName1 = "Zoning"
fieldType = "String"
fieldPrecision = 7
fieldAlias = "Zoning"
fieldLength = 10

# Execute AddField twice for two new fields
arcpy.AddField_management(inFeatures, fieldName1, "TEXT", fieldPrecision,
field_alias=fieldAlias, field_is_nullable="NULLABLE")

# Create insert cursor for table
cursor = arcpy.da.InsertCursor(r"B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb\B_1_Aggregate",
["Zoning"])

# Create new rows. Set the initial row ID and values
for i in range(0,70):
cursor.insertRow([i])

# Calculate Field
arcpy.CalculateField_management("B_1_Aggregate", "Zoning", "'B-1'", "PYTHON3")

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable

I was able to find this and possibly use it as an intermediate step to rename the fields before the second model would run.

 

 

import arcpy

fc = "C:\mygdb\myfile"
f1, f2, f3 = "EDITOR", "SPECIES", "OBJECTID"
clause = arcpy.AddFieldDelimiters(fc, f3) + "= 1"
for row in sorted (arcpy.da.SearchCursor (fc, [f1, f2], clause)):
    print ("{0}_{1}".format(row[0], row[1]))
    name =("{0}_{1}".format(row[0], row[1]))
    arcpy.Rename_management(fc, name)

 

 

 

credit: https://gis.stackexchange.com/questions/126452/using-arcpy-to-rename-feature-class-by-field

 

 

 

 

0 Kudos
DuncanHornby
MVP Frequent Contributor

I've too have come across this behaviour, I think its a bug, the layers all have the same name as shown in your image. You'll find the actual dataset, the Feature Class, will have the unique name with the split value tagged on the end. Look in the workspace where you have stored them. It's just that the output when added to the Table of Contents keeps taking on the name of the variable, the text you see in the green bubble .

View solution in original post

by Anonymous User
Not applicable

@DuncanHornby ,

Thank you for your help!

I have found a major bottle neck in MODEL 2; when I perform the AGGREGATE POLYGONS for all the zoning classes, the Zoning field from the Feature Classes that are created from SPLIT BY ATTRIBUTE is not carried over during the aggregation process. 

So to remedy this I am using this python script to add a row and insert the correct zoning class, but am prompted with the error at the end of the reply. 

Would you be able to check over the error I am getting, it creates the new row, but will not append the zoning class data

 

5.JPG

import arcpy

# Set environment settings
arcpy.env.workspace = "B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb"

# Set local variables
inFeatures = "B_1_Aggregate"
fieldName1 = "Zoning"
fieldType = "String"
fieldPrecision = 7
fieldAlias = "Zoning"
fieldLength = 10

# Execute AddField twice for two new fields
arcpy.AddField_management(inFeatures, fieldName1, "TEXT", fieldPrecision,
field_alias=fieldAlias, field_is_nullable="NULLABLE")

# Create insert cursor for table
cursor = arcpy.da.InsertCursor(r"B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb\B_1_Aggregate",
["Zoning"])

# Create new rows. Set the initial row ID and values
for i in range(0,178):
cursor.insertRow([i, 'B-1'])
Traceback (most recent call last):
File "<string>", line 24, in <module>
TypeError: sequence size must match size of the row

 

0 Kudos
by Anonymous User
Not applicable

import arcpy

# Set environment settings
arcpy.env.workspace = "B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb"

# Set local variables
inFeatures = "B_1_Aggregate"
fieldName1 = "Zoning"
fieldType = "String"
fieldPrecision = 7
fieldAlias = "Zoning"
fieldLength = 10

# Execute AddField twice for two new fields
arcpy.AddField_management(inFeatures, fieldName1, "TEXT", fieldPrecision,
field_alias=fieldAlias, field_is_nullable="NULLABLE")

# Create insert cursor for table
cursor = arcpy.da.InsertCursor(r"B:\Projects\2020\Zoning\Zoning Pro Project\Districts.gdb\B_1_Aggregate",
["Zoning"])

# Create new rows. Set the initial row ID and values
for i in range(0,70):
cursor.insertRow([i])

# Calculate Field
arcpy.CalculateField_management("B_1_Aggregate", "Zoning", "'B-1'", "PYTHON3")

View solution in original post

0 Kudos
DuncanHornby
MVP Frequent Contributor

 The error message is occurring when you try to insert a new row. Look at your insert row line, you are inserting 2 values, now look at the line that creates the insert cursor, you provide only one field, "Zoning". So you are trying to stick 2 values into 1, hence the mismatch in size. You need to update your list of fields in the cursor.

0 Kudos