|
| 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.clip_geometry; |
| 18 | + |
| 19 | +import javafx.application.Application; |
| 20 | +import javafx.geometry.Insets; |
| 21 | +import javafx.geometry.Pos; |
| 22 | +import javafx.scene.Scene; |
| 23 | +import javafx.scene.control.Button; |
| 24 | +import javafx.scene.layout.StackPane; |
| 25 | +import javafx.stage.Stage; |
| 26 | + |
| 27 | +import com.esri.arcgisruntime.geometry.Envelope; |
| 28 | +import com.esri.arcgisruntime.geometry.Geometry; |
| 29 | +import com.esri.arcgisruntime.geometry.GeometryEngine; |
| 30 | +import com.esri.arcgisruntime.geometry.Point; |
| 31 | +import com.esri.arcgisruntime.geometry.SpatialReferences; |
| 32 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 33 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 34 | +import com.esri.arcgisruntime.mapping.view.Graphic; |
| 35 | +import com.esri.arcgisruntime.mapping.view.GraphicsOverlay; |
| 36 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 37 | +import com.esri.arcgisruntime.symbology.SimpleFillSymbol; |
| 38 | +import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
| 39 | + |
| 40 | +public class ClipGeometrySample extends Application { |
| 41 | + |
| 42 | + private MapView mapView; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void start(Stage stage) { |
| 46 | + |
| 47 | + try { |
| 48 | + // create stack pane and application scene |
| 49 | + StackPane stackPane = new StackPane(); |
| 50 | + Scene scene = new Scene(stackPane); |
| 51 | + |
| 52 | + // set title, size, and add scene to stage |
| 53 | + stage.setTitle("Clip Geometry Sample"); |
| 54 | + stage.setWidth(800); |
| 55 | + stage.setHeight(700); |
| 56 | + stage.setScene(scene); |
| 57 | + stage.show(); |
| 58 | + |
| 59 | + // create a map with a basemap and add it to the map view |
| 60 | + ArcGISMap map = new ArcGISMap(SpatialReferences.getWebMercator()); |
| 61 | + map.setBasemap(Basemap.createTopographic()); |
| 62 | + mapView = new MapView(); |
| 63 | + mapView.setMap(map); |
| 64 | + |
| 65 | + // create a graphics overlay to contain the geometry to clip |
| 66 | + GraphicsOverlay graphicsOverlay = new GraphicsOverlay(); |
| 67 | + mapView.getGraphicsOverlays().add(graphicsOverlay); |
| 68 | + |
| 69 | + // create a blue graphic of Colorado |
| 70 | + Envelope colorado = new Envelope(new Point(-11362327.128340, 5012861.290274), |
| 71 | + new Point(-12138232.018408, 4441198.773776)); |
| 72 | + SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0x220000FF, |
| 73 | + new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0000FF, 2)); |
| 74 | + Graphic coloradoGraphic = new Graphic(colorado, fillSymbol); |
| 75 | + graphicsOverlay.getGraphics().add(coloradoGraphic); |
| 76 | + |
| 77 | + // create a graphics overlay to contain the clipping envelopes |
| 78 | + GraphicsOverlay envelopesOverlay = new GraphicsOverlay(); |
| 79 | + mapView.getGraphicsOverlays().add(envelopesOverlay); |
| 80 | + |
| 81 | + // create a dotted red outline symbol |
| 82 | + SimpleLineSymbol redOutline = new SimpleLineSymbol(SimpleLineSymbol.Style.DOT, 0xFFFF0000, 3); |
| 83 | + |
| 84 | + // create a envelope outside Colorado |
| 85 | + Envelope outsideEnvelope = |
| 86 | + new Envelope(new Point(-11858344.321294, 5147942.225174), new Point(-12201990.219681, 5297071.577304)); |
| 87 | + Graphic outside = new Graphic(outsideEnvelope, redOutline); |
| 88 | + envelopesOverlay.getGraphics().add(outside); |
| 89 | + |
| 90 | + // create a envelope intersecting Colorado |
| 91 | + Envelope intersectingEnvelope = |
| 92 | + new Envelope(new Point(-11962086.479298, 4566553.881363), new Point(-12260345.183558, 4332053.378376)); |
| 93 | + Graphic intersecting = new Graphic(intersectingEnvelope, redOutline); |
| 94 | + envelopesOverlay.getGraphics().add(intersecting); |
| 95 | + |
| 96 | + // create a envelope inside Colorado |
| 97 | + Envelope containedEnvelope = |
| 98 | + new Envelope(new Point(-11655182.595204, 4741618.772994), new Point(-11431488.567009, 4593570.068343)); |
| 99 | + Graphic contained = new Graphic(containedEnvelope, redOutline); |
| 100 | + envelopesOverlay.getGraphics().add(contained); |
| 101 | + |
| 102 | + // zoom to show the polygon graphic |
| 103 | + mapView.setViewpointGeometryAsync(coloradoGraphic.getGeometry(), 200); |
| 104 | + |
| 105 | + // create a graphics overlay to contain the clipped areas |
| 106 | + GraphicsOverlay clipAreasOverlay = new GraphicsOverlay(); |
| 107 | + mapView.getGraphicsOverlays().add(clipAreasOverlay); |
| 108 | + |
| 109 | + // create a button to perform the clip operation |
| 110 | + Button clipButton = new Button("Clip"); |
| 111 | + clipButton.setOnAction(e -> { |
| 112 | + // for each envelope, clip the Colorado geometry and show the result as a green graphic |
| 113 | + SimpleFillSymbol clippedAreaSymbol = |
| 114 | + new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF00FF00, null); |
| 115 | + envelopesOverlay.getGraphics().forEach(graphic -> { |
| 116 | + Geometry geometry = GeometryEngine.clip(coloradoGraphic.getGeometry(), (Envelope) graphic.getGeometry()); |
| 117 | + if (geometry != null) { |
| 118 | + Graphic clippedGraphic = new Graphic(geometry, clippedAreaSymbol); |
| 119 | + clipAreasOverlay.getGraphics().add(clippedGraphic); |
| 120 | + } |
| 121 | + }); |
| 122 | + // only clip once |
| 123 | + clipButton.setDisable(true); |
| 124 | + }); |
| 125 | + |
| 126 | + // add the map view to the stack pane |
| 127 | + stackPane.getChildren().addAll(mapView, clipButton); |
| 128 | + StackPane.setAlignment(clipButton, Pos.TOP_LEFT); |
| 129 | + StackPane.setMargin(clipButton, new Insets(10, 0, 0, 10)); |
| 130 | + } catch (Exception e) { |
| 131 | + // on any error, display the stack trace. |
| 132 | + e.printStackTrace(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Stops and releases all resources used in application. |
| 138 | + */ |
| 139 | + @Override |
| 140 | + public void stop() { |
| 141 | + |
| 142 | + if (mapView != null) { |
| 143 | + mapView.dispose(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Opens and runs application. |
| 149 | + * |
| 150 | + * @param args arguments passed to this application |
| 151 | + */ |
| 152 | + public static void main(String[] args) { |
| 153 | + |
| 154 | + Application.launch(args); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments