package org.gcube.portlets.user.geoportaldataviewer.client.ui.cms.layers; import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDIViewerLayerDV; import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.BBOXDV; import org.gcube.application.geoportalcommon.shared.gis.BoundsMap; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants.MAP_PROJECTION; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil.Location; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil.PLACE; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.MapView; import org.gcube.portlets.user.geoportaldataviewer.client.util.URLUtil; import com.github.gwtbootstrap.client.ui.DropdownButton; import com.github.gwtbootstrap.client.ui.NavLink; import com.github.gwtbootstrap.client.ui.constants.ButtonType; import com.github.gwtbootstrap.client.ui.constants.IconType; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.Widget; import ol.Coordinate; public class LayersSectionViewer extends Composite { private static LayersSectionViewerUiBinder uiBinder = GWT.create(LayersSectionViewerUiBinder.class); interface LayersSectionViewerUiBinder extends UiBinder { } @UiField HTMLPanel layerPanel; @UiField HTMLPanel mapViewPanel; private static enum IMAGE_EXPORT_AS { PNG, JPEG }; int downloadAttempt = 0; public LayersSectionViewer(GCubeSDIViewerLayerDV layer) { initWidget(uiBinder.createAndBindUi(this)); GWT.log("Showing: " + layer); String wmsLink = null; try { wmsLink = layer.getOgcLinks().get("wms"); }catch (Exception e) { return; } String layerName = URLUtil.extractValueOfParameterFromURL("layers", wmsLink); GWT.log("WMS LINK: " + wmsLink); GWT.log("layerName: " + layerName); DropdownButton exportMapButton = new DropdownButton("Export Map as..."); exportMapButton.setType(ButtonType.LINK); exportMapButton.setIcon(IconType.DOWNLOAD_ALT); exportMapButton.setTitle("Export the map view (OSM + layer) as an image..."); Style exportStyle = exportMapButton.getElement().getStyle(); //exportStyle.setWidth(100, Unit.PCT); exportStyle.setMarginLeft(70, Unit.PCT); Location italyLocation = ExtentMapUtil.getLocation(PLACE.ITALY); Coordinate transformedCenterCoordinate = italyLocation.getCoordinate(MAP_PROJECTION.EPSG_3857); MapView mapView = new MapView(transformedCenterCoordinate, GeoportalDataViewerConstants.LIGHT_MAP_ITALY_FIT_ZOOM_ON, "70%", "300px"); mapViewPanel.add(mapView); mapViewPanel.add(exportMapButton); String mapServerHost = wmsLink.contains("?") ? wmsLink.substring(0,wmsLink.indexOf("?")) : wmsLink; GWT.log("mapServerHost: " + mapServerHost); BBOXDV bbox = layer.getBbox(); GWT.log("bbox: " + bbox); GWT.log("bbox keys: " + bbox.keySet()); GWT.log("bbox values: " + bbox.values()); GWT.log("minX: " + bbox.getMinX()); GWT.log("minY: " + bbox.getMinY()); GWT.log("maxX: " + bbox.getMaxX()); GWT.log("maxY: " + bbox.getMaxY()); BoundsMap bounds = new BoundsMap(bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY(), ""); GWT.log("bounds: " + bounds); mapView.addWMSLayer(mapServerHost, layerName, bounds); String htmlLinkId = mapView.getPanelMapElementId() + "-image-download"; for (IMAGE_EXPORT_AS exportType : IMAGE_EXPORT_AS.values()) { NavLink navLink = new NavLink(exportType.name()); String exportExt = exportType.name().toLowerCase(); String filename = layerName + "." + exportExt; String mimeType = "image/" + exportExt; final HTML htmlLink = new HTML(""); navLink.getElement().appendChild(htmlLink.getElement()); navLink.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { // DOWNLOAD only the layer by Geoserver /* * Map mapLayerURLs = * mapView.getLightOLSM().getLayerURLsProperty(); Collection layerURLs = * mapLayerURLs.values(); ArrayList layers = new * ArrayList(layerURLs); exportPDF(layers.get(0)); String layerURL = * mapLayerURLs.get(layerDV.getLayerName()); GWT.log("layerDV name is: " + * layerDV.getLayerName()); if (layerURL != null) Window.open(layerURL, * "_blank", null); */ downloadAttempt++; if (downloadAttempt > 3) { Window.alert("LOOP"); return; } // DOWNLOAD the OSM + layer by canvas downloadMap(mapView.getLightOLM().getMap(), mapView.getPanelMapElementId(), htmlLinkId, filename, mimeType); // mapExport(mapView.getLightOLSM().getMap(), mapView.getPanelMapElementId()); } }); exportMapButton.add(navLink); } //mapViewPanel.add(exportMapButton); // customTable.addNextKeyValue("Abstract", layerDV.getAbstractSection()); // customTable.addNextKeyValue("Valutazione qualità", layerDV.getValutazioneQualita()); // customTable.addNextKeyValue("Metodo raccolta dati", layerDV.getMetodoRaccoltaDati()); // customTable.addNextKeyValue("Scala acquisizione dati", layerDV.getScalaAcquisizione()); // customTable.addNextKeyValues("Autori", layerDV.getAuthors(), GeoportalDataViewerConstants.NEW_LINE_BR); // layerPanel.add(customTable); } public static native void exportPDF(String layerURL) /*-{ var image = new Image(layerURL); var doc = new $wnd.jspdf.jsPDF(); doc.addImage(layerURL, 'PNG', 0, 0, image.width, image.height); doc.save("map.pdf") }-*/; /** * Prints the. * * @param msg the msg */ public static native void downloadMap(ol.Map map, String mapPanelId, String linkId, String filename, String mimeType)/*-{ console.log("map: " + map); map.once('rendercomplete', function() { var mapCanvas = $doc.createElement('canvas'); var size = map.getSize(); mapCanvas.width = size[0]; mapCanvas.height = size[1]; var mapContext = mapCanvas.getContext('2d'); var mapContainer = $doc.querySelector('#' + mapPanelId); //console.log("mapContainer:" +JSON.stringify(mapContainer, null, 4)); Array.prototype.forEach.call(mapContainer .querySelectorAll('.ol-layer canvas'), function(canvas) { if (canvas.width > 0) { var opacity = canvas.parentNode.style.opacity; mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity); var transform = canvas.style.transform; // Get the transform parameters from the style's transform matrix var matrix = transform.match(/^matrix\(([^\(]*)\)$/)[1] .split(',').map(Number); // Apply the transform to the export map context CanvasRenderingContext2D.prototype.setTransform.apply( mapContext, matrix); mapContext.drawImage(canvas, 0, 0); } }); if (navigator.msSaveBlob) { // link download attribute does not work on MS browsers navigator.msSaveBlob(mapCanvas.msToBlob(), filename); } else { var link = $doc.getElementById(linkId); link.href = mapCanvas.toDataURL(mimeType); link.click(); } }); map.renderSync(); }-*/; }