Maps Creator

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineGeoSpatialExtension@85565 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Gianpaolo Coro 2013-11-15 18:11:40 +00:00
parent 2caff6c745
commit 354de31967
1 changed files with 335 additions and 0 deletions

View File

@ -0,0 +1,335 @@
package org.gcube.dataanalysis.geo.algorithms;
import it.cnr.aquamaps.CSquare;
import it.geosolutions.geonetwork.util.GNInsertConfiguration;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.datatypes.ColumnType;
import org.gcube.dataanalysis.ecoengine.datatypes.DatabaseType;
import org.gcube.dataanalysis.ecoengine.datatypes.InputTable;
import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType;
import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.TableTemplates;
import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalExternalAlgorithm;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
import org.gcube.spatial.data.geonetwork.iso.GcubeISOMetadata;
import org.gcube.spatial.data.geonetwork.iso.Thesaurus;
import org.gcube.spatial.data.gis.GISInterface;
import org.gcube.spatial.data.gis.model.report.PublishResponse;
import org.geotoolkit.metadata.iso.extent.DefaultExtent;
import org.hibernate.SessionFactory;
import org.opengis.metadata.citation.PresentationForm;
import org.opengis.metadata.identification.TopicCategory;
import org.opengis.metadata.spatial.GeometricObjectType;
import org.opengis.metadata.spatial.TopologyLevel;
import scala.collection.Iterator;
public class MapsCreator extends StandardLocalExternalAlgorithm {
private static final String crs = "GEOGCS[\"WGS 84\", DATUM[\"World Geodetic System 1984\", SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]," + "AUTHORITY[\"EPSG\",\"6326\"]], PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], UNIT[\"degree\", 0.017453292519943295]," + "AXIS[\"Geodetic longitude\", EAST], AXIS[\"Geodetic latitude\", NORTH], AUTHORITY[\"EPSG\",\"4326\"]]";
static String databaseParameterName = "FishBase";
static String dbuserParameterName = "user";
static String dbpasswordParameterName = "password";
static String dburlParameterName = "FishBase";
static String inputTableParameter = "InputTable";
static String outputTableParameter = "OutputTable";
static String xParameter = "xDimension";
static String yParameter = "yDimension";
static String csquareParameter = "csquaresDimension";
static String probabilityParameter = "Probability";
static String resolutionParameter = "Resolution";
static String layerNameParameter = "MapName";
static int maxNPoints = 19000;
SessionFactory gisdbconnection = null;
SessionFactory smdbconnection = null;
private static String createTable = "create table %1$s (geomid serial, x real, y real, probability real);";
private static String columnsNames = " geomid, x , y, probability";
private static String addGeometryColumn = "Select AddGeometryColumn('%1$s','the_geom',4326,'POLYGON',2);";
static String makeSquare = "ST_SetSRID(ST_MakePolygon(ST_GeomFromText('LINESTRING(%1$s ,%2$s, %3$s, %4$s, %1$s)')),4326)";
@Override
public String getDescription() {
return "A transducer algorithm to produce a GIS map either from a probability distribution or from a set of points. A maximum of " + maxNPoints + " is allowed";
}
@Override
public void init() throws Exception {
log("MAPS_CREATOR");
}
@Override
protected void process() throws Exception {
try {
log("Beginning process");
String databaseJdbc = getInputParameter(dburlParameterName);
String databaseUser = getInputParameter(dbuserParameterName);
String databasePwd = getInputParameter(dbpasswordParameterName);
log("GIS Database Parameters to use: " + databaseJdbc + " , " + databaseUser);
double resolution = Double.parseDouble(config.getParam(resolutionParameter));
log("Resolution:" + resolution);
log("Connecting to gisDB...");
AlgorithmConfiguration gisconfig = new AlgorithmConfiguration();
gisconfig.setParam("DatabaseDriver", "org.postgresql.Driver");
gisconfig.setParam("DatabaseURL", "jdbc:postgresql://geoserver-test.d4science-ii.research-infrastructures.eu/timeseriesgisdb");
gisconfig.setParam("DatabaseUserName", databaseUser);
gisconfig.setParam("DatabasePassword", databasePwd);
gisconfig.setConfigPath(config.getConfigPath());
gisdbconnection = DatabaseUtils.initDBSession(gisconfig);
// connect to the GIS DB
log("Initialized gisDBConnection!");
// connect to the SM DB
smdbconnection = DatabaseUtils.initDBSession(config);
log("Initialized SMDBConnection!");
// points retrieval
List<Object> points = null;
log("Retrieving points..");
if (config.getParam(xParameter) != null && config.getParam(yParameter) != null) {
log("..from coordinates");
// select the points from the SM DB up to a maximum of 190000 points
points = DatabaseFactory.executeSQLQuery("select " + config.getParam(xParameter) + "," + config.getParam(yParameter) + "," + config.getParam(probabilityParameter) + " from " + config.getParam(inputTableParameter) + " limit " + maxNPoints, smdbconnection);
}
else if (config.getParam(csquareParameter)!=null){
log("..from csquares");
List<Object> csquares= DatabaseFactory.executeSQLQuery("select " + config.getParam(csquareParameter) +" from " + config.getParam(inputTableParameter) + " limit " + maxNPoints, smdbconnection);
points = new ArrayList<Object>();
//build points from csquares
for (Object csquare:csquares){
CSquare c = it.cnr.aquamaps.CSquare.apply(""+csquare);
// x,y
Iterator<Object> iterator = c.center().valuesIterator();
double x = Double.parseDouble(""+iterator.next());
double y = Double.parseDouble(""+iterator.next());
Object[] pair = {x,y};
points.add(pair);
}
log("Points built from csquares!");
}
log("Creating GIS table");
String gisTableName = "stat" + UUID.randomUUID().toString().replace("-", "");
// create a table on the gis db
String createTable$ = String.format(createTable, gisTableName);
log(createTable$);
try {
DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(gisTableName), gisdbconnection);
} catch (Exception e) {
log("Impossible to drop table:" + e.getLocalizedMessage());
}
DatabaseFactory.executeSQLUpdate(String.format(createTable, gisTableName), gisdbconnection);
DatabaseFactory.executeSQLQuery(String.format(addGeometryColumn, gisTableName), gisdbconnection);
log("Fulfilling elements");
List<String[]> values = new ArrayList<String[]>();
int i = 0;
List<Object> squares = new ArrayList<Object>();
for (Object point : points) {
Object[] elements = (Object[]) point;
double x = Double.parseDouble("" + elements[0]);
double y = Double.parseDouble("" + elements[1]);
String probS = "" + elements[2];
double x1 = Math.max(-180, x - resolution);
double x2 = Math.min(180, x + resolution);
double y1 = Math.max(-90, y - resolution);
double y2 = Math.max(180, y + resolution);
String square = String.format(makeSquare, "" + x1 + " " + y1, x1 + " " + y2, x2 + " " + y2, x2 + " " + y1);
Object[] pair = new Object[2];
pair[0] = "" + i;
pair[1] = square;
squares.add(pair);
// insert into stat34cbc4ce921a410fbf4bac0d9c21fcf5 ( x , y, probability, the_geom) values ('0.25','0.25','34.0', ST_SetSRID(ST_MakePolygon(ST_GeomFromText('LINESTRING(-0.25 -0.25 ,-0.25 180.0, 0.75 180.0, 0.75 -0.25, -0.25 -0.25)')),4326))
String[] selements = { "" + i, "" + x, "" + y, probS };
values.add(selements);
i++;
}
log("Writing chunks");
// write chunks into the DB
DatabaseUtils.insertChunksIntoTable(gisTableName, columnsNames, values, 5000, gisdbconnection);
log("Writing Geometries");
DatabaseFactory.executeSQLUpdate(DatabaseUtils.insertIntoColumn(gisTableName, "geomid", "the_geom", squares), gisdbconnection);
log("Publishing Table");
String username = config.getParam("ServiceUserName");
if (username == null)
username = "statistical.manager";
String layerName = config.getParam(layerNameParameter);
PublishResponse response = publishTable(config.getGcubeScope(), gisTableName, resolution, username, layerName);
if (response == null) {
log("Error in generating map - dropping gis table");
try {
DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(gisTableName), gisdbconnection);
log("gis table dropped");
} catch (Exception e) {
log("Impossible to drop table:" + e.getLocalizedMessage());
}
throw new Exception("Impossible to publish on GeoNetwork or GeoServer: " + gisTableName);
} else {
addOutputString("GIS map title", layerName);
addOutputString("GIS map UUID", "" + response.getPublishedMetadata().getFileIdentifier());
addOutputString("Associated Geospatial Table", gisTableName);
addOutputString("Generated by ", username);
addOutputString("Resolution", "" + resolution);
}
log("All Done!");
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (smdbconnection != null)
DatabaseUtils.closeDBConnection(smdbconnection);
if (gisdbconnection != null)
DatabaseUtils.closeDBConnection(gisdbconnection);
}
}
public PublishResponse publishTable(String scope, String tableName, double resolution, String username, String layerName) throws Exception {
String datastore = "timeseriesws";
String defaultStyle = "Species_prob";
String workspace = "aquamaps";
GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder();
fte.setEnabled(true);
fte.setLatLonBoundingBox(-180.0, -90.0, 180.0, 90.0, crs);
fte.setName(tableName);
fte.setNativeCRS(crs);
GSLayerEncoder le = new GSLayerEncoder();
le.setDefaultStyle(defaultStyle);
le.setEnabled(true);
GcubeISOMetadata meta = fillMeta(resolution, username, layerName, scope);
GISInterface gis = GISInterface.get();
Configuration gnConfig = gis.getGeoNetworkReader().getConfiguration();
log("Using the following GNetwork:" + gnConfig.getGeoNetworkEndpoint());
PublishResponse resp = gis.publishDBTable(workspace, datastore, fte, le, meta.getMetadata(), new GNInsertConfiguration(gnConfig.getScopeGroup() + "", "datasets", "_none_", true), LoginLevel.DEFAULT);
log(resp);
log("ID:" + resp.getReturnedMetaId());
log("Result:" + resp.getMetaOperationResult());
if (resp.getReturnedMetaId()==0)
return null;
else
return resp;
}
private GcubeISOMetadata fillMeta(double resolution, String username, String title, String scope) throws Exception {
if (scope == null)
scope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
GcubeISOMetadata meta = new GcubeISOMetadata();
meta.setAbstractField("This metadata is just a test");
meta.setCreationDate(new Date(System.currentTimeMillis()));
meta.setExtent((DefaultExtent) DefaultExtent.WORLD);
meta.setGeometricObjectType(GeometricObjectType.SURFACE);
meta.setPresentationForm(PresentationForm.MAP_DIGITAL);
meta.setPurpose("Publish Geometric Layer");
meta.setResolution(resolution);
if (title == null || title.length() == 0)
meta.setTitle("Probability Distribution");
else
meta.setTitle(title);
meta.setTopologyLevel(TopologyLevel.GEOMETRY_ONLY);
meta.setUser(username);
meta.addCredits("Generated via the Statistical Manager Service");
meta.addGraphicOverview("http://www.d4science.org/D4ScienceOrg-Social-theme/images/custom/D4ScienceInfrastructure.png");
Thesaurus generalThesaurus = meta.getConfig().getThesauri().get("General");
meta.addKeyword(title, generalThesaurus);
meta.addKeyword(username, generalThesaurus);
meta.addKeyword("StatisticalManager", generalThesaurus);
meta.addKeyword("Probability Distribution", generalThesaurus);
meta.addTopicCategory(TopicCategory.BIOTA);
return meta;
}
public static void main(String[] args) throws Exception {
AlgorithmConfiguration config = new AlgorithmConfiguration();
config.setConfigPath("./cfg/");
config.setPersistencePath("./cfg/");
config.setGcubeScope("/gcube");
config.setParam("DatabaseUserName", "utente");
config.setParam("DatabasePassword", "d4science");
config.setParam("DatabaseURL", "jdbc:postgresql://statistical-manager.d.d4science.research-infrastructures.eu/testdb");
config.setParam(dburlParameterName, "jdbc:postgresql://geoserver-test.d4science-ii.research-infrastructures.eu/timeseriesgisdb");
// jdbc:postgresql://statistical-manager.d.d4science.research-infrastructures.eu/testdb, DatabasePassword=d4science, DatabaseUserName=utente, password=d4science2
// config.setParam(dburlParameterName,"jdbc:postgresql://146.48.122.125/timeseriesgisdb");
config.setParam(dbuserParameterName, "postgres");
config.setParam(dbpasswordParameterName, "d4science2");
config.setParam(inputTableParameter, "occcluster_id_59005678_4863_49ba_9c66_ebac80829da3");
config.setParam(xParameter, "centerlong");
config.setParam(yParameter, "centerlat");
config.setParam(probabilityParameter, "faoaream");
config.setParam(resolutionParameter, "0.5");
config.setParam("ServiceUserName", "gianpaolo.coro");
config.setParam(layerNameParameter, "Generic Species");
MapsCreator maps = new MapsCreator();
maps.setConfiguration(config);
maps.compute();
}
@Override
public void shutdown() {
}
@Override
protected void setInputParameters() {
try {
List<TableTemplates> templates = new ArrayList<TableTemplates>();
addRemoteDatabaseInput(databaseParameterName, dburlParameterName, dbuserParameterName, dbpasswordParameterName, "driver", "dialect");
templates.add(TableTemplates.GENERIC);
addStringInput(layerNameParameter, "The name of the layer to produce", "Statistical Prob. Distribution");
InputTable tinput = new InputTable(templates, inputTableParameter, "The table information to geo-spatialize");
ColumnType xColumn = new ColumnType(inputTableParameter, xParameter, "The column containing longitude information", "", false);
ColumnType yColumn = new ColumnType(inputTableParameter, yParameter, "The column containing latitude information", "", false);
ColumnType probabilityDimension = new ColumnType(inputTableParameter, probabilityParameter, "The column containing probability information", "", false);
inputs.add(tinput);
inputs.add(xColumn);
inputs.add(yColumn);
inputs.add(probabilityDimension);
addDoubleInput(resolutionParameter, "The map resolution in degrees", "0.5");
DatabaseType.addDefaultDBPars(inputs);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public StatisticalType getOutput() {
return null;
}
}