Skip to content

Commit

Permalink
Merge pull request #13 from ebocher/division_per0
Browse files Browse the repository at this point in the history
Fix OSM building format method
  • Loading branch information
j3r3m1 authored Jan 11, 2024
2 parents 105c0c2 + 4bb1977 commit 23a4e87
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ Map computeAllGeoIndicators(JdbcDataSource datasource, String zone, String build
def indicatorUse = inputParameters.indicatorUse

//Estimate height
if (inputParameters.buildingHeightModelName && datasource.getRowCount(building) > 0) {
if (inputParameters.buildingHeightModelName && datasource.getRowCount(buildingEstimateTableName) > 0) {
def start = System.currentTimeMillis()
enableTableCache()
def buildingTableName
Expand Down Expand Up @@ -1424,7 +1424,7 @@ Map estimateBuildingHeight(JdbcDataSource datasource, String zone, String buildi
FROM $buildingIndicatorsForHeightEst a
RIGHT JOIN $building_estimate b
ON a.id_build=b.id_build
WHERE b.ESTIMATED = true AND a.ID_RSU IS NOT NULL;""".toString()
WHERE a.ID_RSU IS NOT NULL;""".toString()

info "Collect building indicators to estimate the height"

Expand Down Expand Up @@ -1460,30 +1460,29 @@ Map estimateBuildingHeight(JdbcDataSource datasource, String zone, String buildi

nbBuildingEstimated = datasource.firstRow("select count(*) as count from $buildEstimatedHeight".toString()).count

//We must format only estimated buildings
//Apply format on the new abstract table
def epsg = datasource."$buildEstimatedHeight".srid;
def formatedBuildEstimatedHeight = formatEstimatedBuilding(datasource, buildEstimatedHeight, epsg)
datasource.createIndex(buildEstimatedHeight, "id_build")

datasource.createIndex(formatedBuildEstimatedHeight, "id_build")

buildingTableName = "INPUT_BUILDING_ESTIMATED_${UUID.randomUUID().toString().replaceAll("-", "_")}"
def formatedBuildEstimatedHeight = "INPUT_BUILDING_REFORMATED_${UUID.randomUUID().toString().replaceAll("-", "_")}"

//Use build table indicators
datasource.execute """DROP TABLE IF EXISTS $buildingTableName;
CREATE TABLE $buildingTableName as
datasource.execute """DROP TABLE IF EXISTS $formatedBuildEstimatedHeight;
CREATE TABLE $formatedBuildEstimatedHeight as
SELECT a.THE_GEOM, a.ID_BUILD,a.ID_SOURCE,
CASE WHEN b.HEIGHT_ROOF IS NULL THEN a.HEIGHT_WALL ELSE 0 END AS HEIGHT_WALL ,
COALESCE(b.HEIGHT_ROOF, a.HEIGHT_ROOF) AS HEIGHT_ROOF,
CASE WHEN b.HEIGHT_ROOF IS NULL THEN a.NB_LEV ELSE 0 END AS NB_LEV, a.TYPE,a.MAIN_USE, a.ZINDEX, a.ID_BLOCK, a.ID_RSU
from $buildingIndicatorsForHeightEst
a LEFT JOIN $formatedBuildEstimatedHeight b on a.id_build=b.id_build""".toString()

a LEFT JOIN $buildEstimatedHeight b on a.id_build=b.id_build""".toString()

//We must format only estimated buildings
//Apply format on the new abstract table
def epsg = datasource.getSrid(formatedBuildEstimatedHeight)
buildingTableName = formatEstimatedBuilding(datasource, formatedBuildEstimatedHeight, epsg)
//Drop intermediate tables
datasource.execute """DROP TABLE IF EXISTS $estimated_building_with_indicators,
$formatedBuildEstimatedHeight, $buildEstimatedHeight,
$gatheredScales""".toString()

}

return ["building" : buildingTableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def saveToGeojson(def outputTable, def filePath, H2GIS h2gis_datasource, def out
*/
def saveToCSV(def outputTable, def filePath, def h2gis_datasource, def deleteOutputData) {
if (outputTable && h2gis_datasource.hasTable(outputTable)) {
h2gis_datasource.save("(SELECT ID_BUILD, ID_SOURCE FROM $outputTable WHERE estimated=true)", filePath, deleteOutputData)
h2gis_datasource.save("(SELECT ID_BUILD, ID_SOURCE FROM $outputTable)", filePath, deleteOutputData)
info "${outputTable} has been saved in ${filePath}."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ Map formatBuildingLayer(JdbcDataSource datasource, String building, String zone
DROP TABLE if exists ${outputEstimateTableName};
CREATE TABLE ${outputEstimateTableName} (
id_build INTEGER,
ID_SOURCE VARCHAR,
estimated boolean)
ID_SOURCE VARCHAR)
""".toString()

datasource """
Expand Down Expand Up @@ -124,13 +123,13 @@ Map formatBuildingLayer(JdbcDataSource datasource, String building, String zone
${roof_shape ? "'" + roof_shape + "'" : null})
""".toString()

stmt.addBatch """
if(formatedHeight.estimated) {
stmt.addBatch """
INSERT INTO ${outputEstimateTableName} values(
$id_build,
'${row.id}',
${formatedHeight.estimated})
'${row.id}')
""".toString()

}
id_build++
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ def saveTablesInDatabase(JdbcDataSource output_datasource, JdbcDataSource h2gis_
} else {
output_datasource.execute """CREATE TABLE $output_table(ID_BUILD INTEGER, ID_SOURCE VARCHAR, ID_ZONE VARCHAR)""".toString()
}
IOMethods.exportToDataBase(h2gis_datasource.getConnection(), "(SELECT ID_BUILD, ID_SOURCE, '$id_zone' as ID_ZONE from $h2gis_table_to_save where estimated=true)".toString(),
IOMethods.exportToDataBase(h2gis_datasource.getConnection(), "(SELECT ID_BUILD, ID_SOURCE, '$id_zone' as ID_ZONE from $h2gis_table_to_save)".toString(),
output_datasource.getConnection(), output_table, 2, 100);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,13 @@ class InputDataFormattingTest {
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayer} where HEIGHT_WALL<0").count == 0
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayer} where HEIGHT_ROOF is null").count == 0
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayer} where HEIGHT_ROOF<0").count == 0
assertEquals 1038, h2GIS.getTable(buildingLayers.building_estimated).rowCount
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayers.building_estimated} where ESTIMATED = false").count == 5
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayers.building} join ${buildingLayers.building_estimated} using (id_build, id_source) where 1=1").count == 1038
assertEquals 1033, h2GIS.getTable(buildingLayers.building_estimated).rowCount
assertTrue h2GIS.firstRow("select count(*) as count from ${buildingLayers.building} join ${buildingLayers.building_estimated} using (id_build, id_source) where 1=1").count == 1033

//Buildings without estimation state
buildingLayers = OSM.InputDataFormatting.formatBuildingLayer(h2GIS, extractData.building)
assertEquals 1038, h2GIS.getTable(buildingLayers.building).rowCount
assertEquals 1038, h2GIS.getTable(buildingLayers.building_estimated).rowCount
assertEquals 1033, h2GIS.getTable(buildingLayers.building_estimated).rowCount
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,36 @@ class WorflowOSMTest extends WorkflowAbstractTest {
OSM.WorkflowOSM.workflow(configFile)
}


@Test
void testEstimateBuildingWithAllInputHeight() {
String directory = folder.absolutePath + File.separator + "test_building_height"
File dirFile = new File(directory)
dirFile.delete()
dirFile.mkdir()
def osm_parmeters = [
"geoclimatedb": [
"folder": dirFile.absolutePath,
"name" : "geoclimate_chain_db",
"delete": false
],
"input" : [
"locations": [[43.726898,7.298452,43.727677,7.299632]]],
"output" : [
"folder": ["path" : directory,
"tables": ["building", "zone"]]],
"parameters" :
["distance" : 0,
rsu_indicators: ["indicatorUse" : ["LCZ"]]
]
]
Map process = OSM.WorkflowOSM.workflow(osm_parmeters)
def tableNames = process.values()
def building = tableNames.building[0]
H2GIS h2gis = H2GIS.open("${directory + File.separator}geoclimate_chain_db;AUTO_SERVER=TRUE")
assertTrue h2gis.firstRow("select count(*) as count from $building where HEIGHT_WALL>0 and HEIGHT_ROOF>0").count > 0
}

/**
* Create a configuration file
* @param osmParameters
Expand Down

0 comments on commit 23a4e87

Please sign in to comment.