Polygon creation tool

516
2
11-11-2021 10:37 PM
DavidMrázek
Occasional Contributor II

Hello,

I would like to create a tool that finds a hole in a polygon and then loads it into a new layer as a new polygon. Has anyone had a similar project? Somehow I don't know how to start.

With the code written below, I can create a polygon in which the holes are located, but I don't know how to write them to create those holes.

I will be happy for any advice, idea or solution.

 

 protected override async void OnClick()
        {
            var polylineFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(lyr => lyr.ShapeType == esriGeometryType.esriGeometryPolyline);

            if (polylineFeatureLayer == null)
                return;
            await CreateFcWithAttributesAsync("Polygon", EnumFeatureClassType.POLYGON);

            var polygonFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(lyr => lyr.ShapeType == esriGeometryType.esriGeometryPolygon);

            if (polygonFeatureLayer == null)
                return;

            await ConstructSamplePolygon(polygonFeatureLayer, polylineFeatureLayer);
        }
        private Task<bool> ConstructSamplePolygon(FeatureLayer polygonLayer, FeatureLayer lineLayer)
        {

            return QueuedTask.Run(() =>
            {
                var polygonFeatureClass = polygonLayer.GetTable() as FeatureClass;
                var polygonDefinition = polygonFeatureClass.GetDefinition();
                var lineFeatureClass = lineLayer.GetTable() as FeatureClass;

                var lineCursor = lineFeatureClass.Search(null, false);

                var createOperation = new EditOperation()
                {
                    Name = "Vytvořit polygon",
                    SelectNewFeatures = false
                };

                while (lineCursor.MoveNext())
                {
                    using (var lineFeature = lineCursor.Current as Feature)
                    {
                        var polylineGeometry = lineFeature.GetShape() as Polyline;
                        ReadOnlyPointCollection pts = polylineGeometry.Points;
                        // var polygonBuilder = PolygonBuilder.CreatePolygon(pts, polygonDefinition.GetSpatialReference());
                        List<MapPoint> lineMapPoints = new List<MapPoint>();
                        lineMapPoints.AddRange(pts);
                        var newPolygon =
                            PolygonBuilder.CreatePolygon(lineMapPoints, polygonDefinition.GetSpatialReference());
                        createOperation.Create(polygonLayer, newPolygon);
                    }
                }

                return createOperation.ExecuteAsync();
            });
        }

 

thank you
David

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

A quick tour of creating tools with Python—ArcGIS Pro | Documentation

holes will make the shape multipart

Polygon—ArcGIS Pro | Documentation

Read geometries—ArcGIS Pro | Documentation

if you find holes, then the order of the points needs to be reversed


... sort of retired...
0 Kudos
DavidMrázek
Occasional Contributor II

Thank you. Creating a polygon is no problem.

0 Kudos