Hi,
What is the best way to determine if a layer exists or not? I have some code that runs on a specific layer, but I don't want Arc to crash if the layer doesn't exist when the user presses the button.
Can anyone suggest a check to go through before the code below runs that will check for the layers existence? I would think evaluating MapView.Active.Map.FindLayers("GISWRKS1.WORKS.VLS_Points") to a boolean would work, but I can't seem to get it to.
var vlsLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.VLS_Points").First() as BasicFeatureLayer;
var selection = vlsLayer.GetSelection();
IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs(); //save the selected OBJECTIDs to a list.
Thanks!
Solved! Go to Solution.
Hi,
These are a few ways to check for a layer:
//Finds a layer using a URI.
//The Layer URI you pass in helps you search for a specific layer in a map
var lyrFindLayer = MapView.Active.Map.FindLayer("CIMPATH=map/u_s__states__generalized_.xml");
//This returns a collection of layers of the "name" specified. You can use any Linq expression to query the collection.
var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Any(f => f.Name == "U.S. States (Generalized)");
//Finds layers by name and returns a read only list of Layers
var lyr = MapView.Active.Map.FindLayers("U.S. States (Generalized)").FirstOrDefault();
Thanks
Uma
Hi,
For anyone interested, this seems to work. If you know of another way, please post it below.
IEnumerable<Layer> layerCheck = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.VLS_Points", StringComparison.CurrentCultureIgnoreCase) >= 0);
int count = layerCheck.OrderBy(l => l).Count();
if (count < 1)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Please add the VLS Layer to the map.", "Missing Layer");
return;
}
Hi,
These are a few ways to check for a layer:
//Finds a layer using a URI.
//The Layer URI you pass in helps you search for a specific layer in a map
var lyrFindLayer = MapView.Active.Map.FindLayer("CIMPATH=map/u_s__states__generalized_.xml");
//This returns a collection of layers of the "name" specified. You can use any Linq expression to query the collection.
var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Any(f => f.Name == "U.S. States (Generalized)");
//Finds layers by name and returns a read only list of Layers
var lyr = MapView.Active.Map.FindLayers("U.S. States (Generalized)").FirstOrDefault();
Thanks
Uma