218 lines
9.4 KiB
Java
218 lines
9.4 KiB
Java
package org.gcube.dataanalysis.geo.utils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
|
|
import org.gcube.contentmanagement.graphtools.utils.HttpRequest;
|
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
|
import org.gcube.dataanalysis.geo.meta.OGCFormatter;
|
|
|
|
import com.vividsolutions.jts.geom.Coordinate;
|
|
import com.vividsolutions.jts.geom.GeometryFactory;
|
|
import com.vividsolutions.jts.geom.LinearRing;
|
|
import com.vividsolutions.jts.geom.Point;
|
|
import com.vividsolutions.jts.geom.Polygon;
|
|
import com.vividsolutions.jts.geom.PrecisionModel;
|
|
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
|
|
|
|
public class EnvDataExplorer {
|
|
|
|
private static String callWFS(String geoServer, String layer, double x, double y) {
|
|
|
|
float tolerance = 0.25f;
|
|
String wfsURL = OGCFormatter.getWfsUrl(geoServer, layer, OGCFormatter.pointToBoundingBox(x, y, tolerance), 1, "json");
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Requesting URL: " + wfsURL);
|
|
String returned = null;
|
|
try {
|
|
returned = HttpRequest.sendGetRequest(wfsURL, null);
|
|
} catch (Exception e) {
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> ERROR " + e.getLocalizedMessage());
|
|
}
|
|
if (returned != null)
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Found Intersection: " + returned);
|
|
else
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Found Nothing!");
|
|
|
|
return returned;
|
|
}
|
|
|
|
private static String callWFS(String geoServer, String layer, double xL, double yL, double xR, double yR) {
|
|
|
|
// String wfsURL = OGCFormatter.getWfsUrl(geoServer, layer, OGCFormatter.buildBoundingBox(xL, yL, xR, yR), 0, "json");
|
|
// there is a bug in WFS in the retrieval according to a bounding box: y must be in the range -180;180. then I preferred to take all the features
|
|
String wfsURL = OGCFormatter.getWfsUrl(geoServer, layer, null, 0, "json");
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Requesting URL: " + wfsURL);
|
|
String returned = null;
|
|
try {
|
|
returned = HttpRequest.sendGetRequest(wfsURL, null);
|
|
} catch (Exception e) {
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> ERROR " + e.getLocalizedMessage());
|
|
}
|
|
if (returned != null)
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Found Intersection: " + returned);
|
|
else
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Found Nothing!");
|
|
|
|
return returned;
|
|
}
|
|
|
|
public static LinkedHashMap<String, Double> getFeatures(String geoserver, String layer, double x, double y) {
|
|
try {
|
|
AnalysisLogger.getLogger().debug("Calling WFS towards Geoserver:" + geoserver + " and layer:" + layer);
|
|
String jsonString = callWFS(geoserver, layer, x, y);
|
|
LinkedHashMap<String, Object> map = JsonMapper.parse(jsonString);
|
|
LinkedHashMap<String, String> mapout = (LinkedHashMap<String, String>) ((HashMap<String, Object>) map.get("features")).get("properties");
|
|
LinkedHashMap<String, Double> values = new LinkedHashMap<String, Double>();
|
|
for (String key : mapout.keySet()) {
|
|
values.put(key, Double.parseDouble(mapout.get(key)));
|
|
}
|
|
return values;
|
|
} catch (Exception e) {
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Error in getting properties");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static List<FeaturedPolygon> getFeatures(String geoserver, String layer, double xL, double yL, double xR, double yR) {
|
|
try {
|
|
AnalysisLogger.getLogger().debug("Calling WFS towards Geoserver:" + geoserver + " and layer:" + layer);
|
|
String jsonString = callWFS(geoserver, layer, xL, yL, xR, yR);
|
|
// System.out.println("JSON:"+jsonString);
|
|
LinkedHashMap<String, Object> map = JsonMapper.parse(jsonString);
|
|
List<FeaturedPolygon> fpolygons = new ArrayList<FeaturedPolygon>();
|
|
FeaturedPolygon poly = null;
|
|
for (String key : map.keySet()) {
|
|
if (key.contains("features")) {
|
|
HashMap<String, Object> propertiesMap = (HashMap<String, Object>) map.get(key);
|
|
|
|
//cycle on all the properties
|
|
for (String properties : propertiesMap.keySet()) {
|
|
if (properties.contains("properties")) {
|
|
if (poly==null)
|
|
poly=new FeaturedPolygon();
|
|
|
|
LinkedHashMap<String, String> props = (LinkedHashMap<String, String>) propertiesMap.get(properties);
|
|
//fill the properties of the fpolygon
|
|
for (String keyprop : props.keySet()) {
|
|
try {
|
|
//fulfill the FeaturedPolygon
|
|
String value = props.get(keyprop);
|
|
try{
|
|
String lowcaseprop = keyprop.toLowerCase();
|
|
if ((poly.value==null) &&
|
|
!lowcaseprop.startsWith("id")&&
|
|
!lowcaseprop.endsWith("id"))
|
|
poly.setValue(Double.parseDouble(value));
|
|
else
|
|
poly.addFeature(keyprop, value);
|
|
}catch (Exception e2){
|
|
poly.addFeature(keyprop, value);
|
|
}
|
|
} catch (Exception e) {
|
|
}
|
|
}
|
|
}
|
|
else
|
|
if (properties.contains("geometry") && !properties.contains("geometry_")) {
|
|
|
|
if (poly==null)
|
|
poly=new FeaturedPolygon();
|
|
else if (poly.p!=null){
|
|
if (poly.value==null)
|
|
poly.value=Double.NaN;
|
|
fpolygons.add(poly);
|
|
poly=new FeaturedPolygon();
|
|
}
|
|
|
|
LinkedHashMap<String, String> props = (LinkedHashMap<String, String>) propertiesMap.get(properties);
|
|
List<double[]> coords = WFS2Coordinates(props.toString());
|
|
GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
|
|
Polygon p = null;
|
|
if (coords != null) {
|
|
Coordinate[] coordarray = new Coordinate[coords.size()];
|
|
int i=0;
|
|
for (double[] pair:coords){
|
|
coordarray[i] = new Coordinate(pair[0],pair[1]);
|
|
i++;
|
|
}
|
|
CoordinateArraySequence coordseq = new CoordinateArraySequence(coordarray);
|
|
LinearRing ring = new LinearRing(coordseq, factory);
|
|
p = new Polygon(ring, new LinearRing[] {}, factory);
|
|
}
|
|
poly.setPolygon(p);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}// end for on all the wfs
|
|
|
|
if (poly!=null){
|
|
if (poly.value==null)
|
|
poly.value=Double.NaN;
|
|
fpolygons.add(poly);
|
|
}
|
|
return fpolygons;
|
|
} catch (Exception e) {
|
|
AnalysisLogger.getLogger().debug("EnvDataExplorer-> Error in getting properties");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static List<double[]> WFS2Coordinates(String wfsgeometry) {
|
|
|
|
// geometry935133b1-ba3c-493d-8e18-6fb496ced995={type=MultiPolygon, coordinates={966a275c-23aa-4a43-a943-7e1c7eaf5d65=[[[1.5,125.00000000000011],[1.5,124.5],[2.000000000000057,124.5],[2.000000000000057,125.00000000000011],[1.5,125.00000000000011]]]}},
|
|
String[] coordinatePairs = null;
|
|
List<double[]> dpairs = new ArrayList<double[]>();
|
|
if (wfsgeometry.toLowerCase().contains("multipolygon")) {
|
|
String coordString = "coordinates=";
|
|
String coordinates = wfsgeometry.substring(wfsgeometry.indexOf(coordString) + coordString.length());
|
|
coordinates = coordinates.substring(coordinates.indexOf("=")+1);
|
|
coordinatePairs = coordinates.split("\\],\\[");
|
|
for (String coord : coordinatePairs) {
|
|
coord = coord.replaceAll("(\\[|\\]|\\}|\\{|)", "");
|
|
String[] coordpair = coord.split(",");
|
|
double[] dd = new double[2];
|
|
//invert the coordinates as the final must be are long,lat
|
|
dd[1] = Double.parseDouble(coordpair[0]);
|
|
dd[0] = Double.parseDouble(coordpair[1]);
|
|
dpairs.add(dd);
|
|
}
|
|
}
|
|
return dpairs;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
String geom = "{type=MultiPolygon, coordinates={cce4daf3-966e-4b5f-adea-f88ea2b93d03=[[[-16,-146.49999999999997],[-16,-146.99999999999994],[-15.5,-146.99999999999994],[-15.5,-146.49999999999997],[-16,-146.49999999999997]]]}}";
|
|
List<double[]> coords = WFS2Coordinates(geom);
|
|
|
|
GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
|
|
// GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 0);
|
|
/*
|
|
* CoordinateArraySequence coords = new CoordinateArraySequence(new Coordinate[] { new Coordinate(12.0, 34.23), new Coordinate(12.000, 54.555), new Coordinate(7, 8), new Coordinate(12.0, 34.23) }); LinearRing ring = new LinearRing(coords, factory); Polygon p = new Polygon(ring, null, factory); CoordinateArraySequence pcoords = new CoordinateArraySequence(new Coordinate[] { new Coordinate(12.0, 34.23),});
|
|
*/
|
|
// CoordinateArraySequence coords = new CoordinateArraySequence(new Coordinate[] { new Coordinate(1.5, 125.00000000000011), new Coordinate(1.5, 124.5), new Coordinate(2.000000000000057, 124.5), new Coordinate(2.000000000000057, 125.00000000000011), new Coordinate(1.5, 125.00000000000011) });
|
|
|
|
if (coords != null) {
|
|
Coordinate[] coordarray = new Coordinate[coords.size()];
|
|
int i=0;
|
|
for (double[] pair:coords){
|
|
coordarray[i] = new Coordinate(pair[0],pair[1]);
|
|
i++;
|
|
}
|
|
CoordinateArraySequence coordseq = new CoordinateArraySequence(coordarray);
|
|
LinearRing ring = new LinearRing(coordseq, factory);
|
|
Polygon p = new Polygon(ring, new LinearRing[] {}, factory);
|
|
// CoordinateArraySequence pcoords = new CoordinateArraySequence(new Coordinate[] { new Coordinate(-16,-146.49999999999997), });
|
|
CoordinateArraySequence pcoords = new CoordinateArraySequence(new Coordinate[] { new Coordinate(-150,-16), });
|
|
Point po = new Point(pcoords, factory);
|
|
// po = p.getCentroid();
|
|
System.out.println("contains: " + p.contains(po)+" boundary: "+p.covers(po));
|
|
}
|
|
}
|
|
|
|
}
|