The majority of formats require the same two steps for visualization on a GXY view. To visualize GeoPackage data:

  1. Decode the data into an ILcdModel using an ILcdModelDecoder.

  2. Create an ILcdGXYLayer for the ILcdModel and add it to the ILcdGXYView.

//First create the model
ILcdModelDecoder decoder =
    new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
ILcdModel model = decoder.decode("Data/GeoPackage/states.gpkg");

//Create a layer for the model with default styling
ILcdGXYLayer layer = createGXYGeoPackageLayer(model);
//Wrap the layer with an async layer wrapper to ensure
//that the view remains responsive while data is being painted
layer = ILcdGXYAsynchronousLayerWrapper.create(layer);

//Add the async layer to the GXY view (an ILcdGXYView)
view.addGXYLayer(layer);

A GeoPackage file can contain:

  • One or more vector data sets

  • One or more raster data sets

  • A mix of the above

The actual creation of the layer depends on the kind of model:

/**
 * A GeoPackage file can contain:
 * <ul>
 *   <li>one or more vector data sets</li>
 *   <li>one or more raster data sets</li>
 *   <li>a mix of both</li>
 * </ul>
 * This method can deal with all those scenarios
 */
static ILcdGXYLayer createGXYGeoPackageLayer(ILcdModel model) {
  //The model decoder returns a model tree node when the file contains multiple data sets
  //The node itself is just an empty container. We create an empty layer node for that container.
  //Then we loop over the child models (=the actual data), and create layers for those as well
  if (model instanceof ILcdModelContainer) {
    TLcdGXYLayerTreeNode node = new TLcdGXYLayerTreeNode(model);
    for (int i = 0; i < ((ILcdModelContainer) model).modelCount(); i++) {
      ILcdModel childModel = ((ILcdModelContainer) model).getModel(i);
      //Use recursion to create layers for the child models
      node.addLayer(createGXYGeoPackageLayer(childModel));
    }
    return node;
  } else if (model.getModelDescriptor() instanceof TLcdGeoPackageModelDescriptor) {
    //The TLcdGXYLayer.create method can deal with both raster and vector data,
    //so no need to check what the model contains
    //The class javadoc of TLcdGeoPackageModelDescriptor describes how to
    //discover whether the model contains vector or raster data

    return TLcdGXYLayer.create(model);
  }
  throw new IllegalArgumentException("Cannot create layer for model " + model);
}

The resulting layer(s) use the default raster or vector styling.

This code snippet uses the TLcdCompositeModelDecoder, initialized with all model decoders available in the service registry.

The model decoder class that is actually responsible for decoding GeoPackage data is the TLcdGeoPackageModelDecoder.