Clip geometry

View inQMLC++
View on GitHub
Sample viewer app

Clip a geometry with another geometry.

screenshot

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

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

How it works

  1. Use the static 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.cppClipGeometry.cppClipGeometry.hClipGeometry.qml
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
// [WriteFile Name=ClipGeometry, Category=Geometry]
// [Legal]
// Copyright 2018 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "ClipGeometry.h"

#include "Map.h"
#include "MapQuickView.h"
#include "Geometry.h"
#include "Graphic.h"
#include "GeometryEngine.h"
#include "Viewpoint.h"
#include "GraphicsOverlay.h"
#include "SimpleLineSymbol.h"
#include "SimpleFillSymbol.h"

using namespace Esri::ArcGISRuntime;

ClipGeometry::ClipGeometry(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void ClipGeometry::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ClipGeometry>("Esri.Samples", 1, 0, "ClipGeometrySample");
}

void ClipGeometry::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a map using the topographic basemap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);

  // Create a graphics overlays
  m_coloradoOverlay = new GraphicsOverlay(this);
  m_envelopesOverlay = new GraphicsOverlay(this);
  m_clippedAreasOverlay = new GraphicsOverlay(this);

  // Create an envelope to be used as the Colorado geometry
  m_coloradoEnvelope = Envelope(-12138232.018408, 4441198.773776, -11362327.128340, 5012861.290274, SpatialReference(3857));

  // Create an envelope for each clipping area
  m_outsideEnvelope = Envelope(-12201990.219681, 5147942.225174, -11858344.321294, 5297071.577304, SpatialReference(3857));
  m_intersectingEnvelope = Envelope(-12260345.183558, 4332053.378376, -11962086.479298, 4566553.881363, SpatialReference(3857));
  m_insideEnvelope = Envelope(-11655182.595204, 4593570.068343, -11431488.567009, 4741618.772994, SpatialReference(3857));

  // Set initial Viewpoint
  const Viewpoint initialViewpoint(Point(-11655182, 4741618, SpatialReference(3857)), 15151632);
  m_map->setInitialViewpoint(initialViewpoint);

  // Set map to map view and append the two initial graphics overlays
  m_mapView->setMap(m_map);
  m_mapView->graphicsOverlays()->append(m_coloradoOverlay);
  m_mapView->graphicsOverlays()->append(m_envelopesOverlay);

  // Create the graphics
  createGraphics();
}

// Creates the visual elements and adds them to their respective overlays
void ClipGeometry::createGraphics()
{
  // Colorado outline + fill
  m_coloradoOutline = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("#FF0000FF"), 2.0f, this);
  m_coloradoFill = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor("#220000FF"), m_coloradoOutline, this);

  // Create and add the Colorado graphic to its overlay
  m_coloradoGraphic = new Graphic(m_coloradoEnvelope, m_coloradoFill, this);
  m_coloradoOverlay->graphics()->append(m_coloradoGraphic);

  // Clipping envelopes outline
  SimpleLineSymbol* envelopeOutline = new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor("#FFFF0000"), 3.0f, this);

  // Create and add the clipping envelope graphics to their overlay
  m_outsideGraphic = new Graphic(m_outsideEnvelope, envelopeOutline, this);
  m_intersectingGraphic = new Graphic(m_intersectingEnvelope, envelopeOutline, this);
  m_insideGraphic = new Graphic(m_insideEnvelope, envelopeOutline, this);
  m_envelopesOverlay->graphics()->append(m_outsideGraphic);
  m_envelopesOverlay->graphics()->append(m_intersectingGraphic);
  m_envelopesOverlay->graphics()->append(m_insideGraphic);

  // Append the (empty) final graphicsOverlay to the mapView to render. Later on, clipAreas() will populate this with the clipped graphics
  m_mapView->graphicsOverlays()->append(m_clippedAreasOverlay);
}

// Clips the overlapping geometries and displays them on the map
void ClipGeometry::clipAreas()
{
  // Immediately hide the reference graphic
  m_coloradoGraphic->setVisible(false);

  // Get the graphicsListModel from the overlay containing the clipping envelopes
  GraphicListModel* graphics = m_envelopesOverlay->graphics();

  /* Iterate through the graphics' geometries and call the static 'clip' function, passing in
   * the reference graphic's geometry.
   * If the return value is valid, create a new graphic using the returned clipped geometry and the
   * fillSymbol of the reference graphic as arguments. Append the new graphic to its respective
   * graphicsOverlay. */
  const int graphicsSize = graphics->size();
  const Geometry coloradoGeometry = m_coloradoGraphic->geometry();
  for (int i = 0; i < graphicsSize; i++)
  {
    m_clippedGeometry = GeometryEngine::clip(coloradoGeometry, graphics->at(i)->geometry().extent());
    if (!m_clippedGeometry.isEmpty())
    {
      Graphic* clippedGraphic = new Graphic(m_clippedGeometry, m_coloradoFill, this);
      m_clippedAreasOverlay->graphics()->append(clippedGraphic);
    }
  }
}

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