Skip to content

Commit

Permalink
Merge pull request #830 from ebocher/clean_up
Browse files Browse the repository at this point in the history
Add a method to check if the worldpop coverageId is available
  • Loading branch information
ebocher authored Sep 7, 2023
2 parents a372486 + db7708a commit fdbcafc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
5 changes: 5 additions & 0 deletions worldpoptools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>groovy</artifactId>
<version>${groovy-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>${groovy-version}</version>
</dependency>
<dependency>
<groupId>org.orbisgis.data</groupId>
<artifactId>h2gis</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,36 @@ 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()
}
}

/**
* Test to extract a grid and load it in database
*/
@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"))
}
}

0 comments on commit fdbcafc

Please sign in to comment.