Skip to content

Commit

Permalink
Merge pull request #972 from ebocher/bdtopo_file_path
Browse files Browse the repository at this point in the history
Starting some hudge changes to manage geoclimate exception
  • Loading branch information
ebocher authored Jun 6, 2024
2 parents d99fa36 + 368c981 commit d5d7c5b
Show file tree
Hide file tree
Showing 45 changed files with 3,222 additions and 3,495 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,16 @@ abstract class BDTopo extends BDTopoUtils {
* Meteorological Society 93, no. 12 (2012): 1879-1900.
*
*/
static Map workflow(def input, int version) {
static Map workflow(def input, int version) throws Exception{
if (version == 2) {
BDTopoV2Workflow bdtopo_v2_workflow = new BDTopoV2Workflow()
return bdtopo_v2_workflow.execute(input)
} else if (version == 3) {
BDTopoV3Workflow bdTopoV3Workflow = new BDTopoV3Workflow()
return bdTopoV3Workflow.execute(input)
} else {
LoggerUtils.error "Unsupported version. Set number 2 or 3"
return null
throw new Exception("Unsupported version. Set number 2 or 3")
}
return null
}

static Map v2(def input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import java.sql.SQLException


@Override
Integer loadDataFromPostGIS(Object input_database_properties, Object code, Object distance, Object inputTables, Object inputSRID, H2GIS h2gis_datasource) {
Integer loadDataFromPostGIS(Object input_database_properties, Object code, Object distance, Object inputTables,
Object inputSRID, H2GIS h2gis_datasource) throws Exception{
def commune_location = inputTables.commune
if (!commune_location) {
error "The commune table must be specified to run Geoclimate"
return
throw new Exception("The commune table must be specified to run Geoclimate")
}
PostGISDBFactory dataSourceFactory = new PostGISDBFactory()
Connection sourceConnection = null
Expand All @@ -52,32 +52,27 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
DataSource ds = dataSourceFactory.createDataSource(props)
sourceConnection = ds.getConnection()
} catch (SQLException e) {
error("Cannot connect to the database to import the data ")
throw new SQLException("Cannot connect to the database to import the data ")
}

if (sourceConnection == null) {
error("Cannot connect to the database to import the data ")
return
throw new Exception("Cannot connect to the database to import the data ")
}

//Check if the commune table exists
if (!JDBCUtilities.tableExists(sourceConnection, commune_location)) {
error("The commune table doesn't exist")
return
throw new Exception("The commune table doesn't exist")
}

//Find the SRID of the commune table
def commune_srid = GeometryTableUtilities.getSRID(sourceConnection, commune_location)
if (commune_srid <= 0) {
error("The commune table doesn't have any SRID")
return
throw new Exception("The commune table doesn't have any SRID")
}
if (commune_srid == 0 && inputSRID) {
commune_srid = inputSRID
} else if (commune_srid <= 0) {
warn "Cannot find a SRID value for the layer commune.\n" +
"Please set a valid OGC prj or use the parameter srid to force it."
return null
throw new Exception( "Cannot find a SRID value for the layer commune.\n" +
"Please set a valid OGC prj or use the parameter srid to force it.")
}

String outputTableName = "COMMUNE"
Expand All @@ -87,8 +82,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
if (code in Collection) {
if(code.size()==3){
if(code[2]<100){
error("The distance to create a bbox from a point must be greater than 100 meters")
return
throw new Exception("The distance to create a bbox from a point must be greater than 100 meters")
}
code = BDTopoUtils.bbox(code[0], code[1],code[2])
}
Expand All @@ -104,7 +98,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
debug "Loading in the H2GIS database $outputTableName"
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableName, -1, 1000)
}
def count = h2gis_datasource."$outputTableName".rowCount
def count = h2gis_datasource.getRowCount(outputTableName)
if (count > 0) {
//Compute the envelope of the extracted area to extract the thematic tables
def geomToExtract = h2gis_datasource.firstRow("SELECT ST_EXPAND(ST_UNION(ST_ACCUM(the_geom)), ${distance}) AS THE_GEOM FROM $outputTableName".toString()).THE_GEOM
Expand Down Expand Up @@ -140,8 +134,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
debug "Loading in the H2GIS database $outputTableNameRoad"
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableNameRoad, -1, 1000)
} else {
error "The route table must be provided"
return
throw new Exception("The route table must be provided")
}

if (inputTables.troncon_voie_ferree) {
Expand Down Expand Up @@ -215,14 +208,11 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
debug "Loading in the H2GIS database $outputTableName"
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableName, -1, 1000)
}

sourceConnection.close()

return commune_srid

} else {
error "Cannot find any commune with the insee code : $code"
return
throw new Exception("Cannot find any commune with the insee code : $code".toString())
}
}

