From 0e082c0c37b8188b24bd04b8c1dddcc34a6a47ac Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 6 Sep 2021 16:39:12 +0200 Subject: [PATCH] removed PointsPath and passed to Geometry JSON string --- .../client/GeoportalDataViewerConstants.java | 17 ++- .../client/LayerManager.java | 81 ++++++++-- .../server/gis/FeatureParser.java | 140 +++++++++--------- .../shared/gis/wfs/FeatureGeometry.java | 44 ++++-- .../shared/gis/wfs/PointsPath.java | 60 -------- .../shared/gis/wfs/WFSGeometry.java | 11 +- 6 files changed, 188 insertions(+), 165 deletions(-) delete mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/PointsPath.java diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java index a19011f..d7ae1db 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java @@ -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; } @@ -78,17 +84,16 @@ public class GeoportalDataViewerConstants { public static native void printJs(String msg)/*-{ console.log("js console: " + msg); }-*/; - + /** * Prints the. * * @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. * diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java index b359f34..fee25ae 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java @@ -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()); + + 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()); - 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()); } + 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 @@ -538,8 +597,8 @@ public class LayerManager { * @param isNcWms the is nc wms * @param UUID the uuid * @param zAxis the z axis - * @param minResolution the min resolution - * @param maxResolution the max resolution + * @param minResolution the min resolution + * @param maxResolution the max resolution * @return the layer item */ private LayerItem toLayerItem(LayerType layerType, String layerTitle, String layerName, String layerURL, diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/gis/FeatureParser.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/gis/FeatureParser.java index fa3362b..f7f52b8 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/gis/FeatureParser.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/gis/FeatureParser.java @@ -19,23 +19,20 @@ 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. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * - * Nov 13, 2020 + * Nov 13, 2020 */ public class FeatureParser { @@ -44,125 +41,132 @@ public class FeatureParser { /** * Gets the WFS features. * - * @param layerItem the layer item - * @param mapSrsName the map srs name - * @param selectBBOX the select BBOX + * @param layerItem the layer item + * @param mapSrsName the map srs name + * @param selectBBOX the select BBOX * @param maxWFSFeature the max WFS feature * @return the WFS features */ - public static List getWFSFeatures(LayerItem layerItem, String mapSrsName, BoundsMap selectBBOX, int maxWFSFeature) { + public static List 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. * - * @param layerItem the layer item - * @param mapSrsName the map srs name - * @param selectBBOX the select BBOX + * @param layerItem the layer item + * @param mapSrsName the map srs name + * @param selectBBOX the select BBOX * @param maxWFSFeature the max WFS feature * @return the WFS feature properties */ @SuppressWarnings("unchecked") - private static List getWFSFeatureProperties(LayerItem layerItem, String mapSrsName, BoundsMap selectBBOX, int maxWFSFeature) { + private static List 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 listFeaturesRow = new ArrayList(); 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); - - 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); + 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); 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\":[]}"; } - + // get json object 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; } - + String featureCRSName = ""; try { 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()); - - for (int i=0; i> mapProperties = new HashMap>(); + Map> mapProperties = new HashMap>(); @SuppressWarnings("unchecked") Iterator ii = properties.keys(); while (ii.hasNext()) { String key = ii.next(); - String value = properties.optString(key,""); - + String value = properties.optString(key, ""); + List 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; } } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/FeatureGeometry.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/FeatureGeometry.java index 89f62e5..a8364ba 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/FeatureGeometry.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/FeatureGeometry.java @@ -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,10 +64,10 @@ 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(); } -} +} \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/PointsPath.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/PointsPath.java deleted file mode 100644 index d153b0f..0000000 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/PointsPath.java +++ /dev/null @@ -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(); - } - - - -} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/WFSGeometry.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/WFSGeometry.java index 7a81954..f70685e 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/WFSGeometry.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/wfs/WFSGeometry.java @@ -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 { @@ -16,13 +16,12 @@ public interface WFSGeometry { * @return the type */ String getType(); - /** - * Gets the path. + * Gets the coordinates JSON. * - * @return the path + * @return the coordinates JSON */ - PointsPath getPath(); + String getCoordinatesJSON(); }