optimized Export to PDF

This commit is contained in:
Francesco Mangiacrapa 2023-12-19 16:10:30 +01:00
parent 96c19ca6dd
commit 9325d5aaef
3 changed files with 47 additions and 40 deletions

View File

@ -2,11 +2,9 @@ package org.gcube.application.geoportaldatamapper.exporter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@ -17,6 +15,8 @@ import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.rest.TempFile;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
@ -312,22 +312,21 @@ public class Geoportal_PDF_Exporter {
String projectTitleSanitized = projectTitle.replaceAll("[^a-zA-Z0-9]", "_");
// Save PDF on storage VOLATILE
// StorageUtils storage = new StorageUtils();
// TempFile tempFile = storage.putOntoStorage(in, projectTitleSanitized + ".pdf");
// String storageURL = storage.getURL(tempFile.getId());
// LOG.trace("mongo pdf ID: " + tempFile.getId());
// LOG.trace("mongo pdf filename: " + tempFile.getFilename());
//
// pdfFileURL = String.format("%s?contentType=%s&fileName=%s&content-disposition=%s", storageURL,
// "application/pdf", tempFile.getFilename(), "inline");
// LOG.info("returning pdf URL: " + pdfFileURL);
StorageUtils storage = new StorageUtils();
TempFile tempFile = storage.putOntoStorage(in, projectTitleSanitized + ".pdf");
String storageURL = storage.getURL(tempFile.getId());
LOG.trace("mongo pdf ID: " + tempFile.getId());
LOG.trace("mongo pdf filename: " + tempFile.getFilename());
// TEST save file locally
File targetFile = new File(projectTitleSanitized + ".pdf");
java.nio.file.Files.copy(in, targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING); LOG.info("targetFile at: " +
targetFile.getAbsolutePath());
pdfFileURL = String.format("%s?contentType=%s&fileName=%s&content-disposition=%s", storageURL,
"application/pdf", tempFile.getFilename(), "inline");
LOG.info("returning pdf URL: " + pdfFileURL);
// TEST save file locally
// File targetFile = new File(projectTitleSanitized + ".pdf");
// java.nio.file.Files.copy(in, targetFile.toPath(),
// StandardCopyOption.REPLACE_EXISTING); LOG.info("targetFile at: " +
// targetFile.getAbsolutePath());
IOUtils.closeQuietly(in);
@ -715,28 +714,29 @@ public class Geoportal_PDF_Exporter {
double bboxOffset = 0.0;
LOG.trace("source_bbox: " + source_bbox);
Recalculate_WGS84_Offset recalculate = new Recalculate_WGS84_Offset(wmsVersion, source_bbox, bboxOffset);
Recalculate_WGS84_Offset source_Projection = new Recalculate_WGS84_Offset(wmsVersion, source_bbox, bboxOffset);
LOG.trace("source width: " + recalculate.getAspectratioWidth());
LOG.trace("source heigth: " + recalculate.getAspectratioHeight());
LOG.trace("source ratio: " + recalculate.getRatio_Width_Height());
LOG.trace("source bbox: " + recalculate.getBboxWithOffset());
LOG.trace("source width: " + source_Projection.getAspectratioWidth());
LOG.trace("source heigth: " + source_Projection.getAspectratioHeight());
LOG.trace("source ratio: " + source_Projection.getRatio_Width_Height());
LOG.trace("source bbox: " + source_Projection.getBboxWithOffset());
//Calculate best BBOX offset
bboxOffset = BBOXConverter.bestBBOXOffset(wmsVersion, source_bbox);
//bboxOffset = 0.0001;
LOG.debug("applying offset: "+bboxOffset);
recalculate = new Recalculate_WGS84_Offset(wmsVersion, source_bbox, bboxOffset);
Recalculate_WGS84_Offset recalculate_Projection = new Recalculate_WGS84_Offset(wmsVersion, source_bbox, bboxOffset);
LOG.trace("offset width: " + recalculate.getAspectratioWidth());
LOG.trace("offset heigth: " + recalculate.getAspectratioHeight());
LOG.trace("ratio: " + recalculate.getRatio_Width_Height());
LOG.trace("bbox with offset: " + recalculate.getBboxWithOffset());
LOG.trace("offset width: " + recalculate_Projection.getAspectratioWidth());
LOG.trace("offset heigth: " + recalculate_Projection.getAspectratioHeight());
LOG.trace("ratio: " + recalculate_Projection.getRatio_Width_Height());
LOG.trace("bbox with offset: " + recalculate_Projection.getBboxWithOffset());
int aspectratioWidth = recalculate.getAspectratioWidth();
int aspectratioHeight = recalculate.getAspectratioHeight();
String recaulculateBBOX = recalculate.getBboxWithOffset();
int aspectratioWidth = recalculate_Projection.getAspectratioWidth();
int aspectratioHeight = recalculate_Projection.getAspectratioHeight();
String recaulculateBBOX = recalculate_Projection.getBboxWithOffset();
// Layer with aspect ratio on width/height according to bbox width/height
wmsLink = URLParserUtil.setValueOfParameter("width", wmsLink, aspectratioWidth + "",
@ -747,19 +747,19 @@ public class Geoportal_PDF_Exporter {
wmsLinkTol += "&TRANSPARENT=TRUE";
LOG.trace("wmsLink aspect ratio link: " + wmsLink);
int maxWidthMap = 300;
int maxWidthOfMapWithLayer = 250;
Image image2 = toImageFromLink(wmsLinkTol, maxWidthMap);
Image image2 = toImageFromLink(wmsLinkTol, maxWidthOfMapWithLayer);
// MapBox static as background
String linkToMapBox = linkToMapBoxStaticMap(recaulculateBBOX,
aspectratioWidth + "x" + aspectratioHeight, sap);
LOG.trace("linkToMapBox: " + linkToMapBox);
Image backImg = toImageFromLink(linkToMapBox, maxWidthMap);
Image backImg = toImageFromLink(linkToMapBox, maxWidthOfMapWithLayer);
Cell mapCell = new Cell();
mapCell.setMaxWidth(maxWidthMap);
mapCell.setMaxWidth(maxWidthOfMapWithLayer);
mapCell.setNextRenderer(new ImageBackgroundCellRenderer(mapCell, backImg));
mapCell.add(image2);
mapCell.setTextAlignment(TextAlignment.CENTER);

View File

@ -4,6 +4,10 @@ import java.util.Arrays;
import java.util.List;
import org.gcube.spatial.data.geoutility.shared.BBOX;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.allen_sauer.gwt.log.client.Log;
/**
* The Class BBOXConverter.
@ -17,6 +21,7 @@ public class BBOXConverter {
Integer[] size = new Integer[2];
Double[] bbox = new Double[4];
public static final double EQUATOR = 40075016.68557849;
private Logger LOG = LoggerFactory.getLogger(BBOXConverter.class);
/**
* The Class BBOXPixel.
@ -182,7 +187,7 @@ public class BBOXConverter {
*/
public static Double bboxOffset(String cMin, String cMax) {
String bboxOffsetString = "0.";
int startIndex = cMax.indexOf(".") + 1;
int startIndex = cMax.indexOf(".")+1;
int lenght = cMax.length();
if (startIndex < lenght) {
for (int i = startIndex; i < lenght; i++) {
@ -215,13 +220,15 @@ public class BBOXConverter {
Double offsetWidth = bboxOffset(xMin, xMax);
//System.out.println("offsetWidth: " + offsetWidth);
Log.debug("offset width: "+offsetWidth);
String yMin = new Double(theBBOX.getLowerLeftY()).toString();
String yMax = new Double(theBBOX.getUpperRightY()).toString();
Double offsetHeight = bboxOffset(yMax, yMin);
//System.out.println("offsetHeight: " + offsetHeight);
return offsetWidth < offsetHeight ? offsetWidth : offsetHeight;
Log.debug("offset width: "+offsetHeight);
return offsetWidth < offsetHeight ? offsetHeight : offsetWidth;
}
}

View File

@ -45,7 +45,7 @@ public class Geoportal_Export_To_PDF_Tests {
"646353c5d7fb4a4633022803", "63f8c481e9d6ac16f394f1e6", "638888fba1e60e66b7b581dd",
"650304487814b87373631242", "6388ea17a1e60e66b7b584dd", "64537208d7fb4a4633022039", "638885fea1e60e66b7b581bd"};
//private static String[] PROJECT_IDS = { "646353c5d7fb4a4633022803" };
// private static String[] PROJECT_IDS = { "646353c5d7fb4a4633022803" };
// DEV
// 654e07a75bdd5478cca320c0
@ -104,7 +104,7 @@ public class Geoportal_Export_To_PDF_Tests {
*
* @return the client
*/
@Before
//@Before
public void getClient() {
readContextSettings();
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
@ -131,7 +131,7 @@ public class Geoportal_Export_To_PDF_Tests {
/**
* Test read project view.
*/
@Test
//@Test
public void testExportToPDF() {
ScopeProvider.instance.set(CONTEXT);