Skip to content

Commit

Permalink
Fix return execption
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher committed Jun 5, 2024
1 parent 35261c6 commit 22201b4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ import static org.orbisgis.geoclimate.osmtools.utils.OSMElement.*
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Le Saux (UBS LAB-STICC)
*/
Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) {
Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) throws Exception{
if (!datasource) {
error("No datasource provided.")
return
throw new Exception("No datasource provided.")
}
if (!filterArea) {
error("Filter area not defined")
return
throw new Exception("Filter area not defined")
}
def outputZoneTable = postfix "ZONE"
def outputZoneEnvelopeTable = postfix "ZONE_ENVELOPE"
Expand All @@ -71,8 +69,7 @@ Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) {
geom = Utilities.geometryFromValues(filterArea)
}
else {
error "The filter area must be an Envelope or a Polygon"
return
throw new Exception("The filter area must be an Envelope or a Polygon")
}

def epsg = DEFAULT_SRID
Expand Down Expand Up @@ -102,10 +99,10 @@ Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) {
prefix : osmTablesPrefix,
epsg : epsg]
} else {
error "Cannot load the OSM data from the area $filterArea"
throw new Exception("Cannot load the OSM data from the area $filterArea".toString())
}
} else {
error "Cannot download OSM data from the area $filterArea"
throw new Exception("Cannot download OSM data from the area $filterArea".toString())
}
}

Expand All @@ -123,14 +120,12 @@ Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) {
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Le Saux (UBS LAB-STICC)
*/
Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) {
Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) throws Exception{
if (!placeName) {
error("Cannot find an area from a void place name.")
return
throw new Exception("Cannot find an area from a void place name.")
}
if (!datasource) {
error("No datasource provided.")
return
throw new Exception("No datasource provided.")
}
def formatedPlaceName = placeName.trim().replaceAll("([\\s|,|\\-|])+", "_")
def outputZoneTable = postfix "ZONE_$formatedPlaceName"
Expand All @@ -141,17 +136,14 @@ Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) {
Map nominatimRes = OSMTools.Utilities.getNominatimData(placeName);

if(!nominatimRes){
error("Cannot find an area from the place name $placeName")
return
throw new Exception("Cannot find an area from the place name $placeName".toString())
}
def geom = nominatimRes["geom"]
if (!geom) {
error("Cannot find an area from the place name $placeName")
return
throw new Exception("Cannot find an area from the place name $placeName".toString())
}
if (distance < 0) {
error("Cannot use a negative distance")
return
throw new Exception("Cannot use a negative distance")
}
def env = org.h2gis.utilities.GeographyUtilities.expandEnvelopeByMeters(geom.getEnvelopeInternal(), distance)

Expand All @@ -178,11 +170,11 @@ Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) {
envelope: outputZoneEnvelopeTable,
prefix : osmTablesPrefix]
} else {
error "Cannot load the OSM data from the place $placeName"
throw new Exception("Cannot load the OSM data from the place $placeName".toString())
}

} else {
error "Cannot download OSM data from the place $placeName"
throw new Exception("Cannot download OSM data from the place $placeName".toString())
}
}

