Skip to content

Various Examples

n-lagomarsini edited this page Sep 22, 2014 · 7 revisions

Intro

This library allows to communicate to GeoServer via REST using a Java library.

It uses only a few external libraries, such as apache-http-common and jdom.

You will only need to instantiate GeoServerRESTReader for reading data from a running GeoServer instance

public GeoServerRESTReader(String restUrl, String username, String password);

or GeoServerRESTPublisher to modify the catalog (publish or remove styles, featuretypes or coverages).

public GeoServerRESTPublisher(String restURL, String username, String pw);

These classes just handles the REST URLs, and the upper level logic (e.g.: publishing a layer means sending the data, optionally a style, and configuring the layer). The basic HTTP calls are handled by HTTPUtils.

There are no beans mirroring GeoServer's ones. The info for publishing layers are simple Strings in method calls.

The info read from GeoServer are stored as XML elements and parsed using JDOM only when requested. The parsing is performed by the classes in the decoder package.

Some examples

In the following a few brief examples on how to use the library are provided.

Init reader and publisher

        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        
        GeoServerRESTReader reader = new GeoServerRESTReader(RESTURL, RESTUSER, RESTPW);
        GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW);

Create a Workspace

        boolean created = publisher.createWorkspace("myWorkspace")

Create a Style

        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile); // Will take the name from SLD contents
        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile, "myStyle");

You may find other doc about styles here.

Create a layer from a Shapefile

        File zipFile = ...;
        boolean published = publisher.publishShp("myWorkspace", "myStore", "cities", zipFile, "EPSG:4326", "default_point");

Note that the layername ("cities") should be the same as the shapefile in the .zip file. In our case, we have "cities.shp" in the zipfile.

If you want to remove this layer entirely from the configuration, you will have to remove the layer and the datastore:

        boolean ftRemoved = publisher.unpublishFeatureType("myWorkspace", "myStore", "cities");
        // remove  datastore
        boolean dsRemoved = publisher.removeDatastore("myWorkspace", "myStore");

Create a layer from a DB table

        boolean ok = publisher.publishDBLayer("myWorkspace", "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon");
  • ''pg_kids'' is a PostGIS datastore already configured in GeoServer
  • ''easia_gaul_0_aggr'' is a table that has not yet been published in GeoServer

Check if a resource exists

        boolean exists = reader.existsDatastore("myWorkspace", "myStore");

The result will be true if the datastore exists, otherwise false will be returned. The same behaviour can be applied to other resources:

  • Coverage stores
        boolean exists = reader.existsCoveragestore("myWorkspace", "myStore");
  • Coverages
        boolean exists = reader.existsCoverage("myWorkspace", "myStore", "myCoverage");
  • Feature Typse
        boolean exists = reader.existsFeatureType("myWorkspace", "myStore", "myFeatureType");
  • Granules (NOTE: useful only for StructuredCoverages like ImageMosaic!!!)
        boolean exists = reader.existsGranule("myWorkspace", "myStore", "myCoverage", "myGranuleId");
  • Layers
        boolean exists = reader.existsLayer("myWorkspace", "myLayer");
  • LayerGroups
        boolean exists = reader.existsLayerGroup("myWorkspace", "myLayerGroup");
  • Namespaces
        boolean exists = reader.existsNamespace("myNamespace");
  • Styles
        boolean exists = reader.existsStyle("myStyle");
  • Workspaces
        boolean exists = reader.existsWorkspace("myWorkspace");

Note that also exists another version of the same method with a boolean parameter called quietOnNotFound which at the moment will not be used, because is a feature introduced with GeoServer 2.6. This parameter avoids to throw an exception when a resource has not been found.

Layer Groups

Check this page.

Working with ImageMosaic layers

Check this page.

Working with Styles

Check this page.