Geodesic operations

View on GitHubSample viewer app

Calculate a geodesic path between two points and measure its distance.

Image of geodesic operations

Use case

A geodesic distance provides an accurate, real-world distance between two points. Visualizing flight paths between cities is a common example of a geodesic operation since the flight path between two airports takes into account the curvature of the earth, rather than following the planar path between those points, which appears as a straight line on a projected map.

How to use the sample

Tap anywhere on the map. A line graphic will display the geodesic line between the two points. In addition, text that indicates the geodesic distance between the two points will be updated. Tap elsewhere and a new line will be created.

How it works

  1. Create an AGSPoint in New York City and display it as an AGSGraphic.
  2. Obtain a new point when a tap occurs on the AGSMapView and add this point as a graphic.
  3. Create an AGSPolyline from the two points.
  4. Execute class AGSGeometryEngine.geodeticDensifyGeometry(_:maxSegmentLength:lengthUnit:curveType:) by passing in the created polyline, then create a graphic from the returned AGSGeometry.
  5. Execute class AGSGeometryEngine.geodeticLength(of:lengthUnit:curveType:) by passing in the two points and display the returned length on the screen.

Relevant API

  • class AGSGeometryEngine.geodeticDensifyGeometry(_:maxSegmentLength:lengthUnit:curveType:)
  • class AGSGeometryEngine.geodeticLength(of:lengthUnit:curveType:)

About the data

The Imagery basemap provides the global context for the displayed geodesic line.

Tags

densify, distance, geodesic, geodetic

Sample Code

GeodesicOperationsViewController.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
// 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
import ArcGIS

class GeodesicOperationsViewController: UIViewController, AGSGeoViewTouchDelegate {
    @IBOutlet private var mapView: AGSMapView!

    private let destinationGraphic: AGSGraphic
    private let pathGraphic: AGSGraphic

    private let graphicsOverlay = AGSGraphicsOverlay()
    private let measurementFormatter = MeasurementFormatter()
    private let JFKAirportLocation = AGSPoint(x: -73.7781, y: 40.6413, spatialReference: .wgs84())

    /// Add graphics representing origin, destination, and path to a graphics overlay.
    required init?(coder aDecoder: NSCoder) {
        // Create graphic symbols.
        let locationMarker = AGSSimpleMarkerSymbol(style: .circle, color: .blue, size: 10)
        locationMarker.outline = AGSSimpleLineSymbol(style: .solid, color: .green, width: 5)
        let pathSymbol = AGSSimpleLineSymbol(style: .dash, color: .blue, width: 5)

        // Create an origin graphic.
        let originGraphic = AGSGraphic(geometry: JFKAirportLocation, symbol: locationMarker, attributes: nil)

        // Create a destination graphic.
        destinationGraphic = AGSGraphic(geometry: nil, symbol: locationMarker, attributes: nil)

        // Create a graphic to represent geodesic path between origin and destination.
        pathGraphic = AGSGraphic(geometry: nil, symbol: pathSymbol, attributes: nil)

        // Add graphics to the graphics overlay.
        graphicsOverlay.graphics.addObjects(from: [originGraphic, destinationGraphic, pathGraphic])

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Add the source code button item to the right of navigation bar.
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["GeodesicOperationsViewController"]

        // Assign map with imagery basemap to the map view.
        mapView.map = AGSMap(basemapStyle: .arcGISImageryStandard)

        // Add the graphics overlay to the map view.
        mapView.graphicsOverlays.add(graphicsOverlay)

        // Set touch delegate on map view as self.
        mapView.touchDelegate = self
    }

    // MARK: - AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        // If callout is open, dismiss it.
        if !mapView.callout.isHidden {
            mapView.callout.dismiss()
        }

        // Get the tapped geometry projected to WGS84.
        guard let destinationLocation = AGSGeometryEngine.projectGeometry(mapPoint, to: .wgs84()) as? AGSPoint else {
            return
        }

        // Update geometry of the destination graphic.
        destinationGraphic.geometry = destinationLocation

        // Create a straight line path from origin to destination.
        let path = AGSPolyline(points: [JFKAirportLocation, destinationLocation])

        // Densify the polyline to show the geodesic curve.
        guard let geodeticPath = AGSGeometryEngine.geodeticDensifyGeometry(path, maxSegmentLength: 1, lengthUnit: .kilometers(), curveType: .geodesic) else {
            return
        }

        // Update geometry of the path graphic.
        pathGraphic.geometry = geodeticPath

        // Calculate geodetic distance between origin and destination.
        let distance = AGSGeometryEngine.geodeticLength(of: geodeticPath, lengthUnit: .kilometers(), curveType: .geodesic)

        // Display the distance in mapview's callout.
        mapView.callout.title = "Distance"

        let measurement = Measurement<UnitLength>(value: distance, unit: .kilometers)
        mapView.callout.detail = measurementFormatter.string(from: measurement)

        mapView.callout.isAccessoryButtonHidden = true
        mapView.callout.show(at: mapPoint, screenOffset: .zero, rotateOffsetWithMap: false, animated: true)
    }
}

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