Expand All @@ -194,11 +186,10 @@ Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) {
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Le Saux (UBS LAB-STICC)
*/
String extract(String overpassQuery) {
String extract(String overpassQuery) throws Exception{
info "Extract the OSM data"
if (!overpassQuery) {
error "The query should not be null or empty."
return
throw new Exception("The query should not be null or empty.")
}
def bboxUrl = OSMTools.Utilities.utf8ToUrl(overpassQuery);
//hash the query to cache it
Expand All @@ -213,8 +204,7 @@ String extract(String overpassQuery) {
info "The OSM file has been downloaded at ${osmFilePath}."
} else {
outputOSMFile.delete()
error "Cannot extract the OSM data for the query $overpassQuery"
return
throw new Exception("Cannot extract the OSM data for the query $overpassQuery")
}
}
} else {
Expand All @@ -226,8 +216,7 @@ String extract(String overpassQuery) {
info "The OSM file has been downloaded at ${osmFilePath}."
} else {
outputOSMFile.delete()
error "Cannot extract the OSM data for the query $overpassQuery"
return
throw new Exception("Cannot extract the OSM data for the query $overpassQuery")
}
}
}
Expand All @@ -246,35 +235,30 @@ String extract(String overpassQuery) {
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Le Saux (UBS LAB-STICC)
*/
boolean load(JdbcDataSource datasource, String osmTablesPrefix, String osmFilePath) {
boolean load(JdbcDataSource datasource, String osmTablesPrefix, String osmFilePath) throws Exception{
if (!datasource) {
error "Please set a valid database connection."
return false
throw new Exception("Please set a valid database connection.")
}

if (!osmTablesPrefix ||
!Pattern.compile('^[a-zA-Z0-9_]*$').matcher(osmTablesPrefix).matches()) {
error "Please set a valid table prefix."
return false
throw new Exception("Please set a valid table prefix.")
}

if (!osmFilePath) {
error "Please set a valid osm file path."
return false
throw new Exception("Please set a valid osm file path.")
}
def osmFile = new File(osmFilePath)
if (!osmFile.exists()) {
error "The input OSM file does not exist."
return false
throw new Exception("The input OSM file does not exist.")
}

info "Load the OSM file in the database."
if (datasource.load(osmFile, osmTablesPrefix, true)) {
info "The input OSM file has been loaded in the database."
//We must check if there is some data at least one tag
if (datasource.getRowCount("${osmTablesPrefix}_node".toString())==0) {
error "The downloaded OSM file doesn't contain any data.\n Please check the file ${osmFile} to see what happens.".toString()
return false
throw new Exception("The downloaded OSM file doesn't contain any data.\n Please check the file ${osmFile} to see what happens.".toString())
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import static org.orbisgis.geoclimate.osmtools.utils.GeometryTypes.POLYGONS
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Lesaux (UBS LAB-STICC)
*/
String toPoints(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [], Geometry geometry) {
String toPoints(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [],
Geometry geometry) {
String outputTableName = postfix "OSM_POINTS"
def pointsNodes = OSMTools.TransformUtils.extractNodesAsPoints(datasource, osmTablesPrefix, epsgCode, outputTableName, tags, columnsToKeep, geometry)
if (pointsNodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,21 @@ boolean extractNodesAsPoints(JdbcDataSource datasource, String osmTablesPrefix,
* @author Elisabeth Lesaux (UBS LAB-STICC)
*/
boolean extractNodesAsPoints(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode,
String outputNodesPoints, def tags, def columnsToKeep, Geometry geometry) {
String outputNodesPoints, def tags, def columnsToKeep, Geometry geometry) throws Exception{
if (!datasource) {
error("The datasource should not be null")
return false
throw new Exception("The datasource should not be null")
}
if (osmTablesPrefix == null) {
error "Invalid null OSM table prefix"
return false
throw new Exception("Invalid null OSM table prefix")
}
if (epsgCode == -1) {
error "Invalid EPSG code"
return false
throw new Exception("Invalid EPSG code")
}
if (tags == null) {
error "The tag list cannot be null"
return
throw new Exception("The tag list cannot be null")
}
if (outputNodesPoints == null) {
error "Invalid null output node points table name"
return false
throw new Exception("Invalid null output node points table name")
}
def tableNode = "${osmTablesPrefix}_node"
def tableNodeTag = "${osmTablesPrefix}_node_tag"
Expand Down Expand Up @@ -516,14 +511,12 @@ def createTagList(JdbcDataSource datasource, def selectTableQuery, List columnsT
* @author Erwan Bocher (CNRS LAB-STICC)
* @author Elisabeth Lesaux (UBS LAB-STICC)
*/
def buildIndexes(JdbcDataSource datasource, String osmTablesPrefix) {
def buildIndexes(JdbcDataSource datasource, String osmTablesPrefix) throws Exception{
if (!datasource) {
error "The datasource should not be null."
return false
throw new Exception("The datasource should not be null.")
}
if (!osmTablesPrefix) {
error "The osmTablesPrefix should not be null or empty."
return false
throw new Exception("The osmTablesPrefix should not be null or empty.")
}
datasource.execute """
CREATE INDEX IF NOT EXISTS ${osmTablesPrefix}_node_index ON ${osmTablesPrefix}_node(id_node);
Expand Down
Loading

0 comments on commit 22201b4

Please sign in to comment.