Intersect For Each loop fails after 1 run

279
0
02-21-2022 02:57 PM
MapVis
by
New Contributor II

I have a button that looks through the active map for a layer group named "LinearRoute" and then gets a feature layer from the route within that group, then it looks for a second layer group called "Crossings" and attempts to intersect the route with each of these layers based on if its a polyline or polygon feature. This is working for the first feature run but the second time through the foreach loop after running intersect it isn't getting the updated feature list from MapView.Active.Map Should I be running this in a different way to ensure I get the results layer from each pass through the Geoprocessing.ExecuteToolAsync loop so my Count doesn't end up null?

 

protected override async void OnClick()
        {
            var project = Project.Current;
            var projGDBPath = Project.Current.DefaultGeodatabasePath;
            Map map = MapView.Active.Map;

            //get group of route feature then Route featuer layer
            map.GetLayersAsFlattenedList().OfType<GroupLayer>().ToList();
            map.GetLayersAsFlattenedList().OfType<GroupLayer>().Where(l => l.Parent.ToString() == "LinearRoute");
            var lyrRoute = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Parent.ToString() == "LinearRoute" && l.IsVisible == true).FirstOrDefault();
            //null check for a feature in the route feature in the linear route group
            if (lyrRoute is null)
            {
                //SendMsg("Intersect", "No Route Layer found in Routes Layer Group");

            }

            else
            {
                //check for crossings group layer
                var lyrCrossings = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Parent.ToString() == "Crossings" && l.IsVisible == true).ToList();
                
                if (lyrCrossings is null)
                {
                    //SendMsg("Intersect", "No Layers found in Crossings Layer Group");

                }
                else
                {
                    //for each feature in crossings group run intersect based on if its a polyline or polygon crossing. 
                    foreach (var lyr in lyrCrossings)
                    {
                        if (lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPolyline)
                        {
                            
                            Map mapNew = MapView.Active.Map;
                            string outputPath = Path.Combine(projGDBPath, lyr.Name + "_Crossing_" + lyrRoute.Name);
                            string resultLyrName = lyr.Name + "_Crossing_" + lyrRoute.Name;
                            List<string> inputList = new List<string>();
                            inputList.Add(lyrRoute.Name);
                            inputList.Add(lyr.Name);
                            var args = Geoprocessing.MakeValueArray(inputList, outputPath, "", "", "POINT");
                            string toolPath = "analysis.Intersect";
                            IGPResult result = await Geoprocessing.ExecuteToolAsync(toolPath, args);

                            //update mapView after geoproccesing tool is run to get newly added results layers
                            mapNew = MapView.Active.Map;

                            int nCount = 0;
                            //GetLayerAsFlattenedList works for the first intersect layer but teh second pass through the foreach loop comes up null
                            FeatureLayer fl1 = mapNew.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Equals(resultLyrName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                            Debug.WriteLine(resultLyrName);
                            await QueuedTask.Run(() =>
                            {
                                FeatureClass featureClass = fl1.GetFeatureClass();
                                nCount = featureClass.GetCount();

                            });

                            FrameworkApplication.AddNotification(new Notification()
                            {
                                Title = "MSG",
                                Message = "Feature Count Is: " + nCount.ToString(), 

                            });


                        }
                        else if (lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPolygon)
                        {

                            //same principal but Geoprocessing.MakeValueArray(inputList, outputPath, "", "", "LINE");
                            

                        }
                        else
                        {
                            //SendMsg("Intersect", "No polyline or polygon crossing layers found");
                        }


                    }





                }

            }



        }

 

 

0 Kudos
0 Replies