Select to view content in your preferred language

Adding First Layer to Active Map Changes the Map's Extent

885
5
03-25-2024 01:13 PM
Ed-Conrad
Occasional Contributor

When I create and add a layer to a map, the map view's extent changes to the layer's extent if it is the first layer added to the Table of Contents (the first layer does not count the basemap).  However, if I do the same thing but the created/added layer is not the first, the map view's extent stays the same, which is what I want.

The ArcGIS.Desktop.Mapping.LayerCreationParams class has the AutoZoomOnEmptyMap property, which details exactly what I want, but I cannot get it to work.  What is going on?


Here is the applicable code:

 

// I tried FeatureLayerCreationParams too, which also didn't work, but I need the more general case.

LayerCreationParams lyrParams = new(uri)  // uri is a path to a .lyrx file
{
     AutoZoomOnEmptyMap = false   
};

LayerFactory.Instance.CreateLayer<Layer>(lyrParams, MapView.Active.Map);

 

 

 I have ArcGIS Pro 3.1.2 installed and am using the ArcGIS Pro 3.1 SDK.

0 Kudos
5 Replies
Ed-Conrad
Occasional Contributor

-

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

Have you tried to set AutoZoomOnEmptyMap outside LayerCreationParams constructor. Sometimes it could help. Like in code below:

LayerCreationParams lyrParams = new(uri);  // uri is a path to a .lyrx file
lyrParams.AutoZoomOnEmptyMap = false   
LayerFactory.Instance.CreateLayer<Layer>(lyrParams, MapView.Active.Map);

 

0 Kudos
Ed-Conrad
Occasional Contributor

The LayerCreationParams constructor works as expected (see it change in debugger) and updates the various fields that are past to it.  Setting it outside the constructor also works (see it change in debugger), but again setting

lyrParams.AutoZoomOnEmptyMap = false

 doesn't achieve the effect it's supposed to.  Other properties past through the constructor or to the properties directly do achieve the documented effect such as IsVisible, MapMemberIndex, and MapMemberPosition).  I've tried AutoZoomOnEmptyMap by itself and with the other properties.

0 Kudos
NarelleChedzey
Esri Contributor

Hi Ed, 

I can duplicate your problem with the AutoZoomOnEmpty property when you specify a lyrx file uri to the LayerCreationParams.    

The assumption with this AutoZoomOnEmpty property is that there is only a single data source (and consequently only a single layer) being added to the map.   Because you are specifying a lyrx file it is possible that there is more than one data source and layer being added .   The code and AutoZoomOnEmpty property works as expected if the LayerCreationParams uri points to a single data source (such as a feature class or table in a geodatabase). 

Having said that, if you know that your lyrx file contains a single layer then the following is a possible workaround if you still wish to use a lyrx file. 

 

  var layerdoc = new LayerDocument(path_to_lyrx_file);
  var cimLayerDoc = layerdoc.GetCIMLayerDocument();

  var layerDefs = cimLayerDoc.LayerDefinitions;
  var layerDef = layerDefs[0];
  var dataConnection = (layerDef as CIMFeatureLayer).FeatureTable.DataConnection;


   LayerCreationParams lyrParams = new(dataConnection);
   lyrParams.AutoZoomOnEmptyMap = false;

   LayerFactory.Instance.CreateLayer<Layer>(lyrParams, map);

 

I'll add an issue into our backlog to examine updating the code to take a lyrx file uri into account.

Narelle

0 Kudos
Ed-Conrad
Occasional Contributor

Hi Narelle,

Thank you for the response and explanation. I tried the code you provided and do see that the AutoZoomOnEmpty property works as expected when it's a single data source (i.e., feature class or table).

Unfortunately, since the workaround references the underlying feature class, bypassing all the capabilities that a layer file provides (e.g., symbology, definition queries, etc.), it creates/adds the layer as it would from a feature class/table with random symbology, the name of the feature class as seen in the GDB and definition queries removed (attachments), defeating the purpose of using a layer file.

Please consider lyrx files being supported with the AutoZoomOnEmpty property. Adding a lyrx file when there are already other layers in the Table of Contents besides the basemap works wonderfully.  It's just the edge case when the created/added layer is the very first one where the behavior isn't awesome.  The other properties one could possibly set on a layer file directly behave nicely - it's just AutoZoomOnEmpty of those that I've tried that does not.  For instance,

Uri uri = new(layerPath);
LayerCreationParams lyrParams = new(uri)
{
   IsVisible = false,
   MapMemberIndex = tocIndex,
   MapMemberPosition = MapMemberPosition.Index,
   AutoZoomOnEmptyMap = false   // won't work this way
};
LayerFactory.Instance.CreateLayer<Layer>(lyrParams, MapView.Active.Map);

 

Attachment Description:

  1.  Example_of_various_lyrx_referencing_same_feature_class.png
    A view of the table of contents where lyrx files were added as layer files.  Those 3 refer to the 3 with the checkboxes being checked getting to be re-added.

 

 

Uri uri = new(layerPath);
LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map, index: tocIndex);

 

     2.  Checked_layers_added_after_new_code.png

The AutoZoomOnEmpty property works when referencing the layer's underlying feature class. However, when they are added, adding them to the map bypasses the layer file completely so symbology, layer naming and definition queries are lost.

 

 

 

LayerDocument layerdoc = new(layerPath);
CIMLayerDocument cimLayerDoc = layerdoc.GetCIMLayerDocument();
CIMDefinition[] layerDefs = cimLayerDoc.LayerDefinitions;
CIMDefinition layerDef = layerDefs[0];
CIMDataConnection? dataConnection = (layerDef as CIMFeatureLayer)?.FeatureTable.DataConnection;
LayerCreationParams lyrParams = new(dataConnection)
{
	MapMemberIndex = tocIndex,
        MapMemberPosition = MapMemberPosition.Index,
        AutoZoomOnEmptyMap = false
};
LayerFactory.Instance.CreateLayer<Layer>(lyrParams, MapView.Active.Map);

 

 

0 Kudos