package org.gcube.portlets.user.geoportaldataviewer.client.ui.map; 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.gis.ExtentWrapped; import org.gcube.portlets.user.geoportaldataviewer.client.gis.LightOpenLayerOSM; import org.gcube.portlets.user.geoportaldataviewer.client.gis.MapUtils; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil.Location; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil.PLACE; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.Random; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Widget; import ol.Coordinate; import ol.OLFactory; /** * The Class MapView. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * * Nov 11, 2020 */ public class MapView extends Composite { private static MapViewUiBinder uiBinder = GWT.create(MapViewUiBinder.class); /** * The Interface MapViewUiBinder. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * * Nov 11, 2020 */ interface MapViewUiBinder extends UiBinder { } @UiField HTMLPanel theMap; @UiField HorizontalPanel coordinatePanel; private LightOpenLayerOSM lightOLSM; /** * Instantiates a new map view. * * @param centerTo the center to * @param zoom the zoom */ public MapView(Coordinate centerTo, int zoom, String internalMapWidth, String internalMapHeight) { initWidget(uiBinder.createAndBindUi(this)); String theMapId = "map" + Random.nextInt(); theMap.getElement().setId(theMapId); theMap.setWidth(internalMapWidth); theMap.setHeight(internalMapHeight); Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { lightOLSM = new LightOpenLayerOSM(theMapId); // EPSG_3857 LOCATION TO ITALY Location italyLocation = ExtentMapUtil.getLocation(PLACE.ITALY); Coordinate transformedCenterCoordinate = italyLocation.getCoordinate(MAP_PROJECTION.EPSG_3857); lightOLSM.setCenter(transformedCenterCoordinate); lightOLSM.setZoom(GeoportalDataViewerConstants.LIGHT_MAP_ITALY_FIT_ZOOM_ON); } }); } private void setMapSize() { Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { int width = theMap.getParent().getOffsetWidth(); int height = theMap.getParent().getOffsetHeight(); if (width == 0) width = 300; if (height == 0) height = 300; GWT.log("Internal Map w: " + width + ", h: " + height); theMap.setSize(width + "px", height + "px"); } }); } /** * Adds the marker. * * @param coordinate the coordinate * @param showCoordinateText the show coordinate text */ public void addMarker(Coordinate coordinate, boolean showCoordinateText) { Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { if (lightOLSM != null) { lightOLSM.addPoint(coordinate, showCoordinateText, true); lightOLSM.getMap().getView().setCenter(coordinate); } } }); } /** * Adds the WMS layer. * * @param mapServerHost the map server host * @param layerName the layer name * @param bbox the bbox */ public void addWMSLayer(String mapServerHost, String layerName, BoundsMap bbox) { Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { ExtentWrapped ew = null; BoundsMap theBBOX = bbox; if (bbox != null) { Coordinate lower = OLFactory.createCoordinate(bbox.getLowerLeftX(), bbox.getLowerLeftY()); Coordinate lowerCoord = MapUtils.transformCoordiante(lower, MAP_PROJECTION.EPSG_4326.getName(), MAP_PROJECTION.EPSG_3857.getName()); Coordinate upper = OLFactory.createCoordinate(bbox.getUpperRightX(), bbox.getUpperRightY()); Coordinate upperCoord = MapUtils.transformCoordiante(upper, MAP_PROJECTION.EPSG_4326.getName(), MAP_PROJECTION.EPSG_3857.getName()); ew = new ExtentWrapped(lowerCoord.getX(), lowerCoord.getY(), upperCoord.getX(), upperCoord.getY()); theBBOX = new BoundsMap(lowerCoord.getX(), lowerCoord.getY(), upperCoord.getX(), upperCoord.getY(), null); } lightOLSM.addWMSLayer(mapServerHost, layerName, theBBOX); if (ew != null) { lightOLSM.getMap().getView().fit(ew); } } }); } public LightOpenLayerOSM getLightOLSM() { return lightOLSM; } }