Skip to content

Commit 123a0c8

Browse files
committed
project (geometry) sample
1 parent d2e22f5 commit 123a0c8

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
416 KB
Loading
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2018 Esri.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.esri.samples.geometry.project;
18+
19+
import java.text.DecimalFormat;
20+
21+
import javafx.application.Application;
22+
import javafx.geometry.Point2D;
23+
import javafx.scene.Scene;
24+
import javafx.scene.input.MouseButton;
25+
import javafx.scene.layout.StackPane;
26+
import javafx.stage.Stage;
27+
28+
import com.esri.arcgisruntime.geometry.Envelope;
29+
import com.esri.arcgisruntime.geometry.Geometry;
30+
import com.esri.arcgisruntime.geometry.GeometryEngine;
31+
import com.esri.arcgisruntime.geometry.Point;
32+
import com.esri.arcgisruntime.geometry.SpatialReferences;
33+
import com.esri.arcgisruntime.mapping.ArcGISMap;
34+
import com.esri.arcgisruntime.mapping.Basemap;
35+
import com.esri.arcgisruntime.mapping.view.Callout;
36+
import com.esri.arcgisruntime.mapping.view.Graphic;
37+
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
38+
import com.esri.arcgisruntime.mapping.view.MapView;
39+
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
40+
41+
public class ProjectSample extends Application {
42+
43+
private MapView mapView;
44+
45+
@Override
46+
public void start(Stage stage) {
47+
48+
try {
49+
// create stack pane and application scene
50+
StackPane stackPane = new StackPane();
51+
Scene scene = new Scene(stackPane);
52+
53+
// set title, size, and add scene to stage
54+
stage.setTitle("Project Sample");
55+
stage.setWidth(800);
56+
stage.setHeight(700);
57+
stage.setScene(scene);
58+
stage.show();
59+
60+
// create a map with a web mercator basemap
61+
ArcGISMap map = new ArcGISMap(SpatialReferences.getWebMercator());
62+
map.setBasemap(Basemap.createNationalGeographic());
63+
mapView = new MapView();
64+
mapView.setMap(map);
65+
66+
// zoom to Minneapolis
67+
Geometry startingEnvelope = new Envelope(-10995912.335747, 5267868.874421, -9880363.974046, 5960699.183877,
68+
SpatialReferences.getWebMercator());
69+
mapView.setViewpointGeometryAsync(startingEnvelope);
70+
71+
// create a graphics to show the input location
72+
GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
73+
mapView.getGraphicsOverlays().add(graphicsOverlay);
74+
75+
// create a red marker symbol for the input point
76+
final SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 5);
77+
Graphic inputPointGraphic = new Graphic();
78+
inputPointGraphic.setSymbol(markerSymbol);
79+
graphicsOverlay.getGraphics().add(inputPointGraphic);
80+
81+
DecimalFormat decimalFormat = new DecimalFormat("#.00000");
82+
83+
// show the input location where the user clicks on the map
84+
mapView.setOnMouseClicked(e -> {
85+
if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
86+
Point2D point2D = new Point2D(e.getX(), e.getY());
87+
// show the clicked location on the map with a graphic
88+
Point originalPoint = mapView.screenToLocation(point2D);
89+
inputPointGraphic.setGeometry(originalPoint);
90+
// project the web mercator point to WGS84
91+
Point projectedPoint = (Point) GeometryEngine.project(originalPoint, SpatialReferences.getWgs84());
92+
// show the original and projected point coordinates in a callout from the graphic
93+
Callout callout = mapView.getCallout();
94+
callout.setTitle("Projection");
95+
String ox = decimalFormat.format(originalPoint.getX());
96+
String oy = decimalFormat.format(originalPoint.getY());
97+
String px = decimalFormat.format(projectedPoint.getX());
98+
String py = decimalFormat.format(projectedPoint.getY());
99+
callout.setDetail("Original: " + ox + ", " + oy + "\n" + "Projected: " + px + ", " + py);
100+
callout.showCalloutAt(inputPointGraphic, originalPoint);
101+
}
102+
});
103+
104+
// add the map view to the stack pane
105+
stackPane.getChildren().add(mapView);
106+
} catch (Exception e) {
107+
// on any error, display the stack trace.
108+
e.printStackTrace();
109+
}
110+
}
111+
112+
/**
113+
* Stops and releases all resources used in application.
114+
*/
115+
@Override
116+
public void stop() {
117+
118+
if (mapView != null) {
119+
mapView.dispose();
120+
}
121+
}
122+
123+
/**
124+
* Opens and runs application.
125+
*
126+
* @param args arguments passed to this application
127+
*/
128+
public static void main(String[] args) {
129+
130+
Application.launch(args);
131+
}
132+
133+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h1>Project</h1>
2+
3+
<p>This sample demonstrates how to project a point from one spatial reference to another.</p>
4+
5+
<p>Being able to project between spatial references is fundamental to a GIS. An example of when you would need to
6+
re-project data is if you had data in two different spatial references, but wanted to perform an intersect analysis
7+
with the <code>GeometryEngine::intersect</code> function. This function takes two geometries as parameters, and both
8+
geometries must be in the same spatial reference. If they are not, you could first use
9+
<code>GeometryEngine::project</code> to convert the geometries so they match.</p>
10+
11+
<p><img src="Project.png"></p>
12+
13+
<h2>How to use the sample</h2>
14+
<p>Click anywhere on the map. A callout will display the clicked location's coordinate in the original (basemap's)
15+
spatial reference and in the projected spatial reference.</p>
16+
17+
<h2>How it works</h2>
18+
<p>To project a geometry to another spatial reference:</p>
19+
<ol>
20+
<li>Call the static method, <code>GeometryEngine.project</code>, passing in the original <code>Geometry</code> and a
21+
<code>SpatialReference</code> to project to.</li>
22+
</ol>
23+
24+
<h2>Features</h2>
25+
<ul>
26+
<li>GeometryEngine</li>
27+
<li>Point</li>
28+
<li>SpatialReference</li>
29+
</ul>

0 commit comments

Comments
 (0)