Updated PDF Exporter

This commit is contained in:
Francesco Mangiacrapa 2023-12-18 16:26:06 +01:00
parent 8ccafd4062
commit 2bb81cbd76
9 changed files with 1561 additions and 329 deletions

View File

@ -1,73 +0,0 @@
package org.gcube.application.geoportaldatamapper.exporter;
import org.gcube.spatial.data.geoutility.shared.BBOX;
public class BBOXConverter {
Integer[] size = new Integer[2];
Double[] bbox = new Double[4];
public static class BBOXPixel {
public double x;
public double y;
public double w;
public double h;
public BBOXPixel(double x, double y, double w, double h) {
super();
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BBOXPixel [x=");
builder.append(x);
builder.append(", y=");
builder.append(y);
builder.append(", w=");
builder.append(w);
builder.append(", h=");
builder.append(h);
builder.append("]");
return builder.toString();
}
}
public static BBOXConverter.BBOXPixel convertToPixel(Integer[] size, Double[] bbox) {
double dw = 1. / size[0];
double dh = 1. / size[1];
double x = (bbox[0] + bbox[1]) / 2.0;
double y = (bbox[2] + bbox[3]) / 2.0;
double w = bbox[1] - bbox[0];
double h = bbox[3] - bbox[2];
x = x * dw;
w = w * dw;
y = y * dh;
h = h * dh;
return new BBOXPixel(x, y, w, h);
}
public static BBOX convertToCoordinate(BBOXPixel bboxP, Integer[] size) {
double h = bboxP.h;
double l = bboxP.x;
double t = bboxP.y;
double w = bboxP.w;
Integer img_w = size[0];
Integer img_h = size[1];
double x1 = l / img_w;
double y1 = t / img_h;
double x2 = (l + w) / img_w;
double y2 = (t + h) / img_h;
return new BBOX(x1, y1, x2, y2, "");
}
}

View File

@ -17,8 +17,6 @@ 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;
@ -37,11 +35,11 @@ import org.gcube.application.geoportaldatamapper.exporter.beans.Credits;
import org.gcube.application.geoportaldatamapper.exporter.beans.PDFExporterConfig;
import org.gcube.application.geoportaldatamapper.exporter.beans.PageNumber;
import org.gcube.application.geoportaldatamapper.exporter.beans.Watermarker;
import org.gcube.application.geoportaldatamapper.exporter.gis.Recalculate_WGS84_Offset;
import org.gcube.application.geoportaldatamapper.reader.MapBoxEndpointReader;
import org.gcube.application.geoportaldatamapper.reader.ServiceAccessPoint;
import org.gcube.application.geoportaldatamapper.shared.ExporterProjectSource;
import org.gcube.spatial.data.geoutility.bean.WmsParameters;
import org.gcube.spatial.data.geoutility.shared.BBOX;
import org.gcube.spatial.data.geoutility.shared.wfs.WFSParameter;
import org.gcube.spatial.data.geoutility.wfs.WFSQueryBuilder;
//import org.imgscalr.Scalr;
@ -312,20 +310,23 @@ 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());
//
// 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());*/
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);
} catch (Exception e) {
@ -696,41 +697,30 @@ public class Geoportal_PDF_Exporter {
List<GCubeSDIViewerLayerDV> layers = subDocumentView.getListLayers();
if (layers != null) {
for (GCubeSDIViewerLayerDV gCubeLayer : layers) {
Paragraph pLayer = new Paragraph();
pLayer.setTextAlignment(TextAlignment.CENTER);
Table tableLayer = new Table(1);
tableLayer.setTextAlignment(TextAlignment.CENTER);
Table tableMeta = new Table(2);
tableMeta = jsonToTable(tableMeta, 1, "", jsonObject);
String wmsLink = gCubeLayer.getWMSLink();
LOG.trace("wmsLink(1): " + wmsLink);
wmsLink = URLParserUtil.setValueOfParameter("format", wmsLink, "image/png", true);
String bbox = URLParserUtil.extractValueOfParameterFromURL("bbox", wmsLink);
String source_bbox = URLParserUtil.extractValueOfParameterFromURL("bbox", wmsLink);
String wmsVersion = URLParserUtil.extractValueOfParameterFromURL("version", wmsLink);
/*
Double expWidth = 500.0;
Double expHeight = 500.0;
BBOX.COORDINATE_FORMAT bboxFormat = wmsVersion.contains("1.3")
? BBOX.COORDINATE_FORMAT.YX
: BBOX.COORDINATE_FORMAT.XY;
BBOX theBBOX = new BBOX(bbox, bboxFormat);
// double bboxOffset = 0.003;
double bboxOffset = 0.01; // no offset
double lowerXR = theBBOX.getLowerLeftX() - bboxOffset;
double lowerYR = theBBOX.getLowerLeftY() - bboxOffset;
double upperXR = theBBOX.getUpperRightX() + bboxOffset;
double upperYR = theBBOX.getUpperRightY() + bboxOffset;
String parmBBOX = BBOX.toBBOXString(new BBOX(lowerXR, lowerYR, upperXR, upperYR, ""),
bboxFormat);
double bboxOffset = 0.01;
String parmBBOX = BBOXConverter.bboxWithOffset(wmsVersion, bbox, bboxOffset);
// Calculating bounding box dimension
String bbox3857 = CoordinatesConverter.toEPSG3857_BBOX(parmBBOX);
List<Double> bbox3857_BB = CoordinatesConverter.toEPSG3857_BBOX(parmBBOX);
String bbox3857 = CoordinatesConverter.toString(bbox3857_BB);
LOG.debug("toEPSG3857_BBOX: " + bbox3857);
ArrayList<Double> diff = CoordinatesConverter.diffBBox(bbox3857);
LOG.debug("BoudingBox width - height: " + diff);
@ -749,23 +739,60 @@ public class Geoportal_PDF_Exporter {
}
int aspectratioWidth = expWidth.intValue();
int aspectratioHeight = expHeight.intValue();
int aspectratioHeight = expHeight.intValue();*/
double bboxOffset = 0.0;
LOG.trace("source_bbox: " + source_bbox);
Recalculate_WGS84_Offset recalculate = 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());
int height = recalculate.getAspectratioHeight();
int width = recalculate.getAspectratioWidth();
if (width < 100 || height < 100) {
bboxOffset = 0.00001;
} else if (width < 200 || height < 200) {
bboxOffset = 0.0001;
} else if (width < 500 || height < 500) {
bboxOffset = 0.001;
} else if (width < 1000 || height < 1000) {
bboxOffset = 0.01;
}
//bboxOffset = 0.0001;
LOG.debug("applying offset: "+bboxOffset);
recalculate = 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());
int aspectratioWidth = recalculate.getAspectratioWidth();
int aspectratioHeight = recalculate.getAspectratioHeight();
String recaulculateBBOX = recalculate.getBboxWithOffset();
// Layer with aspect ratio on width/height according to bbox width/height
wmsLink = URLParserUtil.setValueOfParameter("width", wmsLink, aspectratioWidth + "",
true);
wmsLink = URLParserUtil.setValueOfParameter("height", wmsLink, aspectratioHeight + "",
true);
String wmsLinkTol = URLParserUtil.setValueOfParameter("bbox", wmsLink, parmBBOX, false);
String wmsLinkTol = URLParserUtil.setValueOfParameter("bbox", wmsLink, recaulculateBBOX, false);
wmsLinkTol += "&TRANSPARENT=TRUE";
LOG.trace("wmsLink aspect ratio link : " + wmsLink);
LOG.trace("wmsLink aspect ratio link: " + wmsLink);
int maxWidthMap = 350;
Image image2 = toImageFromLink(wmsLinkTol, null, null);
// MapBox static as background
String linkToMapBox = linkToMapBoxStaticMap(parmBBOX,
String linkToMapBox = linkToMapBoxStaticMap(recaulculateBBOX,
aspectratioWidth + "x" + aspectratioHeight, sap);
LOG.trace("linkToMapBox: " + linkToMapBox);
@ -781,7 +808,7 @@ public class Geoportal_PDF_Exporter {
removeBorder(tableLayer);
pLayer.add(tableLayer);
//adding tables with layer and meta to table container
// adding tables with layer and meta to table container
// Row with layers
tableContainerL.addCell(pLayer);
// Row with layer metadata

View File

@ -0,0 +1,116 @@
package org.gcube.application.geoportaldatamapper.exporter.gis;
import java.util.Arrays;
import java.util.List;
import org.gcube.spatial.data.geoutility.shared.BBOX;
public class BBOXConverter {
Integer[] size = new Integer[2];
Double[] bbox = new Double[4];
public static final double EQUATOR = 40075016.68557849;
public static class BBOXPixel {
public double x;
public double y;
public double w;
public double h;
public BBOXPixel(double x, double y, double w, double h) {
super();
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BBOXPixel [x=");
builder.append(x);
builder.append(", y=");
builder.append(y);
builder.append(", w=");
builder.append(w);
builder.append(", h=");
builder.append(h);
builder.append("]");
return builder.toString();
}
}
public static BBOXConverter.BBOXPixel convertToPixel(Integer[] size, Double[] bbox) {
double dw = 1. / size[0];
double dh = 1. / size[1];
double x = (bbox[0] + bbox[1]) / 2.0;
double y = (bbox[2] + bbox[3]) / 2.0;
double w = bbox[1] - bbox[0];
double h = bbox[3] - bbox[2];
x = x * dw;
w = w * dw;
y = y * dh;
h = h * dh;
return new BBOXPixel(x, y, w, h);
}
public static BBOX convertToCoordinate(BBOXPixel bboxP, Integer[] size) {
double h = bboxP.h;
double l = bboxP.x;
double t = bboxP.y;
double w = bboxP.w;
Integer img_w = size[0];
Integer img_h = size[1];
double x1 = l / img_w;
double y1 = t / img_h;
double x2 = (l + w) / img_w;
double y2 = (t + h) / img_h;
return new BBOX(x1, y1, x2, y2, "");
}
public static String bboxWithOffset(String wmsVersion, String bbox, double bboxOffset) {
BBOX.COORDINATE_FORMAT bboxFormat = wmsVersion.contains("1.3") ? BBOX.COORDINATE_FORMAT.YX
: BBOX.COORDINATE_FORMAT.XY;
BBOX theBBOX = new BBOX(bbox, bboxFormat);
// double lowerXR = theBBOX.getLowerLeftX() - (theBBOX.getLowerLeftX()*bboxOffset/100);
// double lowerYR = theBBOX.getLowerLeftY() - (theBBOX.getLowerLeftY()*bboxOffset/100);
// double upperXR = theBBOX.getUpperRightX() + (theBBOX.getUpperRightX()*bboxOffset/100);
// double upperYR = theBBOX.getUpperRightY() + (theBBOX.getUpperRightY()*bboxOffset/100);
double lowerXR = theBBOX.getLowerLeftX() - bboxOffset;
double lowerYR = theBBOX.getLowerLeftY() - bboxOffset;
double upperXR = theBBOX.getUpperRightX() + bboxOffset;
double upperYR = theBBOX.getUpperRightY() + bboxOffset;
return BBOX.toBBOXString(new BBOX(lowerXR, lowerYR, upperXR, upperYR, ""), bboxFormat);
}
/**
* Converts spherical web mercator to tile pixel X/Y at zoom level 0 for 256x256
* tile size and inverts y coordinates.
*
* @param bbox the bbox
* @return {L.point} point with tile pixel x and y coordinates.
*/
public static List<Double> mercatorToPixels(List<Double> bbox) {
double pixelX1 = (bbox.get(0) + (EQUATOR / 2.0)) / (EQUATOR / 256.0);
double pixelY1 = ((bbox.get(1) - (EQUATOR / 2.0)) / (EQUATOR / -256.0));
double pixelX2 = (bbox.get(2) + (EQUATOR / 2.0)) / (EQUATOR / 256.0);
double pixelY2 = ((bbox.get(3) - (EQUATOR / 2.0)) / (EQUATOR / -256.0));
return Arrays.asList(pixelX1, pixelY1, pixelX2, pixelY2);
}
}

View File

@ -1,11 +1,11 @@
package org.gcube.application.geoportaldatamapper.exporter;
package org.gcube.application.geoportaldatamapper.exporter.gis;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* The Class CoordinatesConverter.
* The Class CoordinatesConverter_Tests.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
@ -18,7 +18,7 @@ public class CoordinatesConverter {
/**
* To epsg 3857.
*
* @param coordinates the coordinates as long/lat
* @param coordinates the coordinates as long/lat
* @return the list
*/
public static List<Double> toEpsg3857(List<Double> coordinates) {
@ -40,12 +40,12 @@ public class CoordinatesConverter {
}
/**
* To epsg 3857 BBOX.
* To EPSG 3857 BBOX.
*
* @param bbox the bbox as long/lat
* @return the string
* @param bbox the bbox
* @return the list
*/
public static String toEPSG3857_BBOX(String bbox) {
public static List<Double> toEPSG3857_BBOX(String bbox) {
String[] parts = bbox.split(","); // remove spaces and split on ,
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);
@ -53,16 +53,28 @@ public class CoordinatesConverter {
double maxY = Double.parseDouble(parts[3]);
List<Double> l1 = toEpsg3857(Arrays.asList(minX, minY));
List<Double> l2 = toEpsg3857(Arrays.asList(maxX, maxY));
return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0), l2.get(1));
// return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0),
// l2.get(1));
return Arrays.asList(l1.get(0), l1.get(1), l2.get(0), l2.get(1));
}
/**
* To string.
*
* @param bbox the bbox
* @return the string
*/
public static String toString(List<Double> bbox) {
return String.format("%s,%s,%s,%s", bbox.get(0), bbox.get(1), bbox.get(2), bbox.get(3));
}
/**
* To EPSG 4326 BBOX.
*
* @param bbox the bbox as long/lat
* @return the string
* @param bbox the bbox
* @return the list
*/
public static String toEPSG4326_BBOX(String bbox) {
public static List<Double> toEPSG4326_BBOX(String bbox) {
String[] parts = bbox.split(","); // remove spaces and split on ,
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);
@ -70,7 +82,9 @@ public class CoordinatesConverter {
double maxY = Double.parseDouble(parts[3]);
List<Double> l1 = to4326(Arrays.asList(minX, minY));
List<Double> l2 = to4326(Arrays.asList(maxX, maxY));
return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0), l2.get(1));
// return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0),
// l2.get(1));
return Arrays.asList(l1.get(0), l1.get(1), l2.get(0), l2.get(1));
}
/**
@ -79,9 +93,8 @@ public class CoordinatesConverter {
* @param bbox the bbox
* @return the array list
*/
public static ArrayList<Double> diffBBox(String bbox) {
public static ArrayList<Double> diffBBOX(String bbox) {
String[] parts = bbox.replaceAll("\\s", "").split(","); // remove spaces and split on ,
System.out.println("parts" + " : " + Arrays.asList(parts));
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);

View File

@ -0,0 +1,93 @@
package org.gcube.application.geoportaldatamapper.exporter.gis;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Recalculate_WGS84_Offset {
private String wmsVersion;
private String sourceBBOX;
private double bboxOffset;
private String bboxWithOffset;
public Double expWidth = 700.0;
public Double expHeight = 700.0;
private int aspectratioWidth;
private int aspectratioHeight;
private double ratio_Width_Height;
private Logger LOG = LoggerFactory.getLogger(Recalculate_WGS84_Offset.class);
public Recalculate_WGS84_Offset(String wmsVersion, String bbox, double bboxOffset) {
this.wmsVersion = wmsVersion;
this.sourceBBOX = bbox;
this.bboxOffset = bboxOffset;
this.bboxWithOffset = BBOXConverter.bboxWithOffset(wmsVersion, sourceBBOX, this.bboxOffset);
// Calculating bounding box dimension
List<Double> bbox3857_BB = CoordinatesConverter.toEPSG3857_BBOX(bboxWithOffset);
String bbox3857 = CoordinatesConverter.toString(bbox3857_BB);
LOG.debug("toEPSG3857_BBOX: " + bbox3857);
ArrayList<Double> diff = CoordinatesConverter.diffBBOX(bbox3857);
LOG.debug("BoudingBox [width dimension, height dimension]: " + diff);
double width = diff.get(0);
double height = diff.get(1);
ratio_Width_Height = width / height;
LOG.trace("bbox width: " + width);
LOG.trace("bbox heigth: " + height);
LOG.trace("bbox ratio: " + ratio_Width_Height);
// Calculating width/height ratio
if (ratio_Width_Height > 1) {
expWidth = expWidth * ratio_Width_Height;
} else {
expHeight = expHeight * ratio_Width_Height;
}
this.aspectratioWidth = expWidth.intValue();
LOG.debug("aspectratioWidth: " + aspectratioWidth);
this.aspectratioHeight = expHeight.intValue();
LOG.debug("aspectratioHeight: " + aspectratioHeight);
}
public String getWmsVersion() {
return wmsVersion;
}
public String getSourceBBOX() {
return sourceBBOX;
}
public double getBboxOffset() {
return bboxOffset;
}
public String getBboxWithOffset() {
return bboxWithOffset;
}
public Double getExpWidth() {
return expWidth;
}
public Double getExpHeight() {
return expHeight;
}
public int getAspectratioWidth() {
return aspectratioWidth;
}
public int getAspectratioHeight() {
return aspectratioHeight;
}
public double getRatio_Width_Height() {
return ratio_Width_Height;
}
}

View File

@ -1,126 +0,0 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CoordinatesConverter {
// public static List<Double> epsg4326toEpsg3857(List<Double> coordinates) {
// double x = (coordinates.get(0) * 20037508.34) / 180;
// double y = Math.log(Math.tan(((90 + coordinates.get(1)) * Math.PI) / 360)) / (Math.PI / 180);
// y = (y * 20037508.34) / 180;
// return Arrays.asList(x, y);
// }
// public static String convertBBOXFromEpsg4326toEpsg3857(String bbox) {
// String[] parts = bbox.split(","); // remove spaces and split on ,
// System.out.println("bbox parts: " + bbox);
// double minX = Double.parseDouble(parts[0]);
// double minY = Double.parseDouble(parts[1]);
// double maxX = Double.parseDouble(parts[2]);
// double maxY = Double.parseDouble(parts[3]);
// List<Double> l1 = epsg4326toEpsg3857(Arrays.asList(minX, minY));
// List<Double> l2 = epsg4326toEpsg3857(Arrays.asList(maxX, maxY));
// return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0), l2.get(1));
// }
// public static LatLng to4326(Wgs84 wgs84) {
// double lat = Math.toDegrees(Math.atan(Math.exp(wgs84.getY() / EARTH_RADIUS)) * 2 - Math.PI/2);
// double lng = Math.toDegrees(wgs84.getX() / EARTH_RADIUS);
// return new LatLng(lat, lng);
// }
private static double EARTH_RADIUS = 6378137.0;
public static List<Double> toEpsg3857(List<Double> coordinates) {
double x = Math.toRadians(coordinates.get(0)) * EARTH_RADIUS;
double y = Math.log(Math.tan(Math.PI / 4 + Math.toRadians(coordinates.get(1)) / 2)) * EARTH_RADIUS;
return Arrays.asList(x, y);
}
public static String toEpsg3857BBOX(String bbox) {
String[] parts = bbox.split(","); // remove spaces and split on ,
System.out.println("bbox parts: " + bbox);
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);
double maxX = Double.parseDouble(parts[2]);
double maxY = Double.parseDouble(parts[3]);
List<Double> l1 = toEpsg3857(Arrays.asList(minX, minY));
List<Double> l2 = toEpsg3857(Arrays.asList(maxX, maxY));
return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0), l2.get(1));
}
public static String toEpsg4326BBOX(String bbox) {
String[] parts = bbox.split(","); // remove spaces and split on ,
System.out.println("bbox parts: " + bbox);
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);
double maxX = Double.parseDouble(parts[2]);
double maxY = Double.parseDouble(parts[3]);
List<Double> l1 = to4326(Arrays.asList(minX, minY));
List<Double> l2 = to4326(Arrays.asList(maxX, maxY));
return String.format("%s,%s,%s,%s", l1.get(0), l1.get(1), l2.get(0), l2.get(1));
}
public static List<Double> to4326(List<Double> coordinates) {
double lat = Math.toDegrees(Math.atan(Math.exp(coordinates.get(1) / EARTH_RADIUS)) * 2 - Math.PI / 2);
double lng = Math.toDegrees(coordinates.get(0) / EARTH_RADIUS);
return Arrays.asList(lng, lat);
}
public static ArrayList<Double> diffBBox(String bbox) {
String[] parts = bbox.replaceAll("\\s", "").split(","); // remove spaces and split on ,
System.out.println("parts" + " : " + Arrays.asList(parts));
double minX = Double.parseDouble(parts[0]);
double minY = Double.parseDouble(parts[1]);
double maxX = Double.parseDouble(parts[2]);
double maxY = Double.parseDouble(parts[3]);
double width = maxX - minX;
double height = maxY - minY;
ArrayList size = new ArrayList<Double>();
size.add(width);
size.add(height);
return size;
}
public static void main(String[] args) {
String bbox = "14.350604,40.632238,14.353074,40.634530";
// String bbox = "12.263867,44.231887,12.345057,44.263416";
String bbox3857 = toEpsg3857BBOX(bbox);
System.out.println("bbox3857: "+bbox3857);
String bbox4326 = toEpsg4326BBOX(bbox3857);
System.out.println("bbox4326: "+bbox4326);
//bbox3857 = "-13888944.635145342,2867630.7173607955,-7451112.364854658,6340929.2826392045";
ArrayList<Double> diff = diffBBox(bbox3857);
System.out.println(diff);
double width = diff.get(0);
double height = diff.get(1);
double ratio = width/height;
// double width = diff.get(0) * 0.90;
// double height = diff.get(1) * 0.90;
System.out.println("width: "+width);
System.out.println("heigth: "+height);
System.out.println("ratio: "+ratio);
double expectedWidth = 500;
double expectedHeight = 500;
if(width>height) {
expectedHeight = expectedWidth * ratio;
}else {
expectedWidth = expectedHeight * ratio;
}
System.out.println("expectedWidth: "+expectedWidth);
System.out.println("expectedHeight: "+expectedHeight);
}
}

View File

@ -0,0 +1,102 @@
import java.util.ArrayList;
import org.gcube.application.geoportaldatamapper.exporter.gis.CoordinatesConverter;
import org.gcube.application.geoportaldatamapper.exporter.gis.Recalculate_WGS84_Offset;
public class CoordinatesConverter_Tests {
private static double EARTH_RADIUS = 6378137.0;
// public static void main(String[] args) {
//
// // double bboxOffset = 0.003;
// String theBBOX = "15.208508,41.442976,15.209360,41.443437";
// double bboxOffset = 0.01; // no offset
// double lowerXR = theBBOX.getLowerLeftX() - bboxOffset;
// double lowerYR = theBBOX.getLowerLeftY() - bboxOffset;
// double upperXR = theBBOX.getUpperRightX() + bboxOffset;
// double upperYR = theBBOX.getUpperRightY() + bboxOffset;
//
// String parmBBOX = BBOX.toBBOXString(new BBOX(lowerXR, lowerYR, upperXR, upperYR, ""),
// bboxFormat);
//
// // Calculating bounding box dimension
// String bbox3857 = CoordinatesConverter.toEPSG3857_BBOX(parmBBOX);
// LOG.debug("toEPSG3857_BBOX: " + bbox3857);
// ArrayList<Double> diff = CoordinatesConverter.diffBBox(bbox3857);
// LOG.debug("BoudingBox width - height: " + diff);
// double width = diff.get(0);
// double height = diff.get(1);
// double ratio = width / height;
// LOG.trace("bbox width: " + width);
// LOG.trace("bbox heigth: " + height);
// LOG.trace("bbox ratio: " + ratio);
//
// // Calculating width/height ratio
// if (ratio > 1) {
// expWidth = expWidth * ratio;
// } else {
// expHeight = expHeight * ratio;
// }
//
// int aspectratioWidth = expWidth.intValue();
// int aspectratioHeight = expHeight.intValue();
//
// }
public static void main(String[] args) {
// String bbox = "14.350604,40.632238,14.353074,40.634530";
String source_bbox = "15.208508,41.442976,15.209360,41.443437";
System.out.println("source bbox: " + source_bbox);
String wmsVersion = "1.1";
double bboxOffset = 0.0;
Recalculate_WGS84_Offset recalculate = new Recalculate_WGS84_Offset(wmsVersion, source_bbox, bboxOffset);
System.out.println("offset width: " + recalculate.getAspectratioWidth());
System.out.println("offset heigth: " + recalculate.getAspectratioHeight());
System.out.println("ratio: " + recalculate.getRatio_Width_Height());
System.out.println("bbox with offset: " + recalculate.getBboxWithOffset());
int height = recalculate.getAspectratioHeight();
int width = recalculate.getAspectratioWidth();
ArrayList<Double> diff = CoordinatesConverter.diffBBOX(source_bbox);
System.out.println("diff WGS84: "+diff);
// int expWidth = getExponentForNumber(diff.get(0));
// int expHeight = getExponentForNumber(diff.get(1));
// System.out.println("expWidth: " + expWidth);
// System.out.println("expHeight: " + expHeight);
if (width < 100 || height < 100) {
bboxOffset = 0.00001;
} else if (width < 200 || height < 200) {
bboxOffset = 0.0001;
} else if (width < 500 || height < 500) {
bboxOffset = 0.001;
} else if (width < 1000 || height < 1000) {
bboxOffset = 0.01;
}
System.out.println("applying offset: "+bboxOffset);
recalculate = new Recalculate_WGS84_Offset(source_bbox, source_bbox, bboxOffset);
System.out.println("offset width: " + recalculate.getAspectratioWidth());
System.out.println("offset heigth: " + recalculate.getAspectratioHeight());
System.out.println("ratio: " + recalculate.getRatio_Width_Height());
System.out.println("bbox with offset: " + recalculate.getBboxWithOffset());
}
private static int getExponentForNumber(double number)
{
String numberAsString = String.valueOf(number);
return numberAsString.substring(numberAsString.indexOf('.') + 1).length() * -1;
}
}

View File

@ -41,7 +41,7 @@ public class Geoportal_Export_To_PDF_Tests {
// private static String PROFILE_ID = "concessioni-estere";
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "638885fea1e60e66b7b581bd";
private static String PROJECT_ID = "654e07a75bdd5478cca320c0";
// DEV
// 654e07a75bdd5478cca320c0
@ -223,6 +223,7 @@ public class Geoportal_Export_To_PDF_Tests {
}
}
// public static void main(String[] args) {
// gdal.AllRegister();
@ -232,82 +233,6 @@ public class Geoportal_Export_To_PDF_Tests {
//
// }
/**
* // TODO Make safe for multiple calls !Not safe on multiple calls as drivers
* are destroyed at the end of the method need so when called with multiple jobs
* chance of issues.
*
* @param geoJson GeoJson string which contain position in degrees for lat
* lon.
* @param output Output file to create tiff.
* @param resX The x resolution of new image
* @param resY The x resolution of new image
* @param burnColor The color which the geojson features will be colored in.
* @param noDataValue The nodata value for geotiff image.
*/
// public void createGeoTiffFromGeoJson(String geoJson, File output, int resX, int resY, Color burnColor, int noDataValue)
// {
// gdal.AllRegister();
// Driver driver = gdal.GetDriverByName("GTiff");
// driver.Register();
//
// DataSource dataSource = ogr.Open(geoJson);
// Layer layer = dataSource.GetLayer(0);
// final Vector<String> imageCreationOptions = new Vector<>(1);
// imageCreationOptions.add("COMPRESS=LZW");
// Dataset newDateSet = driver.Create(output.getAbsolutePath(), resX, resY, 4, gdalconst.GDT_Byte, imageCreationOptions);
// double[] extent = layer.GetExtent();
// double x_res = ((extent[1] - extent[0]) / resX);
// double y_res = ((extent[3] - extent[2]) / resY);
// newDateSet.SetGeoTransform(new double[]
// {
// extent[0], x_res, 0, extent[3], 0, -y_res
// });
// newDateSet.SetProjection("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]");
// newDateSet.GetRasterBand(1).SetNoDataValue(noDataValue);
// newDateSet.GetRasterBand(2).SetNoDataValue(noDataValue);
// newDateSet.GetRasterBand(3).SetNoDataValue(noDataValue);
// newDateSet.GetRasterBand(4).SetNoDataValue(noDataValue);
// newDateSet.GetRasterBand(1).FlushCache();
// newDateSet.GetRasterBand(2).FlushCache();
// newDateSet.GetRasterBand(3).FlushCache();
// newDateSet.GetRasterBand(4).FlushCache();
// int[] band =
// {
// 1, 2, 3, 4
// };
// double[] burn =
// {
// burnColor.getRed(), burnColor.getGreen(), burnColor.getBlue(), burnColor.getAlpha()
// };
// gdal.RasterizeLayer(newDateSet, band, layer, burn);
// driver.Deregister();
// org.gdal.gdal.gdal.GDALDestroyDriverManager();
// }
//
// public static void main(String[] args) {
//
// String location = "16.440317,40.072127,16.451167,40.082117";
//
// // EXTENT minx miny maxx maxy
// // EXTENT west south east north
//
// double minX = 12.263867;
// double minY = 44.231887;
// double maxX = 12.345057;
// double maxY = 44.263416;
//
// // public double north, south, east, west;
// // min
// String location = minX + "," + minY;
//
// new BoundingBox(location, 1);
//
// // max
// location = maxX + "," + maxY;
//
// new BoundingBox(location, 1);
// }