Gianpaolo Coro 2013-05-21 09:51:13 +00:00
parent b46423e1d6
commit 57f39dcd64
1 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,101 @@
package org.gcube.dataanalysis.geo.insertion;
import java.util.List;
import java.util.UUID;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils;
import org.gcube.dataanalysis.ecoengine.utils.Tuple;
import org.gcube.dataanalysis.geo.retrieval.GeoIntersector;
import org.gcube.dataanalysis.geo.utils.CSquareCodesConverter;
import org.gcube.dataanalysis.geo.utils.EnvDataExplorer;
import org.hibernate.SessionFactory;
/**
* transforms a raster map into a table
* @author coro
*
*/
public class RasterTable {
private double resolution;
private double valuesMatrix[][];
double x1;
double x2;
double y1;
double y2;
double z;
double xResolution;
double yResolution;
private AlgorithmConfiguration configuration;
private String tablename = ""+UUID.randomUUID();
static String createTableStatement = "CREATE TABLE %1$s (csquarecode character varying, x real, y real, z real, probability real)";
static String columnsnames = "csquarecode, x , y , z , probability";
public String getTablename() {
return tablename;
}
public void setTablename(String tablename) {
this.tablename = tablename;
}
public RasterTable(double x1, double x2, double y1, double y2, double z, double xResolution, double yResolution, double[][] values, AlgorithmConfiguration configuration){
this.valuesMatrix=values;
this.configuration=configuration;
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.z=z;
this.xResolution=xResolution;
this.yResolution=yResolution;
}
public void dumpGeoTable(){
//open the connection to the db
SessionFactory dbconnection = DatabaseUtils.initDBSession(configuration);
try{
//create a table
DatabaseFactory.executeSQLUpdate(String.format(createTableStatement, tablename), dbconnection);
List<Tuple<Double>> coordinates = GeoIntersector.generateCoordinateTriplets(x1, x2, y1, y2, z, xResolution, yResolution);
List<Double> values = GeoIntersector.associateValueToCoordinates(coordinates, valuesMatrix);
//for each element in the matrix, build the corresponding csquare code
int triplets = coordinates.size();
StringBuffer sb = new StringBuffer();
for (int i=0;i<triplets;i++){
//save the string in a buffer
Tuple<Double> cset = coordinates.get(i);
double x = cset.getElements().get(0);
double y = cset.getElements().get(1);
String csquare = CSquareCodesConverter.convertAtResolution(x, y, xResolution);
double value = values.get(i);
sb.append("("+csquare+","+x+","+y+","+z+","+value+")");
if (i<triplets-1)
sb.append(",");
}
//save all the strings on the table
DatabaseUtils.insertFromBuffer(tablename, columnsnames, sb);
}catch(Exception e){
e.printStackTrace();
AnalysisLogger.getLogger().debug("Error in dumping table: "+e.getLocalizedMessage());
}
finally{
//close the connection
DatabaseUtils.closeDBConnection(dbconnection);
}
}
}