removed PointsPath and passed to Geometry JSON string

This commit is contained in:
Francesco Mangiacrapa 2021-09-06 16:39:12 +02:00
parent ad71d45158
commit 0e082c0c37
6 changed files with 188 additions and 165 deletions

View File

@ -48,14 +48,20 @@ public class GeoportalDataViewerConstants {
public static enum MAP_PROJECTION {
EPSG_4326("EPSG:4326"), EPSG_3857("EPSG:3857");
EPSG_4326("4326", "EPSG:4326"), EPSG_3857("3857", "EPSG:3857");
String id;
String name;
MAP_PROJECTION(String name) {
MAP_PROJECTION(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
@ -85,10 +91,9 @@ public class GeoportalDataViewerConstants {
* @param msg the msg
*/
public static native void printJsObj(Object object)/*-{
console.log("js obj: "+JSON.stringify(object, null, 4));
console.log("js obj: " + JSON.stringify(object, null, 4));
}-*/;
/**
* Prints the.
*

View File

@ -21,6 +21,8 @@ import org.gcube.portlets.user.geoportaldataviewer.client.events.QueryDataEventH
import org.gcube.portlets.user.geoportaldataviewer.client.events.ShowDetailsEvent;
import org.gcube.portlets.user.geoportaldataviewer.client.events.ZoomOutOverMinimumEvent;
import org.gcube.portlets.user.geoportaldataviewer.client.events.ZoomOutOverMinimumEventHandler;
import org.gcube.portlets.user.geoportaldataviewer.client.gis.ExtentWrapped;
import org.gcube.portlets.user.geoportaldataviewer.client.gis.MapUtils;
import org.gcube.portlets.user.geoportaldataviewer.client.gis.OpenLayerOSM;
import org.gcube.portlets.user.geoportaldataviewer.client.util.StringUtil;
import org.gcube.portlets.user.geoportaldataviewer.client.util.URLUtil;
@ -29,7 +31,6 @@ import org.gcube.portlets.user.geoportaldataviewer.shared.gis.GeoQuery;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.LayerItem;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.LayerObject;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.FeatureRow;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.PointsPath;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.GeoInformationForWMSRequest;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.ZAxis;
@ -41,6 +42,8 @@ import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
@ -52,6 +55,10 @@ import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import ol.Coordinate;
import ol.Extent;
import ol.OLFactory;
import ol.format.GeoJson;
import ol.geom.Geometry;
/**
* The Class LayerManager.
@ -350,17 +357,69 @@ public class LayerManager {
}
Coordinate centerTo = null;
GWT.log("geometry is: " + feature.getGeometry());
if (feature.getGeometry() != null) {
GWT.log("trasforming geometry: " + feature.getGeometry().getToJSONObject());
PointsPath path = feature.getGeometry().getPath();
GWT.log("Points Path: " + path);
if (path != null && path.getCoordinates() != null
&& path.getCoordinates().length > 0) {
org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.Coordinate center = path
.getCoordinates()[0];
centerTo = new Coordinate(center.getX(), center.getY());
Geometry geom = new GeoJson()
.readGeometry(feature.getGeometry().getToJSONObject(), null);
//POINT
if (geom.getType().equalsIgnoreCase("Point")) {
GWT.log("geometry: is a point");
String coordinateJSON = feature.getGeometry().getCoordinatesJSON();
JSONArray centerJSON = (JSONArray) JSONParser
.parseStrict(coordinateJSON);
// Coordinate center = OLFactory.createCoordinate(
// new Double(centerJSON.get(0).toString()),
// new Double(centerJSON.get(1).toString()));
Coordinate center = new Coordinate(new Double(centerJSON.get(0).toString()),
new Double(centerJSON.get(1).toString()));
if (feature.getCrsName() != null && feature.getCrsName().endsWith(MAP_PROJECTION.EPSG_4326.getId())) {
center = MapUtils.transformCoordiante(center,
MAP_PROJECTION.EPSG_4326.getName(),
MAP_PROJECTION.EPSG_3857.getName());
}
centerTo = center;
} else {
Extent geomExtent = geom.getExtent();
Coordinate lower = OLFactory.createCoordinate(
geomExtent.getLowerLeftX(), geomExtent.getLowerLeftY());
Coordinate upper = OLFactory.createCoordinate(
geomExtent.getUpperRightX(), geomExtent.getUpperRightY());
Coordinate lowerCoord = lower;
Coordinate upperCoord = upper;
if (feature.getCrsName() != null && feature.getCrsName()
.endsWith(MAP_PROJECTION.EPSG_4326.getId())) {
lowerCoord = MapUtils.transformCoordiante(lower,
MAP_PROJECTION.EPSG_4326.getName(),
MAP_PROJECTION.EPSG_3857.getName());
upperCoord = MapUtils.transformCoordiante(upper,
MAP_PROJECTION.EPSG_4326.getName(),
MAP_PROJECTION.EPSG_3857.getName());
}
ExtentWrapped ew = new ExtentWrapped(lowerCoord.getX(),
lowerCoord.getY(), upperCoord.getX(), upperCoord.getY());
centerTo = new Coordinate(ew.getCenter().getX(), ew.getCenter().getY());
}
GWT.log("center is: "+centerTo);
// PointsPath path = feature.getGeometry().getPath();
// GWT.log("Points Path: " + path);
// if (path != null && path.getCoordinates() != null
// && path.getCoordinates().length > 0) {
// org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.Coordinate center = path
// .getCoordinates()[0];
// centerTo = new Coordinate(center.getX(), center.getY());
// }
}
// fallback

View File

@ -19,17 +19,14 @@ import org.gcube.application.geoportalcommon.shared.gis.BoundsMap;
import org.gcube.application.geoportalcommon.util.URLParserUtil;
import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.LayerItem;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.Coordinate;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.FeatureGeometry;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.FeatureRow;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.PointsPath;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class FeatureParser.
*
@ -50,17 +47,15 @@ public class FeatureParser {
* @param maxWFSFeature the max WFS feature
* @return the WFS features
*/
public static List<FeatureRow> getWFSFeatures(LayerItem layerItem, String mapSrsName, BoundsMap selectBBOX, int maxWFSFeature) {
public static List<FeatureRow> getWFSFeatures(LayerItem layerItem, String mapSrsName, BoundsMap selectBBOX,
int maxWFSFeature) {
if(maxWFSFeature<0) {
if (maxWFSFeature < 0) {
maxWFSFeature = GeoportalDataViewerConstants.MAX_WFS_FEATURES;
}
return getWFSFeatureProperties(layerItem, mapSrsName, selectBBOX, maxWFSFeature);
}
/**
* Gets the WFS feature properties.
*
@ -71,29 +66,33 @@ public class FeatureParser {
* @return the WFS feature properties
*/
@SuppressWarnings("unchecked")
private static List<FeatureRow> getWFSFeatureProperties(LayerItem layerItem, String mapSrsName, BoundsMap selectBBOX, int maxWFSFeature) {
private static List<FeatureRow> getWFSFeatureProperties(LayerItem layerItem, String mapSrsName,
BoundsMap selectBBOX, int maxWFSFeature) {
LOG.info("getWFSFeatureProperties for layerItem: "+layerItem.getName() +" in the "+selectBBOX +" and maxWFSFeature: "+maxWFSFeature);
LOG.info("getWFSFeatureProperties for layerItem: " + layerItem.getName() + " in the " + selectBBOX
+ " and maxWFSFeature: " + maxWFSFeature);
InputStream is = null;
List<FeatureRow> listFeaturesRow = new ArrayList<FeatureRow>();
try {
String url = GisMakers.buildWFSFeatureQuery(layerItem, mapSrsName, selectBBOX, maxWFSFeature, GisMakers.JSON);
String url = GisMakers.buildWFSFeatureQuery(layerItem, mapSrsName, selectBBOX, maxWFSFeature,
GisMakers.JSON);
String cqlFilterValue = URLParserUtil.extractValueOfParameterFromURL(GisMakers.CQL_FILTER_PARAMETER, url);
LOG.info("Found CQL filter value into query string: "+cqlFilterValue);
LOG.info("Found CQL filter value into query string: " + cqlFilterValue);
if(cqlFilterValue!=null) {
String notEncodedCQLFilter = String.format("%s=%s",GisMakers.CQL_FILTER_PARAMETER,cqlFilterValue);
//log.info("Found CQL filter: "+notEncodedCQLFilter);
String toEncodeCQLFilter = String.format("%s=%s",GisMakers.CQL_FILTER_PARAMETER,URLEncoder.encode(cqlFilterValue,"UTF-8"));
LOG.debug("Encoded CQL filter: "+toEncodeCQLFilter);
if (cqlFilterValue != null) {
String notEncodedCQLFilter = String.format("%s=%s", GisMakers.CQL_FILTER_PARAMETER, cqlFilterValue);
// log.info("Found CQL filter: "+notEncodedCQLFilter);
String toEncodeCQLFilter = String.format("%s=%s", GisMakers.CQL_FILTER_PARAMETER,
URLEncoder.encode(cqlFilterValue, "UTF-8"));
LOG.debug("Encoded CQL filter: " + toEncodeCQLFilter);
url = url.replace(notEncodedCQLFilter, toEncodeCQLFilter);
}
LOG.info("Built WFS URL: "+url);
LOG.info("Built WFS URL: " + url);
is = new URL(url).openStream();
String jsonTxt = IOUtils.toString(is);
if(jsonTxt==null || jsonTxt.isEmpty()){
if (jsonTxt == null || jsonTxt.isEmpty()) {
jsonTxt = "{\"type\":\"FeatureCollection\",\"features\":[]}";
}
@ -101,7 +100,7 @@ public class FeatureParser {
JSONObject json = new JSONObject(jsonTxt);
// iterate features
JSONArray features = json.getJSONArray("features");
if(features.length()==0) {
if (features.length() == 0) {
LOG.info("No features detected in the response, returning empty list");
return listFeaturesRow;
}
@ -111,58 +110,63 @@ public class FeatureParser {
JSONObject crs = json.getJSONObject("crs");
JSONObject crsProp = crs.getJSONObject("properties");
featureCRSName = crsProp.getString("name");
LOG.info("Crs name found: "+featureCRSName);
}catch (Exception e) {
LOG.info("Crs name found: " + featureCRSName);
} catch (Exception e) {
LOG.warn("Unable to read the field 'crs'");
}
LOG.info("Features are: "+features.length());
LOG.info("Features are: " + features.length());
for (int i=0; i<features.length(); i++) {
for (int i = 0; i < features.length(); i++) {
final FeatureRow row = new FeatureRow();
row.setCrsName(featureCRSName);
JSONObject theFeature = ((JSONObject)features.get(i));
LOG.debug("Building at index: "+i);
JSONObject theFeature = ((JSONObject) features.get(i));
LOG.debug("Building at index: " + i);
try {
JSONObject geometry = theFeature.getJSONObject("geometry");
String typeValue = geometry.getString("type");
JSONArray coordinates = geometry.getJSONArray("coordinates");
//String toCoordinates = coordinates.toString();
String x1 = coordinates.get(0).toString();
String y1 = coordinates.get(1).toString();
LOG.debug("Coordinate x1: "+x1);
LOG.debug("Coordinate y1: "+y1);
Double coordX = null;
Double coordY = null;
// JSONArray coordinates = geometry.getJSONArray("coordinates");
// String toCoordinates = coordinates.toString();
// String x1 = coordinates.get(0).toString();
// String y1 = coordinates.get(1).toString();
// LOG.debug("Coordinate x1: "+x1);
// LOG.debug("Coordinate y1: "+y1);
// Double coordX = null;
// Double coordY = null;
FeatureGeometry fg = new FeatureGeometry();
fg.setType(typeValue);
//TODO ONLY POINT GEOMETRY
// TODO ONLY POINT GEOMETRY
try {
coordX = Double.parseDouble(x1);
coordY = Double.parseDouble(y1);
Coordinate coord = new Coordinate(coordX, coordY);
fg.setPath(new PointsPath(new Coordinate[] {coord}));
}catch (Exception e) {
LOG.warn("Not able to parse the 'coordinates' field: ",e);
JSONArray coordinates = geometry.getJSONArray("coordinates");
String coordinateJSONString = coordinates.toString();
LOG.debug("coordinates are: " + coordinateJSONString);
fg.setCoordinatesJSON(coordinates.toString());
// coordX = Double.parseDouble(x1);
// coordY = Double.parseDouble(y1);
// Coordinate coord = new Coordinate(coordX, coordY);
// fg.setPath(new PointsPath(new Coordinate[] {coord}));
} catch (Exception e) {
LOG.warn("Not able to parse the 'coordinates' field: ", e);
}
row.setGeometry(fg);
}catch (Exception e) {
LOG.debug("Unable to pase geometry at index: "+i);
} catch (Exception e) {
LOG.debug("Unable to parse geometry at index: " + i);
}
// // iterate properties
JSONObject properties = theFeature.getJSONObject("properties");
Map<String,List<String>> mapProperties = new HashMap<String,List<String>>();
Map<String, List<String>> mapProperties = new HashMap<String, List<String>>();
@SuppressWarnings("unchecked")
Iterator<String> ii = properties.keys();
while (ii.hasNext()) {
String key = ii.next();
String value = properties.optString(key,"");
String value = properties.optString(key, "");
List<String> theValues = mapProperties.get(key);
if(theValues==null)
if (theValues == null)
mapProperties.put(key, Arrays.asList(value));
else {
theValues.add(value);
@ -171,18 +175,18 @@ public class FeatureParser {
}
row.setMapProperties(mapProperties);
listFeaturesRow.add(row);
LOG.info("Added row "+row+" to exported properties");
LOG.info("Added row " + row + " to exported properties");
}
} catch (IOException e) {
LOG.error("Error for layerItem name: "+layerItem.getName(), e);
LOG.error("Error for layerItem name: " + layerItem.getName(), e);
} catch (JSONException e) {
LOG.error("Error for layerItem name: "+layerItem.getName(), e);
}finally{
LOG.error("Error for layerItem name: " + layerItem.getName(), e);
} finally {
IOUtils.closeQuietly(is);
}
LOG.info("Returning "+listFeaturesRow.size()+" features");
LOG.info("Returning " + listFeaturesRow.size() + " features");
return listFeaturesRow;
}
}

View File

@ -3,44 +3,60 @@ package org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs;
import java.io.Serializable;
/**
* The Class FeatureGeoemtry.
* The Class FeatureGeometry.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Oct 29, 2020
* Sep 6, 2021
*/
public class FeatureGeometry implements WFSGeometry, Serializable {
/**
*
*/
private static final long serialVersionUID = 6251162740446739453L;
private static final long serialVersionUID = 8134176011135233810L;
private String type;
private PointsPath path;
private String coordinatesJSON;
private String toJSON;
/**
* Instantiates a new feature geometry.
*/
public FeatureGeometry() {
}
public FeatureGeometry(String type, PointsPath path) {
/**
* Instantiates a new feature geometry.
*
* @param type the type
* @param coordinatesJSON the coordinates JSON
*/
public FeatureGeometry(String type, String coordinatesJSON) {
super();
this.type = type;
this.path = path;
this.coordinatesJSON = coordinatesJSON;
}
public String getType() {
return type;
}
public String getCoordinatesJSON() {
return coordinatesJSON;
}
public void setType(String type) {
this.type = type;
}
public PointsPath getPath() {
return path;
public void setCoordinatesJSON(String coordinatesJSON) {
this.coordinatesJSON = coordinatesJSON;
}
public void setPath(PointsPath path) {
this.path = path;
public String getToJSONObject() {
if(toJSON==null)
toJSON = "{\"type\":\""+type+"\",\"coordinates\":"+coordinatesJSON+"}";
return toJSON;
}
@Override
@ -48,8 +64,8 @@ public class FeatureGeometry implements WFSGeometry, Serializable {
StringBuilder builder = new StringBuilder();
builder.append("FeatureGeometry [type=");
builder.append(type);
builder.append(", path=");
builder.append(path);
builder.append(", coordinatesJSON=");
builder.append(coordinatesJSON);
builder.append("]");
return builder.toString();
}

View File

@ -1,60 +0,0 @@
package org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs;
import java.io.Serializable;
import java.util.Arrays;
/**
* The Class PointsPath.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Nov 16, 2020
*/
public class PointsPath implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3295493507974317970L;
private Coordinate[] coordinates;
/**
* Instantiates a new points path.
*/
public PointsPath() {
}
/**
* Instantiates a new points path.
*
* @param coordinates the coordinates
*/
public PointsPath(Coordinate[] coordinates) {
super();
this.coordinates = coordinates;
}
public Coordinate[] getCoordinates() {
return coordinates;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PointsPath [coordinates=");
builder.append(Arrays.toString(coordinates));
builder.append("]");
return builder.toString();
}
}

View File

@ -4,9 +4,9 @@ package org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs;
/**
* The Interface WFSGeometry.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Nov 16, 2020
* Sep 6, 2021
*/
public interface WFSGeometry {
@ -17,12 +17,11 @@ public interface WFSGeometry {
*/
String getType();
/**
* Gets the path.
* Gets the coordinates JSON.
*
* @return the path
* @return the coordinates JSON
*/
PointsPath getPath();
String getCoordinatesJSON();
}