use of it.geosolutions.geoserver.rest.encoder.GSLayerEncoder in project coastal-hazards by USGS-CIDA.
the class GeoserverUtil method addRasterLayer.
public static Service addRasterLayer(String geoServerEndpoint, InputStream zipFileStream, String layerId, Bbox bbox, String EPSGcode) throws FileNotFoundException, IOException {
Service rasterService = null;
String fileId = UUID.randomUUID().toString();
String realFileName = TempFileResource.getFileNameForId(fileId);
// temp file must not include fileId, it should include the realFileName. We don't hand out the realFileName.
File tempFile = new File(TempFileResource.getTempFileSubdirectory(), realFileName);
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(tempFile);
// this is the renamed zip file (the raster tif)
IOUtils.copy(zipFileStream, fileOut);
} catch (IOException ex) {
throw new RuntimeException("Error writing zip to file '" + tempFile.getAbsolutePath() + "'.", ex);
} finally {
IOUtils.closeQuietly(zipFileStream);
IOUtils.closeQuietly(fileOut);
}
// tempFile should now have all the data transferred to it
// this puts it under <tomcat>/temp/cch-temp<randomkeyA>/<randomkeyB> without the .zip ext
log.info("Data should now have been copied to the tempFile located here:" + tempFile.getAbsoluteFile());
log.info("The file id is: " + fileId);
String uri = props.getProperty("coastal-hazards.base.url");
log.info("The uri from the props is: " + uri);
uri += DataURI.DATA_SERVICE_ENDPOINT + DataURI.TEMP_FILE_PATH + "/" + fileId;
// String zipUrl <- create a url for the retrieval of the file ... this is now coastal-hazards-portal/temp-file/<randommonkey>
String unzippedFilePath = null;
try {
// get the security token from DynamicReadOnlyProperties
String token = props.getProperty("gov.usgs.cida.coastalhazards.wps.fetch.and.unzip.process.token");
// call the wps process
unzippedFilePath = importRasterUsingWps(token, uri);
} finally {
// regardless of success or failure of wps proc
tempFile.delete();
log.info("Deleted contents of temp file");
}
if (unzippedFilePath == null || unzippedFilePath.isEmpty()) {
log.error("File path to unzipped geotiff returned null. Is the Geoserver wps fetchAndUnzip call working? ");
throw new RuntimeException("Error attempting to call wps process '" + tempFile.getAbsolutePath() + "'.");
}
log.info("______File path to unzipped raster on the portal is: " + unzippedFilePath);
File unzippedFile = new File(unzippedFilePath);
String fileName = unzippedFile.getName();
// Publish the raster tiff as a layer on Geoserver
GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoServerEndpoint, geoserverUser, geoserverPass);
// Then use the GeoServerRESTPublisher to create the stores and layers.
GSCoverageEncoder coverageEncoder = new GSCoverageEncoder();
// potential todo: set coverageEncoder description here based on text from the metadata file, or from a service parameter
// this would provide descriptive info for a user browsing our GeoServer with a generic WMS client
// (fileName); //BUG noted below : goeserver v2.4 requires the file name match the coverage name
coverageEncoder.setName(fileName);
coverageEncoder.setNativeName(fileName);
coverageEncoder.setTitle(fileName);
coverageEncoder.setSRS(EPSGcode);
coverageEncoder.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED);
GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setDefaultStyle(DEFAULT_RASTER_STYLE);
// Geoserver Manager BUG Alert for v2.4 ...Creating a GeoTIFF coverage via REST works if the coverage's name is the same as the store name, but fails otherwise. Setting nativeName or nativeCoverageName does not help. Changing the name after creating the coverage works fine.
// #TODO# what to do if the workspace:fileName is already in use?
RESTCoverageStore store = publisher.publishExternalGeoTIFF(PROXY_WORKSPACE, fileName, unzippedFile, coverageEncoder, layerEncoder);
if (null == store) {
// if store or layer creation failed
log.info("Error publishing GeoTiff in GeoServer.");
rasterService = null;
} else {
String newLayerName = store.getWorkspaceName() + ":" + fileName;
log.info("Published GeoTiff!!!");
log.info("In GeoserverUtil, about to add wmsService with layer name: " + layerId);
rasterService = wmsService(newLayerName);
log.info("Added layer to wms service.");
}
return rasterService;
}
use of it.geosolutions.geoserver.rest.encoder.GSLayerEncoder in project sldeditor by robward-scisys.
the class GeoServerClient method updateLayerStyles.
/**
* Update layer styles.
*
* @param layer the original layer
* @return true, if successful
*/
@Override
public boolean updateLayerStyles(GeoServerLayer layer) {
if (layer == null) {
return false;
}
StyleWrapper updatedStyle = layer.getStyle();
boolean ok = false;
GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
GeoServerRESTPublisher publisher = manager.getPublisher();
if (publisher != null) {
GSLayerEncoder layerEncoder = new GSLayerEncoder();
String defaultStyle;
if (isDefaultWorkspace(updatedStyle.getWorkspace())) {
defaultStyle = updatedStyle.getStyle();
} else {
defaultStyle = updatedStyle.getWorkspace() + ":" + updatedStyle.getStyle();
}
layerEncoder.setDefaultStyle(defaultStyle);
ok = publisher.configureLayer(layer.getLayerWorkspace(), layer.getLayerName(), layerEncoder);
}
return ok;
}
Aggregations