From db7708a2254890998deb7671b53e40fb90cc0783 Mon Sep 17 00:00:00 2001 From: ebocher Date: Thu, 7 Sep 2023 12:51:10 +0200 Subject: [PATCH] Add a method to check if the worldpop coverageId is available --- .../geoclimate/osm/WorflowOSMTest.groovy | 1 + worldpoptools/pom.xml | 5 ++ .../worldpoptools/WorldPopExtract.groovy | 46 ++++++++++++++++++- .../worldpoptools/WorldPopExtractTest.groovy | 33 +++++++++---- 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorflowOSMTest.groovy b/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorflowOSMTest.groovy index a4f1ef2f93..ab372c5387 100644 --- a/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorflowOSMTest.groovy +++ b/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorflowOSMTest.groovy @@ -303,6 +303,7 @@ class WorflowOSMTest extends WorkflowAbstractTest { "output" : [ "folder": directory] ] + println(osm_parmeters) OSM.WorkflowOSM.workflow(osm_parmeters) def folder = new File(directory + File.separator + "osm_" + bbox.join("_")) def resultFiles = [] diff --git a/worldpoptools/pom.xml b/worldpoptools/pom.xml index 2667f0d2e3..44a29160b5 100644 --- a/worldpoptools/pom.xml +++ b/worldpoptools/pom.xml @@ -23,6 +23,11 @@ groovy ${groovy-version} + + org.codehaus.groovy + groovy-xml + ${groovy-version} + org.orbisgis.data h2gis diff --git a/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtract.groovy b/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtract.groovy index b1894321b0..ba59f001f0 100644 --- a/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtract.groovy +++ b/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtract.groovy @@ -20,6 +20,8 @@ package org.orbisgis.geoclimate.worldpoptools import groovy.transform.BaseScript +import groovy.xml.XmlSlurper +import groovy.xml.slurpersupport.GPathResult import org.cts.util.UTMUtils import org.h2gis.api.EmptyProgressVisitor import org.h2gis.functions.io.asc.AscReaderDriver @@ -63,10 +65,10 @@ String extractWorldPopLayer(String coverageId, List bbox) { } else { if (outputGridFile.createNewFile()) { if (grid(gridRequest, outputGridFile)) { - info "The OSM file has been downloaded at ${popGridFilePath}." + info "The WorldPop file has been downloaded at ${popGridFilePath}." } else { outputGridFile.delete() - error "Cannot extract the OSM data for the query $gridRequest" + error "Cannot extract the WorldPop file for the query $gridRequest" return } } @@ -225,6 +227,46 @@ boolean grid(String wcsRequest, File outputGridFile) { } } +/** + * A method to test if the coverageId is available + * @param coverageId + * @return + */ +boolean isCoverageAvailable(String coverageId){ + String describeRequest = """https://ogc.worldpop.org/geoserver/ows?service=WCS&version=2.0.1&request=DescribeCoverage&coverageId=$coverageId""".toString() + def queryUrl = new URL(describeRequest) + final String proxyHost = System.getProperty("http.proxyHost"); + final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "80")); + def connection + if (proxyHost != null) { + def proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); + connection = queryUrl.openConnection(proxy) as HttpURLConnection + } else { + connection = queryUrl.openConnection() as HttpURLConnection + } + info queryUrl.toString() + connection.connect() + + info "Executing query... $queryUrl" + //Save the result in a file + if (connection.responseCode == 200) { + XmlSlurper xmlParser = new XmlSlurper() + GPathResult nodes = xmlParser.parse(connection.inputStream) + if(nodes.Exception){ + return true + }else { + error "The service is not available for the coverageId : $coverageId" + return false + } + } else if (connection.responseCode == 404) { + error "The service is not available for the coverageId : $coverageId" + return false + } else { + error "Cannot request the WorldPop service" + return false + } +} + /** * This method is used to build the subset filter used by the WCS request * from bbox defined by a set of latitude and longitude coordinates diff --git a/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy b/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy index 355d8e7c3b..ce2567403e 100644 --- a/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy +++ b/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy @@ -78,9 +78,11 @@ class WorldPopExtractTest { */ @Test void extractGridProcess() { - String outputFilePath = WorldPopTools.Extract.extractWorldPopLayer("wpGlobal:ppp_2018", [47.63324, -2.78087, 47.65749, -2.75979]) - assertNotNull(outputFilePath) - assertTrue new File(outputFilePath).exists() + if(WorldPopExtract.Extract.isCoverageAvailable("wpGlobal:ppp_2018")) { + String outputFilePath = WorldPopTools.Extract.extractWorldPopLayer("wpGlobal:ppp_2018", [47.63324, -2.78087, 47.65749, -2.75979]) + assertNotNull(outputFilePath) + assertTrue new File(outputFilePath).exists() + } } /** @@ -88,13 +90,24 @@ class WorldPopExtractTest { */ @Test void extractLoadGridProcess() { - String outputFilePath = WorldPopTools.Extract.extractWorldPopLayer("wpGlobal:ppp_2018", [47.63324, -2.78087, 47.65749, -2.75979]) - if (outputFilePath) { - assertTrue new File(outputFilePath).exists() - String outputTableWorldPopName = WorldPopTools.Extract.importAscGrid(h2GIS, outputFilePath) - assertNotNull outputTableWorldPopName - assertEquals(720, h2GIS.getSpatialTable(outputTableWorldPopName).rowCount) - assertEquals(["ID_POP", "THE_GEOM", "POP"], h2GIS.getTable(outputTableWorldPopName).columns) + if(WorldPopExtract.Extract.isCoverageAvailable("wpGlobal:ppp_2018")) { + String outputFilePath = WorldPopTools.Extract.extractWorldPopLayer("wpGlobal:ppp_2018", [47.63324, -2.78087, 47.65749, -2.75979]) + if (outputFilePath) { + assertTrue new File(outputFilePath).exists() + String outputTableWorldPopName = WorldPopTools.Extract.importAscGrid(h2GIS, outputFilePath) + assertNotNull outputTableWorldPopName + assertEquals(720, h2GIS.getSpatialTable(outputTableWorldPopName).rowCount) + assertEquals(["ID_POP", "THE_GEOM", "POP"], h2GIS.getTable(outputTableWorldPopName).columns) + } } } + + /** + * Test to extract a grid from process + */ + @Test + void testCoverageAvailable() { + assertFalse(WorldPopExtract.Extract.isCoverageAvailable("wpGlobal:ppp_2050")) + assertTrue(WorldPopExtract.Extract.isCoverageAvailable("wpGlobal:ppp_2018")) + } }