2019-02-21 17:24:43 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.openlayerwidget;
|
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.event.SelectAreaDialogEvent;
|
|
|
|
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.event.SelectAreaDialogEvent.SelectAreaDialogEventHandler;
|
2019-02-22 15:15:25 +01:00
|
|
|
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.event.SelectAreaDialogEventType;
|
2019-02-21 17:24:43 +01:00
|
|
|
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.widgets.AreaSelectionDialog;
|
2019-04-05 15:13:01 +02:00
|
|
|
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.widgets.GeometryType;
|
2019-02-21 17:24:43 +01:00
|
|
|
|
2019-04-10 15:53:29 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ListBox;
|
2019-02-21 17:24:43 +01:00
|
|
|
import com.google.gwt.core.client.JavaScriptObject;
|
|
|
|
import com.google.gwt.core.client.JsonUtils;
|
|
|
|
import com.google.gwt.json.client.JSONObject;
|
|
|
|
import com.google.gwt.user.client.Command;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class GeoJsonAreaSelectionDialog.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Feb 20, 2019
|
|
|
|
*/
|
|
|
|
public class GeoJsonAreaSelectionDialog extends AreaSelectionDialog{
|
|
|
|
|
|
|
|
|
|
|
|
private String wktArea;
|
|
|
|
private Command onResponseCommand;
|
2019-04-10 15:53:29 +02:00
|
|
|
private ListBox geometries;
|
2019-02-21 17:24:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new geo json area selection dialog.
|
2019-04-10 15:53:29 +02:00
|
|
|
*
|
|
|
|
* @param geometry the geometry
|
2019-02-21 17:24:43 +01:00
|
|
|
*/
|
2019-04-10 15:53:29 +02:00
|
|
|
public GeoJsonAreaSelectionDialog(GeometryType geometry) {
|
|
|
|
super(geometry);
|
2019-02-21 17:24:43 +01:00
|
|
|
|
|
|
|
//THE HANDLER
|
|
|
|
SelectAreaDialogEventHandler handler = new SelectAreaDialogEventHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(SelectAreaDialogEvent event) {
|
2019-02-22 16:20:06 +01:00
|
|
|
print("SelectAreaDialog Response: "+event);
|
|
|
|
|
|
|
|
if(event==null)
|
|
|
|
return;
|
2019-02-22 15:15:25 +01:00
|
|
|
|
|
|
|
SelectAreaDialogEventType closedType = event.getSelectAreaDialogEventType();
|
2019-02-22 16:20:06 +01:00
|
|
|
|
|
|
|
if(closedType==null)
|
|
|
|
return;
|
|
|
|
|
2019-02-22 15:15:25 +01:00
|
|
|
wktArea = null;
|
|
|
|
if(closedType.equals(SelectAreaDialogEventType.Completed)){
|
|
|
|
wktArea = event.getArea();
|
|
|
|
onResponseCommand.execute();
|
|
|
|
}
|
2019-02-21 17:24:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addSelectAreaDialogEventHandler(handler);
|
2019-02-22 12:04:28 +01:00
|
|
|
this.getElement().addClassName("GeoJson-DialogBox");
|
|
|
|
setZIndex(10000);
|
2019-02-21 17:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire command on response.
|
|
|
|
*
|
|
|
|
* @param command the command
|
|
|
|
*/
|
|
|
|
public void fireCommandOnResponse(Command command){
|
|
|
|
this.onResponseCommand = command;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert wkt to geo json.
|
|
|
|
*
|
|
|
|
* @param wktData the wkt data
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
private static native String convertWKTToGeoJSON(String wktData) /*-{
|
2019-02-22 16:20:06 +01:00
|
|
|
try {
|
|
|
|
var ol = $wnd.ol;
|
|
|
|
var geojson_options = {};
|
|
|
|
var wkt_format = new ol.format.WKT();
|
2019-04-03 17:10:22 +02:00
|
|
|
var wktFeature = wkt_format.readFeature(wktData);
|
|
|
|
//console.log('WKT feature: '+wktFeature);
|
2019-02-22 16:20:06 +01:00
|
|
|
var wkt_options = {};
|
|
|
|
var geojson_format = new ol.format.GeoJSON(wkt_options);
|
2019-04-03 17:10:22 +02:00
|
|
|
console.log('geojson_format: '+geojson_format);
|
|
|
|
var geoJsonFeature = geojson_format.writeFeature(wktFeature);
|
2019-04-10 15:53:29 +02:00
|
|
|
//console.log('GeoJSON Feature: '+geoJsnameonFeature);
|
2019-04-03 17:10:22 +02:00
|
|
|
return geoJsonFeature;
|
2019-02-22 16:20:06 +01:00
|
|
|
}catch(err) {
|
|
|
|
console.log(err.message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}-*/;
|
|
|
|
|
2019-04-10 15:53:29 +02:00
|
|
|
/**
|
|
|
|
* Prints the.
|
|
|
|
*
|
|
|
|
* @param txt the txt
|
|
|
|
* @return the string
|
|
|
|
*/
|
2019-04-03 17:10:22 +02:00
|
|
|
public static native String print(String txt) /*-{
|
|
|
|
console.log(txt)
|
2019-02-21 17:24:43 +01:00
|
|
|
}-*/;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wkt to geo json.
|
|
|
|
*
|
|
|
|
* @param wktTxt the wkt txt
|
|
|
|
* @return the string
|
2019-04-03 17:10:22 +02:00
|
|
|
* @throws Exception the exception
|
2019-02-21 17:24:43 +01:00
|
|
|
*/
|
2019-04-03 17:10:22 +02:00
|
|
|
public String wktToGeoJSON(String wktTxt) throws Exception{
|
|
|
|
try {
|
|
|
|
String geoJSON = convertWKTToGeoJSON(wktTxt);
|
|
|
|
//Window.alert("geoJSON: "+geoJSON);
|
|
|
|
print("geoJSON: "+geoJSON);
|
|
|
|
|
|
|
|
if(geoJSON==null)
|
|
|
|
throw new Exception();
|
|
|
|
|
|
|
|
JavaScriptObject toJSON = JsonUtils.safeEval(geoJSON);
|
|
|
|
JSONObject objJson = new JSONObject(toJSON);
|
|
|
|
return objJson.get("geometry").toString();
|
|
|
|
}catch(Exception e) {
|
|
|
|
//silent
|
|
|
|
throw new Exception("Sorry, an error occurred while getting GeoJSON format for the drawn Geometry");
|
|
|
|
}
|
2019-02-21 17:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the WKT to geo json.
|
|
|
|
*
|
|
|
|
* @return the WKT to geo json
|
2019-04-03 17:10:22 +02:00
|
|
|
* @throws Exception the exception
|
2019-02-21 17:24:43 +01:00
|
|
|
*/
|
2019-04-03 17:10:22 +02:00
|
|
|
public String getWKTToGeoJSON() throws Exception{
|
2019-02-21 17:24:43 +01:00
|
|
|
|
2019-02-22 16:20:06 +01:00
|
|
|
if(wktArea==null){
|
|
|
|
print("wktArea is null");
|
2019-04-03 17:10:22 +02:00
|
|
|
throw new Exception("Sorry, an error occurred while reading the drawn Geometry");
|
2019-02-22 16:20:06 +01:00
|
|
|
}
|
2019-04-03 17:10:22 +02:00
|
|
|
//print("wktArea is: "+wktArea);
|
2019-02-21 17:24:43 +01:00
|
|
|
return wktToGeoJSON(wktArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the wkt area.
|
|
|
|
*
|
|
|
|
* @return the wktArea
|
|
|
|
*/
|
|
|
|
public String getWktArea() {
|
|
|
|
|
|
|
|
return wktArea;
|
|
|
|
}
|
2019-04-10 15:53:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the geometries.
|
|
|
|
*
|
|
|
|
* @return the geometries
|
|
|
|
*/
|
|
|
|
public static ListBox getGeometries() {
|
|
|
|
ListBox geometries = new ListBox();
|
|
|
|
for (GeometryType geometry : GeometryType.values()) {
|
|
|
|
geometries.addItem(geometry.name(),geometry.name());
|
|
|
|
}
|
|
|
|
return geometries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To geometry.
|
|
|
|
*
|
|
|
|
* @param name the name
|
|
|
|
* @return the geometry type
|
|
|
|
*/
|
|
|
|
public static GeometryType toGeometry(String name){
|
|
|
|
|
|
|
|
GeometryType theGeom = null;
|
|
|
|
try {
|
|
|
|
theGeom = GeometryType.valueOf(name);
|
|
|
|
}catch (Exception e) {
|
|
|
|
//silent
|
|
|
|
}
|
|
|
|
|
|
|
|
return theGeom;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-21 17:24:43 +01:00
|
|
|
}
|