TransWikia.com

Accessing and manipulating fields in Layer file

Geographic Information Systems Asked by occystrap on April 25, 2021

I am trying to update several fields in a mosaic dataset, to do this I am creating a mosaic layer and then running a loop over each row and calculating fields.

A NAME field contains a string which I would like to use to calculate several other fields. It is formatted like the following:

Project_2020_1x_YZ_Type_20200101_0900VLAT_102a

I would like to use split and use the output list indices in a field calculation (ie Project, Year and Date). The issue is I don’t know to access the NAME field within a mosaic layer.

# create mosaic layer
mdl = arcpy.MakeMosaicLayer_management(md_path, "md_layer")

# calculate fields loop
print("Calculating fields in footprint...")
for lyr in mdl:
    split_name = lyr.NAME.split('_')
    arcpy.CalculateField_management(lyr, "YEAR", ????, "PYTHON")
    print("Year calculated...")
    arcpy.CalculateField_management(lyr, "PROJECT",????, "PYTHON")
    print("Project calculated...")

It falls over at the first step – split_name = lyr.NAME.split('_')

AttributeError: 'Layer' object has no attribute 'NAME'

I would like to know the best approach to accessing specific fields within the mosaic layer.

One Answer

Based on the 1st example (which uses split) in the help:

for lyr in mdl:  #Note replace PYTHON with PYTHON3 for ArcGIS Pro

    split_name = '!NAME!.split("_"){}'

    arcpy.CalculateField_management(lyr, "YEAR", split_name.format('[1]'), "PYTHON")
    print("Year calculated...")

    arcpy.CalculateField_management(lyr, "PROJECT", split_name.format('[0]'), "PYTHON")
    print("Project calculated...")

    arcpy.CalculateField_management(lyr, "DATE", split_name.format('[5]'), "PYTHON")
    print("Date calculated...")

However... an arcpy.da.UpdateCursor will probably be quicker than 3x arcpy.CalculateField_management calls as you can calculate all fields required at once.

Correct answer by user2856 on April 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP