Building proper syntax for importing .lyr symbology for Feature Layer

3570
15
02-10-2017 11:54 AM
ThomasCox
Occasional Contributor

I hesitate to throw another syntax question out so quickly following a recent similar syntax request. Up until this week I had been able to pretty well navigate syntax required by the various pro tools. However, I am struggling with applying symbology from a manually classified .lyr file to a feature layer. Through the sdk I am attempting to use the tool “ApplySymbologyFromLayer_management”. 

I have successfully used this tool in code to apply symbology to a raster layer using the following:

var gpresult = await Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", new string[] { rasterLayer.Name, strPathToLyrFile });

I have interactively used the tool in the Pro session without issue for the feature layer.  I noticed that when I specify the target .lyr file interactively, the Type field, the Source Field, and the Target Field are automatically assigned to the correct values. (Displayed in Attachment).  This initially led me to believe that I might not need to provide the symbology field arguments. However, when I used the same approach for the feature layer with the classified .lyr file, I received the error:

      {<msg code="309" type="100">ERROR 000309: The value is not a field
      ERROR 000309: The value is not a field</msg>}

 

I have looked at various documentation pages such as:

http://pro.arcgis.com/en/pro-app/tool-reference/data-management/apply-symbology-from-layer.htm, which provides python syntax examples.   Based off the help page, I started by building this:

string strTestArgs = "[VALUE_FIELD, gridcode, gridcode]";

var gpresult = await Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", new string[] { rasterLayer.Name, strPathToLyrFile, strTestArgs });

I have tried numerous sytax versions of the argument string to pass to the tool in code but can’t seem to find the proper format.  All the versions of the argument string I have passed produced either:

{<msg code="309" type="100">ERROR 000309: The value is not a field

or

{<msg code="800" type="100">ERROR 000800: The value is not a member of VALUE_FIELD | NORMALIZATION_FIELD | EXCLUSION_CLAUSE_FIELD | CHART_RENDERER_PIE_SIZE_FIELD | ROTATION_XEXPRESSION_FIELD | ROTATION_YEXPRESSION_FIELD | ROTATION_ZEXPRESSION_FIELD | TRANSPARENCY_EXPRESSION_FIELD | TRANSPARENCY_NORMALIZATION_FIELD | SIZE_EXPRESSION_FIELD | COLOR_EXPRESSION_FIELD | PRIMITIVE_OVERRIDE_EXPRESSION_FIELD.
ERROR 000309: The value is not a field

Any assistance of how to construct the string for the symbology field arguments would be appreciated.

 

 

 

 

Tags (1)
0 Kudos
15 Replies
GKmieliauskas
Esri Regular Contributor

Hi Brian,

Look at that issue:

Pro 2.0 Bug "Apply Symbology From Layer" GeoProcessing Tool 

At the end of the issue you will found solution with GP flag GPThread. If it will not work I will research deeper.

0 Kudos
BrianBulla
Occasional Contributor III

Hi Giantautas,

I saw that thread yesterday, but the suggested .GPThread fix did not work for me.  Still no errors, but nothing happens in the map.

GPExecuteToolFlags flags = GPExecuteToolFlags.GPThread;

foreach (Layer layer in map.GetLayersAsFlattenedList().OfType<FeatureLayer>())
                {
                    var gpresult = await Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", new string[] { layer.Name.ToString(), @"K:\DSM Shared\ArcMap Symbology\10.2 Schema\Editing Symbols\" + layer.Name.ToString() + ".lyr" }, null, null, flags);                    
                }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've tried using .lyr and .lyrx files, but nothing seems to work.

I've also tried the Python command line "ApplySymbologyFromLayer_management", but still no luck.  When run from python I can see that the layer gets 'locked' in the TOC, but the symbology never actually changes.

If you want me to try anything else, let me know.  Is this a known issue??

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi Brian,

For GP parameters I use Geoprocessing.MakeValueArray method. It returns IReadOnlyList<string> value. Maybe simple string[] is not valid for geoprocessing?

Have you tried to put your code inside try/catch and check for exceptions?

0 Kudos
BrianBulla
Occasional Contributor III

Yes, I have tried .MakeValueArray too.  I see some action on the screen (ie. a flicker) but still nothing happens.

It is all in a Try-Catch, but no exceptions are thrown.

0 Kudos
BrianBulla
Occasional Contributor III

This is what I got....the commented out code is different stuff I have been trying.

        async private void btnSymbolize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //QueuedTask.Run(() =>
                //{
                //    Uri myUri = new Uri(@"K:\DSM Shared\ArcMap Symbology\10.2 Schema\Editing Symbols\GISWRKS1.WORKS.WAT_Hydrant.lyr");
                //    Layer newLayer = LayerFactory.Instance.CreateFeatureLayer(myUri, MapView.Active.Map, LayerPosition.AddToTop);
                //}
                //);

                //var parameters = Geoprocessing.MakeValueArray("GISWRKS1.WORKS.WAT_Hydrant.lyr", @"K:\DSM Shared\ArcMap Symbology\TEST\Editing Symbols\GISWRKS1.WORKS.WAT_Hydrant.lyrx");

                //var gpresult = await Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", new string[] { "GISWRKS1.WORKS.WAT_Hydrant", @"K:\DSM Shared\ArcMap Symbology\10.2 Schema\Editing Symbols\GISWRKS1.WORKS.WAT_Hydrant.lyr" });

                Map map = MapView.Active.Map;

                pBar.Minimum = 0;
                pBar.Maximum = map.Layers.Count;

                this.Cursor = Cursors.Wait;

                GPExecuteToolFlags flags = GPExecuteToolFlags.GPThread;

                foreach (Layer layer in map.GetLayersAsFlattenedList().OfType<FeatureLayer>())
                {   
                    var gpresult = await Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", new string[] { layer.Name.ToString(), @"K:\DSM Shared\ArcMap Symbology\10.2 Schema\Editing Symbols\" + layer.Name.ToString() + ".lyr" }, null, null, flags);
                    pBar.Value = pBar.Value + 1;
                }
            }

            catch  (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString());
            }
            
            finally
            {
                this.Cursor = Cursors.Arrow;
                this.Close();
            }
            
        }
0 Kudos
GKmieliauskas
Esri Regular Contributor

For GP process logging I use GPExecuteToolFlags.AddToHistory flag. After executing geoprocessing task I can check the status. In ArcGIS Pro 2.3 Beta there is a flag GPExecuteToolFlags.RefreshProjectItems.

I load data to my project to, but I use alternative technique. I create feature layer from lyr file and then update source. I repeat these steps for each layer. You can find more on:
Pro 2.0 Bug "Apply Symbology From Layer" GeoProcessing Tool 

Your way did not work for me as I remember. I needed to go to layer properties, clear cache and set Don't cache any data locally. Then ArcGIS Pro refreshed layer with applied symbology.

0 Kudos