Skip to content

Commit

Permalink
Merge pull request #855 from ebocher/type_objects
Browse files Browse the repository at this point in the history
Fix a zero DIV when a building doesn't have any pop.
  • Loading branch information
ebocher authored Oct 12, 2023
2 parents 1ecd510 + 5d42510 commit c0f29db
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ String buildingPopulation(JdbcDataSource datasource, String inputBuilding, Strin
create index on $inputBuildingTableName_area_sum($ID_POP);
create table $inputBuildingTableName_pop_sum
as select a.$ID_BUILDING, ${sum_popColumns.join(",")}
from $inputBuildingTableName_pop as a, $inputBuildingTableName_area_sum as b where a.$ID_POP=b.$ID_POP group by $ID_BUILDING;
from $inputBuildingTableName_pop as a, $inputBuildingTableName_area_sum as b where a.$ID_POP=b.$ID_POP and
b.sum_area_building!=0
group by $ID_BUILDING;
CREATE INDEX ON $inputBuildingTableName_pop_sum ($ID_BUILDING);
DROP TABLE IF EXISTS $outputTableName;
CREATE TABLE $outputTableName AS SELECT a.*, ${popColumns.join(",")} from $inputBuilding a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class InputDataFormattingTest {

zoneToExtract = "Göteborgs Stad"

zoneToExtract = "Ploemeur"
zoneToExtract = "Barcelona Eixample"

//zoneToExtract = [69.24666, 28.3359, 69.32999, 28.41923]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class InputDataLoadingTest {
@Test
@Disabled
void createGISLayersFromFileTestIntegration() {
Map extract = OSM.InputDataLoading.createGISLayers(h2GIS, "/tmp/map.osm", 2154)
Map extract = OSM.InputDataLoading.createGISLayers(h2GIS, "/tmp/3be46f5e1060bd53fe7c5b97a04102b3f36fa6f6257c87664dbaec3e954879ab.osm", 2154)

//h2GIS.getTable(extract.vegetation).save("./target/osm_vegetation.shp")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ class WorflowOSMTest extends WorkflowAbstractTest {
dirFile.delete()
dirFile.mkdir()

def location = "Redon"

//def nominatim = OSMTools.Utilities.getNominatimData("Lorient")

Expand All @@ -663,7 +664,7 @@ class WorflowOSMTest extends WorkflowAbstractTest {
"delete": false
],
"input" : [
"locations": [[47.081403,-1.481352,47.110619,-1.423845]],//["Pont-de-Veyle"],//[nominatim["bbox"]],//["Lorient"],
"locations": [location],//["Pont-de-Veyle"],//[nominatim["bbox"]],//["Lorient"],
"area": 2800,
/*"timeout":182,
"maxsize": 536870918,
Expand All @@ -683,12 +684,12 @@ class WorflowOSMTest extends WorkflowAbstractTest {
"BUILDING_TYPE_FRACTION","WATER_FRACTION","VEGETATION_FRACTION",
"ROAD_FRACTION", "IMPERVIOUS_FRACTION",
"BUILDING_HEIGHT_WEIGHTED", "BUILDING_SURFACE_DENSITY", "SEA_LAND_FRACTION",
"ASPECT_RATIO","SVF",
"ASPECT_RATIO",//"SVF",
"HEIGHT_OF_ROUGHNESS_ELEMENTS", "TERRAIN_ROUGHNESS_CLASS"]
], "worldpop_indicators": false,
"road_traffic" : false,
], "worldpop_indicators": true,
"road_traffic" : true,
"noise_indicators" : [
"ground_acoustic": false
"ground_acoustic": true
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ Map fromArea(JdbcDataSource datasource, Object filterArea, float distance = 0) {
if (load(datasource, osmTablesPrefix, extract)) {
info "Loading OSM data from the area $filterArea"
return [zone : outputZoneTable,
zoneEnvelopeTableName: outputZoneEnvelopeTable,
osmTablesPrefix : osmTablesPrefix,
envelope : outputZoneEnvelopeTable,
prefix : osmTablesPrefix,
epsg : epsg]
} else {
error "Cannot load the OSM data from the area $filterArea"
Expand Down Expand Up @@ -175,8 +175,8 @@ Map fromPlace(JdbcDataSource datasource, String placeName, float distance = 0) {
if (load(datasource, osmTablesPrefix, extract)) {
info "Loading OSM data from the place $placeName"
return [zone : outputZoneTable,
zoneEnvelopeTableName: outputZoneEnvelopeTable,
osmTablesPrefix : osmTablesPrefix]
envelope: outputZoneEnvelopeTable,
prefix : osmTablesPrefix]
} else {
error "Cannot load the OSM data from the place $placeName"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
*/
package org.orbisgis.geoclimate.osmtools

import org.orbisgis.geoclimate.osmtools.Loader as LOADER
import org.orbisgis.geoclimate.osmtools.Transform as TRANSFORM
import org.orbisgis.geoclimate.osmtools.utils.TransformUtils as TRANSFORM_UTILS
import org.orbisgis.geoclimate.osmtools.utils.Utilities as UTILITIES
import org.orbisgis.geoclimate.osmtools.utils.TransformUtils
import org.orbisgis.geoclimate.osmtools.utils.Utilities
import org.orbisgis.geoclimate.utils.AbstractScript

/**
Expand All @@ -34,10 +32,10 @@ import org.orbisgis.geoclimate.utils.AbstractScript
*/

abstract class OSMTools extends AbstractScript {
def static Loader = new LOADER()
def static Transform = new TRANSFORM()
def static Utilities = new UTILITIES()
def static TransformUtils = new TRANSFORM_UTILS()
static Loader Loader = new Loader()
static Transform Transform = new Transform()
static Utilities Utilities = new Utilities()
static TransformUtils TransformUtils = new TransformUtils()

OSMTools() {
super(OSMTools.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class LoaderTest extends AbstractOSMToolsTest {
assertFalse r.isEmpty()
assertTrue r.containsKey("zone")
assertTrue Pattern.compile("ZONE_$uuidRegex").matcher(r.zone as String).matches()
assertTrue r.containsKey("zoneEnvelopeTableName")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.zoneEnvelopeTableName as String).matches()
assertTrue r.containsKey("osmTablesPrefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.osmTablesPrefix as String).matches()
assertTrue r.containsKey("envelope")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.envelope as String).matches()
assertTrue r.containsKey("prefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.prefix as String).matches()
assertTrue r.containsKey("epsg")
assertEquals 4326, r.epsg

Expand All @@ -112,7 +112,7 @@ class LoaderTest extends AbstractOSMToolsTest {
zone.next()
assertEquals "POLYGON ((-3.076869 48.733493, -3.076869 48.733995, -3.075829 48.733995, -3.075829 48.733493, -3.076869 48.733493))", zone.getGeometry(1).toText()

def zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
def zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 1, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand All @@ -126,10 +126,10 @@ class LoaderTest extends AbstractOSMToolsTest {
assertFalse r.isEmpty()
assertTrue r.containsKey("zone")
assertTrue Pattern.compile("ZONE_$uuidRegex").matcher(r.zone as String).matches()
assertTrue r.containsKey("zoneEnvelopeTableName")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.zoneEnvelopeTableName as String).matches()
assertTrue r.containsKey("osmTablesPrefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.osmTablesPrefix as String).matches()
assertTrue r.containsKey("envelope")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.envelope as String).matches()
assertTrue r.containsKey("prefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.prefix as String).matches()
assertTrue r.containsKey("epsg")
assertEquals 4326, r.epsg

Expand All @@ -140,7 +140,7 @@ class LoaderTest extends AbstractOSMToolsTest {
zone.next()
assertEquals "POLYGON ((-3.076869 48.733493, -3.076869 48.733995, -3.075829 48.733995, -3.075829 48.733493, -3.076869 48.733493))", zone.getGeometry(1).toText()

zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 1, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand Down Expand Up @@ -170,10 +170,10 @@ class LoaderTest extends AbstractOSMToolsTest {
assertFalse r.isEmpty()
assertTrue r.containsKey("zone")
assertTrue Pattern.compile("ZONE_$uuidRegex").matcher(r.zone as String).matches()
assertTrue r.containsKey("zoneEnvelopeTableName")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.zoneEnvelopeTableName as String).matches()
assertTrue r.containsKey("osmTablesPrefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.osmTablesPrefix as String).matches()
assertTrue r.containsKey("envelope")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.envelope as String).matches()
assertTrue r.containsKey("prefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.prefix as String).matches()

def zone = ds.getSpatialTable(r.zone)
assertEquals 1, zone.rowCount
Expand All @@ -182,7 +182,7 @@ class LoaderTest extends AbstractOSMToolsTest {
zone.next()
assertEquals wktReader.read("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))"), zone.getGeometry(1)

def zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
def zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 1, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand All @@ -196,10 +196,10 @@ class LoaderTest extends AbstractOSMToolsTest {
assertFalse r.isEmpty()
assertTrue r.containsKey("zone")
assertTrue Pattern.compile("ZONE_$uuidRegex").matcher(r.zone as String).matches()
assertTrue r.containsKey("zoneEnvelopeTableName")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.zoneEnvelopeTableName as String).matches()
assertTrue r.containsKey("osmTablesPrefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.osmTablesPrefix as String).matches()
assertTrue r.containsKey("envelope")
assertTrue Pattern.compile("ZONE_ENVELOPE_$uuidRegex").matcher(r.envelope as String).matches()
assertTrue r.containsKey("prefix")
assertTrue Pattern.compile("OSM_DATA_$uuidRegex").matcher(r.prefix as String).matches()

zone = ds.getSpatialTable(r.zone)
assertEquals 1, zone.rowCount
Expand All @@ -208,7 +208,7 @@ class LoaderTest extends AbstractOSMToolsTest {
zone.next()
assertEquals wktReader.read("POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))"), zone.getGeometry(1)

zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 1, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand All @@ -227,9 +227,9 @@ class LoaderTest extends AbstractOSMToolsTest {
Map r = OSMTools.Loader.fromPlace(ds, placeName)
assertFalse r.isEmpty()
assertTrue r.containsKey("zone")
assertTrue r.containsKey("zoneEnvelopeTableName")
assertTrue r.containsKey("osmTablesPrefix")
assertTrue Pattern.compile("OSM_DATA_$formattedPlaceName$uuidRegex").matcher(r.osmTablesPrefix as String).matches()
assertTrue r.containsKey("envelope")
assertTrue r.containsKey("prefix")
assertTrue Pattern.compile("OSM_DATA_$formattedPlaceName$uuidRegex").matcher(r.prefix as String).matches()

def zone = ds.getSpatialTable(r.zone)
assertEquals 1, zone.rowCount
Expand All @@ -239,7 +239,7 @@ class LoaderTest extends AbstractOSMToolsTest {
zone.next()
assertNotNull zone.getGeometry(1)

def zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
def zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 2, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand Down Expand Up @@ -269,8 +269,7 @@ class LoaderTest extends AbstractOSMToolsTest {
assertTrue zone.columns.contains("ID_ZONE")
zone.next()
assertNotNull zone.getGeometry(1)

def zoneEnv = ds.getSpatialTable(r.zoneEnvelopeTableName)
def zoneEnv = ds.getSpatialTable(r.envelope)
assertEquals 1, zoneEnv.rowCount
assertEquals 2, zoneEnv.getColumnCount()
assertTrue zoneEnv.columns.contains("THE_GEOM")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class TransformTest extends AbstractOSMToolsTest {
def columnsToKeep = []

LOGGER.warn("An error will be thrown next")
assertNull OSMTools.Transform.toLines(null, prefix, epsgCode, tags, columnsToKeep)
assertNull OSMTools.Transform.toLines(null, prefix, epsgCode, tags, columnsToKeep)

LOGGER.warn("An error will be thrown next")
assertNull OSMTools.Transform.toLines(ds, prefix, -1, tags, columnsToKeep)
Expand Down Expand Up @@ -875,7 +875,20 @@ class TransformTest extends AbstractOSMToolsTest {
}
}
}
}


/**
* It uses for test purpose
*/
@Disabled
@Test
void testTransformForDebug() {
H2GIS h2GIS = H2GIS.open("/tmp/geoclimate;AUTO_SERVER=TRUE")
Map r = OSMTools.Loader.fromArea(h2GIS, [48.733493,-3.076869,48.733995,-3.075829])
println(r)
def lines = OSMTools.Transform.toPolygons(h2GIS, r.prefix,4326, [], [])
h2GIS.save(lines, "/tmp/building.geojson")
}


}

0 comments on commit c0f29db

Please sign in to comment.