Beispiel #1
0
        private Task <bool> createFunnyGarden(Geometry geometry)
        {
            if (geometry == null)
            {
                return(Task.FromResult(false));
            }

            // get the point layer from the active map
            var pointLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType <FeatureLayer>()
                             .Where(lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPoint).FirstOrDefault();

            if (pointLayer == null)
            {
                return(Task.FromResult(false));
            }

            // get the polygon layer from the active map
            var polygonLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType <FeatureLayer>()
                               .Where(lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPolygon).FirstOrDefault();

            if (polygonLayer == null)
            {
                return(Task.FromResult(false));
            }

            // Create an edit operation
            var createOperation = new EditOperation();

            createOperation.Name = string.Format("Create green");

            // we'll use the sketch geometry to create a spatial filter
            SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter();

            spatialQueryFilter.FilterGeometry      = geometry;
            spatialQueryFilter.SpatialRelationship = SpatialRelationship.Contains;

            // retrieve the point features that are inside the sketch geometry
            var searchCursor = pointLayer.Search(spatialQueryFilter);

            // Construct a polygon placeholder for the point buffers
            var treeBuffers = PolygonBuilder.CreatePolygon(geometry.SpatialReference);

            // TODO
            // 1. for each point geometry
            //     2. buffer the geometry  (Hint : use the MapView.Active.Extent.Width/50 as the buffer distance)
            //     3. union this result with the existing treeBuffers
            // 4. subtract the collection of buffers from the sketch geometry
            // HINT: use the GeometryEngine to buffer, union and subtract (difference) the geometries
            // Polygon greenGeometry = null;

            // for each found feature
            while (searchCursor.MoveNext())
            {
                // retrieve the geometry
                var treeFeature = searchCursor.Current as Feature;
                var treePoint   = treeFeature.GetShape() as MapPoint;

                // buffer the the location
                var treeBuffer = GeometryEngine.Buffer(treePoint, MapView.Active.Extent.Width / 50);

                // and union the new buffer to the existing buffer polygons
                treeBuffers = GeometryEngine.Union(treeBuffer, treeBuffers) as Polygon;
            }

            // ensure the sketch geometry is simple
            if (!GeometryEngine.IsSimpleAsFeature(geometry, true))
            {
                geometry = GeometryEngine.SimplifyAsFeature(geometry);
            }

            // construct the difference geometry between the sketch geometry and the buffered point polygons
            var greenGeometry = GeometryEngine.Difference(geometry, treeBuffers);

            // cue the create
            createOperation.Create(polygonLayer, greenGeometry);

            // execute the operation
            return(createOperation.ExecuteAsync());
        }