ArcGIS Runtime SDK for Java

Read GeoPackage

Demonstrates how to read rasters and feature tables from geoPackages to show them as layers in a map.

How it works

To read layers from a geoPackage and show them in a map:

  1. Create a GeoPackage with the path to the local geoPackage file.
  2. Load the geoPackage with GeoPackage.loadAsync.
  3. Create raster layers for each of these with new RasterLayer(geoPackageRaster).
  4. Add each layer to the map as an operational layer with map.getOperationalLayers().add(layer).
  5. When it's done loading, get the GeoPackageFeatureTables inside with geoPackage .getGeoPackageFeatureTables().
  6. For each feature table, create a feature layer with new FeatureLayer(featureTable).
  7. You can also get the GeoPackageRasters inside using GeoPackage.getGeoPackageRasters().


 * Copyright 2018 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
 * 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.



import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.util.Callback;

import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.layers.RasterLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;

public class ReadGeoPackageSample extends Application {

  private MapView mapView;

  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("Read GeoPackage Sample");

      // create a map and add it to the map view
      final ArcGISMap map = new ArcGISMap(Basemap.Type.STREETS, 39.7294, -104.8319, 11);
      mapView = new MapView();

      // create two list views, one showing the layers in the map,
      // the other, showing the layers in the geoPackage not yet added to the map
      ListView<Layer> mapLayers = new ListView<>();
      ListView<Layer> geoPackageLayers = new ListView<>();

      // create labels for the lists
      Label mapLayersLabel = new Label("Map layers");
      Label geoPackageLayersLabel = new Label("GeoPackage layers (not in the map)");

      // create a control panel
      VBox controlsVBox = new VBox(6);
      controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
      controlsVBox.setPadding(new Insets(10.0));
      controlsVBox.setMaxSize(250, 260);

      // add labels and lists to the control panel
      controlsVBox.getChildren().addAll(mapLayersLabel, mapLayers, geoPackageLayersLabel, geoPackageLayers);

      // create a cell factory to show the layer names in the list view
      Callback<ListView<Layer>, ListCell<Layer>> cellFactory = list -> new ListCell<Layer>() {

        protected void updateItem(Layer layer, boolean bln) {

          super.updateItem(layer, bln);
          if (layer != null) {
            if (layer instanceof FeatureLayer) {
              FeatureLayer featureLayer = (FeatureLayer) layer;
            } else if (layer instanceof RasterLayer) {
              RasterLayer rasterLayer = (RasterLayer) layer;
              // use the raster file name if the raster layer name is empty
              String path = rasterLayer.getRaster().getPath();
              setText(rasterLayer.getName().isEmpty() ? path.substring(path.lastIndexOf('/') + 1) : rasterLayer.getName());
          } else {



      // when you click on a layer in the geopackage layers list view, add it to the map
      geoPackageLayers.setOnMouseClicked(e -> {
        if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
          // get selected layer
          Layer layer = geoPackageLayers.getSelectionModel().getSelectedItem();
          if (layer != null) {
            // add it to the map and the top of the map layers list
            mapLayers.getItems().add(0, layer);
            // remove it from the geoPackage layers list

      // when you click on a layer in the map layers list view, remove it from the map
      mapLayers.setOnMouseClicked(e -> {
        if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
          // get selected layer
          Layer layer = mapLayers.getSelectionModel().getSelectedItem();
          if (layer != null) {
            // remove it from the map and the map layers list
            // add it back to the geoPackage layers list

      // read the raster and feature layers from the geoPackage and show them in the list view
      File geoPackageFile = new File("./samples-data/auroraCO/AuroraCO.gpkg");
      GeoPackage geoPackage = new GeoPackage(geoPackageFile.getAbsolutePath());
      geoPackage.addDoneLoadingListener(() -> {
        geoPackage.getGeoPackageRasters().forEach(raster -> {
          RasterLayer rasterLayer = new RasterLayer(raster);
          // make the raster layer semi-transparent so we can see layers below it
        geoPackage.getGeoPackageFeatureTables().forEach(table -> {
          FeatureLayer featureLayer = new FeatureLayer(table);

      // add the map view and control box to stack pane
      stackPane.getChildren().addAll(mapView, controlsVBox);
      StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);
      StackPane.setMargin(controlsVBox, new Insets(10, 0, 0, 10));
    } catch (Exception e) {
      // on any error, display the stack trace

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

    if (mapView != null) {

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



In this topic
  1. How it works
  2. Code