RESTStructuredGridCoverageExamples - simboss/geoserver-manager GitHub Wiki
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 String
s 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.
Latelly a new entity has been created to deal with StructuredGridCoverageReaders:
public GeoServerRESTStructuredGridCoverageReaderManager(URL restURL, String username, String pw);
In the following a few brief examples on how to use the library are provided.
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);
boolean created = publisher.createWorkspace("myWorkspace")
File sldFile = ...;
boolean published = publisher.publishStyle(sldFile); // Will take the name from SLD contents
File sldFile = ...;
boolean published = publisher.publishStyle(sldFile, "myStyle");
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");
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
Layer Group REST encoding has changed in GeoServer 2.3: if you want to read Layer Groups in GeoServer 2.3 or greater, you need at least geoserver-manager 1.5.1.
To get all Layer Groups:
RESTLayerGroupList allGroups = reader.getLayerGroups();
RESTLayerGroupList workspaceGroups = reader.getLayerGroups(workspace);
To get a specific Layer Group:
RESTLayerGroup groupReader = reader.getLayerGroup(groupName);
RESTLayerGroup groupReader = reader.getLayerGroup(workspace, groupName);
Available properties in GeoServer 2.2:
groupReader.getName();
groupReader.getWorkspace();
groupReader.getCRS();
groupReader.getMinX();
groupReader.getMaxX();
groupReader.getMinY();
groupReader.getMaxY();
Additional properties in GeoServer 2.3:
groupReader.getMode();
groupReader.getTitle();
groupReader.getAbstract();
groupReader.getRootLayer();
To get a list of layers contained in the group, in GeoServer 2.2:
RESTLayerList layers = groupReader.getLayerList();
Since GeoServer 2.3, a Layer Group can contain also other Layer Groups, its xml encoding doesn't contain a list of layers
, but a list of publishable
elements.
To get a list of layers and layer groups contained in the group, in GeoServer 2.3:
RESTPublishedList publishables = groupReader.getPublishedList();
To configure a Layer Group for GeoServer 2.2 you can use the encoder GSLayerGroupEncoder
.
For GeoServer 2.3 you must use GSLayerGroupEncoder23
.
To create a new Layer Group:
GSLayerGroupEncoder23 groupWriter = new GSLayerGroupEncoder23();
groupWriter.addLayer("topp:cities");
groupWriter.addLayerGroup("tasmania");
publisher.createLayerGroup(workspace, groupName, groupWriter)
To update an existing Layer Group:
publisher.configureLayerGroup(workspace, groupName, groupWriter)
String RESTURL = "http://localhost:8080/geoserver";
String RESTUSER = "admin";
String RESTPW = "geoserver";
GeoServerRESTStructuredGridCoverageReaderManager manager =
new GeoServerRESTStructuredGridCoverageReaderManager(new URL(RESTURL), RESTUSER, RESTPW);
To get a StructuredCoverage Index Schema:
RESTStructuredCoverageIndexSchema indexFormat = manager.getGranuleIndexSchema("myWorkspace", "myStore", "myCoverage");
This is the XML REST schema representation:
<Schema>
<attributes>
<Attribute>
<name>the_geom</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>com.vividsolutions.jts.geom.Polygon</binding>
</Attribute>
<Attribute>
<name>location</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.lang.String</binding>
</Attribute>
<Attribute>
<name>imageindex</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.lang.Integer</binding>
</Attribute>
<Attribute>
<name>time</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.sql.Timestamp</binding>
</Attribute>
<Attribute>
<name>elevation</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.lang.Double</binding>
</Attribute>
<Attribute>
<name>fileDate</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.sql.Timestamp</binding>
</Attribute>
<Attribute>
<name>updated</name>
<minOccurs>0</minOccurs>
<maxOccurs>1</maxOccurs>
<nillable>true</nillable>
<binding>java.sql.Timestamp</binding>
</Attribute>
</attributes>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/myWorkspace/coveragestores/myStore/coverages/myCoverage/index/granules.xml" type="application/xml"/>
</Schema>
To get all granules from myCoverage:
RESTStructuredCoverageGranulesList granulesList =
manager.getGranules("myWorkspace", "myStore", "myCoverage", null, null, null);
To get only 10 granules from myCoverage, starting from an offset of 20:
RESTStructuredCoverageGranulesList granulesList =
manager.getGranules("myWorkspace", "myStore", "myCoverage", null, 20, 10);
To get only the granules from myCoverage, having depth = 100 and time = 20081101T0000000:
RESTStructuredCoverageGranulesList granulesList =
manager.getGranules("myWorkspace", "myStore", "myCoverage",
"depth = 100 AND time='20081101T0000000'", null, null);
To get a granule by Id (=43):
RESTStructuredCoverageGranulesList granulesList =
manager.getGranuleById("myWorkspace", "myStore", "myCoverage", "43");
To remove all granules from myCoverage, related to file having location = /data/sampleFile.tif.
final String fileLocation = "/data/sampleFile.tif";
boolean result = manager.removeGranulesByCQL("myWorkspace", "myStore", "myCoverage",
"location = '" + fileLocation + "'");