From 43b33c0788de696eab90ce55429356adf011d2cd Mon Sep 17 00:00:00 2001 From: Gianpaolo Coro Date: Wed, 29 May 2013 15:08:58 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineGeoSpatialExtension@76474 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../geo/algorithms/MapsComparator.java | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsComparator.java diff --git a/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsComparator.java b/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsComparator.java new file mode 100644 index 0000000..e4e63dd --- /dev/null +++ b/src/main/java/org/gcube/dataanalysis/geo/algorithms/MapsComparator.java @@ -0,0 +1,170 @@ +package org.gcube.dataanalysis.geo.algorithms; + +import java.util.Map; + +import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; +import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory; +import org.gcube.dataanalysis.ecoengine.datatypes.DatabaseType; +import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType; +import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType; +import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes; +import org.gcube.dataanalysis.ecoengine.evaluation.DiscrepancyAnalysis; +import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalExternalAlgorithm; +import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils; +import org.gcube.dataanalysis.geo.insertion.RasterTable; +import org.gcube.dataanalysis.geo.meta.features.FeaturesManager; +import org.gcube.dataanalysis.geo.retrieval.GeoIntersector; +import org.hibernate.SessionFactory; +import org.opengis.metadata.Metadata; + +public class MapsComparator extends StandardLocalExternalAlgorithm{ + + static String layer1 = "LayerTitle_1"; + static String layer2 = "LayerTitle_2"; + static String zString = "Z"; + static String valuesThr = "ValuesComparisonThreshold"; + + protected SessionFactory dbConnection; + + @Override + public void init() throws Exception { + AnalysisLogger.getLogger().debug("Initialization"); + + } + + @Override + public String getDescription() { + return "An algorithm for comparing two OGC/NetCDF maps in seamless way to the user"; + } + + @Override + protected void process() throws Exception { + status = 0; + long t0 = System.currentTimeMillis(); + String layerT1 = getInputParameter(layer1); + String layerT2 = getInputParameter(layer2); + String z$ = getInputParameter(zString); + String valuesthr$ = getInputParameter(valuesThr); + double valuesthreshold = 0.1; + if ((valuesthr$!=null)&&(valuesthr$.trim().length()>0)) + try{valuesthreshold =Double.parseDouble(valuesthr$);}catch(Exception ee){} + + double z = 0; + if ((z$!=null)&&(z$.trim().length()>0)) + try{z =Double.parseDouble(z$);}catch(Exception ee){} + try{ + + String scope = config.getGcubeScope(); + AnalysisLogger.getLogger().debug("MapsComparator: Using Scope: "+scope+" Z: "+z+" Values Threshold: "+valuesthreshold+" Layer1: "+layerT1+" vs "+layerT2); + GeoIntersector intersector = new GeoIntersector(scope, config.getConfigPath()); + AnalysisLogger.getLogger().debug("MapsComparator: GeoIntersector initialized"); + int t = 0; + double x1 = -180; + double x2 = 180; + double y1 = -90; + double y2 = 90; + status = 10; + FeaturesManager fm = intersector.getFeaturer(); + AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: "+layerT1); + Metadata meta1 = fm.checkForMetadatabyTitle(FeaturesManager.treatTitleForGN(layerT1), layerT1); + double resolution1 = FeaturesManager.getResolution(meta1); + AnalysisLogger.getLogger().debug("MapsComparator: Resolution: "+resolution1); + status = 15; + AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: "+layerT2); + Metadata meta2 = fm.checkForMetadatabyTitle(FeaturesManager.treatTitleForGN(layerT2), layerT2); + double resolution2 = FeaturesManager.getResolution(meta2); + AnalysisLogger.getLogger().debug("MapsComparator: Resolution: "+resolution2); + status = 20; + //take the lowest resolution to perform the comparison + double resolution = Math.max(resolution1, resolution2); + if (resolution==0) + resolution = 0.5d; + AnalysisLogger.getLogger().debug("MapsComparator: Evaluation Resolution: "+resolution); + + AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 1****"); + double[][] slice1 = intersector.takeTimeSlice(layerT1, t, x1, x2, y1,y2,z,resolution,resolution); + AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 1"); + status = 30; + RasterTable raster1 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice1, config); + raster1.dumpGeoTable(); + String rastertable1 = raster1.getTablename(); + AnalysisLogger.getLogger().debug("MapsComparator: Map 1 was dumped in table: "+rastertable1); + status = 40; + AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 2****"); + double[][] slice2 = intersector.takeTimeSlice(layerT2, t, x1, x2, y1,y2,z,resolution,resolution); + AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 2"); + status = 50; + RasterTable raster2 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice2, config); + raster2.dumpGeoTable(); + String rastertable2 = raster2.getTablename(); + AnalysisLogger.getLogger().debug("MapsComparator: Map 2 was dumped in table: "+rastertable2); + status = 60; + /* + String rastertable1 = "rstr909f60c1d3f1472e9de998e844990724"; + String rastertable2 = "rstre52e744c99224de3a1c5354263c6c8d8"; + String resolution = "0.5"; + */ + config.setNumberOfResources(1); + config.setParam("FirstTable", rastertable1); + config.setParam("SecondTable", rastertable2); + config.setParam("FirstTableCsquareColumn", RasterTable.csquareColumn); + config.setParam("SecondTableCsquareColumn",RasterTable.csquareColumn); + config.setParam("FirstTableProbabilityColumn", RasterTable.probabilityColumn); + config.setParam("SecondTableProbabilityColumn", RasterTable.probabilityColumn); + config.setParam("ComparisonThreshold", ""+valuesthreshold); + AnalysisLogger.getLogger().debug("MapsComparator: Analyzing discrepancy between maps: "+rastertable1+" and "+rastertable2); + DiscrepancyAnalysis da = new DiscrepancyAnalysis(); + da.setConfiguration(config); + da.init(); + outputParameters = da.analyze(); + outputParameters.put("Resolution", ""+resolution); + status = 80; + AnalysisLogger.getLogger().debug("MapsComparator: Output: "+outputParameters); + + //delete the tables + + dbConnection = DatabaseUtils.initDBSession(config); + AnalysisLogger.getLogger().debug("MapsComparator: Deleting table "+rastertable1); + DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable1),dbConnection); + status = 90; + AnalysisLogger.getLogger().debug("MapsComparator: Deleting table "+rastertable2); + DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable2),dbConnection); + AnalysisLogger.getLogger().debug("MapsComparator: Elapsed: Whole operation completed in "+((double)(System.currentTimeMillis()-t0)/1000d)+"s"); + + + }catch(Exception e){ + e.printStackTrace(); + AnalysisLogger.getLogger().debug("MapsComparator: ERROR!: "+e.getLocalizedMessage()); + } + finally{ + DatabaseUtils.closeDBConnection(dbConnection); + status = 100; + } + + + } + + @Override + protected void setInputParameters() { + + addStringInput(layer1,"The title of a layer indexed in the e-Infrastructure on GeoNetwork","Sarda australis"); + addStringInput(layer2,"The title of a second layer indexed in the e-Infrastructure on GeoNetwork","Sarda orientalis"); + addNumberInput(zString,"value of Z. Default is 0, that means comparison will be at surface level","0"); + addNumberInput(valuesThr,"A comparison threshold for the values in the map. Null equals to 0.1","0.1"); + DatabaseType.addDefaultDBPars(inputs); + + } + + @Override + public void shutdown() { + AnalysisLogger.getLogger().debug("Shutdown"); + + } + + @Override + public StatisticalType getOutput() { + PrimitiveType p = new PrimitiveType(Map.class.getName(), PrimitiveType.stringMap2StatisticalMap(outputParameters), PrimitiveTypes.MAP, "Discrepancy Analysis",""); + return p; + } + +}