You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
geoportal-data-viewer-app/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/map/MapView.java

165 lines
4.8 KiB
Java

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.gis.ExtentWrapped;
import org.gcube.portlets.user.geoportaldataviewer.client.gis.LightOpenLayerOSM;
import org.gcube.portlets.user.geoportaldataviewer.client.gis.MapUtils;
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<Widget, MapView> {
}
@UiField
HTMLPanel theMap;
@UiField
HorizontalPanel coordinatePanel;
private LightOpenLayerOSM olsm;
/**
* 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() {
olsm = new LightOpenLayerOSM(theMapId);
//EPSG_4326_TO_ITALY
Coordinate centerCoordinate = OLFactory.createCoordinate(GeoportalDataViewerConstants.ITALY_CENTER_LONG, GeoportalDataViewerConstants.ITALY_CENTER_LAT);
Coordinate transformedCenterCoordinate = MapUtils.transformCoordiante(centerCoordinate, GeoportalDataViewerConstants.EPSG_4326, GeoportalDataViewerConstants.EPSG_3857);
olsm.setCenter(transformedCenterCoordinate);
olsm.setZoom(GeoportalDataViewerConstants.LIGHT_MAP_ITALY_FIT_ZOOM_ON);
//setMapSize();
}
});
}
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(olsm!=null) {
olsm.addPoint(coordinate, showCoordinateText, true);
olsm.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, GeoportalDataViewerConstants.EPSG_4326,
GeoportalDataViewerConstants.EPSG_3857);
Coordinate upper = OLFactory.createCoordinate(bbox.getUpperRightX(), bbox.getUpperRightY());
Coordinate upperCoord = MapUtils.transformCoordiante(upper, GeoportalDataViewerConstants.EPSG_4326,
GeoportalDataViewerConstants.EPSG_3857);
ew = new ExtentWrapped(lowerCoord.getX(), lowerCoord.getY(), upperCoord.getX(), upperCoord.getY());
theBBOX = new BoundsMap(lowerCoord.getX(), lowerCoord.getY(), upperCoord.getX(), upperCoord.getY(), null);
}
olsm.addWMSLayer(mapServerHost, layerName, theBBOX);
if (ew != null) {
olsm.getMap().getView().fit(ew);
}
}
});
}
}