Skip to content

Commit 1b29847

Browse files
Rachael-ERachael Ellenjenmerritt
authored
Update Nearest Vertex sample (#681)
* update sample * Update README.md * Update geometry/nearest-vertex/src/main/java/com/esri/samples/nearest_vertex/NearestVertexSample.java Co-authored-by: Jen Merritt <[email protected]> * Update geometry/nearest-vertex/src/main/java/com/esri/samples/nearest_vertex/NearestVertexSample.java Co-authored-by: Jen Merritt <[email protected]> * replace colors with color util, and make colors more visible against new basemap * scale new image Co-authored-by: Rachael Ellen <[email protected]> Co-authored-by: Jen Merritt <[email protected]>
1 parent fb5bbf3 commit 1b29847

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed
-363 KB
Loading

geometry/nearest-vertex/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Click anywhere on the map. An orange cross will show at that location. A blue ci
2525
* GeometryEngine
2626
* ProximityResult
2727

28+
## Additional information
29+
30+
The value of `ProximityResult.getDistance()` is planar (Euclidean) distance. Planar distances are only accurate for geometries that have a defined projected coordinate system, which maintain the desired level of accuracy. The example polygon in this sample is defined in California State Plane Coordinate System - Zone 5 (WKID 2229), which maintains accuracy near Southern California. Accuracy declines outside the state plane zone.
31+
2832
## Tags
2933

3034
analysis, coordinate, geometry, nearest, proximity, vertex

geometry/nearest-vertex/src/main/java/com/esri/samples/nearest_vertex/NearestVertexSample.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,34 @@
3131
import javafx.scene.layout.CornerRadii;
3232
import javafx.scene.layout.StackPane;
3333
import javafx.scene.layout.VBox;
34+
import javafx.scene.paint.Color;
3435
import javafx.scene.paint.Paint;
3536
import javafx.stage.Stage;
3637

3738
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
39+
import com.esri.arcgisruntime.geometry.SpatialReference;
3840
import com.esri.arcgisruntime.geometry.GeometryEngine;
3941
import com.esri.arcgisruntime.geometry.Point;
4042
import com.esri.arcgisruntime.geometry.PointCollection;
4143
import com.esri.arcgisruntime.geometry.Polygon;
4244
import com.esri.arcgisruntime.geometry.ProximityResult;
43-
import com.esri.arcgisruntime.geometry.SpatialReferences;
45+
import com.esri.arcgisruntime.layers.FeatureLayer;
4446
import com.esri.arcgisruntime.mapping.ArcGISMap;
45-
import com.esri.arcgisruntime.mapping.BasemapStyle;
4647
import com.esri.arcgisruntime.mapping.view.Graphic;
4748
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
4849
import com.esri.arcgisruntime.mapping.view.MapView;
50+
import com.esri.arcgisruntime.portal.Portal;
51+
import com.esri.arcgisruntime.portal.PortalItem;
52+
import com.esri.arcgisruntime.symbology.ColorUtil;
4953
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
5054
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
5155
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
5256

5357
public class NearestVertexSample extends Application {
5458

5559
private MapView mapView;
60+
// California zone 5 (ftUS) state plane coordinate system
61+
private final SpatialReference statePlaneCaliforniaZone5SpatialReference = SpatialReference.create(2229);
5662

5763
@Override
5864
public void start(Stage stage) {
@@ -74,8 +80,14 @@ public void start(Stage stage) {
7480
String yourAPIKey = System.getProperty("apiKey");
7581
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);
7682

77-
// create a map with the topographic basemap style
78-
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
83+
// create a new feature layer from a new portal item
84+
var portalItem = new PortalItem(
85+
new Portal("https://arcgisruntime.maps.arcgis.com", false), "99fd67933e754a1181cc755146be21ca");
86+
FeatureLayer usStatesGeneralizedLayer = new FeatureLayer(portalItem, 0);
87+
// create a new map using the California zone 5 spatial reference
88+
ArcGISMap map = new ArcGISMap(statePlaneCaliforniaZone5SpatialReference);
89+
// add the feature layer to the map's list of base layers
90+
map.getBasemap().getBaseLayers().add(usStatesGeneralizedLayer);
7991

8092
// create a map view and set the map to it
8193
mapView = new MapView();
@@ -85,24 +97,26 @@ public void start(Stage stage) {
8597
GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
8698
mapView.getGraphicsOverlays().add(graphicsOverlay);
8799

88-
// create a graphic for the polygon
89-
PointCollection polygonPoints = new PointCollection(SpatialReferences.getWebMercator());
100+
// construct a polygon from a point collection that uses the California zone 5 (ftUS) state plane coordinate system
101+
PointCollection polygonPoints = new PointCollection(statePlaneCaliforniaZone5SpatialReference);
90102
polygonPoints.addAll(Arrays.asList(
91-
new Point(-5991501.677830, 5599295.131468),
92-
new Point(-6928550.398185, 2087936.739807),
93-
new Point(-3149463.800709, 1840803.011362),
94-
new Point(-1563689.043184, 3714900.452072),
95-
new Point(-3180355.516764, 5619889.608838)));
103+
new Point(6627416.41469281, 1804532.53233782),
104+
new Point(6669147.89779046, 2479145.16609522),
105+
new Point(7265673.02678292, 2484254.50442408),
106+
new Point(7676192.55880379, 2001458.66365744),
107+
new Point(7175695.94143837, 1840722.34474458)));
108+
109+
// create a graphic for the polygon
96110
Polygon polygon = new Polygon(polygonPoints);
97-
SimpleLineSymbol polygonOutlineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF00FF00, 2);
98-
SimpleFillSymbol polygonFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.FORWARD_DIAGONAL, 0xFF00FF00, polygonOutlineSymbol);
111+
SimpleLineSymbol polygonOutlineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, ColorUtil.colorToArgb(Color.LIMEGREEN), 2);
112+
SimpleFillSymbol polygonFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.FORWARD_DIAGONAL, ColorUtil.colorToArgb(Color.LIMEGREEN), polygonOutlineSymbol);
99113
Graphic polygonGraphic = new Graphic(polygon, polygonFillSymbol);
100114
graphicsOverlay.getGraphics().add(polygonGraphic);
101115

