Skip to content

Commit 060ee24

Browse files
authored
Merge pull request #206 from Esri/tschie/symbolize-shapefile
Implementation of symbolize shapefile sample
2 parents feaae3e + 4105086 commit 060ee24

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h1>Symbolize Shapefile</h1>
2+
3+
<p>Demonstrates how to override symbols of the default renderer for a shapefile.</p>
4+
5+
<p><img src="SymbolizeShapefile.png"/></p>
6+
7+
<h2>How to use the sample</h2>
8+
9+
<p>Press the toggle button to switch between red and yellow symbols and the default renderer.</p>
10+
11+
<h2>How it works</h2>
12+
13+
<p>To change the renderer of a shapefile feature layer:</p>
14+
15+
<ol>
16+
<li>Create a <code>ShapefileFeatureTable</code> passing in the URL of a shapefile.</li>
17+
<li>Create a <code>FeatureLayer</code> using the <code>ShapefileFeatureTable</code>.</li>
18+
<li>Create a <code>SimpleLineSymbol</code> and <code>SimpleFillSymbol</code> (uses the line symbol).</li>
19+
<li>Make a <code>SimpleRenderer</code> with the <code>SimpleFillSymbol</code>.</li>
20+
<li>To apply the renderer, use <code>featureLayer.setRenderer(renderer)</code>.</li>
21+
<li>To go back to the default renderer, use <code>featureLayer.resetRenderer()</code>.</li>
22+
</ol>
23+
24+
<h2>Features</h2>
25+
26+
<ul>
27+
<li>FeatureLayer</li>
28+
<li>ShapefileFeatureTable</li>
29+
<li>SimpleFillSymbol</li>
30+
<li>SimpleLineSymbol</li>
31+
<li>SimpleRenderer</li>
32+
</ul>
33+
34+
35+
403 KB
Loading
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.symbology.symbolize_shapefile;
18+
19+
import java.io.File;
20+
21+
import javafx.application.Application;
22+
import javafx.geometry.Insets;
23+
import javafx.geometry.Pos;
24+
import javafx.scene.Scene;
25+
import javafx.scene.control.Alert;
26+
import javafx.scene.control.ToggleButton;
27+
import javafx.scene.layout.StackPane;
28+
import javafx.stage.Stage;
29+
30+
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
31+
import com.esri.arcgisruntime.layers.FeatureLayer;
32+
import com.esri.arcgisruntime.loadable.LoadStatus;
33+
import com.esri.arcgisruntime.mapping.ArcGISMap;
34+
import com.esri.arcgisruntime.mapping.Basemap;
35+
import com.esri.arcgisruntime.mapping.view.MapView;
36+
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
37+
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
38+
import com.esri.arcgisruntime.symbology.SimpleRenderer;
39+
40+
public class SymbolizeShapefileSample extends Application {
41+
42+
private MapView mapView;
43+
44+
@Override
45+
public void start(Stage stage) throws Exception {
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("Symbolize Shapefile Sample");
54+
stage.setWidth(800);
55+
stage.setHeight(700);
56+
stage.setScene(scene);
57+
stage.show();
58+
59+
// create a map with a basemap
60+
ArcGISMap map = new ArcGISMap(Basemap.createTopographic());
61+
62+
// set the map to the map view
63+
mapView = new MapView();
64+
mapView.setMap(map);
65+
66+
// create a shapefile feature table from the local data
67+
File shapefile = new File("./samples-data/auroraCO/Subdivisions.shp");
68+
ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(shapefile.getAbsolutePath());
69+
70+
// use the shapefile feature table to create a feature layer
71+
FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable);
72+
featureLayer.addDoneLoadingListener(() -> {
73+
if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
74+
// zoom to the feature layer's extent
75+
mapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
76+
} else {
77+
Alert alert = new Alert(Alert.AlertType.ERROR, featureLayer.getLoadError().getMessage());
78+
alert.show();
79+
}
80+
});
81+
82+
// add the feature layer to the map
83+
map.getOperationalLayers().add(featureLayer);
84+
85+
// create the symbols and renderer
86+
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF0000, 1.0f);
87+
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0xFFFFFF00, lineSymbol);
88+
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
89+
90+
// create a toggle button to switch between renderers
91+
ToggleButton symbolizeButton = new ToggleButton("Toggle Symbology");
92+
symbolizeButton.setOnAction(e -> {
93+
if (symbolizeButton.isSelected()) {
94+
featureLayer.setRenderer(renderer);
95+
} else {
96+
// switch back to the default renderer
97+
featureLayer.resetRenderer();
98+
}
99+
});
100+
101+
// add the map view and toggle button to the stack pane
102+
stackPane.getChildren().addAll(mapView, symbolizeButton);
103+
StackPane.setAlignment(symbolizeButton, Pos.TOP_LEFT);
104+
StackPane.setMargin(symbolizeButton, new Insets(10, 0, 0, 10));
105+
} catch (Exception e) {
106+
// on any error, display the stack trace.
107+
e.printStackTrace();
108+
}
109+
}
110+
111+
/**
112+
* Stops and releases all resources used in application.
113+
*/
114+
@Override
115+
public void stop() throws Exception {
116+
117+
if (mapView != null) {
118+
mapView.dispose();
119+
}
120+
}
121+
122+
/**
123+
* Opens and runs application.
124+
*
125+
* @param args arguments passed to this application
126+
*/
127+
public static void main(String[] args) {
128+
129+
Application.launch(args);
130+
}
131+
132+
}

0 commit comments

Comments
 (0)