Select to view content in your preferred language

Field names differ in a memory feature class

516
5
10-25-2024 04:18 AM
tcrammond
Emerging Contributor

I am creating a script that makes a feature class using an existing on SDE, puts some data in it, and runs some processing on it, and then appends the data back to the original feature class using the append tool.

I create a tmp feature class in memory for this, which seems fine, but when I go to append back to the original feature class, the field names are different. 

For example, OBJECTID field on SDE is renamed objectid in memory, thus breaking the append (similar behavior for any st_* field). 

Is there a solution to this? I'd rather not use insert and update cursor... 

0 Kudos
5 Replies
jcarlson
MVP Esteemed Contributor

What functions do you use to create the feature class and do the append? Some thoughts:

  1. to_featureclass (and other functions) has a parameter sanitize_columns that creates snake_case column names. You can set it to False to allow your field names to stay in the format of the input data.
  2. Sometimes the sanitize parameter is "baked in". But you can still explicitly rename the columns before the append. Tedious, but possible.
col_dict = {
  "oldname_1": "NEWNAME1",
  "oldname_2": "NEWNAME2"
}

temp_layer.rename(columns=col_dict, inplace=True)
- Josh Carlson
Kendall County GIS
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Something that might be easier is using an Insert Cursor to avoid stuff like that. Shape@, OID@ are both very useful.

That being said, if you're appending stuff in, I don't think the OBJECTID matters? Or if it does, you should probably have it as a separate field that you manage.

 

The other question is: Why not just do everything in the original feature class, just using a version to do it? You have the enterprise gdb anyway; you may as well take advantage of it.

0 Kudos
tcrammond
Emerging Contributor

I think the issue is that this is essentially an UPSERT operation, where not all the old content is updated. So I want to keep un-updated old records, update some existing ones, and append new ones (if they exist).

 

I'm using insert/create cursors, but hoping to make this more efficient.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try running the Append with the 'NO_TEST' option for the schema_type parameter.

TonyAlmeida
MVP Regular Contributor

Maybe use field mapping,

import arcpy

# Paths to your original feature class (SDE) and  in-memory feature class
sde_fc = "feature_class"
in_memory_fc = "in_memory/tmp_feature_class"

# Create FieldMappings object
field_mappings = arcpy.FieldMappings()

# Add fields from the in-memory feature class to the FieldMappings
for field in arcpy.ListFields(in_memory_fc):
    field_map = arcpy.FieldMap()
    field_map.addInputField(in_memory_fc, field.name)
    
    # If needed, set the output field name to match the SDE feature class field
    if field.name.lower() == "objectid":
        output_field = field_map.outputField
        output_field.name = "OBJECTID"
        field_map.outputField = output_field
    
    field_mappings.addFieldMap(field_map)

# Append with FieldMappings to ensure they are correct
arcpy.management.Append(in_memory_fc, sde_fc, "NO_TEST", field_mappings)

 

Also, you might try to disable qualified field names.

# Disable qualified field names
arcpy.env.qualifiedFieldNames = False

# Perform  Append
arcpy.management.Append(in_memory_fc, sde_fc, "NO_TEST")