102116
// create graphics for the clicked location, nearest coordinate, and nearest vertex markers
103-
SimpleMarkerSymbol clickedLocationSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.X, 0xFFFFA500, 15);
104-
SimpleMarkerSymbol nearestCoordinateSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.DIAMOND, 0xFFFF0000, 10);
105-
SimpleMarkerSymbol nearestVertexSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFF0000FF, 15);
117+
SimpleMarkerSymbol clickedLocationSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.X, ColorUtil.colorToArgb(Color.DARKORANGE), 15);
118+
SimpleMarkerSymbol nearestCoordinateSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.DIAMOND, ColorUtil.colorToArgb(Color.RED), 10);
119+
SimpleMarkerSymbol nearestVertexSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, ColorUtil.colorToArgb(Color.BLUE), 15);
106120
Graphic clickedLocationGraphic = new Graphic();
107121
clickedLocationGraphic.setSymbol(clickedLocationSymbol);
108122
Graphic nearestCoordinateGraphic = new Graphic();
@@ -135,22 +149,20 @@ public void start(Stage stage) {
135149

136150
// create a map point from a point
137151
Point mapPoint = mapView.screenToLocation(point);
138-
139-
// the map point should be normalized to the central meridian when wrapping around a map, so its value stays within the coordinate system of the map view
140-
Point normalizedMapPoint = (Point) GeometryEngine.normalizeCentralMeridian(mapPoint);
152+
141153
// show where the user clicked
142-
clickedLocationGraphic.setGeometry(normalizedMapPoint);
154+
clickedLocationGraphic.setGeometry(mapPoint);
143155

144156
// show the nearest coordinate and vertex
145-
ProximityResult nearestCoordinateResult = GeometryEngine.nearestCoordinate(polygon, normalizedMapPoint);
146-
ProximityResult nearestVertexResult = GeometryEngine.nearestVertex(polygon, normalizedMapPoint);
157+
ProximityResult nearestCoordinateResult = GeometryEngine.nearestCoordinate(polygon, mapPoint);
158+
ProximityResult nearestVertexResult = GeometryEngine.nearestVertex(polygon, mapPoint);
147159
nearestVertexGraphic.setGeometry(nearestVertexResult.getCoordinate());
148160
nearestCoordinateGraphic.setGeometry(nearestCoordinateResult.getCoordinate());
149161

150-
// show the distances to the nearest vertex and nearest coordinate rounded to the nearest kilometer
151-
int vertexDistance = (int) (nearestVertexResult.getDistance() / 1000.0);
152-
int coordinateDistance = (int) (nearestCoordinateResult.getDistance() / 1000.0);
153-
distancesLabel.setText("Vertex distance: " + vertexDistance + " km\nCoordinate distance: " + coordinateDistance + " km");
162+
// show the distances to the nearest vertex and nearest coordinate, converted from feet to miles
163+
int vertexDistance = (int) (nearestVertexResult.getDistance() / 5280.0);
164+
int coordinateDistance = (int) (nearestCoordinateResult.getDistance() / 5280.0);
165+
distancesLabel.setText("Vertex distance: " + vertexDistance + " mi\nCoordinate distance: " + coordinateDistance + " mi");
154166
}
155167
});
156168

0 commit comments

Comments
 (0)