Are layers in the map??

91
1
Jump to solution
11-17-2017 06:48 AM
Highlighted
Regular Contributor

I'm trying to determine if specific layers are in the map before proceeding with more code.  The following produces an error at the line where I am checking to the value == null of each layer.  

Is this the best way to do this?  Or is there a better way to check for specific layers in the map??

protected override void OnClick()
{
var gridLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_Grid").FirstOrDefault() as BasicFeatureLayer;
var localMunicipalityLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_LocalMunicipality").FirstOrDefault() as BasicFeatureLayer;
var administrativeAreaLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_AdministrativeArea").FirstOrDefault() as BasicFeatureLayer;
var communitiesLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_Communities").FirstOrDefault() as BasicFeatureLayer;
var depotAreaLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_DepotArea").FirstOrDefault() as BasicFeatureLayer;
var pressureZoneLayer = MapView.Active.Map.FindLayers("GISWRKS1.WORKS.LAND_PressureZone").FirstOrDefault() as BasicFeatureLayer;

****This is where I get the error!!  If I check these one at a time, it works, but when combining with the || is when the error happens.  In ArcMap 10.2, I have similar code running with no problems.****

if ((gridLayer == null) || (localMunicipalityLayer == null) || (administrativeAreaLayer == null) || (communitiesLayer == null) || (depotAreaLayer == null) || (pressureZoneLayer = null))
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Please add the LAND and Water Pressure Zone layers before proceeding.");
return;
}

ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("All the layers are there!!");

}

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor

OK.  It looks like the problem is a C# issue with the || operator and those null values.  Not exactly sure why, but this seems to fix the problem:

Map map = MapView.Active.Map;
IEnumerable<Layer> gridLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_Grid", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> localMunicipalityLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_LocalMunicipality", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> administrativeAreaLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_AdministrativeArea", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> communitiesLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_Communities", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> depotAreaLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_DepotArea", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> pressureZoneLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.WAT_PressureZone", StringComparison.CurrentCultureIgnoreCase) >= 0);

if (gridLayer.Count() == 0 || localMunicipalityLayer.Count() == 0 || administrativeAreaLayer.Count() == 0 || communitiesLayer.Count() == 0 || depotAreaLayer.Count() == 0 || pressureZoneLayer.Count() == 0)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Please add the LAND and Water Pressure Zone layers before proceeding.", "Missing Layers");
return;
}

View solution in original post

Reply
0 Kudos
1 Reply
Highlighted
Regular Contributor

OK.  It looks like the problem is a C# issue with the || operator and those null values.  Not exactly sure why, but this seems to fix the problem:

Map map = MapView.Active.Map;
IEnumerable<Layer> gridLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_Grid", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> localMunicipalityLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_LocalMunicipality", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> administrativeAreaLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_AdministrativeArea", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> communitiesLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_Communities", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> depotAreaLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.LAND_DepotArea", StringComparison.CurrentCultureIgnoreCase) >= 0);
IEnumerable<Layer> pressureZoneLayer = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.WAT_PressureZone", StringComparison.CurrentCultureIgnoreCase) >= 0);

if (gridLayer.Count() == 0 || localMunicipalityLayer.Count() == 0 || administrativeAreaLayer.Count() == 0 || communitiesLayer.Count() == 0 || depotAreaLayer.Count() == 0 || pressureZoneLayer.Count() == 0)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Please add the LAND and Water Pressure Zone layers before proceeding.", "Missing Layers");
return;
}

View solution in original post

Reply
0 Kudos