Using new publishGeoTIFF method (not deprecated)

This commit is contained in:
Francesco Mangiacrapa 2024-05-09 11:31:33 +02:00
parent 2dd01c0c82
commit faa01e40da
1 changed files with 246 additions and 255 deletions

View File

@ -44,24 +44,22 @@ import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class SDIManagerWrapper extends SDIManager{ public class SDIManagerWrapper extends SDIManager {
public SDIManagerWrapper() throws SDIInteractionException { public SDIManagerWrapper() throws SDIInteractionException {
} }
/** /**
* Expected paramters : * Expected paramters : - "workspace" - "layerTitle" - "documentID" -
* - "workspace" * "basePersistencePath" (useCaseDescriptor specific, e.g. "GNA")
* - "layerTitle"
* - "documentID"
* - "basePersistencePath" (useCaseDescriptor specific, e.g. "GNA")
* *
* @param fileSet * @param fileSet
* @param params * @param params
* @return * @return
* @throws SDIInteractionException * @throws SDIInteractionException
*/ */
public RegisteredFileSet materializeLayer(RegisteredFileSet fileSet, Document params) throws SDIInteractionException{ public RegisteredFileSet materializeLayer(RegisteredFileSet fileSet, Document params)
throws SDIInteractionException {
try { try {
log.debug("Materializing FS {} on {} ", fileSet, getGeoserverHostName()); log.debug("Materializing FS {} on {} ", fileSet, getGeoserverHostName());
@ -70,48 +68,45 @@ public class SDIManagerWrapper extends SDIManager{
String documentID = BaseExecutionRequest.getMandatory("documentID", params); String documentID = BaseExecutionRequest.getMandatory("documentID", params);
String basePersistencePAth = BaseExecutionRequest.getMandatory("basePersistencePath", params); String basePersistencePAth = BaseExecutionRequest.getMandatory("basePersistencePath", params);
// check if empty // check if empty
if (fileSet.getPayloads().isEmpty()) throw new SDIInteractionException("No payload to materialize"); if (fileSet.getPayloads().isEmpty())
throw new SDIInteractionException("No payload to materialize");
// Document geoserverInfo = new Document(); // Document geoserverInfo = new Document();
GCubeSDILayerBuilder layerBuilder=new GCubeSDILayerBuilder(); GCubeSDILayerBuilder layerBuilder = new GCubeSDILayerBuilder();
layerBuilder.setWorkspace(workspace); layerBuilder.setWorkspace(workspace);
layerBuilder.setHost(getGeoserverHostName()); layerBuilder.setHost(getGeoserverHostName());
// Evaluate layer data in filesets // Evaluate layer data in filesets
// TODO optimize cicles // TODO optimize cicles
List<SupportedFormat> toCheckFormats=SupportedFormat.getByExtension(".tif",".shp"); List<SupportedFormat> toCheckFormats = SupportedFormat.getByExtension(".tif", ".shp");
SupportedFormat selectedFormat = null; SupportedFormat selectedFormat = null;
String baseName=null; String baseName = null;
for(SupportedFormat format: toCheckFormats){ for (SupportedFormat format : toCheckFormats) {
log.debug("Checking for {}",format); log.debug("Checking for {}", format);
for (Object o : fileSet.getPayloads()) { for (Object o : fileSet.getPayloads()) {
RegisteredFile file = Serialization.convert(o, RegisteredFile.class); RegisteredFile file = Serialization.convert(o, RegisteredFile.class);
format.consider(file); format.consider(file);
} }
if(format.getIsProposedFilesetValid()){ if (format.getIsProposedFilesetValid()) {
log.debug("Selected format is {}. Fileset is {}",format,format.getToUseFileSet()); log.debug("Selected format is {}. Fileset is {}", format, format.getToUseFileSet());
selectedFormat = format; selectedFormat = format;
// get basename for layer // get basename for layer
baseName = format.getToUseFileSet().get(0).getName(); baseName = format.getToUseFileSet().get(0).getName();
baseName= Files.fixFilename(baseName).substring(0,baseName.lastIndexOf('.')); baseName = Files.fixFilename(baseName).substring(0, baseName.lastIndexOf('.'));
break; break;
} }
} }
if(selectedFormat==null) if (selectedFormat == null)
throw new SDIInteractionException("Unable to identify layer format. Configured formats are "+toCheckFormats); throw new SDIInteractionException(
"Unable to identify layer format. Configured formats are " + toCheckFormats);
// Evaluate Layer Name // Evaluate Layer Name
log.debug("Base layer name is {}, checking conflicts.. ",baseName); log.debug("Base layer name is {}, checking conflicts.. ", baseName);
String toSetLayerName = baseName; String toSetLayerName = baseName;
//Checking if layer already exists // Checking if layer already exists
int count = 0; int count = 0;
GeoServerRESTReader gsReader = getCurrentGeoserver().getReader(); GeoServerRESTReader gsReader = getCurrentGeoserver().getReader();
while (gsReader.getLayer(workspace, toSetLayerName) != null) { while (gsReader.getLayer(workspace, toSetLayerName) != null) {
@ -122,9 +117,6 @@ public class SDIManagerWrapper extends SDIManager{
log.debug("Layer name will be {}", toSetLayerName); log.debug("Layer name will be {}", toSetLayerName);
layerBuilder.setLayerName(toSetLayerName); layerBuilder.setLayerName(toSetLayerName);
// Publishing layer in GS // Publishing layer in GS
String storeName = toSetLayerName + "_store"; String storeName = toSetLayerName + "_store";
layerBuilder.setStoreName(storeName); layerBuilder.setStoreName(storeName);
@ -133,37 +125,36 @@ public class SDIManagerWrapper extends SDIManager{
log.debug("Trying to create remote workspace : " + workspace); log.debug("Trying to create remote workspace : " + workspace);
createWorkspace(workspace); createWorkspace(workspace);
// Actually publishing files into GS // Actually publishing files into GS
Boolean published = null; Boolean published = null;
switch(selectedFormat.getFileExtension()){ switch (selectedFormat.getFileExtension()) {
case ".tif" : { case ".tif": {
RegisteredFile f = selectedFormat.getToUseFileSet().get(0); RegisteredFile f = selectedFormat.getToUseFileSet().get(0);
File temp= null; File temp = null;
try { try {
temp = Files.downloadFromUrl(f.getName(),f.getLink()); temp = Files.downloadFromUrl(f.getName(), f.getLink());
published = publisher.publishGeoTIFF(workspace, storeName, toSetLayerName,temp,EPSG_4326,REPROJECT_TO_DECLARED,"raster"); //Added by Francesco M.
}finally{ if (temp!=null) java.nio.file.Files.deleteIfExists(temp.toPath());} published = publisher.publishGeoTIFF(workspace, storeName, toSetLayerName, temp, EPSG_4326,
REPROJECT_TO_DECLARED, "raster", null);
// published = publisher.publishGeoTIFF(workspace, storeName,
// toSetLayerName,temp,EPSG_4326,REPROJECT_TO_DECLARED,"raster");
} finally {
if (temp != null)
java.nio.file.Files.deleteIfExists(temp.toPath());
}
break; break;
} }
case ".shp" :{ case ".shp": {
//TODO Evaluate SRS // TODO Evaluate SRS
String absolutePath = transferFilesetToGS(basePersistencePAth,documentID,selectedFormat.getToUseFileSet(), String absolutePath = transferFilesetToGS(basePersistencePAth, documentID,
fileSet.getUUID(),toSetLayerName,baseName,layerBuilder); selectedFormat.getToUseFileSet(), fileSet.getUUID(), toSetLayerName, baseName, layerBuilder);
URL directoryPath = new URL("file:" + absolutePath + "/" + toSetLayerName + ".shp"); URL directoryPath = new URL("file:" + absolutePath + "/" + toSetLayerName + ".shp");
published = publisher.publishShp( published = publisher.publishShp(workspace, storeName, null, toSetLayerName,
workspace,
storeName,
null,
toSetLayerName,
// UploadMethod.FILE, // neeeds zip // UploadMethod.FILE, // neeeds zip
GeoServerRESTPublisher.UploadMethod.EXTERNAL, // needs shp GeoServerRESTPublisher.UploadMethod.EXTERNAL, // needs shp
directoryPath.toURI(), directoryPath.toURI(), EPSG_4326, // SRS
EPSG_4326, //SRS
""); "");
break; break;
} }
@ -174,36 +165,36 @@ public class SDIManagerWrapper extends SDIManager{
// check if success // check if success
if (!published) { if (!published) {
throw new SDIInteractionException("Unable to publish layer " + toSetLayerName + " under " + workspace + ". Unknown Geoserver fault."); throw new SDIInteractionException("Unable to publish layer " + toSetLayerName + " under " + workspace
+ ". Unknown Geoserver fault.");
} }
RESTLayer l = gsReader.getLayer(workspace, toSetLayerName); RESTLayer l = gsReader.getLayer(workspace, toSetLayerName);
RESTFeatureType f = gsReader.getFeatureType(l); RESTFeatureType f = gsReader.getFeatureType(l);
layerBuilder.setBBOX(f.getMaxX(), f.getMinX(), f.getMaxY(), f.getMinY(), 0d, 0d);
layerBuilder.setBBOX(f.getMaxX(),f.getMinX(),f.getMaxY(),f.getMinY(),0d,0d);
GCubeSDILayer materialization = layerBuilder.getLayer(); GCubeSDILayer materialization = layerBuilder.getLayer();
log.info("Generated Materialization {}", materialization); log.info("Generated Materialization {}", materialization);
//Add Materialization to registered file set // Add Materialization to registered file set
List materializations = fileSet.getMaterializations(); List materializations = fileSet.getMaterializations();
if (materializations == null) materializations = new ArrayList(); if (materializations == null)
materializations = new ArrayList();
materializations.add(materialization); materializations.add(materialization);
fileSet.put(RegisteredFileSet.MATERIALIZATIONS, materializations); fileSet.put(RegisteredFileSet.MATERIALIZATIONS, materializations);
return fileSet; return fileSet;
}catch(SDIInteractionException e){ } catch (SDIInteractionException e) {
throw e; throw e;
}catch (Throwable t){ } catch (Throwable t) {
throw new SDIInteractionException("Unexpected exception while trying to materialize File Set "+t.getMessage(),t); throw new SDIInteractionException(
"Unexpected exception while trying to materialize File Set " + t.getMessage(), t);
} }
} }
private String transferFilesetToGS(String basePersistencePAth,String documentID,List<RegisteredFile> fileSet, private String transferFilesetToGS(String basePersistencePAth, String documentID, List<RegisteredFile> fileSet,
String fileSetUUID, String fileSetUUID, String toSetLayerName, String baseName, GCubeSDILayerBuilder layerBuilder)
String toSetLayerName, String baseName, GCubeSDILayerBuilder layerBuilder)
throws MalformedURLException, InvalidSourceException, SourceNotSetException, InvalidDestinationException, throws MalformedURLException, InvalidSourceException, SourceNotSetException, InvalidDestinationException,
DestinationNotSetException, InitializationException, FailedTransferException { DestinationNotSetException, InitializationException, FailedTransferException {
String folderRelativePath = basePersistencePAth + "/" + documentID + "/" + fileSetUUID + "/" + toSetLayerName; String folderRelativePath = basePersistencePAth + "/" + documentID + "/" + fileSetUUID + "/" + toSetLayerName;
@ -219,7 +210,6 @@ public class SDIManagerWrapper extends SDIManager{
String completeFilename = Files.fixFilename(file.getName()); String completeFilename = Files.fixFilename(file.getName());
completeFilename = completeFilename.replaceAll(baseName, toSetLayerName); completeFilename = completeFilename.replaceAll(baseName, toSetLayerName);
Destination destination = new Destination(completeFilename); Destination destination = new Destination(completeFilename);
destination.setCreateSubfolders(true); destination.setCreateSubfolders(true);
destination.setOnExistingFileName(DestinationClashPolicy.REWRITE); destination.setOnExistingFileName(DestinationClashPolicy.REWRITE);
@ -232,10 +222,11 @@ public class SDIManagerWrapper extends SDIManager{
TransferResult result = getDtGeoServer().httpSource(new URL(file.getLink()), destination); TransferResult result = getDtGeoServer().httpSource(new URL(file.getLink()), destination);
log.debug("Transferred " + result); log.debug("Transferred " + result);
filenames.add(completeFilename); filenames.add(completeFilename);
// NB Clash con subfolder is APPEND, thus FOLDER is expected to be the one specified by caller // NB Clash con subfolder is APPEND, thus FOLDER is expected to be the one
//geoserverInfo.put(""result.getRemotePath().substring(0, result.getRemotePath().lastIndexOf("/"))); // specified by caller
// geoserverInfo.put(""result.getRemotePath().substring(0,
// result.getRemotePath().lastIndexOf("/")));
absolutePath = result.getRemotePath().substring(0, result.getRemotePath().lastIndexOf("/")); absolutePath = result.getRemotePath().substring(0, result.getRemotePath().lastIndexOf("/"));
} }
layerBuilder.setFiles(filenames); layerBuilder.setFiles(filenames);
@ -243,105 +234,105 @@ public class SDIManagerWrapper extends SDIManager{
} }
public void deleteLayer(GCubeSDILayer toDelete) throws SDIInteractionException { public void deleteLayer(GCubeSDILayer toDelete) throws SDIInteractionException {
log.trace("Deleting {}",toDelete); log.trace("Deleting {}", toDelete);
try{ try {
AbstractGeoServerDescriptor gs=getCurrentGeoserver(); AbstractGeoServerDescriptor gs = getCurrentGeoserver();
GeoServerRESTPublisher publisher = gs.getPublisher(); GeoServerRESTPublisher publisher = gs.getPublisher();
for(Object platformObj : toDelete.getPlatformInfo()){ for (Object platformObj : toDelete.getPlatformInfo()) {
PlatformInfo info =Serialization.convert(platformObj, PlatformInfo.class); PlatformInfo info = Serialization.convert(platformObj, PlatformInfo.class);
switch(info.getType()){ switch (info.getType()) {
case GeoServerPlatform.GS_PLATFORM:{ case GeoServerPlatform.GS_PLATFORM: {
GeoServerPlatform gsInfo = Serialization.convert(info,GeoServerPlatform.class); GeoServerPlatform gsInfo = Serialization.convert(info, GeoServerPlatform.class);
log.trace("Deleting {} ",gsInfo); log.trace("Deleting {} ", gsInfo);
// remove store (recursion deletes related layers) // remove store (recursion deletes related layers)
log.trace("Removing datastore {}:{}",gsInfo.getWorkspace(),gsInfo.getStoreName()); log.trace("Removing datastore {}:{}", gsInfo.getWorkspace(), gsInfo.getStoreName());
if(!publisher.removeDatastore(gsInfo.getWorkspace(),gsInfo.getStoreName(),true)) if (!publisher.removeDatastore(gsInfo.getWorkspace(), gsInfo.getStoreName(), true))
throw new SDIInteractionException("Unable to remove store "+gsInfo.getWorkspace()+":"+gsInfo.getStoreName()); throw new SDIInteractionException(
"Unable to remove store " + gsInfo.getWorkspace() + ":" + gsInfo.getStoreName());
// remove ws if empty // remove ws if empty
log.trace("Checking if empty WS {}",gsInfo.getWorkspace()); log.trace("Checking if empty WS {}", gsInfo.getWorkspace());
if(gs.getReader().getDatastores(gsInfo.getWorkspace()).isEmpty()) if (gs.getReader().getDatastores(gsInfo.getWorkspace()).isEmpty())
if(!publisher.removeWorkspace(gsInfo.getWorkspace(),true)) if (!publisher.removeWorkspace(gsInfo.getWorkspace(), true))
throw new SDIInteractionException("Unable to remove WS "+gsInfo.getWorkspace()); throw new SDIInteractionException("Unable to remove WS " + gsInfo.getWorkspace());
// remove actual files data // remove actual files data
// TODO REMOVE HARDCODED PATCH // TODO REMOVE HARDCODED PATCH
String path="geoserver/"+gsInfo.getPersistencePath(); String path = "geoserver/" + gsInfo.getPersistencePath();
log.info("Deleting files at {}@{}",path,getGeoserverHostName()); log.info("Deleting files at {}@{}", path, getGeoserverHostName());
try{ try {
Client dtClient = new Client("https://"+getGeoserverHostName()); Client dtClient = new Client("https://" + getGeoserverHostName());
dtClient.delete(path); dtClient.delete(path);
// getDtGeoServer().getWebClient().delete("geoserver/"+path); // getDtGeoServer().getWebClient().delete("geoserver/"+path);
}catch(Exception e){ } catch (Exception e) {
throw new Exception("Data Transfer ERROR. Unable to delete files at "+getGeoserverHostName()+" PATH "+path,e); throw new Exception("Data Transfer ERROR. Unable to delete files at " + getGeoserverHostName()
+ " PATH " + path, e);
} }
break; break;
} }
default : { default: {
throw new SDIInteractionException("Unable to manage platform "+info); throw new SDIInteractionException("Unable to manage platform " + info);
} }
} }
} }
}catch(SDIInteractionException e){ } catch (SDIInteractionException e) {
throw e; throw e;
}catch (Throwable t){ } catch (Throwable t) {
throw new SDIInteractionException("Unexpected exception while trying to delete Layer : "+t.getMessage(),t); throw new SDIInteractionException("Unexpected exception while trying to delete Layer : " + t.getMessage(),
t);
} }
} }
private static class CQL_GS_Feature extends GSFeatureTypeEncoder{ private static class CQL_GS_Feature extends GSFeatureTypeEncoder {
private static final String CQL = "cqlFilter"; private static final String CQL = "cqlFilter";
public void setCQL(String cql){this.set(CQL,cql);}
public void setCQL(String cql) {
this.set(CQL, cql);
}
} }
public GCubeSDILayer configureCentroidLayer(String name, String workspace, String storeName, PostgisTable table, DatabaseConnection connection) throws SDIInteractionException { public GCubeSDILayer configureCentroidLayer(String name, String workspace, String storeName, PostgisTable table,
DatabaseConnection connection) throws SDIInteractionException {
GCubeSDILayerBuilder builder = new GCubeSDILayerBuilder() GCubeSDILayerBuilder builder = new GCubeSDILayerBuilder().setWorkspace(workspace).setStoreName(storeName)
.setWorkspace(workspace) .setHost(getGeoserverHostName()).setLayerName(name);
.setStoreName(storeName)
.setHost(getGeoserverHostName())
.setLayerName(name);
CQL_GS_Feature fte=new CQL_GS_Feature(); CQL_GS_Feature fte = new CQL_GS_Feature();
fte.setAbstract("Centroid layer for "+name); fte.setAbstract("Centroid layer for " + name);
fte.setEnabled(true); fte.setEnabled(true);
fte.setNativeCRS(WGS84_FULL); fte.setNativeCRS(WGS84_FULL);
fte.setTitle(name); fte.setTitle(name);
fte.setName(name); fte.setName(name);
fte.setCQL(DBConstants.Defaults.DISPLAYED+" = true"); fte.setCQL(DBConstants.Defaults.DISPLAYED + " = true");
fte.setLatLonBoundingBox(-180.0, -90.0, 180.0, 90.0, WGS84_FULL); fte.setLatLonBoundingBox(-180.0, -90.0, 180.0, 90.0, WGS84_FULL);
String style="clustered_centroids"; String style = "clustered_centroids";
GSLayerEncoder layerEncoder=new GSLayerEncoder(); GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setDefaultStyle(style); layerEncoder.setDefaultStyle(style);
layerEncoder.setEnabled(true); layerEncoder.setEnabled(true);
layerEncoder.setQueryable(true); layerEncoder.setQueryable(true);
try { try {
//Checking workspace // Checking workspace
createWorkspace(workspace); createWorkspace(workspace);
//Checking store // Checking store
createStoreFromPostgisDB(workspace, storeName,connection); createStoreFromPostgisDB(workspace, storeName, connection);
//Checking layer // Checking layer
publishStyle(Files.getFileFromResources("styles/clustered_points.sld"),style); publishStyle(Files.getFileFromResources("styles/clustered_points.sld"), style);
log.info("Creating layer in {} : {} with FTE {} , LE {}",workspace,storeName,fte,layerEncoder);
if(getCurrentGeoserver().getReader().getLayer(workspace, name)==null)
if(!getCurrentGeoserver().getPublisher().publishDBLayer(workspace, storeName, fte, layerEncoder))
throw new SDIInteractionException("Unable to create layer "+name);
log.debug("layer "+name+" already exists");
log.info("Creating layer in {} : {} with FTE {} , LE {}", workspace, storeName, fte, layerEncoder);
if (getCurrentGeoserver().getReader().getLayer(workspace, name) == null)
if (!getCurrentGeoserver().getPublisher().publishDBLayer(workspace, storeName, fte, layerEncoder))
throw new SDIInteractionException("Unable to create layer " + name);
log.debug("layer " + name + " already exists");
return builder.getLayer(); return builder.getLayer();
} catch (IllegalArgumentException | MalformedURLException e) { } catch (IllegalArgumentException | MalformedURLException e) {
throw new SDIInteractionException("Unable to create layer "+name,e); throw new SDIInteractionException("Unable to create layer " + name, e);
} }
} }
} }