Local Server feature layer

View on GitHubSample viewer app

Start a local feature service and display its features in a map.

Image of local server feature layer

Use case

For executing offline geoprocessing tasks in your ArcGIS Runtime apps via an offline (local) server.

How to use the sample

A Local Server and Local Feature Service will automatically be started. Once started then a FeatureLayer will be created and added to the map.

How it works

  1. Create and run a local server with LocalServer.INSTANCE.
  2. Start the server asynchronously with Server.startAsync().
  3. Wait for server to be in the LocalServerStatus.STARTED state.
    • Callbacks attached to Server.addStatusChangedListener() will invoke whenever the status of the local server has changed.
  4. Create and run a local feature service.
    1. Instantiate LocalFeatureService(Url) to create a local feature service with the given url path to mpkx file.
    2. Start the service asynchronously with LocalFeatureService.startAsync().
      • The service will be added to the local server automatically.
  5. Wait for state of the feature service to be LocalServerStatus.STARTED.
    • Callbacks attached to LocalFeatureService.addStatusChangedListener() will invoke whenever the status of the local service has changed.
  6. Create a feature layer from local feature service.
    1. Create a ServiceFeatureTable(Url) from local feature service url provided by LocalFeatureService.getUrl().
    2. Load the table asynchronously using ServiceFeatureTable.loadAsync().
    3. Create feature layer from service feature table using new FeatureLayer(ServiceFeatureTable).
    4. Load the layer asynchronously using FeatureLayer.loadAsync().
  7. Add feature layer to map using ArcGISMap.getOperationalLayers().add(FeatureLayer).

Relevant API

  • FeatureLayer
  • LocalFeatureService
  • LocalServer
  • LocalServerStatus
  • StatusChangedEvent

Additional information

Local Server can be downloaded for Windows and Linux platforms from your ArcGIS Developers dashboard. Local Server is not supported on macOS.

Specific versions of ArcGIS Runtime Local Server are compatible with the version of ArcGIS Pro you use to create geoprocessing and map packages. For example, the ArcGIS Runtime API for Java v100.11.0 is configured for Local Server v100.10.0 which provides compatibility for packages created with ArcGIS Pro 2.7.x. For more information see the ArcGIS Developers guide.

To configure the ArcGIS Runtime API for Java v100.11.0 to work with Local Server 100.9.0:

  • Development machine:
    • Locate the Local Server installation directory and rename the folder from LocalServer100.9 to LocalServer100.10.
    • Update the environment variable from RUNTIME_LOCAL_SERVER_100_9 to RUNTIME_LOCAL_SERVER_100_10.
  • Deployment machine(s): Rename the deployment folder included with your application (or referenced by the LocalServerEnvironment.InstallPath property) from LocalServer100.9 to LocalServer100.10.

Tags

feature service, local, offline, server, service

Sample Code

LocalServerFeatureLayerSample.java
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright 2017 Esri.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.esri.samples.local_server_feature_layer;

import java.io.File;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.localserver.LocalFeatureService;
import com.esri.arcgisruntime.localserver.LocalServer;
import com.esri.arcgisruntime.localserver.LocalServerStatus;
import com.esri.arcgisruntime.localserver.LocalService.StatusChangedEvent;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;

public class LocalServerFeatureLayerSample extends Application {

  private ArcGISMap map;
  private FeatureLayer featureLayer; // keep loadable in scope to avoid garbage collection
  private LocalFeatureService featureService;
  private MapView mapView;
  private ProgressIndicator featureLayerProgress;

  private static LocalServer server;

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Local Server Feature Layer Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a map with the streets basemap style
      map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      // track progress of loading feature layer to map
      featureLayerProgress = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS);
      featureLayerProgress.setMaxWidth(30);

      // check that local server install path can be accessed
      if (LocalServer.INSTANCE.checkInstallValid()) {
        server = LocalServer.INSTANCE;
        server.addStatusChangedListener(status -> {
          if (server.getStatus() == LocalServerStatus.STARTED) {
            // start feature service
            File mpkxFile = new File(System.getProperty("data.dir"), "./samples-data/local_server/PointsofInterest.mpkx");
            featureService = new LocalFeatureService(mpkxFile.getAbsolutePath());
            featureService.addStatusChangedListener(this::addLocalFeatureLayer);
            featureService.startAsync();
          }
        });
        // start local server
        server.startAsync();
      } else {
        Platform.runLater(() -> {
          Alert dialog = new Alert(AlertType.INFORMATION);
          dialog.initOwner(mapView.getScene().getWindow());
          dialog.setHeaderText("Local Server Load Error");
          dialog.setContentText("Local Geoprocessing Failed to load.");
          dialog.showAndWait();

          Platform.exit();
        });
      }

      // add the map view and progress indicator to the stack pane
      stackPane.getChildren().addAll(mapView, featureLayerProgress);
      StackPane.setAlignment(featureLayerProgress, Pos.CENTER);
    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Once the feature service starts, a feature layer is created from that service and added to the map.
   * <p>
   * When the feature layer is done loading the view will zoom to the location of were the features were added.
   *
   * @param status status of feature service
   */
  private void addLocalFeatureLayer(StatusChangedEvent status) {

    // check that the feature service has started
    if (status.getNewStatus() == LocalServerStatus.STARTED && mapView.getMap() != null) {
      // get the url of where feature service is located
      String url = featureService.getUrl() + "/0";
      // create a feature layer using the url
      ServiceFeatureTable featureTable = new ServiceFeatureTable(url);
      featureTable.loadAsync();
      featureLayer = new FeatureLayer(featureTable);
      featureLayer.addDoneLoadingListener(() -> {
        Envelope extent = featureLayer.getFullExtent();
        if (featureLayer.getLoadStatus() == LoadStatus.LOADED && extent != null) {
          mapView.setViewpoint(new Viewpoint(extent));
          Platform.runLater(() -> featureLayerProgress.setVisible(false));
        } else {
          new Alert(Alert.AlertType.ERROR, "Feature Layer Failed to Load!").show();
        }
      });
      featureLayer.loadAsync();
      // add feature layer to map
      map.getOperationalLayers().add(featureLayer);

    }
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

    if (mapView != null) {
      mapView.dispose();
    }
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.