Clip geometry

View inMAUIUWPWPFWinUIView on GitHub

Clip a geometry with another geometry.

Image of clip geometry

Use case

Create a new set of geometries for analysis (e.g. displaying buffer zones around abandoned coal mine shafts in an area of planned urban development) by clipping intersecting geometries.

How to use the sample

Tap the "Clip" button to clip the blue graphic with the red dashed envelopes.

How it works

  1. Use the static extension method GeometryEngine.Clip() to generate a clipped Geometry, passing in an existing Geometry and an Envelope as parameters. The existing geometry will be clipped where it intersects an envelope.
  2. Create a new Graphic from the clipped geometry and add it to a GraphicsOverlay on the MapView.

Relevant API

  • Envelope
  • Geometry
  • GeometryEngine.Clip
  • Graphic
  • GraphicsOverlay

Additional information

Note: the resulting geometry may be null if the envelope does not intersect the geometry being clipped.

Tags

analysis, clip, geometry

Sample Code

ClipGeometry.xaml.csClipGeometry.xaml.csClipGeometry.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;

namespace ArcGIS.Samples.ClipGeometry
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Clip geometry",
        category: "Geometry",
        description: "Clip a geometry with another geometry.",
        instructions: "Tap the \"Clip\" button to clip the blue graphic with the red dashed envelopes.",
        tags: new[] { "analysis", "clip", "geometry" })]
    public partial class ClipGeometry : ContentPage
    {
        // Graphics overlay to display input geometries for the clip operation.
        private GraphicsOverlay _inputGeometriesGraphicsOverlay;

        // Graphics overlay to display the resulting geometries from the three GeometryEngine.Clip operations.
        private GraphicsOverlay _clipAreasGraphicsOverlay;

        // Graphics to represent geometry involved in the clip operation.
        private Graphic _coloradoGraphic;
        private Graphic _outsideGraphic;
        private Graphic _containedGraphic;
        private Graphic _intersectingGraphic;

        private bool _clipped;

        public ClipGeometry()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            // Create a graphics overlay to hold the input geometries for the clip operation.
            _inputGeometriesGraphicsOverlay = new GraphicsOverlay();

            // Add the input geometries graphics overlay to the MapView.
            MyMapView.GraphicsOverlays.Add(_inputGeometriesGraphicsOverlay);

            // Create a graphics overlay to hold the resulting geometries from the three GeometryEngine.Clip operations.
            _clipAreasGraphicsOverlay = new GraphicsOverlay();

            // Add the resulting geometries graphics overlay to the MapView.
            MyMapView.GraphicsOverlays.Add(_clipAreasGraphicsOverlay);

            // Create a simple line symbol for the 1st parameter of the GeometryEngine.Clip operation - it follows the
            // boundary of Colorado.
            SimpleLineSymbol coloradoSimpleLineSymbol = new SimpleLineSymbol(
                SimpleLineSymbolStyle.Solid, System.Drawing.Color.Blue, 4);

            // Create the color that will be used as the fill for the Colorado graphic. It will be a semi-transparent, blue color.
            System.Drawing.Color coloradoFillColor = System.Drawing.Color.FromArgb(34, 0, 0, 255);

            // Create the simple fill symbol for the Colorado graphic - comprised of a fill style, fill color and outline.
            SimpleFillSymbol coloradoSimpleFillSymbol = new SimpleFillSymbol(
                SimpleFillSymbolStyle.Solid, coloradoFillColor, coloradoSimpleLineSymbol);

            // Create the geometry of the Colorado graphic.
            Envelope colorado = new Envelope(
                new MapPoint(-11362327.128340, 5012861.290274, SpatialReferences.WebMercator),
                new MapPoint(-12138232.018408, 4441198.773776, SpatialReferences.WebMercator));

            // Create the graphic for Colorado - comprised of a polygon shape and fill symbol.
            _coloradoGraphic = new Graphic(colorado, coloradoSimpleFillSymbol);

            // Add the Colorado graphic to the input geometries graphics overlay collection.
            _inputGeometriesGraphicsOverlay.Graphics.Add(_coloradoGraphic);

            // Create a simple line symbol for the three different clip geometries.
            SimpleLineSymbol clipGeomtriesSimpleLineSymbol = new SimpleLineSymbol(
                SimpleLineSymbolStyle.Dot, System.Drawing.Color.Red, 5);

            // Create an envelope outside Colorado.
            Envelope outsideEnvelope = new Envelope(
                new MapPoint(-11858344.321294, 5147942.225174, SpatialReferences.WebMercator),
                new MapPoint(-12201990.219681, 5297071.577304, SpatialReferences.WebMercator));

            // Create the graphic for an envelope outside Colorado - comprised of a polyline shape and line symbol.
            _outsideGraphic = new Graphic(outsideEnvelope, clipGeomtriesSimpleLineSymbol);

            // Add the envelope outside Colorado graphic to the graphics overlay collection.
            _inputGeometriesGraphicsOverlay.Graphics.Add(_outsideGraphic);

            // Create an envelope intersecting Colorado.
            Envelope intersectingEnvelope = new Envelope(
                new MapPoint(-11962086.479298, 4566553.881363, SpatialReferences.WebMercator),
                new MapPoint(-12260345.183558, 4332053.378376, SpatialReferences.WebMercator));

            // Create the graphic for an envelope intersecting Colorado - comprised of a polyline shape and line symbol.
            _intersectingGraphic = new Graphic(intersectingEnvelope, clipGeomtriesSimpleLineSymbol);

            // Add the envelope intersecting Colorado graphic to the graphics overlay collection.
            _inputGeometriesGraphicsOverlay.Graphics.Add(_intersectingGraphic);

            // Create a envelope inside Colorado.
            Envelope containedEnvelope = new Envelope(
                new MapPoint(-11655182.595204, 4741618.772994, SpatialReferences.WebMercator),
                new MapPoint(-11431488.567009, 4593570.068343, SpatialReferences.WebMercator));

            // Create the graphic for an envelope inside Colorado - comprised of a polyline shape and line symbol.
            _containedGraphic = new Graphic(containedEnvelope, clipGeomtriesSimpleLineSymbol);

            // Add the envelope inside Colorado graphic to the graphics overlay collection.
            _inputGeometriesGraphicsOverlay.Graphics.Add(_containedGraphic);

            // Get the extent of all of the graphics in the graphics overlay.
            Geometry visibleExtent = GeometryEngine.CombineExtents(_inputGeometriesGraphicsOverlay.Graphics.Select(graphic => graphic.Geometry));

            // Create the map for the MapView.
            MyMapView.Map = new Map(BasemapStyle.ArcGISTopographic) { InitialViewpoint = new Viewpoint(visibleExtent) };
        }

        private void ClipButton_Clicked(object sender, EventArgs e)
        {
            if (!_clipped)
            {
                ClipGraphics();
                _clipped = true;
            }
            else
            {
                Reset();
                _clipped = false;
            }

            ClipButton.Text = _clipped ? "Reset" : "Clip";
        }

        private void ClipGraphics()
        {
            // Remove the Colorado graphic from the input geometries graphics overlay collection. That way it will be easier
            // to see the clip versions of the GeometryEngine.Clip operation.
            _inputGeometriesGraphicsOverlay.Graphics.Remove(_coloradoGraphic);

            // Loop through each graphic in the input geometries for the clip operation.
            foreach (Graphic graphic in _inputGeometriesGraphicsOverlay.Graphics)
            {
                // Perform the clip operation. The first parameter of the clip operation will always be the Colorado graphic.
                // The second parameter of the clip operation will be one of the 3 different clip geometries (_outsideGraphic,
                // _containedGraphic, or _intersectingGraphic).
                Geometry myGeometry = _coloradoGraphic.Geometry.Clip((Envelope)graphic.Geometry);

                // Only work on returned geometries that are not null.
                if (myGeometry != null)
                {
                    // Create the graphic as a result of the clip operation using the same symbology that was defined for
                    // the _coloradoGraphic defined in the Initialize() method previously.
                    Graphic clippedGraphic = new Graphic(myGeometry, _coloradoGraphic.Symbol);

                    // Add the clipped graphic to the clip areas graphics overlay collection.
                    _clipAreasGraphicsOverlay.Graphics.Add(clippedGraphic);
                }
            }
        }

        private void Reset()
        {
            // Add the colorado graphic.
            _inputGeometriesGraphicsOverlay.Graphics.Add(_coloradoGraphic);

            // Remove the clip graphics.
            _clipAreasGraphicsOverlay.Graphics.Clear();
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.