Skip to content
View on GitHub

Clip a geometry with another geometry.

Screenshot of clip geometry sample

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 button to clip the blue graphic with the dotted red envelopes.

How it works

  1. Use the static GeometryEngine.clip(_:to:) method to generate a clipped Geometry, specifying an existing Geometry and an Envelope as parameters. The existing geometry will be clipped where it intersects an envelope.
  2. Create a Graphic instance from the clipped geometry and add it to a GraphicsOverlay.

Relevant API

  • Envelope
  • Geometry
  • GeometryEngine
  • Graphic
  • GraphicsOverlay
  • static GeometryEngine.clip(_:to:)

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

ClipGeometryView.swift
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 179 180 181 182 // 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 // // https://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.  import ArcGIS import SwiftUI  struct ClipGeometryView: View {  /// A Boolean value indicating whether the geometries are clipped.  @State private var geometriesAreClipped = false   /// The view model for the sample.  @StateObject private var model = Model()   var body: some View {  MapView(map: model.map, graphicsOverlays: model.graphicsOverlays)  .toolbar {  ToolbarItem(placement: .bottomBar) {  Button(geometriesAreClipped ? "Reset" : "Clip") {  if geometriesAreClipped {  model.reset()  } else {  model.clipGeometries()  }  geometriesAreClipped.toggle()  }  }  }  } }  private extension ClipGeometryView {  /// The model used to store the geo model and other expensive objects  /// used in this view.  class Model: ObservableObject {  /// A map with a topographic basemap style and an initial viewpoint of Colorado.  let map: Map = {  let map = Map(basemapStyle: .arcGISTopographic)  // Sets the initial viewpoint to Colorado and adds additional padding.  map.initialViewpoint = Viewpoint(boundingGeometry: .coloradoEnvelope.expanded(by: 2))  return map  }()   /// The graphics overlay containing an unclipped graphic of Colorado.  private let coloradoGraphicsOverlay = GraphicsOverlay(  graphics: [  Graphic(geometry: .coloradoEnvelope, symbol: .coloradoFill)  ]  )   /// The graphics overlay containing graphics of the other envelopes, outlined by a dotted red line.  private let envelopesGraphicsOverlay = GraphicsOverlay(  graphics: Geometry.envelopes.map {  Graphic(geometry: $0, symbol: SimpleLineSymbol(style: .dot, color: .red, width: 3))  }  )   /// The graphics overlay containing clipped graphics.  private let clippedGraphicsOverlay = GraphicsOverlay()   /// The unclipped graphic of Colorado.  private var coloradoGraphic: Graphic { coloradoGraphicsOverlay.graphics.first! }   /// The graphics overlays used in this sample.  var graphicsOverlays: [GraphicsOverlay] {  return [coloradoGraphicsOverlay, envelopesGraphicsOverlay, clippedGraphicsOverlay]  }   /// Clips the geometry of Colorado to the given envelope.  /// - Parameter envelope: The envelope to clip the geometry of Colorado to.  /// - Returns: The clipped graphic.  private func clipColoradoGeometry(to envelope: Envelope) -> Graphic {  // Uses the geometry engine to create a new geometry for the area of  // Colorado that overlaps the given envelope.  let clippedGeometry = GeometryEngine.clip(coloradoGraphic.geometry!, to: envelope)  // Creates and returns the clipped graphic from the clipped geometry if there is an overlap.  return Graphic(geometry: clippedGeometry, symbol: .coloradoFill)  }   /// Clips geometries and adds resulting graphics.  func clipGeometries() {  // Hides the Colorado graphic.  coloradoGraphic.isVisible = false  // Clips Colorado's geometry to each envelope.  let clippedGraphics = envelopesGraphicsOverlay.graphics.map { clipColoradoGeometry(to: $0.geometry as! Envelope) }  // Adds the clipped graphics.  clippedGraphicsOverlay.addGraphics(clippedGraphics)  }   /// Removes all clipped graphics.  func reset() {  clippedGraphicsOverlay.removeAllGraphics()  coloradoGraphic.isVisible = true  }  } }  private extension Symbol {  /// The simple fill symbol for Colorado graphics.  static var coloradoFill: SimpleFillSymbol {  SimpleFillSymbol(  color: .blue.withAlphaComponent(0.2),  outline: SimpleLineSymbol(  style: .solid,  color: .blue,  width: 2  )  )  } }  private extension Geometry {  /// An envelope approximating the boundary of Colorado.  static var coloradoEnvelope: Envelope {  Envelope(  xMin: -11362327.1283,  yMin: 5012861.2903,  xMax: -12138232.0184,  yMax: 4441198.7738,  spatialReference: .webMercator  )  }  /// An envelope inside the boundary of Colorado.  static var containedEnvelope: Envelope {  Envelope(  xMin: -11655182.5952,  yMin: 4741618.773,  xMax: -11431488.567,  yMax: 4593570.0683,  spatialReference: .webMercator  )  }  /// An envelope intersecting the boundary of Colorado.  static var intersectingEnvelope: Envelope {  Envelope(  xMin: -11962086.4793,  yMin: 4566553.8814,  xMax: -12260345.1836,  yMax: 4332053.3784,  spatialReference: .webMercator  )  }  /// An envelope outside the boundary of Colorado.  static var outsideEnvelope: Envelope {  Envelope(  xMin: -11858344.3213,  yMin: 5147942.2252,  xMax: -12201990.2197,  yMax: 5297071.5773,  spatialReference: .webMercator  )  }  /// The envelopes to clip the geometry of Colorado to.  static var envelopes: [Envelope] {  return [.containedEnvelope, .intersectingEnvelope, .outsideEnvelope]  } }  private extension Envelope {  /// Expands the envelope by a given factor.  func expanded(by factor: Double) -> Envelope {  let builder = EnvelopeBuilder(envelope: self)  builder.expand(by: factor)  return builder.toGeometry()  } }  #Preview {  NavigationStack {  ClipGeometryView()  } }

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