Project geometry

View on GitHubSample viewer app

Project a point from one spatial reference to another.

Image of project

Use case

Being able to project between spatial references is fundamental to a GIS. An example of when you would need to re-project data is if you had data in two different spatial references, but wanted to perform an intersect analysis with the GeometryEngine.intersect function. This function takes two geometries as parameters, and both geometries must be in the same spatial reference. If they are not, you could first use GeometryEngine.project to convert the geometries so they match.

How to use the sample

Click anywhere on the map. A Textview will display the clicked location's coordinate in the original (basemap's) spatial reference and in the projected spatial reference.

How it works

  1. Call the static method, GeometryEngine.project, passing in the original Geometry and a SpatialReference to which it should be projected.

Relevant API

  • GeometryEngine
  • Point
  • SpatialReference

Additional information

In cases where the the output spatial reference uses a different geographic coordinate system than that of the input spatial reference, see the GeometryEngine.project method that additionally takes in a DatumTransformation parameter.

Tags

coordinate system, coordinates, latitude, longitude, projected, projection, spatial reference, Web Mercator, WGS 84

Sample Code

MainActivity.kt
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
/*
 * Copyright 2023 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.
 *
 */

package com.esri.arcgismaps.sample.projectgeometry

import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.geometry.*
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.symbology.PictureMarkerSymbol
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.mapping.view.MapView
import com.esri.arcgismaps.sample.projectgeometry.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    // set up data binding for the activity
    private val activityMainBinding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    // setup the data binding for the MapView
    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    // shows the projection information as a TextView
    private val infoTextView: TextView by lazy {
        activityMainBinding.infoTextView
    }

    // setup the red pin marker image as bitmap drawable
    private val markerDrawable: BitmapDrawable by lazy {
        // load the bitmap from resources and create a drawable
        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.pin_symbol)
        BitmapDrawable(resources, bitmap)
    }

    // setup the red pin marker as a Graphic
    private val markerGraphic: Graphic by lazy {
        // creates a symbol from the marker drawable
        val markerSymbol = PictureMarkerSymbol.createWithImage(markerDrawable).apply {
            // resize the symbol into a smaller size
            width = 30f
            height = 30f
            // offset in +y axis so the marker spawned
            // is right on the touch point
            offsetY = 25f
        }
        // create the graphic from the symbol
        Graphic(symbol = markerSymbol)
    }

    // creates a graphic overlay
    private val graphicsOverlay: GraphicsOverlay = GraphicsOverlay()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // authentication with an API key or named user is
        // required to access basemaps and other location services
        ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
        lifecycle.addObserver(mapView)
        // create and add a map with a navigation night basemap style
        val map = ArcGISMap(BasemapStyle.ArcGISNavigationNight)
        // configure mapView assignments
        mapView.apply {
            this.map = map
            // add our marker overlay to the graphics overlay
            graphicsOverlay.graphics.add(markerGraphic)
            // add the graphics overlay to display marker graphics
            graphicsOverlays.add(graphicsOverlay)
            // set the default viewpoint to Redlands,CA
            setViewpoint(Viewpoint(34.058, -117.195, 5e4))
        }

        lifecycleScope.launch {
            // check if the map has loaded successfully
            map.load().onSuccess {
                // capture and collect when the user taps on the screen
                mapView.onSingleTapConfirmed.collect { event ->
                    event.mapPoint?.let { point -> onGeoViewTapped(point) }
                }
            }.onFailure {
                // if map load failed, show the error
                showError("Error Loading Map", mapView)
            }
        }
    }

    /**
     * Handles the SingleTapEvent by drawing a marker, re-centering the mapView to the marker
     * and performs a Spatial reference transformation of the tapped Location
     * using GeometryEngine and displays the result.
     */
    private suspend fun onGeoViewTapped(point: Point) {
        // update the marker location to where the user tapped on the map
        markerGraphic.geometry = point
        // set mapview to recenter to the tapped location
        mapView.setViewpointGeometry(point.extent)
        // project the web mercator location into a WGS84
        val projectedPoint = GeometryEngine.projectOrNull(point, SpatialReference.wgs84())
        // build and display the projection result as a string
        infoTextView.text = getString(
            R.string.projection_info_text,
            point.toDisplayFormat(),
            projectedPoint?.toDisplayFormat()
        )
    }

    /**
     * Displays an error onscreen
     */
    private fun showError(message: String, view: View) {
        Log.e(localClassName, message)
        Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show()
    }
}

/**
 * Extension function for the Point type that returns
 * a float-precision formatted string suitable for display
 */
private fun Point.toDisplayFormat() =
    "${String.format("%.5f", x)}, ${String.format("%.5f", y)}"

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