diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsCreator.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsCreator.java index 0ced728..667cb3a 100644 --- a/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsCreator.java +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsCreator.java @@ -1,48 +1,27 @@ 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.dataanalysis.geo.meta.PolyMapMetadata; 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 { +public abstract 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 databaseParameterName = "TimeSeriesDataStore"; static String dbuserParameterName = "user"; static String dbpasswordParameterName = "password"; - static String dburlParameterName = "FishBase"; + static String dburlParameterName = "STOREURL"; static String inputTableParameter = "InputTable"; static String outputTableParameter = "OutputTable"; @@ -51,42 +30,53 @@ public class MapsCreator extends StandardLocalExternalAlgorithm { static String csquareParameter = "csquaresDimension"; static String probabilityParameter = "Probability"; + static String infoParameter = "Info"; static String resolutionParameter = "Resolution"; static String layerNameParameter = "MapName"; - static int maxNPoints = 19000; + static int maxNPoints = 259000; 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 createProbTable = "create table %1$s (geomid serial, x real, y real, probability real);"; + private static String columnsProbNames = " geomid, x , y, probability, the_geom"; + + private static String createInfoTable = "create table %1$s (geomid serial, x real, y real, info character varying);"; + private static String columnsInfoNames = " geomid, x , y, info, the_geom"; + 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 + static String makeSquare = "ST_GeomFromText('POLYGON((%1$s ,%2$s, %3$s, %4$s, %1$s))',4326)"; + + //changeable parameters for application purposes + String datastore = ""; + String defaultStyle = ""; + String workspace = ""; + String username = ""; + String purpose = ""; + String credits = ""; + String keyword = ""; + + @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"); - - } + public abstract void init() throws Exception; @Override protected void process() throws Exception { - try { log("Beginning process"); + log("Using scope:"+config.getGcubeScope()); + //initialize Gis DB parameters 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); + //getting resolution + String res$ = config.getParam(resolutionParameter); + double resolution = res$!=null?Double.parseDouble(res$):0; + //connection to the GIS DB log("Connecting to gisDB..."); AlgorithmConfiguration gisconfig = new AlgorithmConfiguration(); gisconfig.setParam("DatabaseDriver", "org.postgresql.Driver"); @@ -94,87 +84,98 @@ public class MapsCreator extends StandardLocalExternalAlgorithm { 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!"); + + //select info to attach + String infoPar = config.getParam(probabilityParameter); + if (infoPar == null) + infoPar = config.getParam(infoParameter); // points retrieval List 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); + points = DatabaseFactory.executeSQLQuery("select " + config.getParam(xParameter) + "," + config.getParam(yParameter) + "," +infoPar + " from " + config.getParam(inputTableParameter) + " limit " + maxNPoints, smdbconnection); } + //points from csquares else if (config.getParam(csquareParameter)!=null){ log("..from csquares"); - List csquares= DatabaseFactory.executeSQLQuery("select " + config.getParam(csquareParameter) +" from " + config.getParam(inputTableParameter) + " limit " + maxNPoints, smdbconnection); + String queryCsquare = "select " + config.getParam(csquareParameter) + "," + infoPar +" from " + config.getParam(inputTableParameter) + " limit " + maxNPoints; + List csquares= DatabaseFactory.executeSQLQuery(queryCsquare, smdbconnection); points = new ArrayList(); //build points from csquares for (Object csquare:csquares){ - CSquare c = it.cnr.aquamaps.CSquare.apply(""+csquare); + Object[] csquareandprob = (Object[]) csquare; + CSquare c = it.cnr.aquamaps.CSquare.apply(""+csquareandprob[0]); + if (resolution==0) + resolution=c.size(); // x,y Iterator iterator = c.center().valuesIterator(); - double x = Double.parseDouble(""+iterator.next()); - double y = Double.parseDouble(""+iterator.next()); - Object[] pair = {x,y}; + String x = ""+iterator.next(); + String y = ""+iterator.next(); + String prob = ""+csquareandprob[1]; + Object[] pair = {x,y,prob}; points.add(pair); } log("Points built from csquares!"); } - + //GIS Table creation log("Creating GIS table"); String gisTableName = "stat" + UUID.randomUUID().toString().replace("-", ""); - // create a table on the gis db - String createTable$ = String.format(createTable, gisTableName); + + String createTable$ = String.format(createProbTable, gisTableName); + String columnNames$ = columnsProbNames; + if (config.getParam(probabilityParameter)==null){ + createTable$ = String.format(createInfoTable, gisTableName); + columnNames$=columnsInfoNames; + } log(createTable$); + //drop previous table try { DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(gisTableName), gisdbconnection); } catch (Exception e) { log("Impossible to drop table:" + e.getLocalizedMessage()); } - DatabaseFactory.executeSQLUpdate(String.format(createTable, gisTableName), gisdbconnection); + //table creation + DatabaseFactory.executeSQLUpdate(createTable$, gisdbconnection); DatabaseFactory.executeSQLQuery(String.format(addGeometryColumn, gisTableName), gisdbconnection); log("Fulfilling elements"); + log("Resolution:" + resolution); + //points fulfilling List values = new ArrayList(); int i = 0; - List squares = new ArrayList(); 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); + double x1 = x - resolution; + double x2 = x + resolution; + double y1 = y - resolution; + double y2 = 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 }; +// System.out.println(square); + String[] selements = { "" + i, "" + x, "" + y, probS,square }; 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); + insertGeoChunksIntoTable(gisTableName, columnNames$, values, 5000, gisdbconnection); log("Publishing Table"); - String username = config.getParam("ServiceUserName"); - if (username == null) - username = "statistical.manager"; - + String usernameP = config.getParam("ServiceUserName"); + if (usernameP != null) + username = usernameP; + String layerName = config.getParam(layerNameParameter); - PublishResponse response = publishTable(config.getGcubeScope(), gisTableName, resolution, username, layerName); + PublishResponse response = PolyMapMetadata.publishTable(config.getGcubeScope(), gisTableName, resolution, username, layerName, defaultStyle, workspace, datastore, purpose, credits, keyword); + //analyzing response if (response == null) { log("Error in generating map - dropping gis table"); try { @@ -185,13 +186,18 @@ public class MapsCreator extends StandardLocalExternalAlgorithm { } throw new Exception("Impossible to publish on GeoNetwork or GeoServer: " + gisTableName); } else { + //writing output addOutputString("GIS map title", layerName); addOutputString("GIS map UUID", "" + response.getPublishedMetadata().getFileIdentifier()); addOutputString("Associated Geospatial Table", gisTableName); addOutputString("Generated by ", username); addOutputString("Resolution", "" + resolution); + addOutputString("Style", "" + defaultStyle); + addOutputString("Keyword", "" + keyword); } + log("Output:"+outputParameters); log("All Done!"); + } catch (Exception e) { e.printStackTrace(); throw e; @@ -204,132 +210,53 @@ public class MapsCreator extends StandardLocalExternalAlgorithm { } - 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; + public static void insertGeoChunksIntoTable(String table, String columnsNames, List values, int chunkSize,SessionFactory dbconnection) throws Exception{ + int valuesize = values.size(); + StringBuffer sb = new StringBuffer(); + int stopIndex =0; + for (int i=0;i0 && stopIndex%chunkSize==0){ + DatabaseFactory.executeSQLUpdate(DatabaseUtils.insertFromBuffer(table, columnsNames, sb), dbconnection); + stopIndex=chunkSize; + sb = new StringBuffer(); + } + else if (i0){ + try{ + DatabaseFactory.executeSQLUpdate(DatabaseUtils.insertFromBuffer(table, columnsNames, sb), dbconnection); + }catch(Exception e){ + System.out.println("Query:"+sb); + throw e; + } + + } + } + } - - 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() { - + log("shutdown invoked!"); } @Override - protected void setInputParameters() { - try { - List templates = new ArrayList(); - 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; - } + protected abstract void setInputParameters(); + + } diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/PointsMapsCreator.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/PointsMapsCreator.java new file mode 100644 index 0000000..040ec19 --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/PointsMapsCreator.java @@ -0,0 +1,76 @@ +package org.gcube.dataanalysis.geo.algorithms; + +import java.util.ArrayList; +import java.util.List; + +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.enumtypes.TableTemplates; + +public class PointsMapsCreator extends MapsCreator { + + @Override + public void init() throws Exception { + log("MAPS_CREATOR"); + datastore = "timeseriesws"; + defaultStyle = "point"; + workspace = "aquamaps"; + username = "statistical.manager"; + purpose = "To Publish Geometric Layers for Points Maps"; + credits = "Generated via the Statistical Manager Service"; + keyword = "Points Map"; + } + + @Override + protected void setInputParameters() { + try { + List templates = new ArrayList(); + 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 customDimension = new ColumnType(inputTableParameter, infoParameter, "The column containing information you want to attach to each point", "", false); + + inputs.add(tinput); + inputs.add(xColumn); + inputs.add(yColumn); + inputs.add(customDimension); + DatabaseType.addDefaultDBPars(inputs); + + } catch (Throwable e) { + e.printStackTrace(); + } + + } + + 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"); + config.setParam(dbuserParameterName, "postgres"); + config.setParam(dbpasswordParameterName, "d4science2"); + + config.setParam(inputTableParameter, "occurrence_species_id_fb60ce42_1704_43f9_91a3_2df7d82b1b96"); + config.setParam(xParameter, "decimallongitude"); + config.setParam(yParameter, "decimallatitude"); + config.setParam(infoParameter, "scientificname"); + config.setParam("ServiceUserName", "gianpaolo.coro"); + config.setParam(layerNameParameter, "Generic Species"); + + MapsCreator maps = new PointsMapsCreator(); + maps.setConfiguration(config); + maps.init(); + maps.compute(); + + } +} diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/PolygonMapsCreator.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/PolygonMapsCreator.java new file mode 100644 index 0000000..f99fb08 --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/PolygonMapsCreator.java @@ -0,0 +1,78 @@ +package org.gcube.dataanalysis.geo.algorithms; + +import java.util.ArrayList; +import java.util.List; + +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.enumtypes.TableTemplates; + +public class PolygonMapsCreator extends MapsCreator { + + @Override + public void init() throws Exception { + log("MAPS_CREATOR"); + datastore = "timeseriesws"; + defaultStyle = "polygon"; + workspace = "aquamaps"; + username = "statistical.manager"; + purpose = "To Publish Geometric Layers for Polygonal Maps"; + credits = "Generated via the Statistical Manager Service"; + keyword = "Polygonal Map"; + } + + @Override + protected void setInputParameters() { + try { + List templates = new ArrayList(); + 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 customDimension = new ColumnType(inputTableParameter, infoParameter, "The column containing information you want to attach to each polygon", "", false); + + inputs.add(tinput); + inputs.add(xColumn); + inputs.add(yColumn); + inputs.add(customDimension); + addDoubleInput(resolutionParameter, "The map resolution in degrees", "0.5"); + DatabaseType.addDefaultDBPars(inputs); + + } catch (Throwable e) { + e.printStackTrace(); + } + + } + + 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"); + 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(infoParameter, "faoaream"); + config.setParam(resolutionParameter, "0.5"); + config.setParam("ServiceUserName", "gianpaolo.coro"); + config.setParam(layerNameParameter, "Generic Species"); + + MapsCreator maps = new PolygonMapsCreator(); + maps.setConfiguration(config); + maps.init(); + maps.compute(); + + } +} diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromCsquares.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromCsquares.java new file mode 100644 index 0000000..48dd2ee --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromCsquares.java @@ -0,0 +1,74 @@ +package org.gcube.dataanalysis.geo.algorithms; + +import java.util.ArrayList; +import java.util.List; + +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.enumtypes.TableTemplates; + +public class SpeciesDistributionsMapsCreatorFromCsquares extends MapsCreator { + + @Override + public void init() throws Exception { + log("MAPS_CREATOR"); + datastore = "timeseriesws"; + defaultStyle = "Species_prob"; + workspace = "aquamaps"; + username = "statistical.manager"; + purpose = "To Publish Geometric Layers for Species Distribution Maps"; + credits = "Generated via the Statistical Manager Service"; + keyword = "Species Probability Distribution"; + } + + @Override + protected void setInputParameters() { + try { + List templates = new ArrayList(); + 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, csquareParameter, "The column containing csquare codes", "", false); + ColumnType probabilityDimension = new ColumnType(inputTableParameter, probabilityParameter, "The column containing probability information", "", false); + + inputs.add(tinput); + inputs.add(xColumn); + inputs.add(probabilityDimension); + DatabaseType.addDefaultDBPars(inputs); + + } catch (Throwable e) { + e.printStackTrace(); + } + + } + + 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"); + config.setParam(dbuserParameterName, "postgres"); + config.setParam(dbpasswordParameterName, "d4science2"); + + config.setParam(inputTableParameter, "hspec_id_4cd0644e_46c5_4b33_b8fa_85b0c6b01982"); + config.setParam(csquareParameter, "csquarecode"); + config.setParam(probabilityParameter, "probability"); + + config.setParam("ServiceUserName", "gianpaolo.coro"); + config.setParam(layerNameParameter, "Generic Species"); + + MapsCreator maps = new SpeciesDistributionsMapsCreatorFromCsquares(); + maps.setConfiguration(config); + maps.init(); + maps.compute(); + + } +} diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromPoints.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromPoints.java new file mode 100644 index 0000000..91c7b30 --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/SpeciesDistributionsMapsCreatorFromPoints.java @@ -0,0 +1,78 @@ +package org.gcube.dataanalysis.geo.algorithms; + +import java.util.ArrayList; +import java.util.List; + +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.enumtypes.TableTemplates; + +public class SpeciesDistributionsMapsCreatorFromPoints extends MapsCreator { + + @Override + public void init() throws Exception { + log("MAPS_CREATOR"); + datastore = "timeseriesws"; + defaultStyle = "Species_prob"; + workspace = "aquamaps"; + username = "statistical.manager"; + purpose = "To Publish Geometric Layers for Species Distribution Maps"; + credits = "Generated via the Statistical Manager Service"; + keyword = "Species Probability Distribution"; + } + + @Override + protected void setInputParameters() { + try { + List templates = new ArrayList(); + 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(); + } + + } + + 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"); + 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 SpeciesDistributionsMapsCreatorFromPoints(); + maps.setConfiguration(config); + maps.init(); + maps.compute(); + + } +} diff --git a/src/main/java/org/gcube/dataanalysis/geo/meta/PolyMapMetadata.java b/src/main/java/org/gcube/dataanalysis/geo/meta/PolyMapMetadata.java new file mode 100644 index 0000000..b1e013e --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/meta/PolyMapMetadata.java @@ -0,0 +1,88 @@ +package org.gcube.dataanalysis.geo.meta; + +import it.geosolutions.geonetwork.util.GNInsertConfiguration; +import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; +import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; + +import java.util.Date; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; +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.opengis.metadata.citation.PresentationForm; +import org.opengis.metadata.identification.TopicCategory; +import org.opengis.metadata.spatial.GeometricObjectType; +import org.opengis.metadata.spatial.TopologyLevel; + +public class PolyMapMetadata { + + 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\"]]"; + + public static PublishResponse publishTable(String scope, String tableName, double resolution, String username, String layerName, String defaultStyle, String workspace, String datastore, String purpose, String credits, String keyword) throws Exception { + + 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, tableName,purpose, credits, keyword); + GISInterface gis = GISInterface.get(); + Configuration gnConfig = gis.getGeoNetworkReader().getConfiguration(); + AnalysisLogger.getLogger().debug("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); + AnalysisLogger.getLogger().debug(resp); + AnalysisLogger.getLogger().debug("ID:" + resp.getReturnedMetaId()); + AnalysisLogger.getLogger().debug("Result:" + resp.getMetaOperationResult()); + if (resp.getReturnedMetaId()==0) + return null; + else + return resp; + } + + + private static GcubeISOMetadata fillMeta(double resolution, String username, String title, String scope, String tableName ,String purpose, String credits, String keyword) throws Exception { + + if (scope == null) + scope = ScopeProvider.instance.get(); + + ScopeProvider.instance.set(scope); + + GcubeISOMetadata meta = new GcubeISOMetadata(); + meta.setAbstractField("This metadata has been automatically generated from the Statistical Manager on the basis of a distribution of points and according the resolution of "+resolution+" degrees."); + meta.setCreationDate(new Date(System.currentTimeMillis())); + meta.setExtent((DefaultExtent) DefaultExtent.WORLD); + meta.setGeometricObjectType(GeometricObjectType.SURFACE); + meta.setPresentationForm(PresentationForm.MAP_DIGITAL); + meta.setPurpose(purpose); + meta.setResolution(resolution); + if (title == null || title.length() == 0) + meta.setTitle("Distribution"); + else + meta.setTitle(title); + + meta.setTopologyLevel(TopologyLevel.GEOMETRY_ONLY); + meta.setUser(username); + + + meta.addGraphicOverview("http://www.d4science.org/D4ScienceOrg-Social-theme/images/custom/D4ScienceInfrastructure.png"); + meta.addCredits(credits); + Thesaurus generalThesaurus = meta.getConfig().getThesauri().get("General"); + meta.addKeyword(title, generalThesaurus); + meta.addKeyword(username, generalThesaurus); + meta.addKeyword("StatisticalManager", generalThesaurus); + meta.addKeyword(keyword, generalThesaurus); + meta.addKeyword(tableName,generalThesaurus); + meta.addTopicCategory(TopicCategory.BIOTA); + return meta; + } + +}