Add a layer name to layer attribute tables

1872
6
02-15-2022 12:13 AM
DavidMrázek
Occasional Contributor II

Hello,
is it possible to programmatically insert the layer name into its attribute table?

Is it possible to write it down via Calculate Field?

EDIT:

My problem has moved on to the next stage.

var AL016 = "MS_FT_AL016_T";
                var NF103 = "MS_FT_NF103_T";
                var NF109 = "MS_FT_NF109_T";
                var NF121 = "MS_FT_NF121_T";
                var NF127 = "MS_FT_NF127_T";
                var NF128 = "MS_FT_NF128_T";
                var FTJmeno = "MS_FT_JMENO_P_T";
 List<string> names = new List<string>();
                names.Add(AL016);
                names.Add(NF103);
                names.Add(NF109);
                names.Add(NF121);
                names.Add(NF127);
                names.Add(NF128);
names.Add(FTJmeno);
                foreach (var name in names)
                {
                    FeatureLayer featureLayer = mv.Map.FindLayers(name).First() as FeatureLayer;
                    FeatureClass fc = featureLayer.GetTable() as FeatureClass;
                    await AddField(fc, typObjektu, "TEXT", "", "", 10);
                    object[] paraList = { fc, typObjektu, "\\" + name+"\\" };
                    await StartATask("management.CalculateField", paraList);
                }

Here is my code, where I can't prevent "name" from being written to my attribute table.

 

Thank you

David

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

You would have to add the table name to a field and it would be replicated by the number of records.

A table associated with geometry will be referenced by the featureclass.

A standalone table already has a name since it has to be referenced to disk.

There are data structures that enable comments to be embedded into the file (aka, table) so you can put a "name" in there (eg numpy arrays saved to disk as *.npz files)

Perhaps the purpose for needing this would be useful.


... sort of retired...
0 Kudos
DavidMrázek
Occasional Contributor II

I thought of it this way: A layer has an attribute table that contains a column, I would like to add to that column the name of the layer that is the table.

Shown in the picture

vysve.png

Instead of name, I would like the name of the layer.

0 Kudos
DanPatterson
MVP Esteemed Contributor

just use Calculate Field, give it a name and make it text,

parse the featureclass table name from either the layer name or the source name on disk

pass the string to Calculate field

Calculate Field (Data Management)—ArcGIS Pro | Documentation

just be aware that passing strings requires single quoted, double quoted strings   '"single_around_double"'

Then you would have the table name in the table for however many records you have, but it will bloat the table size as you are aware


... sort of retired...
0 Kudos
DavidMrázek
Occasional Contributor II

So I'm making a mistake there, that's why I still can't, my attempt to write doesn't work: "\" "+ name +" \ ""

"name" is a changing value in foreach

0 Kudos
DanPatterson
MVP Esteemed Contributor

parse the featureclass table name from either the layer name or the source name on disk

you have to provide the code when cycling through the featureclasses to get the featureclass name "before" you invoke a field calculation... This has to be done using an updatecursor in a python script, you just can't use CalculateField on its own


... sort of retired...
DavidMrázek
Occasional Contributor II

Thank you for the advice, but I have no idea, maybe a mistake in my language skills.

0 Kudos