Determine if a layer was created using "Make Layer From Selected Features"

711
7
Jump to solution
12-15-2023 09:58 PM
DaveFullerton
Occasional Contributor III

Is there anything in the API that can be used to determine (return a bool) if the "layer comprises a subset of features" as scene on the Selection Tab of the layer's properties here: 

DaveFullerton_0-1702705987682.png

 

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Sorry my bad, i didn't copy all code needed:

protected override async void OnClick()
{
	try
	{
		//get selected layer in toc
		var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
		if (featLayer == null)
			return;
		CIMBaseLayer baseLayer = await QueuedTask.Run (() => featLayer.GetDefinition());
		var definitionSetURI = GetDefinitionSetURI(baseLayer);
		var isSelectionLayer = !string.IsNullOrEmpty(definitionSetURI);
		MessageBox.Show($@"Is selection layer: {isSelectionLayer}");
	}
	catch (Exception ex)
	{
		MessageBox.Show($@"Error: {ex.Message}\n{ex.StackTrace}");
	}
}

internal static string GetDefinitionSetURI(CIMBaseLayer baseLayer)
{
	if (baseLayer is CIMGeoFeatureLayerBase geoFeatureLayerBase)
		return (geoFeatureLayerBase.FeatureTable?.DefinitionSetURI);
	else if (baseLayer is CIMKMLLayer kmlLayer)
		return (null);
	else if (baseLayer is CIMAnnotationLayer annotationLayer)
		return (annotationLayer.FeatureTable?.DefinitionSetURI);
	else if (baseLayer is CIMDimensionLayer dimensionLayer)
		return (dimensionLayer.FeatureTable?.DefinitionSetURI);

	return (null);
}

View solution in original post

7 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This worked for me:

protected override async void OnClick()
{
	try
	{
		//get selected layer in toc
		var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
		if (featLayer == null)
			return;
		CIMBaseLayer baseLayer = await QueuedTask.Run (() => featLayer.GetDefinition());
		var definitionSetURI = GetDefinitionSetURI(baseLayer);
		var isSelectionLayer = !string.IsNullOrEmpty(definitionSetURI);
		MessageBox.Show($@"Is selection layer: {isSelectionLayer}");
	}
	catch (Exception ex)
	{
		MessageBox.Show($@"Error: {ex.Message}\n{ex.StackTrace}");
	}
}
0 Kudos
DaveFullerton
Occasional Contributor III

Sorry to take so long to respond, @Wolf .  I think I kind of get the general idea here of what you are doing, but the call to GetDefintionSetURI does not work for me.  That method does not appear to be in the API documentation:

https://pro.arcgis.com/search/?collection=help&product=arcgis-pro-net&version=pro3.2&language=en&q=G...

Is it a method that you created as part of your solution?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Sorry my bad, i didn't copy all code needed:

protected override async void OnClick()
{
	try
	{
		//get selected layer in toc
		var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
		if (featLayer == null)
			return;
		CIMBaseLayer baseLayer = await QueuedTask.Run (() => featLayer.GetDefinition());
		var definitionSetURI = GetDefinitionSetURI(baseLayer);
		var isSelectionLayer = !string.IsNullOrEmpty(definitionSetURI);
		MessageBox.Show($@"Is selection layer: {isSelectionLayer}");
	}
	catch (Exception ex)
	{
		MessageBox.Show($@"Error: {ex.Message}\n{ex.StackTrace}");
	}
}

internal static string GetDefinitionSetURI(CIMBaseLayer baseLayer)
{
	if (baseLayer is CIMGeoFeatureLayerBase geoFeatureLayerBase)
		return (geoFeatureLayerBase.FeatureTable?.DefinitionSetURI);
	else if (baseLayer is CIMKMLLayer kmlLayer)
		return (null);
	else if (baseLayer is CIMAnnotationLayer annotationLayer)
		return (annotationLayer.FeatureTable?.DefinitionSetURI);
	else if (baseLayer is CIMDimensionLayer dimensionLayer)
		return (dimensionLayer.FeatureTable?.DefinitionSetURI);

	return (null);
}
DaveFullerton
Occasional Contributor III

Thanks, Wolf.  This does appear to work.   Since I may have to maintain the code for my application in the future, I would like to fully understand what is going on here.

It seems to me that the underlying idea is that if a feature layer was created using "Make Layer From Selected Features" it's table will have a DefinitionSetURI.  Otherwise it won't.  Let me know if I am off the mark.  

There isn't much about the DefintionSetURI property in the API documentation (or elsewhere):

https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic16838.html

This is an example: CIMPATH=ObjectIdentifierSet/c940ab464ae672ac39402d5a13034ab1.dat

Looks like there is a data file somewhere.  I couldn't find it on my C drive.  Maybe it is in volatile memory? 

I am also wondering why you specifically check CIMKMLLayer and return null.  It seems to me that it would return null at line 31 of your code if you didn't include that.

	else if (baseLayer is CIMKMLLayer kmlLayer)
		return (null);

 

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can see the DefinitionSetURI as the key to the QueryDefinition that defines your selection set that was used to create your layer (created using "Make Layer From Selected Features").  So in essence if you don't have a selection set then you don't have a layer created using "Make Layer From Selected Features".  And the DefinitionSetURI is persisted (so it's not in memory), because you can close and reopen your project and see the same feature selection.
I don't have an answer on the KML layer, maybe KML layers can't store a 'selection set'.   I noticed that Pro developers added the CIMKMLLayer condition to their code, so I used copy/paste inheritance.    If you have some KML data you can try to create a layer from selection. 

0 Kudos
DaveFullerton
Occasional Contributor III

@Wolf 

It appears that you cannot even select features in a KML layer, much less create a layer from a selection.  Perhaps there is an intention to add these functionalities in the future.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

That is what i suspected.  It appears that the KML underlying data model has some limitations that restrict it's usage.  ArcGIS Pro documentation shows a bit more detail about this and suggests a workaround to overcome those limitations:

What is KML?—ArcGIS Pro | Documentation

"KML elements can include information that can be viewed in pop-ups, but there is no provision for attributes for KML features. A KML layer does not have an associated attribute table. You cannot select KML features or use them in analysis. If you want to work with this data in the same way as other GIS data, use the KML To Layer tool to convert a KML (or KMZ) file into feature classes in a file geodatabase. This tool also creates a corresponding layer file that reflects the symbology established in the KML file."