Create geometries

View on GitHubSample viewer app

Create simple geometry types.

Image of create geometries

Use case

Geometries are used to represent real world features as vector GIS data. Points are used to mark specific XY locations, such as landmarks and other points of interest. Polylines are made up of 2 or more XY vertices and can be used to mark roads, flight paths, or boundaries. Polygons are made up of 3 or more XY vertices and can be used to represent a lake, country, or a park. Geometries can be stored as features in a database, displayed as graphics in a map, or used for performing spatial analysis with AGSGeometryEngine or an AGSGeoprocessingTask.

How to use the sample

Pan and zoom freely to see the different types of geometries placed on the map.

How it works

  1. Use the constructors for the various simple AGSGeometry types including AGSPoint, AGSPolyline, AGSMultipoint, AGSPolygon, and AGSEnvelope.
  2. To display the geometry, create an AGSGraphic passing in the geometry, and an AGSSymbol appropriate for the geometry type.
  3. Add the graphic to an AGSGraphicsOverlay and add the overlay to an AGSMapView.

Relevant API

  • AGSEnvelope
  • AGSMultipoint
  • AGSPoint
  • AGSPolygon
  • AGSPolyline

Tags

area, boundary, line, marker, path, shape

Sample Code

CreateGeometriesViewController.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
// Copyright 2016 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 CreateGeometriesViewController: UIViewController {
    @IBOutlet var mapView: AGSMapView!

    private var graphicsOverlay = AGSGraphicsOverlay()

    override func viewDidLoad() {
        super.viewDidLoad()

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

        // instantiate map using basemap
        let map = AGSMap(basemapStyle: .arcGISTopographic)

        // assign map to the map view
        self.mapView.map = map

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

        // create symbols for drawing graphics
        let markerSymbol = AGSSimpleMarkerSymbol(style: .triangle, color: .blue, size: 14)
        let lineSymbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
        let fillSymbol = AGSSimpleFillSymbol(style: .cross, color: .blue, outline: nil)

        // add a graphic of point, multipoint, polyline and polygon
        self.graphicsOverlay.graphics.add(AGSGraphic(geometry: self.createPoint(), symbol: markerSymbol, attributes: nil))
        self.graphicsOverlay.graphics.add(AGSGraphic(geometry: self.createMultipoint(), symbol: markerSymbol, attributes: nil))
        self.graphicsOverlay.graphics.add(AGSGraphic(geometry: self.createPolyline(), symbol: lineSymbol, attributes: nil))
        self.graphicsOverlay.graphics.add(AGSGraphic(geometry: self.createPolygon(), symbol: fillSymbol, attributes: nil))

        // use the envelope to set the viewpoint of the map view
        self.mapView.setViewpointGeometry(self.createEnvelope(), padding: 100, completion: nil)
    }

    private func createEnvelope() -> AGSEnvelope {
        // create an envelope using minimum and maximum x,y coordinates and a spatial reference
        let envelope = AGSEnvelope(xMin: -123.0, yMin: 33.5, xMax: -101.0, yMax: 48.0, spatialReference: .wgs84())
        return envelope
    }

    private func createPoint() -> AGSPoint {
        // create a point using x,y coordinates and a spatial reference
        let point = AGSPoint(x: 34.056295, y: -117.195800, spatialReference: .wgs84())
        return point
    }

    private func createMultipoint() -> AGSMultipoint {
        // create a multi point geometry
        let multipointBuilder = AGSMultipointBuilder(spatialReference: .wgs84())
        multipointBuilder.points.addPointWith(x: -121.491014, y: 38.579065) // Sacramento, CA
        multipointBuilder.points.addPointWith(x: -122.891366, y: 47.039231) // Olympia, WA
        multipointBuilder.points.addPointWith(x: -123.043814, y: 44.93326) // Salem, OR
        multipointBuilder.points.addPointWith(x: -119.766999, y: 39.164885) // Carson City, NV

        return multipointBuilder.toGeometry()
    }

    private func createPolyline() -> AGSPolyline {
        // create a polyline
        let polylineBuilder = AGSPolylineBuilder(spatialReference: .wgs84())
        polylineBuilder.addPointWith(x: -119.992, y: 41.989)
        polylineBuilder.addPointWith(x: -119.994, y: 38.994)
        polylineBuilder.addPointWith(x: -114.620, y: 35.0)

        return polylineBuilder.toGeometry()
    }

    private func createPolygon() -> AGSPolygon {
        // create a polygon
        let polygonBuilder = AGSPolygonBuilder(spatialReference: .wgs84())
        polygonBuilder.addPointWith(x: -109.048, y: 40.998)
        polygonBuilder.addPointWith(x: -102.047, y: 40.998)
        polygonBuilder.addPointWith(x: -102.037, y: 36.989)
        polygonBuilder.addPointWith(x: -109.048, y: 36.998)

        return polygonBuilder.toGeometry()
    }
}

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