ckan-metadata-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/client/openlayerwidget/GeoJsonAreaSelectionDialog....

125 lines
3.0 KiB
Java

/**
*
*/
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;
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.event.SelectAreaDialogEventType;
import org.gcube.portlets.widgets.openlayerbasicwidgets.client.widgets.AreaSelectionDialog;
import com.google.gwt.core.client.GWT;
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;
/**
* Instantiates a new geo json area selection dialog.
*/
public GeoJsonAreaSelectionDialog() {
//THE HANDLER
SelectAreaDialogEventHandler handler = new SelectAreaDialogEventHandler() {
@Override
public void onResponse(SelectAreaDialogEvent event) {
GWT.log("SelectAreaDialog Response: "+event);
SelectAreaDialogEventType closedType = event.getSelectAreaDialogEventType();
wktArea = null;
if(closedType.equals(SelectAreaDialogEventType.Completed)){
wktArea = event.getArea();
onResponseCommand.execute();
}
}
};
addSelectAreaDialogEventHandler(handler);
this.getElement().addClassName("GeoJson-DialogBox");
setZIndex(10000);
}
/**
* 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) /*-{
var ol = $wnd.ol;
var geojson_options = {};
var wkt_format = new ol.format.WKT();
var testFeature = wkt_format.readFeature(wktData);
var wkt_options = {};
var geojson_format = new ol.format.GeoJSON(wkt_options);
var out = geojson_format.writeFeature(testFeature);
//window.getELementById("my-id").innerhtml(out);
//alert(out);
//console.log(out)
return out;
}-*/;
/**
* Wkt to geo json.
*
* @param wktTxt the wkt txt
* @return the string
*/
public String wktToGeoJSON(String wktTxt){
String geoJSON = convertWKTToGeoJSON(wktTxt);
//Window.alert("geoJSON: "+geoJSON);
GWT.log("geoJSON: "+geoJSON);
JavaScriptObject toJSON = JsonUtils.safeEval(geoJSON);
JSONObject objJson = new JSONObject(toJSON);
return objJson.get("geometry").toString();
}
/**
* Gets the WKT to geo json.
*
* @return the WKT to geo json
*/
public String getWKTToGeoJSON(){
return wktToGeoJSON(wktArea);
}
/**
* Gets the wkt area.
*
* @return the wktArea
*/
public String getWktArea() {
return wktArea;
}
}