Expand All @@ -243,21 +233,19 @@ int getVersion() {
}

@Override
Map formatLayers(JdbcDataSource datasource, Map layers, float distance, float hLevMin) {
Map formatLayers(JdbcDataSource datasource, Map layers, float distance, float hLevMin) throws Exception{
if (!hLevMin) {
hLevMin = 3
}
if (!datasource) {
error "The database to store the BDTopo data doesn't exist"
return
throw new Exception("The database to store the BDTopo data doesn't exist")
}
info "Formating BDTopo GIS layers"
//Prepare the existing bdtopo data in the local database
def importPreprocess = BDTopo.InputDataLoading.loadV2(datasource, layers, distance)

if (!importPreprocess) {
error "Cannot prepare the BDTopo data."
return
throw new Exception("Cannot prepare the BDTopo data.")
}
def zoneTable = importPreprocess.zone
def urbanAreas = importPreprocess.urban_areas
Expand Down Expand Up @@ -302,7 +290,7 @@ Map formatLayers(JdbcDataSource datasource, Map layers, float distance, float hL

@Override
def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTables,
int sourceSRID, int inputSRID, H2GIS h2gis_datasource) {
int sourceSRID, int inputSRID, H2GIS h2gis_datasource) throws Exception{
def formatting_geom = "the_geom"
if (sourceSRID == 0 && sourceSRID != inputSRID) {
formatting_geom = "st_setsrid(the_geom, $inputSRID) as the_geom"
Expand All @@ -317,8 +305,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
if (location in Collection) {
if(location.size()==3){
if(location[2]<100){
error("The distance to create a bbox from a point must be greater than 100 meters")
return
throw new Exception("The distance to create a bbox from a point must be greater than 100 meters")
}
location = BDTopoUtils.bbox(location[0], location[1],location[2])
}
Expand All @@ -331,9 +318,9 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
h2gis_datasource.execute("""DROP TABLE IF EXISTS $outputTableName ; CREATE TABLE $outputTableName as SELECT $formatting_geom,
CODE_INSEE FROM ${inputTables.commune} WHERE CODE_INSEE='$location' or lower(nom)='${location.toLowerCase()}'""".toString())
}else{
return
throw new Exception("Invalid location data type. Please set a text value or a collection of coordinates to specify a bbox")
}
def count = h2gis_datasource."$outputTableName".rowCount
def count = h2gis_datasource.getRowCount(outputTableName)
if (count > 0) {
//Compute the envelope of the extracted area to extract the thematic tables
def geomToExtract = h2gis_datasource.firstRow("SELECT ST_EXPAND(ST_UNION(ST_ACCUM(the_geom)), ${distance}) AS THE_GEOM FROM $outputTableName".toString()).THE_GEOM
Expand Down Expand Up @@ -370,8 +357,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
AND ST_INTERSECTS(the_geom, 'SRID=$sourceSRID;$geomToExtract'::GEOMETRY)
AND NATURE NOT IN ('Bac auto', 'Bac piéton', 'Escalier')""".toString())
} else {
error "The route table must be provided"
return
throw new Exception("The route table must be provided")
}

if (inputTables.troncon_voie_ferree) {
Expand Down Expand Up @@ -442,8 +428,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
return true

} else {
error "Cannot find any commune with the insee code : $location"
return
throw new Exception("Cannot find any commune with the insee code : $location".toString())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import java.sql.SQLException

@Override
def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTables,
int sourceSRID, int inputSRID, H2GIS h2gis_datasource) {
int sourceSRID, int inputSRID, H2GIS h2gis_datasource) throws Exception{
def formatting_geom = "the_geom"
if (sourceSRID == 0 && sourceSRID != inputSRID) {
formatting_geom = "st_setsrid(the_geom, $inputSRID) as the_geom"
Expand All @@ -57,8 +57,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
if(communeColumns.contains("INSEE_COM")) {
if(location.size()==3){
if(location[2]<100){
error("The distance to create a bbox from a point must be greater than 100 meters")
return
throw new IllegalArgumentException("The distance to create a bbox from a point must be greater than 100 meters")
}
location = BDTopoUtils.bbox(location[0], location[1],location[2])
}
Expand All @@ -68,8 +67,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
&& ST_MakeEnvelope(${location[1]},${location[0]},${location[3]},${location[2]}, $sourceSRID) """.toString())
}
else {
error "Cannot find a column insee_com or code_insee to filter the commune"
return
throw new Exception("Cannot find a column insee_com or code_insee to filter the commune")
}
} else if (location instanceof String) {
debug "Loading in the H2GIS database $outputTableName"
Expand All @@ -79,16 +77,14 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
INSEE_COM AS CODE_INSEE FROM ${inputTables.commune} WHERE INSEE_COM='$location' or lower(nom)='${location.toLowerCase()}'""".toString())
}
else {
error "Cannot find a column insee_com or code_insee to filter the commune"
return
throw new Exception("Cannot find a column insee_com or code_insee to filter the commune")
}

}
else {
debug "Invalid location"
return false
throw new IllegalArgumentException("Invalid location")
}
def count = h2gis_datasource."$outputTableName".rowCount
def count = h2gis_datasource.getRowCount(outputTableName)
if (count > 0) {
//Compute the envelope of the extracted area to extract the thematic tables
def geomToExtract = h2gis_datasource.firstRow("SELECT ST_EXPAND(ST_UNION(ST_ACCUM(the_geom)), ${distance}) AS THE_GEOM FROM $outputTableName".toString()).THE_GEOM
Expand Down Expand Up @@ -116,8 +112,7 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
AND NATURE NOT IN ('Bac ou liaison maritime', 'Escalier')
""".toString())
} else {
error "The troncon_de_route table must be provided"
return
throw new Exception("The troncon_de_route table must be provided")
}

if (inputTables.troncon_de_voie_ferree) {
Expand Down Expand Up @@ -214,17 +209,16 @@ def filterLinkedShapeFiles(def location, float distance, LinkedHashMap inputTabl
return true

} else {
error "Cannot find any commune with the insee code : $location"
return
throw new Exception("Cannot find any commune with the insee code : $location")
}
}

@Override
Integer loadDataFromPostGIS(Object input_database_properties, Object code, Object distance, Object inputTables, Object inputSRID, H2GIS h2gis_datasource) {
Integer loadDataFromPostGIS(Object input_database_properties, Object code, Object distance, Object inputTables, Object inputSRID,
H2GIS h2gis_datasource) throws Exception{
def commune_location = inputTables.commune
if (!commune_location) {
error "The commune table must be specified to run Geoclimate"
return
throw new Exception("The commune table must be specified to run Geoclimate")
}
PostGISDBFactory dataSourceFactory = new PostGISDBFactory()
Connection sourceConnection = null
Expand All @@ -234,25 +228,22 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
DataSource ds = dataSourceFactory.createDataSource(props)
sourceConnection = ds.getConnection()
} catch (SQLException e) {
error("Cannot connect to the database to import the data ")
throw new Exception("Cannot connect to the database to import the data ", e)
}

if (sourceConnection == null) {
error("Cannot connect to the database to import the data ")
return
throw new Exception("Cannot connect to the database to import the data ")
}

//Check if the commune table exists
if (!JDBCUtilities.tableExists(sourceConnection, commune_location)) {
error("The commune table doesn't exist")
return
throw new Exception("The commune table doesn't exist")
}

//Find the SRID of the commune table
def commune_srid = GeometryTableUtilities.getSRID(sourceConnection, commune_location)
if (commune_srid <= 0) {
error("The commune table doesn't have any SRID")
return
throw new Exception("The commune table doesn't have any SRID")
}
if (commune_srid == 0 && inputSRID) {
commune_srid = inputSRID
Expand All @@ -271,8 +262,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
if(communeColumns.contains("INSEE_COM")) {
if(code.size()==3){
if(code[2]<100){
error("The distance to create a bbox from a point must be greater than 100 meters")
return
throw new Exception("The distance to create a bbox from a point must be greater than 100 meters")
}
code = BDTopoUtils.bbox(code[0], code[1],code[2])
}
Expand All @@ -284,8 +274,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
debug "Loading in the H2GIS database $outputTableName"
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableName, -1, 100)
}else {
error "Cannot find a column insee_com or code_insee to filter the commune"
return
throw new Exception("Cannot find a column insee_com or code_insee to filter the commune")
}
} else if (code instanceof String) {
def communeColumns = h2gis_datasource.getColumnNames(commune_location)
Expand All @@ -295,11 +284,12 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableName, -1, 1000)
}
else {
error "Cannot find a column insee_com to filter the commune"
return
throw new Exception("Cannot find a column insee_com to filter the commune")
}
}else{
throw new Exception("Invalid location data type. Please set a text value or a collection of coordinates to specify a bbox")
}
def count = h2gis_datasource."$outputTableName".rowCount
def count = h2gis_datasource.getRowCount(outputTableName)
if (count > 0) {
//Compute the envelope of the extracted area to extract the thematic tables
def geomToExtract = h2gis_datasource.firstRow("SELECT ST_EXPAND(ST_UNION(ST_ACCUM(the_geom)), ${distance}) AS THE_GEOM FROM $outputTableName".toString()).THE_GEOM
Expand All @@ -321,8 +311,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
debug "Loading in the H2GIS database $outputTableNameRoad"
IOMethods.exportToDataBase(sourceConnection, inputTableName, h2gis_datasource.getConnection(), outputTableNameRoad, -1, 1000)
} else {
error "The route table must be provided"
return
throw new Exception("The route table must be provided")
}

