Project a point from one spatial reference to another.
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
Tap anywhere on the map. A callout will display the tapped location's coordinate in the original (basemap's) spatial reference and in the projected spatial reference.
How it works
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 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.
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
/* 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.
*
*/package com.esri.arcgisruntime.sample.project;
import java.text.DecimalFormat;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
publicclassMainActivityextendsAppCompatActivity{
private MapView mMapView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// authentication with an API key or named user is required to access basemaps and other// location services ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);
// inflate MapView from layout mMapView = findViewById(R.id.mapView);
// create a map with a web mercator basemap ArcGISMap map = new ArcGISMap(SpatialReferences.getWebMercator());
map.setBasemap(new Basemap(BasemapStyle.ARCGIS_CHARTED_TERRITORY));
// set the map to be displayed in this view mMapView.setMap(map);
// zoom to Minneapolis Geometry startingEnvelope = new Envelope(-10995912.335747, 5267868.874421, -9880363.974046, 5960699.183877,
SpatialReferences.getWebMercator());
mMapView.setViewpointGeometryAsync(startingEnvelope);
// create graphics to show the input location GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
mMapView.getGraphicsOverlays().add(graphicsOverlay);
// create a red marker symbol for the input pointfinal SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 5);
final Graphic inputPointGraphic = new Graphic();
inputPointGraphic.setSymbol(markerSymbol);
graphicsOverlay.getGraphics().add(inputPointGraphic);
final DecimalFormat decimalFormat = new DecimalFormat("#.00000");
// show the input location where the user clicks on the map mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
@OverridepublicbooleanonSingleTapConfirmed(MotionEvent motionEvent){
android.graphics.Point clickedLocation = new android.graphics.Point(Math.round(motionEvent.getX()),
Math.round(motionEvent.getY()));
Point originalPoint = mMapView.screenToLocation(clickedLocation);
inputPointGraphic.setGeometry(originalPoint);
// project the web mercator point to WGS84 (WKID 4236) Point projectedPoint = (Point) GeometryEngine.project(originalPoint, SpatialReferences.getWgs84());
// show the original and projected point coordinates in a callout from the graphic String ox = decimalFormat.format(originalPoint.getX());
String oy = decimalFormat.format(originalPoint.getY());
String px = decimalFormat.format(projectedPoint.getX());
String py = decimalFormat.format(projectedPoint.getY());
// create a textView for the content of the callout TextView calloutContent = new TextView(getApplicationContext());
calloutContent.setTextColor(Color.BLACK);
calloutContent.setText(String.format("Coordinates\nOriginal: %s, %s\nProjected: %s, %s", ox, oy, px, py));
// create calloutfinal Callout callout = mMapView.getCallout();
callout.setLocation(originalPoint);
callout.setContent(calloutContent);
callout.show();
returntrue;
}
});
}
@OverrideprotectedvoidonPause(){
super.onPause();
mMapView.pause();
}
@OverrideprotectedvoidonResume(){
super.onResume();
mMapView.resume();
}
@OverrideprotectedvoidonDestroy(){
super.onDestroy();
mMapView.dispose();
}
}