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.

This sample uses the GeoView-Compose Toolkit module to be able to implement a composable MapView.

Tags

coordinate system, coordinates, geoview-compose, latitude, longitude, projected, projection, spatial reference, toolkit, Web Mercator, WGS 84

Sample Code

ProjectGeometryViewModel.ktProjectGeometryViewModel.ktMainActivity.ktProjectGeometryScreen.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
/* Copyright 2024 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.components

import android.app.Application
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import androidx.core.graphics.drawable.toDrawable
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.geometry.GeometryEngine
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.SpatialReference
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.SingleTapConfirmedEvent
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.projectgeometry.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class ProjectGeometryViewModel(app: Application) : AndroidViewModel(app) {
    // create a map with a navigation night basemap style
    val arcGISMap = ArcGISMap(BasemapStyle.ArcGISStreetsNight).apply {
        // set the default viewpoint to Redlands,CA
        initialViewpoint = Viewpoint(
            latitude = 34.058,
            longitude = -117.195,
            scale = 5e4
        )
    }
    // create a MapViewProxy to interact with the MapView
    val mapViewProxy = MapViewProxy()

    // Create a message dialog view model for handling error messages
    val messageDialogVM = MessageDialogViewModel()

    // state flow of a point and its projection for presentation in UI
    private val _pointProjectionFlow = MutableStateFlow<PointProjection?>(null)
    val pointProjectionFlow = _pointProjectionFlow.asStateFlow()

    // 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(app.resources, R.drawable.pin_symbol)
        bitmap.toDrawable(app.resources)
    }

    // 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)
    }

    val graphicsOverlay = GraphicsOverlay().apply {
        graphics.add(markerGraphic)
    }

    init {
        viewModelScope.launch {
            arcGISMap.load().onFailure { error ->
                messageDialogVM.showMessageDialog(
                    title = "Failed to load map",
                    description = error.message.toString()
                )
            }
        }
    }

    fun onMapViewTapped(event: SingleTapConfirmedEvent) {
        event.mapPoint?.let { point ->
            // update the marker location to where the user tapped on the map
            markerGraphic.geometry = point
            // set mapview to recenter to the tapped location
            viewModelScope.launch {
                // set mapview to recenter to the tapped location
                mapViewProxy.setViewpointCenter(point)
            }
            // project the point from web mercator to WGS84
            val projectedPoint =
                GeometryEngine.projectOrNull(
                    geometry = point,
                    spatialReference = SpatialReference.wgs84()
                )
            projectedPoint?.let { projection ->
                _pointProjectionFlow.value = PointProjection(point, projection)
            } ?: messageDialogVM.showMessageDialog(
                title = "Error",
                description = "Couldn't project point."
            )
        }
    }
}

/**
 * Data class representing a point and its projection into another spatial reference.
 */
data class PointProjection (val original: Point, val projection: Point)

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