geoportal-data-viewer-app/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/products/concessioni/LayerConcessioneView.java

138 lines
5.5 KiB
Java

package org.gcube.portlets.user.geoportaldataviewer.client.ui.products.concessioni;
import java.util.Map;
import org.gcube.application.geoportalcommon.shared.products.model.LayerConcessioneDV;
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.ui.util.CustomFlexTable;
import com.github.gwtbootstrap.client.ui.DropdownButton;
import com.github.gwtbootstrap.client.ui.NavLink;
import com.google.gwt.core.client.GWT;
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 LayerConcessioneView extends Composite {
private static LayerConcessioneUiBinder uiBinder = GWT.create(LayerConcessioneUiBinder.class);
interface LayerConcessioneUiBinder extends UiBinder<Widget, LayerConcessioneView> {
}
@UiField
HTMLPanel layerConcessionePanel;
@UiField
HTMLPanel mapViewPanel;
private CustomFlexTable customTable = new CustomFlexTable();
public LayerConcessioneView(LayerConcessioneDV layerDV) {
initWidget(uiBinder.createAndBindUi(this));
GWT.log("Showing: " + layerDV);
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);
layerConcessionePanel.add(customTable);
GWT.log("WMS LINK: " + layerDV.getWmsLink());
if (layerDV.getLayerName() != null && layerDV.getWmsLink() != null) {
Location italyLocation = ExtentMapUtil.getLocation(PLACE.ITALY);
Coordinate transformedCenterCoordinate = italyLocation.getCoordinate(MAP_PROJECTION.EPSG_3857);
// Coordinate centerCoordinate = OLFactory.createCoordinate(GeoportalDataViewerConstants.ITALY_CENTER_LONG, GeoportalDataViewerConstants.ITALY_CENTER_LAT);
// Coordinate transformedCenterCoordinate = MapUtils.transformCoordiante(centerCoordinate, MAP_PROJECTION.EPSG_4326.getName(), MAP_PROJECTION.EPSG_3857.getName());
MapView mapView = new MapView(transformedCenterCoordinate,
GeoportalDataViewerConstants.LIGHT_MAP_ITALY_FIT_ZOOM_ON, "70%", "300px");
mapViewPanel.add(mapView);
String mapServerHost = layerDV.getWmsLink().contains("?")
? layerDV.getWmsLink().substring(0, layerDV.getWmsLink().indexOf("?"))
: layerDV.getWmsLink();
DropdownButton downloadButt = new DropdownButton("download as...");
NavLink navLink = new NavLink("PNG");
HTML html = new HTML("<a id=\"image-download\" download=\"map.png\"></a>");
navLink.getElement().appendChild(html.getElement());
navLink.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Map<String, String> mapLayerURLs = mapView.getLightOLSM().getLayerURLsProperty();
String layerURL = mapLayerURLs.get(layerDV.getLayerName());
GWT.log("layerDV name is: " + layerDV.getLayerName());
if (layerURL != null)
Window.open(layerURL, "_blank", null);
//downloadMap(mapView.getLightOLSM().getMap());
}
});
downloadButt.add(navLink);
customTable.addNextKeyWidget("Layer", downloadButt);
mapView.addWMSLayer(mapServerHost, layerDV.getLayerName(), layerDV.getBbox());
}
}
/**
* Prints the.
*
* @param msg the msg
*/
public static native void downloadMap(ol.Map map)/*-{
console.log("map: " + map);
map.once('rendercomplete', function() {
var mapCanvas = document.createElement('canvas');
var size = map.getSize();
mapCanvas.width = size[0];
mapCanvas.height = size[1];
var mapContext = mapCanvas.getContext('2d');
Array.prototype.forEach.call(document
.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(), 'map.png');
} else {
console.log('qui');
// var link = document.getElementById('image-download');
// link.href = mapCanvas.toDataURL();
window.open(mapCanvas.toDataURL(),'_blank');
}
});
map.renderSync();
}-*/;
}