Spatial relationships

View on GitHubSample viewer app

Determine spatial relationships between two geometries.

Image of spatial relationships

Use case

In case of a natural disaster, emergency services can represent the affected areas using polygons. By determining the spatial relationships between these and any other existing features such as populated areas, infrastructure, or natural resources, it is possible to quickly determine which of the existing features might be affected or is in further danger, helping to assess risk and define further action.

How to use the sample

Tap on the map to select one of the three graphics, and its geometry will be used to check the spatial relationships with other graphics geometries. The result will be displayed in the popover.

How it works

  1. Get the geometry from two different graphics. In this example the geometry of the selected graphic is compared to the geometry of each unselected graphic.
  2. Use the methods in AGSGeometryEngine - geometry(_:crossesGeometry:), geometry(_:contains:), geometry(_:disjointTo:), geometry(_:intersects:), geometry(_:overlapsGeometry:), geometry(_:touchesGeometry:) and geometry(_:within:), to check the relationship between the geometries, e.g. contains, disjoint, intersects, etc. If the method returns true, the relationship exists.

Relevant API

  • AGSGeometry
  • AGSGeometryEngine
  • AGSGeometryType
  • AGSGraphic
  • AGSPoint
  • AGSPolygon
  • AGSPolyline
  • class AGSGeometryEngine.geometry(_:contains:)
  • class AGSGeometryEngine.geometry(_:crossesGeometry:)
  • class AGSGeometryEngine.geometry(_:disjointTo:)
  • class AGSGeometryEngine.geometry(_:intersects:)
  • class AGSGeometryEngine.geometry(_:overlapsGeometry:)
  • class AGSGeometryEngine.geometry(_:touchesGeometry:)
  • class AGSGeometryEngine.geometry(_:within:)

Tags

geometries, relationship, spatial analysis

Sample Code

SpatialRelationshipsTableViewController.swiftSpatialRelationshipsTableViewController.swiftSpatialRelationshipsViewController.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
//
// 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.

import UIKit

class SpatialRelationshipsTableViewController: UITableViewController {
    var sections: [SpatialRelationshipsViewController.RelationshipsSection] = []

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].relationships.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section].title
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SpatialRelationshipCell", for: indexPath)
        cell.textLabel?.text = sections[indexPath.section].relationships[indexPath.row]
        return cell
    }
}

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