|
| 1 | +/* |
| 2 | + * Copyright 2019 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.ogc.display_wfs_layer; |
| 18 | + |
| 19 | +import com.esri.arcgisruntime.data.QueryParameters; |
| 20 | +import com.esri.arcgisruntime.data.ServiceFeatureTable; |
| 21 | +import com.esri.arcgisruntime.geometry.Envelope; |
| 22 | +import com.esri.arcgisruntime.geometry.Point; |
| 23 | +import com.esri.arcgisruntime.layers.FeatureLayer; |
| 24 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 25 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 26 | +import com.esri.arcgisruntime.mapping.Viewpoint; |
| 27 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 28 | +import com.esri.arcgisruntime.ogc.wfs.WfsFeatureTable; |
| 29 | +import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
| 30 | +import com.esri.arcgisruntime.symbology.SimpleRenderer; |
| 31 | +import javafx.application.Application; |
| 32 | +import javafx.scene.Scene; |
| 33 | +import javafx.scene.layout.StackPane; |
| 34 | +import javafx.stage.Stage; |
| 35 | + |
| 36 | +public class DisplayWFSLayerSample extends Application { |
| 37 | + |
| 38 | + private MapView mapView; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void start(Stage stage) { |
| 42 | + |
| 43 | + // create stack pane and JavaFX app scene |
| 44 | + StackPane stackPane = new StackPane(); |
| 45 | + Scene scene = new Scene(stackPane); |
| 46 | + |
| 47 | + // set title, size, and add JavaFX scene to stage |
| 48 | + stage.setTitle("Display a WFS Layer"); |
| 49 | + stage.setWidth(800); |
| 50 | + stage.setHeight(700); |
| 51 | + stage.setScene(scene); |
| 52 | + stage.show(); |
| 53 | + |
| 54 | + // create an ArcGISMap with topographic basemap and set it to the map view |
| 55 | + ArcGISMap map = new ArcGISMap(Basemap.createTopographic()); |
| 56 | + mapView = new MapView(); |
| 57 | + mapView.setMap(map); |
| 58 | + |
| 59 | + // create an initial extent to load |
| 60 | + Point topLeft = new Point(-13619002.499764, 6043406.351867); |
| 61 | + Point bottomRight = new Point(-13618454.919189, 6042836.793464); |
| 62 | + Envelope initialExtent = new Envelope(topLeft, bottomRight); |
| 63 | + mapView.setViewpoint(new Viewpoint(initialExtent)); |
| 64 | + |
| 65 | + String serviceUrl = "https://dservices2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/services/Seattle_Downtown_Features/WFSServer?service=wfs&request=getcapabilities"; |
| 66 | + String LayerName = "Seattle_Downtown_Features:Buildings"; |
| 67 | + |
| 68 | + // create a FeatureTable from the WFS service URL and name of the layer |
| 69 | + WfsFeatureTable wfsFeatureTable = new WfsFeatureTable(serviceUrl, LayerName); |
| 70 | + |
| 71 | + // set the feature request mode to manual. The table must be manually populated as panning and zooming won't request features automatically. |
| 72 | + wfsFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.MANUAL_CACHE); |
| 73 | + |
| 74 | + // create a feature layer to visualize the WFS features |
| 75 | + FeatureLayer wfsFeatureLayer = new FeatureLayer(wfsFeatureTable); |
| 76 | + |
| 77 | + // apply a renderer to the feature layer |
| 78 | + SimpleRenderer renderer = new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF0000, 3)); |
| 79 | + wfsFeatureLayer.setRenderer(renderer); |
| 80 | + |
| 81 | + // add the layer to the map's operational layers |
| 82 | + map.getOperationalLayers().add(wfsFeatureLayer); |
| 83 | + |
| 84 | + // make an initial call to load the initial extent's data from the WFS, using the WFS spatial reference |
| 85 | + populateFeaturesFromServer(wfsFeatureTable, initialExtent); |
| 86 | + |
| 87 | + // use the navigation completed event to populate the table with the features needed for the current extent |
| 88 | + mapView.addNavigationChangedListener(navigationChangedEvent -> { |
| 89 | + // once the map view has stopped navigating |
| 90 | + if (!navigationChangedEvent.isNavigating()) { |
| 91 | + populateFeaturesFromServer(wfsFeatureTable, mapView.getVisibleArea().getExtent()); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + // add the mapview to the stackpane |
| 96 | + stackPane.getChildren().add(mapView); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Create query parameters using the given extent to populate the WFS table from the service |
| 101 | + * @param wfsTable the WFS feature table to populate |
| 102 | + * @param extent the extent used to define the QueryParameters' geometry |
| 103 | + */ |
| 104 | + private void populateFeaturesFromServer(WfsFeatureTable wfsTable, Envelope extent){ |
| 105 | + |
| 106 | + // create a query based on the current visible extent |
| 107 | + QueryParameters visibleExtentQuery = new QueryParameters(); |
| 108 | + visibleExtentQuery.setGeometry(extent); |
| 109 | + visibleExtentQuery.setSpatialRelationship(QueryParameters.SpatialRelationship.INTERSECTS); |
| 110 | + // populate the WFS feature table based on the current extent |
| 111 | + wfsTable.populateFromServiceAsync(visibleExtentQuery, false, null); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Stops and releases all resources used in application. |
| 116 | + */ |
| 117 | + @Override |
| 118 | + public void stop() { |
| 119 | + if (mapView != null) { |
| 120 | + mapView.dispose(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Opens and runs application. |
| 126 | + * |
| 127 | + * @param args arguments passed to this application |
| 128 | + */ |
| 129 | + public static void main(String[] args) { |
| 130 | + |
| 131 | + Application.launch(args); |
| 132 | + } |
| 133 | +} |
0 commit comments