307 lines
8.2 KiB
Java
307 lines
8.2 KiB
Java
package org.gcube.portlets.user.geoportaldataviewer.client.gis;
|
|
|
|
import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants;
|
|
import org.gcube.portlets.user.geoportaldataviewer.client.resources.Images;
|
|
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.GeoQuery;
|
|
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.GeoQuery.TYPE;
|
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import ol.Coordinate;
|
|
import ol.Feature;
|
|
import ol.Map;
|
|
import ol.MapBrowserEvent;
|
|
import ol.MapOptions;
|
|
import ol.OLFactory;
|
|
import ol.View;
|
|
import ol.ViewOptions;
|
|
import ol.control.Attribution;
|
|
import ol.event.EventListener;
|
|
import ol.geom.Point;
|
|
import ol.interaction.KeyboardPan;
|
|
import ol.interaction.KeyboardZoom;
|
|
import ol.layer.Image;
|
|
import ol.layer.LayerOptions;
|
|
import ol.layer.Tile;
|
|
import ol.layer.VectorLayerOptions;
|
|
import ol.proj.Projection;
|
|
import ol.proj.ProjectionOptions;
|
|
import ol.source.ImageWms;
|
|
import ol.source.ImageWmsOptions;
|
|
import ol.source.ImageWmsParams;
|
|
import ol.source.Osm;
|
|
import ol.source.XyzOptions;
|
|
import ol.style.Icon;
|
|
import ol.style.IconOptions;
|
|
import ol.style.Style;
|
|
import ol.style.Text;
|
|
import ol.style.TextOptions;
|
|
|
|
/**
|
|
* The Class LightOpenLayerOSM.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
*
|
|
* Nov 11, 2020
|
|
*/
|
|
public class LightOpenLayerOSM {
|
|
|
|
/** The map. */
|
|
private Map map;
|
|
|
|
/** The view. */
|
|
private View view;
|
|
|
|
/** The view options. */
|
|
private ViewOptions viewOptions = OLFactory.createOptions();
|
|
|
|
/** The projection options. */
|
|
private ProjectionOptions projectionOptions = OLFactory.createOptions();
|
|
|
|
private boolean isQueryPointActive;
|
|
|
|
private ol.layer.Vector geometryLayer;
|
|
|
|
private String markerURL = Images.ICONS.mapMarkerIcon().getURL();
|
|
|
|
|
|
/**
|
|
* Instantiates a new open layer OSM.
|
|
*
|
|
* @param divTargetId the div target id
|
|
*/
|
|
/* (non-Javadoc)
|
|
* @see de.desjardins.ol3.demo.client.example.Example#show()
|
|
*/
|
|
public LightOpenLayerOSM(String divTargetId) {
|
|
|
|
// create a OSM-layer
|
|
XyzOptions osmSourceOptions = OLFactory.createOptions();
|
|
|
|
Osm osmSource = new Osm(osmSourceOptions);
|
|
LayerOptions osmLayerOptions = OLFactory.createOptions();
|
|
osmLayerOptions.setSource(osmSource);
|
|
|
|
Tile osmLayer = new Tile(osmLayerOptions);
|
|
// create a projection
|
|
projectionOptions.setCode(GeoportalDataViewerConstants.EPSG_3857);
|
|
projectionOptions.setUnits("m");
|
|
|
|
Projection projection = new Projection(projectionOptions);
|
|
viewOptions.setProjection(projection);
|
|
viewOptions.setMaxZoom(19);
|
|
//viewOptions.setExtent(new Extent(-180, -90, 180, 90));
|
|
|
|
// create a view
|
|
view = new View(viewOptions);
|
|
|
|
//EPSG_4326_TO_ITALY
|
|
Coordinate centerCoordinate = OLFactory.createCoordinate(12.45, 42.98);
|
|
Coordinate transformedCenterCoordinate = Projection.transform(centerCoordinate, GeoportalDataViewerConstants.EPSG_4326, GeoportalDataViewerConstants.EPSG_3857);
|
|
|
|
view.setCenter(transformedCenterCoordinate);
|
|
view.setZoom(5);
|
|
|
|
// create the map
|
|
MapOptions mapOptions = OLFactory.createOptions();
|
|
mapOptions.setTarget(divTargetId);
|
|
mapOptions.setView(view);
|
|
|
|
map = new Map(mapOptions);
|
|
|
|
map.addLayer(osmLayer);
|
|
//map.addLayer(tileDebugLayer);
|
|
|
|
// add some controls
|
|
// map.addControl(OLFactory.createScaleLine());
|
|
// DemoUtils.addDefaultControls(map.getControls());
|
|
|
|
Attribution attribution = new Attribution();
|
|
attribution.setCollapsed(true);
|
|
|
|
map.addClickListener(new EventListener<MapBrowserEvent>() {
|
|
|
|
@Override
|
|
public void onEvent(MapBrowserEvent event) {
|
|
// TODO Auto-generated method stub
|
|
Coordinate coordinate = event.getCoordinate();
|
|
if(isQueryPointActive) {
|
|
|
|
double lon = coordinate.getX();
|
|
double lat = coordinate.getY();
|
|
|
|
int w = (int) map.getSize().getWidth();
|
|
int h = (int) map.getSize().getHeight();
|
|
// handler.clickOnMap(x, y, w, h);
|
|
|
|
// ratio - mapPixelWeight : bboxWeight = 10px : geoRectangleWidth
|
|
// where 10px is the pixel diameter dimension of the clicked point
|
|
double bboxWidth = Math.abs(getExtent().getLowerLeftX() - getExtent().getUpperRightX());
|
|
double geoWidth = (bboxWidth / w) * (20 / 2);
|
|
double x1 = Math.min(lon+geoWidth, lon-geoWidth);
|
|
double x2 = Math.max(lon+geoWidth, lon-geoWidth);
|
|
double y1 = Math.min(lat+geoWidth, lat-geoWidth);
|
|
double y2 = Math.max(lat+geoWidth, lat-geoWidth);
|
|
//GWT.log("("+x1+","+y1+")("+x2+","+y2+")");
|
|
|
|
// Point pt = new Point(coordinate);
|
|
// ol.Extent extent = pt.getExtent();
|
|
// //new ClickDataInfo(x1, y1, x2, y2)
|
|
// SelectDataInfo selectDataInfo
|
|
//selectBox(new GeoQuery(x1, y1, x2, y2, GeoQuery.TYPE.POINT));
|
|
GeoQuery select = new GeoQuery(x1, y1, x2, y2, TYPE.POINT);
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
map.addControl(attribution);
|
|
|
|
// add some interactions
|
|
map.addInteraction(new KeyboardPan());
|
|
map.addInteraction(new KeyboardZoom());
|
|
|
|
}
|
|
|
|
/**
|
|
* Adds the WMS layer.
|
|
*
|
|
* @param mapServerHost the map server host
|
|
* @param layerName the layer name
|
|
* @return the image
|
|
*/
|
|
public Image addWMSLayer(String mapServerHost, String layerName) {
|
|
GWT.log("Adding wmsLayer with mapServerHost: "+mapServerHost+", layerName: "+layerName);
|
|
|
|
ImageWmsParams imageWMSParams = OLFactory.createOptions();
|
|
imageWMSParams.setLayers(layerName);
|
|
|
|
ImageWmsOptions imageWMSOptions = OLFactory.createOptions();
|
|
imageWMSOptions.setUrl(mapServerHost);
|
|
imageWMSOptions.setParams(imageWMSParams);
|
|
//imageWMSOptions.setRatio(1.5f);
|
|
|
|
ImageWms imageWMSSource = new ImageWms(imageWMSOptions);
|
|
|
|
LayerOptions layerOptions = OLFactory.createOptions();
|
|
layerOptions.setSource(imageWMSSource);
|
|
|
|
ol.layer.Image wmsLayer = new ol.layer.Image(layerOptions);
|
|
//visibleLayerItems
|
|
map.addLayer(wmsLayer);
|
|
|
|
return wmsLayer;
|
|
}
|
|
|
|
|
|
/**
|
|
* Adds the point.
|
|
*
|
|
* @param coordinate the coordinate
|
|
* @param showCoordinateText the show coordinate text
|
|
* @param asMarker the as marker
|
|
*/
|
|
public void addPoint(Coordinate coordinate, boolean showCoordinateText, boolean asMarker) {
|
|
|
|
if(geometryLayer!=null) {
|
|
map.removeLayer(geometryLayer);
|
|
}else {
|
|
|
|
}
|
|
Style style = new Style();
|
|
|
|
if(asMarker) {
|
|
IconOptions iconOptions = new IconOptions();
|
|
iconOptions.setSrc(markerURL);
|
|
Icon icon = new Icon(iconOptions);
|
|
style.setImage(icon);
|
|
}
|
|
|
|
if(showCoordinateText) {
|
|
TextOptions textOptions = new TextOptions();
|
|
textOptions.setOffsetY(-25);
|
|
textOptions.setText("Long: "+coordinate.getX() + ", Lat: "+coordinate.getY());
|
|
Text text = new Text(textOptions);
|
|
// FillOptions fillOptions = new FillOptions();
|
|
// Color color = new Color(217, 217, 223, 0.0);
|
|
// fillOptions.setColor(color);
|
|
// Fill fill = new Fill(fillOptions);
|
|
// style.setFill(fill);
|
|
style.setText(text);
|
|
|
|
|
|
}
|
|
Point thePoint = new Point(coordinate);
|
|
Feature vf = new Feature(thePoint);
|
|
vf.setStyle(style);
|
|
ol.source.Vector vector = new ol.source.Vector();
|
|
vector.addFeature(vf);
|
|
VectorLayerOptions vectorLayerOptions = new VectorLayerOptions();
|
|
vectorLayerOptions.setSource(vector);
|
|
geometryLayer = new ol.layer.Vector(vectorLayerOptions);
|
|
map.addLayer(geometryLayer);
|
|
}
|
|
|
|
/**
|
|
* Gets the map.
|
|
*
|
|
* @return the map
|
|
*/
|
|
public Map getMap() {
|
|
return map;
|
|
}
|
|
|
|
/**
|
|
* Gets the projection code.
|
|
*
|
|
* @return the projection code
|
|
*/
|
|
public String getProjectionCode() {
|
|
return map.getView().getProjection().getCode();
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the current zoom level.
|
|
*
|
|
* @return the current zoom level
|
|
*/
|
|
public double getCurrentZoomLevel() {
|
|
return map.getView().getZoom();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Gets the bbox.
|
|
*
|
|
* @return the bbox
|
|
*/
|
|
public ol.Extent getBBOX() {
|
|
return getExtent();
|
|
}
|
|
|
|
/**
|
|
* Gets the extent.
|
|
*
|
|
* @return the extent
|
|
*/
|
|
public ol.Extent getExtent() {
|
|
return this.map.getView().calculateExtent(map.getSize());
|
|
}
|
|
|
|
/**
|
|
* Transform.
|
|
*
|
|
* @param centerCoordinate the center coordinate
|
|
* @param source the source
|
|
* @param target the target
|
|
* @return the coordinate
|
|
*/
|
|
public Coordinate transform (Coordinate centerCoordinate, String source, String target){
|
|
return Projection.transform(centerCoordinate, source, target);
|
|
}
|
|
|
|
}
|
|
|