added map with centroid
This commit is contained in:
parent
1b4f1c1e87
commit
9ecee3b2f6
|
@ -1,10 +1,13 @@
|
|||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
|
@ -12,6 +15,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
@ -21,6 +25,7 @@ import org.gcube.application.geoportalcommon.ProjectDVBuilder;
|
|||
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
|
||||
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
|
||||
import org.gcube.application.geoportalcommon.geoportal.UseCaseDescriptorCaller;
|
||||
import org.gcube.application.geoportalcommon.shared.geoportal.geojson.GeoJSON;
|
||||
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDIViewerLayerDV;
|
||||
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV;
|
||||
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV;
|
||||
|
@ -34,6 +39,9 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.imgscalr.Scalr;
|
||||
import org.imgscalr.Scalr.Method;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -46,17 +54,12 @@ import com.itextpdf.io.image.ImageData;
|
|||
import com.itextpdf.io.image.ImageDataFactory;
|
||||
import com.itextpdf.kernel.colors.ColorConstants;
|
||||
import com.itextpdf.kernel.colors.DeviceRgb;
|
||||
import com.itextpdf.kernel.events.Event;
|
||||
import com.itextpdf.kernel.events.IEventHandler;
|
||||
import com.itextpdf.kernel.events.PdfDocumentEvent;
|
||||
import com.itextpdf.kernel.font.PdfFont;
|
||||
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfAnnotationBorder;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.action.PdfAction;
|
||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||
import com.itextpdf.layout.Document;
|
||||
import com.itextpdf.layout.Style;
|
||||
import com.itextpdf.layout.borders.Border;
|
||||
|
@ -94,16 +97,29 @@ 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 = "6384ae78308f5c28c5ee08aa"; // "6384ae78308f5c28c5ee08aa"; //PRE
|
||||
// 63d011c4dcac4551b9a6b930
|
||||
// //6442653ac6a0422d9e9042e0
|
||||
// //6399de3ca0a4545420373251
|
||||
//645a152fd7fb4a463302240d
|
||||
private static String PROJECT_ID = "6388d9b3a1e60e66b7b5843a"; // "6384ae78308f5c28c5ee08aa"; //PRE
|
||||
|
||||
// PROD
|
||||
// 63d011c4dcac4551b9a6b930
|
||||
// //6442653ac6a0422d9e9042e0
|
||||
// //6399de3ca0a4545420373251
|
||||
// 645a152fd7fb4a463302240d
|
||||
|
||||
// IMPORTANT!!!! #NB SET USERNAME = null to test PUBLIC ACCESS
|
||||
|
||||
private static String USERNAME = "francesco.mangiacrapa";
|
||||
|
||||
// private static String OSM_DOWNLOAD_ENDPOINT =
|
||||
// "https://render.openstreetmap.org/cgi-bin/export";
|
||||
// https://api.mapbox.com/styles/v1/mapbox/streets-v12/static/geojson({"type":"Point","coordinates":[-73.99,40.7]})/-73.99,40.70,12/500x300?access_token=pk.eyJ1IjoiZXhhbXBsZXMiLCJhIjoiY2p0MG01MXRqMW45cjQzb2R6b2ptc3J4MSJ9.zA2W0IkI0c6KaAhJfk9bWg
|
||||
private static String MAP_BOX_STATIC_API_ENDPOINT = "https://api.mapbox.com/styles/v1/mapbox/streets-v12/static";
|
||||
|
||||
private static String MAP_BOX_GEOJSON_PARAMETER = "geojson({\"type\":\"Point\",\"coordinates\":%s})";
|
||||
|
||||
private static String MAP_BOX_ACCESS_TOKEN = "access_token=%s";
|
||||
|
||||
private static String accessToken = "{ADD_ACCESS_TOKEN}";
|
||||
|
||||
/**
|
||||
* Read context settings.
|
||||
*/
|
||||
|
@ -174,7 +190,7 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
Document document = new Document(pdf);
|
||||
|
||||
// Adding logo D4GNA
|
||||
Image image = toImageFromLink(logoD4GNA, 70, false);
|
||||
Image image = toImageFromLink(logoD4GNA, 80, false);
|
||||
// image.setMaxHeight(150);
|
||||
image.setFixedPosition(document.getLeftMargin(),
|
||||
pdf.getDefaultPageSize().getHeight() - image.getImageHeight() + 90);
|
||||
|
@ -214,6 +230,20 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
Paragraph p2 = new Paragraph().add(theLink);
|
||||
document.add(p2);
|
||||
|
||||
// Adding centroid MAP
|
||||
String geoJSON = theProjectDV.getSpatialReference().getGeoJSON();
|
||||
String downMapBoxImg = getMapBoxStaticMap(geoJSON, 7, accessToken);
|
||||
|
||||
Image backgroundImage = toImageFromLink(downMapBoxImg, 250, false);
|
||||
Table tableI = new Table(1);
|
||||
tableI.setTextAlignment(TextAlignment.CENTER);
|
||||
tableI.addCell(backgroundImage);
|
||||
|
||||
Paragraph pCentroid = new Paragraph();
|
||||
pCentroid.setTextAlignment(TextAlignment.CENTER);
|
||||
pCentroid.add(tableI);
|
||||
document.add(pCentroid);
|
||||
|
||||
for (SectionView section : projectView.getListSections()) {
|
||||
document = appendSubDocumentToPDF(document, section, section.getListSubDocuments());
|
||||
}
|
||||
|
@ -242,6 +272,45 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
}
|
||||
}
|
||||
|
||||
private static String getMapBoxStaticMap(String geoJSON, int zoom, String accessTokenValue) {
|
||||
|
||||
// String geoJSON = theProjectDV.getSpatialReference().getGeoJSON();
|
||||
|
||||
try {
|
||||
|
||||
JSONObject theGeoJSONObj = new JSONObject(geoJSON);
|
||||
String pointCoordinates = theGeoJSONObj.getString(GeoJSON.COORDINATES);
|
||||
|
||||
String geojson = String.format(MAP_BOX_GEOJSON_PARAMETER, pointCoordinates);
|
||||
JSONArray jsonArray = new JSONArray(pointCoordinates);
|
||||
|
||||
if (zoom < 0)
|
||||
zoom = 7;
|
||||
|
||||
jsonArray.put(jsonArray.length() - 1, zoom);
|
||||
|
||||
pointCoordinates = jsonArray.toString();
|
||||
|
||||
String accessToken = String.format(MAP_BOX_ACCESS_TOKEN, accessTokenValue);
|
||||
//
|
||||
String downMapBoxImg = String.format("%s/%s/%s/500x300?%s", MAP_BOX_STATIC_API_ENDPOINT, geojson,
|
||||
pointCoordinates.replaceAll("\\[", "").replaceAll("\\]", ""), accessToken);
|
||||
|
||||
System.out.println("DOWN IMAGE: " + downMapBoxImg);
|
||||
|
||||
return downMapBoxImg;
|
||||
|
||||
} catch (JSONException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Table getCredits(Document document, String logoD4GNA, String logoPoweredByD4Science) {
|
||||
|
||||
Table tableCredits = toTableFixedLayout(document, 4, false);
|
||||
|
@ -371,9 +440,21 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
System.out.println("wmsLink: " + wmsLink);
|
||||
wmsLink = URLParserUtil.setValueOfParameter("format", wmsLink, "image/jpeg", true);
|
||||
Image image = toImageFromLink(wmsLink, 250, false);
|
||||
|
||||
/*
|
||||
wmsLink = URLParserUtil.setValueOfParameter("format", wmsLink, "application/json", true);
|
||||
String geoJSON = toStringFromLink(wmsLink);
|
||||
String downMapBoxImg = getMapBoxStaticMap(geoJSON, 7, accessToken);
|
||||
Image image = toImageFromLink(downMapBoxImg, 250, false);*/
|
||||
|
||||
tableI.setTextAlignment(TextAlignment.CENTER);
|
||||
tableI.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||
|
||||
// Cell cell = new Cell();
|
||||
//// cell.add(backgroundImage);
|
||||
// cell.add(image);
|
||||
tableI.addCell(image);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -481,6 +562,26 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
return img;
|
||||
}
|
||||
|
||||
private static String toStringFromLink(String link) {
|
||||
String text = null;
|
||||
try {
|
||||
|
||||
ImageData data = null;
|
||||
|
||||
final InputStream is = new URL(link).openStream();
|
||||
|
||||
text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
|
||||
} catch (Exception e) {
|
||||
// silent
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
private static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
|
||||
if (targetWidth > 0) {
|
||||
return Scalr.resize(originalImage, Method.QUALITY, targetWidth);
|
||||
|
|
Loading…
Reference in New Issue