if (inputTables.troncon_de_voie_ferree) {
Expand Down Expand Up @@ -418,8 +407,7 @@ Integer loadDataFromPostGIS(Object input_database_properties, Object code, Objec
return commune_srid

} else {
error "Cannot find any commune with the insee code : $code"
return
throw new Exception("Cannot find any commune with the insee code : $code".toString())
}

return false
Expand All @@ -439,22 +427,20 @@ int getVersion() {
}

@Override
Map formatLayers(JdbcDataSource datasource, Map layers, float distance, float hLevMin) {
Map formatLayers(JdbcDataSource datasource, Map layers, float distance, float hLevMin) throws Exception{
if (!hLevMin) {
hLevMin = 3
}
if (!datasource) {
error "The database to store the BD Topo data doesn't exist"
return
throw new Exception("The database to store the BD Topo data doesn't exist")
}
info "Formating BDTopo GIS layers"

//Prepare the existing bdtopo data in the local database
def importPreprocess = BDTopo.InputDataLoading.loadV3(datasource, layers, distance)

if (!importPreprocess) {
error "Cannot prepare the BDTopo data."
return
throw new Exception("Cannot prepare the BDTopo data.")
}
def zoneTable = importPreprocess.zone
def urbanAreas = importPreprocess.urban_areas
Expand Down
Loading

0 comments on commit d5d7c5b

Please sign in to comment.