Skip to content

Commit

Permalink
Merge pull request #824 from ebocher/complex_oms_geometry
Browse files Browse the repository at this point in the history
Add an option to validate the OSM geometries
  • Loading branch information
ebocher authored Jul 14, 2023
2 parents f4fce89 + 235884c commit 2a485c3
Show file tree
Hide file tree
Showing 7 changed files with 8,981 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Map createGISLayers(JdbcDataSource datasource, String osmFilePath, org.locationt
parametersMap = readJSONParameters(paramsDefaultFile)
tags = parametersMap.get("tags")
columnsToKeep = parametersMap.get("columns")
String vegetation = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry)
String vegetation = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry, true)
debug "Create the vegetation layer"
if (vegetation) {
outputVegetationTableName = postfix("OSM_VEGETATION")
Expand All @@ -224,7 +224,7 @@ Map createGISLayers(JdbcDataSource datasource, String osmFilePath, org.locationt
parametersMap = readJSONParameters(paramsDefaultFile)
tags = parametersMap.get("tags")
columnsToKeep = parametersMap.get("columns")
String water = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry)
String water = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry, true)
debug "Create the water layer"
if (water) {
outputHydroTableName = postfix("OSM_WATER")
Expand All @@ -237,7 +237,7 @@ Map createGISLayers(JdbcDataSource datasource, String osmFilePath, org.locationt
parametersMap = readJSONParameters(paramsDefaultFile)
tags = parametersMap.get("tags")
columnsToKeep = parametersMap.get("columns")
String impervious = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry)
String impervious = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, columnsToKeep,geometry, true)
debug "Create the impervious layer"
if (impervious) {
outputImperviousTableName = postfix("OSM_IMPERVIOUS")
Expand All @@ -250,7 +250,7 @@ Map createGISLayers(JdbcDataSource datasource, String osmFilePath, org.locationt
parametersMap = readJSONParameters(paramsDefaultFile)
tags = parametersMap.get("tags")
columnsToKeep = parametersMap.get("columns")
String urban_areas = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, [],geometry)
String urban_areas = OSMTools.Transform.toPolygons(datasource, prefix, epsg, tags, [],geometry, true)
debug "Create the urban areas layer"
if (urban_areas) {
outputUrbanAreasTableName = postfix("OSM_URBAN_AREAS")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class InputDataFormattingTest {
//Vegetation
String vegetation = OSM.InputDataFormatting.formatVegetationLayer(h2GIS, extractData.vegetation)
assertNotNull h2GIS.getTable(vegetation).save(new File(folder, "osm_vegetation_formated.shp").absolutePath, true)
assertEquals 135, h2GIS.getTable(vegetation).rowCount
assertEquals 140, h2GIS.getTable(vegetation).rowCount
assertTrue h2GIS.firstRow("select count(*) as count from ${vegetation} where type is null".toString()).count == 0
assertTrue h2GIS.firstRow("select count(*) as count from ${vegetation} where HEIGHT_CLASS is null".toString()).count == 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ class WorflowOSMTest extends WorkflowAbstractTest {
"delete": false
],
"input" : [
"locations": ["Dijon"]//["Pont-de-Veyle"],//[nominatim["bbox"]],//["Lorient"],
// "area": 2800,
"locations": ["San-Diego"],//["Pont-de-Veyle"],//[nominatim["bbox"]],//["Lorient"],
"area": 2800,
/*"timeout":182,
"maxsize": 536870918,
"endpoint":"https://lz4.overpass-api.de/api"*/],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ String toLines(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode =
String toLines(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [], Geometry geometry) {
return OSMTools.TransformUtils.toPolygonOrLine(LINES, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, geometry)
}
/**
* This process is used to extract all the polygons from the OSM tables
* @param datasource a connection to a database
* @param osmTablesPrefix prefix name for OSM tables
* @param epsgCode EPSG code to reproject the geometries
* @param tags list of keys and values to be filtered
* @param columnsToKeep a list of columns to keep. The name of a column corresponds to a key name
* @param geometry a geometry to reduce the area
* @param valid_geom true to valid the geometries
*
* @return outputTableName a name for the table that contains all polygons
* @author Erwan Bocher CNRS LAB-STICC
*/
String toPolygons(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [], Geometry geometry, boolean valid_geom) {
return OSMTools.TransformUtils.toPolygonOrLine(POLYGONS, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, geometry, valid_geom)
}

/**
* This process is used to extract all the polygons from the OSM tables
Expand All @@ -135,7 +151,7 @@ String toLines(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode =
* @author Elisabeth Le Saux UBS LAB-STICC
*/
String toPolygons(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [], Geometry geometry) {
return OSMTools.TransformUtils.toPolygonOrLine(POLYGONS, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, geometry)
return OSMTools.TransformUtils.toPolygonOrLine(POLYGONS, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, geometry, false)
}

/**
Expand All @@ -154,6 +170,22 @@ String toPolygons(JdbcDataSource datasource, String osmTablesPrefix, int epsgCod
return OSMTools.TransformUtils.toPolygonOrLine(POLYGONS, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, null)
}

/**
* This process is used to extract all the polygons from the OSM tables
* @param datasource a connection to a database
* @param osmTablesPrefix prefix name for OSM tables
* @param epsgCode EPSG code to reproject the geometries
* @param tags list of keys and values to be filtered
* @param columnsToKeep a list of columns to keep. The name of a column corresponds to a key name
* @param valid_geom true to valid the geometries
*
* @return outputTableName a name for the table that contains all polygons
* @author Erwan Bocher CNRS LAB-STICC
*/
String toPolygons(JdbcDataSource datasource, String osmTablesPrefix, int epsgCode = 4326, def tags = [], def columnsToKeep = [], boolean valid_geom) {
return OSMTools.TransformUtils.toPolygonOrLine(POLYGONS, datasource, osmTablesPrefix, epsgCode, tags, columnsToKeep, null, valid_geom)
}

/**
* This process is used to extract ways as polygons
*
Expand Down Expand Up @@ -291,7 +323,7 @@ String extractWaysAsPolygons(JdbcDataSource datasource, String osmTablesPrefix,
CREATE TABLE $outputTableName AS
SELECT 'w'||a.id_way AS id,"""
if (valid_geom) {
query += """ st_makevalid(a.the_geom) as the_geom ${OSMTools.TransformUtils.createTagList(datasource, columnsSelector, columnsToKeep)}
query += """ case when st_isvalid(a.the_geom) then a.the_geom else st_makevalid(a.the_geom) end as the_geom ${OSMTools.TransformUtils.createTagList(datasource, columnsSelector, columnsToKeep)}
"""
} else {
query += """ a.the_geom ${OSMTools.TransformUtils.createTagList(datasource, columnsSelector, columnsToKeep)} """
Expand Down Expand Up @@ -533,7 +565,7 @@ def extractRelationsAsPolygons(JdbcDataSource datasource, String osmTablesPrefix
CREATE TABLE $outputTableName AS
SELECT 'r'||a.id_relation AS id,"""
if (valid_geom) {
query += """ st_makevalid(a.the_geom) as the_geom ${OSMTools.TransformUtils.createTagList(datasource, columnsSelector, columnsToKeep)}
query += """ case when st_isvalid(a.the_geom) then a.the_geom else st_makevalid(a.the_geom) end as the_geom ${OSMTools.TransformUtils.createTagList(datasource, columnsSelector, columnsToKeep)}
FROM $relationsMpHoles AS a, ${osmTablesPrefix}_relation_tag b
WHERE a.id_relation=b.id_relation and st_isempty(a.the_geom)=false """
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,24 @@ class TransformTest extends AbstractOSMToolsTest {

}

@Test
void buildPolygonWithComplexHoles() {
def prefix = "OSM_SAN_DIEGO"
assertTrue OSMTools.Loader.load(ds, prefix,
new File(this.class.getResource("san_diegeo_complex_polygon.osm").toURI()).getAbsolutePath())
String outputTableName = OSMTools.Transform.toPolygons(ds, prefix, 4326, ["leisure"], ["leisure"], true)
assertEquals(0,ds.firstRow("SELECT COUNT(*) as count FROM $outputTableName where st_isvalid(the_geom)=false").count)
}

@Test
void buildPolygonWithInvalidGeometries() {
def prefix = "OSM_SAN_DIEGO"
assertTrue OSMTools.Loader.load(ds, prefix,
new File(this.class.getResource("san_diego_invalid_polygon.osm").toURI()).getAbsolutePath())
String outputTableName = OSMTools.Transform.toPolygons(ds, prefix, 4326, ["leisure"], ["leisure"], true)
assertEquals(0,ds.firstRow("SELECT COUNT(*) as count FROM $outputTableName where st_isvalid(the_geom)=false").count)
}

@Test
void buildGISPolygonsFilterGeometryTest() {
def prefix = "OSM_REDON"
Expand Down
Loading

0 comments on commit 2a485c3

Please sign in to comment.