geoportal-data-viewer-app/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/map/MapView.java

135 lines
4.2 KiB
Java

package org.gcube.portlets.user.geoportaldataviewer.client.ui.map;
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 org.gcube.portlets.user.geoportaldataviewer.shared.gis.BoundsMap;
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.
*/
public MapView() {
initWidget(uiBinder.createAndBindUi(this));
String theMapId = "map"+Random.nextInt();
theMap.getElement().setId(theMapId);
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.ITALY_FIT_ZOOM_ON);
}
});
}
/**
* 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() {
olsm.addWMSLayer(mapServerHost, layerName);
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);
ExtentWrapped ew = new ExtentWrapped(lowerCoord.getX(), lowerCoord.getY(), upperCoord.getX(),
upperCoord.getY());
//Coordinate center = ew.getCenter();
//GWT.log("center: "+center);
//Coordinate invertCoordinate = new Coordinate(center.getY(), center.getX());
// Size size = new Size(300, 300);
//olsm.getMap().getView().setCenter(invertCoordinate);
olsm.getMap().getView().fit(ew);
}
}
});
}
}