1

I wrote a python script in order to get all features near a point and its distances. To reach that, I'm using LocateFeaturesAlongRoutes_lr, then performing a join with a feature layer and finally using a field mappings object to rename and remove attributes. This last step is done using FeatureClassToFeatureClass_conversion, which accepts field mappings as parameter.

My problem is that FeatureClassToFeatureClass_conversion is taking about 1'20'' to complete, what is too much considering that only a few features remain after the join.

The issue becomes even more weird when I realized that TableToTable_conversion consumes just a few milliseconds.

Can I speed up FeatureClassToFeatureClass_conversion? Or use another method to rename and remove attributes (field info parameters from MakeFeatureLayer_management could just remove fields)?

def renameAttribute(fms, old_name, new_name):
    index = fms.findFieldMapIndex(old_name)
    fieldMap = fms.getFieldMap(index)
    output_field = fieldMap.outputField
    output_field.name = new_name
    output_field.aliasName = new_name
    fieldMap.outputField = output_field
    fms.replaceFieldMap(index, fieldMap)

points_fc = arcpy.GetParameterAsText(0)
radius = arcpy.GetParameterAsText(1)
lines_grf_fl = arcpy.GetParameterAsText(2)
view_lines = arcpy.GetParameterAsText(3)

distance_table = arcpy.LocateFeaturesAlongRoutes_lr(points_fc, lines_grf_fl, 'ID_LOGRADOURO', radius, 'in_memory\\distance_table', 'ID_LOGRADOURO POINT MEASURE', route_locations='ALL').getOutput(0)
distance_table_view = arcpy.MakeTableView_management(distance_table, 'distance_table_view').getOutput(0)
arcpy.AddJoin_management(view_lines, 'ID_LOGRADOURO_24', distance_table_view, 'ID_LOGRADOURO', 'KEEP_COMMON')
distance_fl = arcpy.MakeFeatureLayer_management(view_lines, 'distance_fl') .getOutput(0)
fms = arcpy.FieldMappings()
fms.addTable(distance_fl)
fieldMapsToRemove = \
    ['CODIGO_MUNICIPIO', \
    'NOME_MUNICIPIO', \
    'ABREV_PREFIXO', \
    'DESC_PREFIXO', \
    'ABREV_TIPO', \
    'DESC_TIPO', \
    'DESC_PREPOSICAO', \
    'ABREV_TITULO', \
    'DESC_TITULO', \
    'NOME', \
    'ID_LOGRADOURO_INICIO', \
    'ID_LOGRADOURO_FIM', \
    'HISTORIA', \
    'STATUS_CANCELAMENTO', \
    'FAIXA_VISIVEL', \
    'BAIRROS', \
    'tabela_distancias_ID_LOGRADOURO', \
    'tabela_distancias_ObjectId']
for name in fieldMapsToRemove:
    index = fms.findFieldMapIndex(name)
    fms.removeFieldMap(index)

renameAttribute(fms, 'ID_LOGRADOURO_24', 'ID_LOGRADOURO_GEO')
renameAttribute(fms, 'ID_LOGRADOURO', 'ID_LOGRADOURO_ALFA')
renameAttribute(fms, 'tabela_distancias_Distance', 'DISTANCIA')
renameAttribute(fms, 'tabela_distancias_MEASURE', 'MEASURE')
renameAttribute(fms, 'CODIGO', 'CODIGO_LOGRADOURO')
renameAttribute(fms, 'NOME_COMPLETO', 'NOME_COMPLETO_LOGRADOURO')
renameAttribute(fms, 'SHAPE_LEN', 'COMPRIMENTO_LOGRADOURO')

results = arcpy.FeatureClassToFeatureClass_conversion(distance_fl, 'in_memory', 'results', field_mapping=fms).getOutput(0)
arcpy.SetParameter(4, results)

[EDIT 1]

I modified the script to use CopyFeatures_management instead of FeatureClassToFeatureClass_conversion but the it is taking a long time to finish yet.

3

I would recommend using a search cursor to write your feature class attributes into a dictionary using the primary key, and a list of attributes as the value pair. Then delete all of the old fields from your output feature class (except the primary key attribute), add new fields in the order that you want them, and populate the attributes using an update cursor.

This method requires a bit more scripting but results in great performance.

1
  • I know this is an old post, but just wanted to +1 this idea. I had a table join on 15k rows that was taking ~30 mins; using dictionaries, down to < 2 mins – a11 Mar 28 '19 at 16:24

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.