Selection to Feature

473
3
04-25-2022 12:59 AM
DavidMrázek
Occasional Contributor II

Good day,
is there any way to make Selection a Feature? It may be primitive, but I'm not sure.

var gsSelection = lyrList.GetSelection();
                    var polyFeature2 = gsSelection as Feature;//here is the error: Cannot convert type 'ArcGIS.Core.Data.Selection' to 'ArcGIS.Core.Data.Feature' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion	
                    var polygonGeometry2 = polyFeature2.GetShape() as Polygon;
                    var mapPoint1 = GeometryEngine.Instance.Centroid(polygonGeometry2);

 

Thank you

David

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

You need to get cursor from selection:

                    using (var rowCursor = gsSelection.Search(null))
                    {
                        while (rowCursor.MoveNext())
                        {
                            using (var polyFeature2 = rowCursor.Current as Feature)
                            {
                                // Your code here
                            }
                        }
                    }
0 Kudos
DavidMrázek
Occasional Contributor II

And is it possible to use while-LOOP for only one pass?

0 Kudos
GKmieliauskas
Esri Regular Contributor

I don't know what you exactly mean "use while-LOOP for only one pass". If you want to get only one feature, you can change "while" to "if". If you want to go through selection few times, so you need each time make new search. Cursor works one time only

0 Kudos