git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineGeoSpatialExtension@92310 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
14f3bcf6f3
commit
e1a653c1fd
|
@ -1,12 +1,16 @@
|
|||
package org.gcube.dataanalysis.geo.matrixmodel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
||||
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
|
||||
import org.gcube.dataanalysis.ecoengine.utils.Tuple;
|
||||
import org.gcube.dataanalysis.geo.connectors.asc.ASC;
|
||||
import org.gcube.dataanalysis.geo.connectors.netcdf.NetCDF;
|
||||
import org.gcube.dataanalysis.geo.connectors.table.Table;
|
||||
import org.gcube.dataanalysis.geo.connectors.table.TableMatrixRepresentation;
|
||||
import org.gcube.dataanalysis.geo.connectors.wfs.WFS;
|
||||
import org.gcube.dataanalysis.geo.infrastructure.GeoNetworkInspector;
|
||||
import org.gcube.dataanalysis.geo.interfaces.GISDataConnector;
|
||||
|
@ -16,21 +20,28 @@ import org.opengis.metadata.identification.Identification;
|
|||
public class MatrixExtractor {
|
||||
|
||||
private GeoNetworkInspector gnInspector;
|
||||
private String configDir;
|
||||
private AlgorithmConfiguration configuration;
|
||||
public static int maxSignalLength = 100000;
|
||||
|
||||
public MatrixExtractor(String scope, String cfgDir) {
|
||||
public MatrixExtractor(AlgorithmConfiguration configuration) {
|
||||
gnInspector = new GeoNetworkInspector();
|
||||
gnInspector.setScope(scope);
|
||||
this.configDir=cfgDir;
|
||||
gnInspector.setScope(configuration.getGcubeScope());
|
||||
this.configuration=configuration;
|
||||
}
|
||||
|
||||
public GeoNetworkInspector getFeaturer(){
|
||||
|
||||
public GeoNetworkInspector getFeaturer() {
|
||||
return gnInspector;
|
||||
}
|
||||
|
||||
|
||||
public static List<Tuple<Double>> generateCoordinateTripletsInBoundingBox(double x1, double x2, double y1, double y2, double z, double xResolution, double yResolution){
|
||||
|
||||
|
||||
public boolean isTable() {
|
||||
if (configuration.getParam(TableMatrixRepresentation.tableNameParameter) != null)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<Tuple<Double>> generateCoordinateTripletsInBoundingBox(double x1, double x2, double y1, double y2, double z, double xResolution, double yResolution) {
|
||||
|
||||
int ysteps = (int) ((y2 - y1) / yResolution);
|
||||
int xsteps = (int) ((x2 - x1) / xResolution);
|
||||
List<Tuple<Double>> tuples = new ArrayList<Tuple<Double>>();
|
||||
|
@ -50,62 +61,121 @@ public static List<Tuple<Double>> generateCoordinateTripletsInBoundingBox(double
|
|||
return tuples;
|
||||
}
|
||||
|
||||
protected List<Double> getRawValuesInTimeInstantAndBoundingBox(String layerTitle, int time, List<Tuple<Double>> coordinates3d, double xL,double xR, double yL, double yR) throws Exception {
|
||||
protected List<Double> getRawValuesInTimeInstantAndBoundingBox(String layerTitle, int time, List<Tuple<Double>> coordinates3d, double xL, double xR, double yL, double yR) throws Exception {
|
||||
return getRawValuesInTimeInstantAndBoundingBox(layerTitle, time, coordinates3d, xL, xR, yL, yR, false);
|
||||
}
|
||||
|
||||
private GISDataConnector currentconnector;
|
||||
private String layer;
|
||||
private String layerURL;
|
||||
|
||||
protected List<Double> getRawValuesInTimeInstantAndBoundingBox(String layerTitle, int time, List<Tuple<Double>> coordinates3d, double xL, double xR, double yL, double yR, boolean saveLayer) throws Exception {
|
||||
|
||||
// get the layer
|
||||
Metadata meta = gnInspector.getGNInfobyUUIDorName(layerTitle);
|
||||
Metadata meta = null;
|
||||
GISDataConnector connector = null;
|
||||
if (currentconnector != null && saveLayer)
|
||||
connector = currentconnector;
|
||||
else {
|
||||
if (isTable()) {
|
||||
AnalysisLogger.getLogger().debug("Extracting grid from table " + configuration.getParam(TableMatrixRepresentation.tableNameParameter));
|
||||
connector = new Table(configuration);
|
||||
currentconnector = connector;
|
||||
} else
|
||||
meta = gnInspector.getGNInfobyUUIDorName(layerTitle);
|
||||
}
|
||||
// if the layer is good
|
||||
if (meta != null) {
|
||||
String layer = gnInspector.getLayerName(meta);
|
||||
layer = gnInspector.getLayerName(meta);
|
||||
if (layer == null)
|
||||
layer = layerTitle;
|
||||
String layerURL = "";
|
||||
GISDataConnector connector = null;
|
||||
layerURL = "";
|
||||
if (gnInspector.isNetCDFFile(meta)) {
|
||||
Identification id = meta.getIdentificationInfo().iterator().next();
|
||||
String title = id.getCitation().getTitle().toString();
|
||||
AnalysisLogger.getLogger().debug("found a netCDF file with title " + title + " and layer name " + layer);
|
||||
layerURL = gnInspector.getOpenDapLink(meta);
|
||||
connector = new NetCDF();
|
||||
}
|
||||
else if (gnInspector.isAscFile(meta)){
|
||||
} else if (gnInspector.isAscFile(meta)) {
|
||||
AnalysisLogger.getLogger().debug("managing ASC File");
|
||||
layerURL = gnInspector.getHttpLink(meta);
|
||||
connector = new ASC();
|
||||
}
|
||||
else if (gnInspector.isWFS(meta)){
|
||||
} else if (gnInspector.isWFS(meta)) {
|
||||
AnalysisLogger.getLogger().debug("found a Geo Layer with reference " + layerTitle + " and layer name " + layer);
|
||||
layerURL = gnInspector.getGeoserverLink(meta);
|
||||
connector = new WFS();
|
||||
}
|
||||
currentconnector = connector;
|
||||
}
|
||||
if (connector!=null)
|
||||
//execute connector
|
||||
if (connector != null)
|
||||
return connector.getFeaturesInTimeInstantAndArea(layerURL, layer, time, coordinates3d, xL, xR, yL, yR);
|
||||
else
|
||||
throw new Exception("ERROR: Connector not found for layer "+layerTitle+" - Cannot Rasterize!");
|
||||
}
|
||||
else
|
||||
throw new Exception("ERROR: Metadata not found for layer "+layerTitle+" - Cannot Rasterize!");
|
||||
throw new Exception("ERROR: Connector not found for layer " + layerTitle + " - Cannot Rasterize!");
|
||||
|
||||
}
|
||||
|
||||
|
||||
boolean log = true;
|
||||
public double[] takeSignalInTime(String layerTitle, double x, double y, double z, double resolution) throws Exception {
|
||||
// HashMap<Integer, Double> signal = new HashMap<Integer, Double>();
|
||||
double[] signal = new double[maxSignalLength];
|
||||
int t = 0;
|
||||
log=false;
|
||||
while (true) {
|
||||
try {
|
||||
if (t%100==0)
|
||||
AnalysisLogger.getLogger().debug("Matrix Extractor-> Extracting Time Instant " + t);
|
||||
double[][] values = takeTimeInstantMatrix(layerTitle, t, x, x, y, y, z, resolution, resolution, true);
|
||||
// signal.put(t,values[0][0]);
|
||||
|
||||
signal[t]=values[0][0];
|
||||
|
||||
t++;
|
||||
if (t==maxSignalLength)
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
AnalysisLogger.getLogger().debug("Matrix Extractor-> No More Time Intervals!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// double[] dsignal = new double[signal.size()];
|
||||
double[] dsignal = new double[t];
|
||||
// int i = 0;
|
||||
// for (Double signald : signal.values()) {
|
||||
for (int i=0;i<t;i++){
|
||||
dsignal[i] = signal[i];
|
||||
i++;
|
||||
}
|
||||
log = true;
|
||||
return dsignal;
|
||||
}
|
||||
|
||||
public double[][] takeTimeInstantMatrix(String layerTitle, int timeInstant, double x1, double x2, double y1, double y2, double z, double xResolution, double yResolution) throws Exception {
|
||||
AnalysisLogger.getLogger().debug("Bounding box: (" + x1 + "," + x2 + ";" + y1 + "," + y2 + ")");
|
||||
return takeTimeInstantMatrix(layerTitle, timeInstant, x1, x2, y1, y2, z, xResolution, yResolution, false);
|
||||
}
|
||||
|
||||
public double[][] takeTimeInstantMatrix(String layerTitle, int timeInstant, double x1, double x2, double y1, double y2, double z, double xResolution, double yResolution, boolean savelayer) throws Exception {
|
||||
|
||||
boolean faolayer = false;
|
||||
if (layerTitle.toLowerCase().contains("MatrixExtractor->FAO aquatic species distribution map") )
|
||||
{
|
||||
if (layerTitle.toLowerCase().contains("MatrixExtractor->FAO aquatic species distribution map")) {
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->FAO DISTRIBUTION LAYER ... TO APPY PATCH!");
|
||||
faolayer=true;
|
||||
faolayer = true;
|
||||
}
|
||||
if ((x2 < x1) || (y2 < y1)) {
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->ERROR: BAD BOUNDING BOX!!!");
|
||||
return new double[0][0];
|
||||
}
|
||||
|
||||
int ysteps = (int) ((y2 - y1) / yResolution);
|
||||
int xsteps = (int) ((x2 - x1) / xResolution);
|
||||
|
||||
double[][] slice = new double[ysteps + 1][xsteps + 1];
|
||||
List<Tuple<Double>> tuples = new ArrayList<Tuple<Double>>();
|
||||
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->Building the points grid according to YRes:" + yResolution + " and XRes:" + xResolution);
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->Points to reassign:" + (ysteps*xsteps));
|
||||
if (log){
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->Building the points grid according to YRes:" + yResolution + " and XRes:" + xResolution);
|
||||
AnalysisLogger.getLogger().debug("MatrixExtractor->Points to reassign:" + (ysteps * xsteps));
|
||||
}
|
||||
|
||||
// build the tuples according to the desired resolution
|
||||
for (int i = 0; i < ysteps + 1; i++) {
|
||||
|
@ -120,76 +190,71 @@ public static List<Tuple<Double>> generateCoordinateTripletsInBoundingBox(double
|
|||
}
|
||||
}
|
||||
|
||||
AnalysisLogger.getLogger().debug("Taking " + ysteps + " values per "+xsteps+"="+(ysteps*xsteps)+ "...");
|
||||
|
||||
List<Double> timeValues = getRawValuesInTimeInstantAndBoundingBox(layerTitle, timeInstant, tuples, x1, x2, y1,y2);
|
||||
|
||||
AnalysisLogger.getLogger().debug("Taken " + timeValues.size() + " values");
|
||||
if (log)
|
||||
AnalysisLogger.getLogger().debug("Taking " + (ysteps + 1) + " values per " + (xsteps + 1) + "=" + (ysteps + 1 * xsteps + 1) + "...");
|
||||
|
||||
// adjust the BB in the case of one single point
|
||||
if (x2 == x1) {
|
||||
x2 = x2 + (xResolution / 2d);
|
||||
x1 = x1 - (xResolution / 2d);
|
||||
}
|
||||
|
||||
if (y2 == y1) {
|
||||
y2 = y2 + (yResolution / 2d);
|
||||
y1 = y1 - (yResolution / 2d);
|
||||
}
|
||||
if (log)
|
||||
AnalysisLogger.getLogger().debug("Bounding box: (" + x1 + "," + x2 + ";" + y1 + "," + y2 + ")");
|
||||
|
||||
// long t0=System.currentTimeMillis();
|
||||
List<Double> timeValues = getRawValuesInTimeInstantAndBoundingBox(layerTitle, timeInstant, tuples, x1, x2, y1, y2, savelayer);
|
||||
// AnalysisLogger.getLogger().debug("Elapsed:"+(System.currentTimeMillis()-t0));
|
||||
|
||||
if (log)
|
||||
AnalysisLogger.getLogger().debug("Taken " + timeValues.size() + " values");
|
||||
|
||||
|
||||
// build back the values matrix
|
||||
int k = 0;
|
||||
int g = 0;
|
||||
int ntriplets = timeValues.size();
|
||||
//cycle on all the triplets to recontruct the matrix
|
||||
// cycle on all the triplets to recontruct the matrix
|
||||
for (int t = 0; t < ntriplets; t++) {
|
||||
//take the corresponding (time,value) pair
|
||||
// take the corresponding (time,value) pair
|
||||
Double value = timeValues.get(t);
|
||||
//if there is value, then set it, otherwise set NaN
|
||||
//the layer is undefined in that point and a value must be generated
|
||||
//assign a value to the matrix
|
||||
|
||||
//WARNING: PATCH FOR FAO LAYERS:. Probability can be equal to 2 for uncertainty
|
||||
if (faolayer && (value>1)){
|
||||
// if there is value, then set it, otherwise set NaN
|
||||
// the layer is undefined in that point and a value must be generated
|
||||
// assign a value to the matrix
|
||||
|
||||
// WARNING: PATCH FOR FAO LAYERS:. Probability can be equal to 2 for uncertainty
|
||||
if (faolayer && (value > 1)) {
|
||||
AnalysisLogger.getLogger().debug("APPLYING FAO PATCH!");
|
||||
slice[k][g] = 0.5;
|
||||
}
|
||||
else
|
||||
} else
|
||||
slice[k][g] = value;
|
||||
//increase the x step according to the matrix
|
||||
// increase the x step according to the matrix
|
||||
if (g == xsteps) {
|
||||
g = 0;
|
||||
k++;
|
||||
}
|
||||
else
|
||||
} else
|
||||
g++;
|
||||
}
|
||||
|
||||
// applyNearestNeighbor();
|
||||
|
||||
//applyNearestNeighbor();
|
||||
|
||||
AnalysisLogger.getLogger().debug("Features map: "+slice.length+","+slice[0].length);
|
||||
if (log)
|
||||
AnalysisLogger.getLogger().debug("Features map: " + slice.length + "," + slice[0].length);
|
||||
return slice;
|
||||
}
|
||||
|
||||
public void applyNearestNeighbor(){
|
||||
|
||||
|
||||
public void applyNearestNeighbor() {
|
||||
|
||||
/*
|
||||
AnalysisLogger.getLogger().debug("Applying nearest Neighbor to all the rows");
|
||||
//apply nearest neighbor to each row
|
||||
AlgorithmConfiguration config = new AlgorithmConfiguration();
|
||||
config.setConfigPath(configDir);
|
||||
boolean rapidinit = false;
|
||||
|
||||
|
||||
for (int i=0;i<slice.length;i++){
|
||||
// AnalysisLogger.getLogger().debug("Checking for unfilled values");
|
||||
boolean tofill = false;
|
||||
for (int j=0;j<slice[i].length;j++) {
|
||||
if (new Double(slice[i][j]).equals(Double.NaN))
|
||||
tofill = true;
|
||||
}
|
||||
if (tofill){
|
||||
if (!rapidinit){
|
||||
config.initRapidMiner();
|
||||
rapidinit=true;
|
||||
}
|
||||
AnalysisLogger.getLogger().debug("Filling signal");
|
||||
double[] ssliced = SignalProcessing.fillSignal(slice[i]);
|
||||
slice[i] = ssliced;
|
||||
}
|
||||
// else
|
||||
// AnalysisLogger.getLogger().debug("Signal yet complete");
|
||||
}
|
||||
*/
|
||||
* AnalysisLogger.getLogger().debug("Applying nearest Neighbor to all the rows"); //apply nearest neighbor to each row AlgorithmConfiguration config = new AlgorithmConfiguration(); config.setConfigPath(configDir); boolean rapidinit = false;
|
||||
*
|
||||
*
|
||||
* for (int i=0;i<slice.length;i++){ // AnalysisLogger.getLogger().debug("Checking for unfilled values"); boolean tofill = false; for (int j=0;j<slice[i].length;j++) { if (new Double(slice[i][j]).equals(Double.NaN)) tofill = true; } if (tofill){ if (!rapidinit){ config.initRapidMiner(); rapidinit=true; } AnalysisLogger.getLogger().debug("Filling signal"); double[] ssliced = SignalProcessing.fillSignal(slice[i]); slice[i] = ssliced; } // else // AnalysisLogger.getLogger().debug("Signal yet complete"); }
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue