Select to view content in your preferred language

learning python, using python how would I ingest feature classes into a GDB, then rename them

602
1
01-16-2023 11:05 AM
Emasia
by
New Contributor

Using Python, how would I take for example [ buildings_a_16_free] from osm and ingest that feature class into a new GDB (named after a file structure such as ***- where *** is the name of the country) into [***_Buildings_Polygon]?

 

Should I figure it out first I will post the solution to it.

Tags (2)
0 Kudos
1 Reply
JohannesLindner
MVP Frequent Contributor

To do that, you can use the tool Export Features (Conversion)—ArcGIS Pro | Documentation.

The documentation of the ArcGIS Tools always has a section which tells you how to call the tool in Python:

JohannesLindner_0-1673959139707.png

 

I don't know your data, but here are two quick examples:

 

Just copy the whole feature class

arcpy.conversion.ExportFeatures(
    "C:/Data/OldGeodatabase.gdb/Buildings",
    "C:/Data/NewGeodatabase.gdb/Buildings"
    )

 

Copy the features into different Featureclasses, depending on a field value

in_features = "C:/Data/OldGeodatabase/Buildings"
classification_field = "Country"

# read all values of the classification field
class_values = []
with arcpy.da.SearchCursor(in_features, [classification_field]) as cursor:
    for row in cursor:
        class_values.append(row[0])

# often, you will find syntax like this:
# class_values = [row[0] for row in arcpy.da.SearchCursor(in_features, [classification_field])]
# this is called list comprehension, lots of good intros into that online.

# convert the list (all values) into a set (distinct values)
distinct_class_values = set(class_values)

# loop over the distinct values
for class_value in distinct_class_values:
    print(class_value)
    # create a SQL query that will filter for that value
    query = f"{classification_field} = '{class_value}'"  # this is called f-string
    # export the filtered features into a new feature class
    arcpy.converion.ExportFeatures(
        in_features,
        f"C:/Data/NewGeodatabase.gdb/{class_value}_Buildings",
        query
        )
    

Have a great day!
Johannes