Rename Feature Classes Based on Polygon Index Attributes

590
5
Jump to solution
04-05-2023 10:20 AM
Labels (1)
ChadR
by
New Contributor II

I have a file gdb containing the following items, and I am using version 10.8.2

200 line feature classes currently named Contour_Tile1, Contour_Tile2, etc...

I also have a single polygon feature class named Index_5000ft.  It contains 200 polygons (one bounding polygon for each of the previously mentioned line feature classes).  In the polygon attribute table there is a column named [Tile_Num] and it contains values like GAW_30310026.  There is no correlation between the name of the line feature class, and the polygon that it might fall within.  For example, all the lines in the Contour_Tile1 FC, might fall inside an index polygon with the attribute of GAW_30310026.  The only thing they have in common is their spatial relationship (extent).

Lines contained within any one of the line feature classes, will always fall completely within a single polygon found in the Index_5000ft FC.  Lines from a single FC will never intersect with more than one index polygon.

What I want to do is rename each of the line feature classes from Contour_Tile1, to something like GAW_30310026, which is found in the [Tile_Num] column of the corresponding polygon attribute table.

FYI: Right now I'm only having to deal with 200 tiles, but I will need to repeat this process many more times, so "automation" is the only way I can make this work.  In other words, I can't resort to manual renaming because I will have many thousands of FCs to deal with.  Does anyone have any ideas on how to rename the line FCs, as described here?

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Obviously try it on a copy and/or subset of your data first (and keep a backup of the original for some time regardless)  also close down arcmap etc to get rid of any locks for renaming:

import arcpy
import os

index_fc = r'C:\ArcGIS\yourFGDBgdb\Your index polygons'
line_gdb = r'C:\ArcGIS\FGDB containing your lines.gdb'


#list the fc names and paths to line FCs
arcpy.env.workspace = line_gdb
lines_fclist = [[fc, os.path.join(line_gdb, fc)] for fc in arcpy.ListFeatureClasses(feature_type="line")]


#create a list to store FC name and
#single line geom from each lines FC
linefc_list=[]
for line_fc in lines_fclist:
    with arcpy.da.SearchCursor(line_fc[1],["SHAPE@"]) as cursor:
        for row in cursor:
            linefc_list.append([line_fc[0],row[0]])
            break


#create a dictionary of line fc path and intersecting index polygon name
index_line_dict = {}
with arcpy.da.SearchCursor(index_fc,[“Tile_Num","SHAPE@"]) as cursor:
    for row in cursor:
        for line in linefc_list:
            if row[1].disjoint(line[1]) is False:
                index_line_dict[os.path.join(line_gdb, line[0])] = row[0]
                break

print(index_line_dict)

#rename lines to match
for filepath in index_line_dict:
    arcpy.management.Rename(filepath,index_line_dict[filepath],"FeatureClass")

 

 

View solution in original post

5 Replies
DavidPike
MVP Frequent Contributor

Are the lines in their own FGDB separate from the index polygon, what's the layout of the data - is there anhything else in that FGDB?  If you iterated through that workspace it would only return the line FCs?

0 Kudos
ChadR
by
New Contributor II

Right now the 200 line FCs and the one Index polygon FC are in the same FGDB, but I can easily move the index polygon FC to its on FGDB, so an iterator would only see the line FCs.

0 Kudos
DavidPike
MVP Frequent Contributor

Obviously try it on a copy and/or subset of your data first (and keep a backup of the original for some time regardless)  also close down arcmap etc to get rid of any locks for renaming:

import arcpy
import os

index_fc = r'C:\ArcGIS\yourFGDBgdb\Your index polygons'
line_gdb = r'C:\ArcGIS\FGDB containing your lines.gdb'


#list the fc names and paths to line FCs
arcpy.env.workspace = line_gdb
lines_fclist = [[fc, os.path.join(line_gdb, fc)] for fc in arcpy.ListFeatureClasses(feature_type="line")]


#create a list to store FC name and
#single line geom from each lines FC
linefc_list=[]
for line_fc in lines_fclist:
    with arcpy.da.SearchCursor(line_fc[1],["SHAPE@"]) as cursor:
        for row in cursor:
            linefc_list.append([line_fc[0],row[0]])
            break


#create a dictionary of line fc path and intersecting index polygon name
index_line_dict = {}
with arcpy.da.SearchCursor(index_fc,[“Tile_Num","SHAPE@"]) as cursor:
    for row in cursor:
        for line in linefc_list:
            if row[1].disjoint(line[1]) is False:
                index_line_dict[os.path.join(line_gdb, line[0])] = row[0]
                break

print(index_line_dict)

#rename lines to match
for filepath in index_line_dict:
    arcpy.management.Rename(filepath,index_line_dict[filepath],"FeatureClass")

 

 

ChadR
by
New Contributor II

I will give this a shot tomorrow and let you know how it goes.  Thank you very much for responding!

0 Kudos
ChadR
by
New Contributor II

David - that did the trick.  You're awesome!