Skip to content

Commit

Permalink
Improve doc and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher committed Oct 27, 2023
1 parent 97b7253 commit 1942638
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,30 +280,6 @@ String formatRoadLayer(JdbcDataSource datasource, String road, String zone = "")
SURFACE VARCHAR, SIDEWALK VARCHAR, MAXSPEED INTEGER, DIRECTION INTEGER, ZINDEX INTEGER);
""".toString()
if (road) {
//Define the mapping between the values in BDTopo and those used in the abstract model
def road_types =
["Autoroute" : "motorway",
"Type autoroutier" : "motorway",
'Quasi-autoroute' : 'trunk',
'Bretelle' : 'highway_link',
'Route à 2 chaussées': 'primary',
'Route à 1 chaussée' : 'unclassified',
'Route empierrée' : 'track',
'Chemin' : 'track',
'Bac auto' : 'ferry',
'Bac piéton' : 'ferry',
'Piste cyclable' : 'cycleway',
'Sentier' : 'path',
'Escalier' : 'steps',
'Gué ou radier' : null,
'Pont' : 'bridge',
'Tunnel' : 'tunnel',
'NC' : null,
'Rond-point' : 'roundabout',
'Nationale' : 'primary',
'Départementale' : 'secondary'
]

def road_types_width =
["highway" : 8,
"motorway" : 24,
Expand All @@ -323,10 +299,8 @@ String formatRoadLayer(JdbcDataSource datasource, String road, String zone = "")
"ferry" : 0,
"pedestrian" : 3,
"service" : 3]

def queryMapper = "SELECT "
if (datasource.hasTable(road)) {
queryMapper += Geoindicators.DataUtils.aliasColumns(datasource, road, "a", ["THE_GEOM"])
def queryMapper = "SELECT a.ID_SOURCE, a.WIDTH, a.TYPE, a.ZINDEX, a.CROSSING, a.DIRECTION, a.RANK, a.ADMIN_SCALE"
if (zone) {
datasource.createSpatialIndex(road, "the_geom")
queryMapper += ", ST_CollectionExtract(st_intersection(a.the_geom, b.the_geom), 2) as the_geom " +
Expand All @@ -335,7 +309,7 @@ String formatRoadLayer(JdbcDataSource datasource, String road, String zone = "")
"WHERE " +
"a.the_geom && b.the_geom "
} else {
queryMapper += ", the_geom FROM $road as a"
queryMapper += ", a.the_geom FROM $road as a"
}
int rowcount = 1
datasource.withBatch(100) { stmt ->
Expand Down Expand Up @@ -591,9 +565,8 @@ String formatRailsLayer(JdbcDataSource datasource, String rail, String zone = ""
ID_SOURCE VARCHAR, TYPE VARCHAR,CROSSING VARCHAR(30), ZINDEX INTEGER, WIDTH FLOAT, USAGE VARCHAR(30));""".toString()

if (rail) {
def queryMapper = "SELECT "
if (datasource.hasTable(rail)) {
queryMapper += Geoindicators.DataUtils.aliasColumns(datasource, rail, "a", ["THE_GEOM"])
def queryMapper = "SELECT a.ID_SOURCE, a.TYPE, a.ZINDEX, a.CROSSING, a.WIDTH"
if (zone) {
datasource.createSpatialIndex(rail, "the_geom")
queryMapper += ", st_intersection(a.the_geom, b.the_geom) as the_geom " +
Expand All @@ -602,7 +575,7 @@ String formatRailsLayer(JdbcDataSource datasource, String rail, String zone = ""
"WHERE " +
"a.the_geom && b.the_geom "
} else {
queryMapper += ", the_geom FROM $rail as a"
queryMapper += ", a.the_geom FROM $rail as a"

}

Expand Down Expand Up @@ -641,14 +614,8 @@ String formatRailsLayer(JdbcDataSource datasource, String rail, String zone = ""
//1.435 default value for standard gauge
//1 constant for balasting
def rail_width = !row.WIDTH ? 1.435 + 1 : row.WIDTH + 1
def rail_crossing = rail_types.get(row.CROSSING)

def rail_crossing = row.CROSSING
if (rail_crossing) {
rail_crossing = rail_types.get(rail_crossing)
if (!rail_zindex && rail_crossing) {
rail_zindex = 1
}
}
if (rail_zindex >= 0 && rail_type) {
Geometry geom = row.the_geom
if (!geom.isEmpty()) {
Expand Down Expand Up @@ -694,9 +661,8 @@ String formatVegetationLayer(JdbcDataSource datasource, String vegetation, Strin
DROP TABLE IF EXISTS $outputTableName;
CREATE TABLE $outputTableName (THE_GEOM GEOMETRY, id_veget serial, ID_SOURCE VARCHAR, TYPE VARCHAR, HEIGHT_CLASS VARCHAR(4), ZINDEX INTEGER);""".toString()
if (vegetation) {
def queryMapper = "SELECT "
if (datasource.hasTable(vegetation)) {
queryMapper += Geoindicators.DataUtils.aliasColumns(datasource, vegetation, "a", ["THE_GEOM"])
def queryMapper = "SELECT a.ID_SOURCE, a.TYPE, a.ZINDEX"
if (zone) {
datasource.createSpatialIndex(vegetation, "the_geom")
queryMapper += ", st_intersection(a.the_geom, b.the_geom) as the_geom " +
Expand All @@ -705,7 +671,7 @@ String formatVegetationLayer(JdbcDataSource datasource, String vegetation, Strin
"WHERE " +
"a.the_geom && b.the_geom "
} else {
queryMapper += ", the_geom FROM $vegetation as a"
queryMapper += ", a.the_geom FROM $vegetation as a"
}
def vegetation_types = ['Zone arborée' : 'wood',
'Forêt fermée de feuillus' : 'forest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static String aliasColumns(JdbcDataSource datasource, String tableName, String a
* @param exceptColumns
* @return
*/
static String aliasColumns(JdbcDataSource datasource, String tableName, String alias, Collection exceptColumns){
static String aliasColumns(JdbcDataSource datasource, def tableName, def alias, def exceptColumns){
Collection columnNames = datasource.getColumnNames(tableName)
columnNames.removeAll(exceptColumns)
return columnNames.inject([]) { result, iter ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin

datasource "DROP TABLE IF EXISTS $vegetation_tmp, $vegetation_graph, $subGraphTableNodes, $subGraphTableEdges, $subGraphBlocksLow, $subGraphBlocksHigh".toString()

datasource.createIndex(vegetation, "ID")
datasource.createIndex(vegetation, "ID_VEGET")
datasource.createSpatialIndex(vegetation, "THE_GEOM")
datasource.execute """
CREATE TABLE $vegetation_graph (EDGE_ID SERIAL, START_NODE INT, END_NODE INT) AS
SELECT CAST((row_number() over()) as Integer), a.ID as START_NODE, b.ID AS END_NODE
SELECT CAST((row_number() over()) as Integer), a.ID_VEGET as START_NODE, b.ID_VEGET AS END_NODE
FROM $vegetation AS a, $vegetation AS b
WHERE a.ID <>b.ID AND a.the_geom && b.the_geom
WHERE a.ID_VEGET <>b.ID_VEGET AND a.the_geom && b.the_geom
AND ST_INTERSECTS(b.the_geom,a.the_geom);
""".toString()

Expand All @@ -235,14 +235,14 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin
CREATE INDEX ON $subGraphTableNodes (NODE_ID);
CREATE TABLE $subGraphBlocksLow AS SELECT ST_ToMultiLine(ST_UNION(ST_ACCUM(A.THE_GEOM))) AS THE_GEOM
FROM $vegetation A, $subGraphTableNodes B
WHERE a.id=b.NODE_ID AND a.HEIGHT_CLASS= 'low' GROUP BY B.CONNECTED_COMPONENT
WHERE a.ID_VEGET=b.NODE_ID AND a.HEIGHT_CLASS= 'low' GROUP BY B.CONNECTED_COMPONENT
HAVING SUM(st_area(A.THE_GEOM)) >= $surface_vegetation;""".toString()

//Processing high vegetation
datasource """
CREATE TABLE $subGraphBlocksHigh AS SELECT ST_ToMultiLine(ST_UNION(ST_ACCUM(A.THE_GEOM))) AS THE_GEOM
FROM $vegetation A, $subGraphTableNodes B
WHERE a.id=b.NODE_ID AND a.HEIGHT_CLASS= 'high' GROUP BY B.CONNECTED_COMPONENT
WHERE a.ID_VEGET=b.NODE_ID AND a.HEIGHT_CLASS= 'high' GROUP BY B.CONNECTED_COMPONENT
HAVING SUM(st_area(A.THE_GEOM)) >= $surface_vegetation;""".toString()

debug "Creating the vegetation block table..."
Expand All @@ -252,7 +252,7 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin
AS SELECT the_geom FROM $subGraphBlocksLow
UNION ALL SELECT the_geom FROM $subGraphBlocksHigh
UNION ALL SELECT ST_ToMultiLine(a.the_geom) as the_geom FROM $vegetation a
LEFT JOIN $subGraphTableNodes b ON a.id = b.NODE_ID WHERE b.NODE_ID IS NULL
LEFT JOIN $subGraphTableNodes b ON a.ID_VEGET = b.NODE_ID WHERE b.NODE_ID IS NULL
AND st_area(a.the_geom)>=$surface_vegetation;
DROP TABLE $subGraphTableNodes,$subGraphTableEdges, $vegetation_graph, $subGraphBlocksLow, $subGraphBlocksHigh;""".toString()
queryCreateOutputTable += [vegetation_tmp: "(SELECT the_geom FROM $vegetation_tmp)"]
Expand All @@ -272,13 +272,13 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin

datasource "DROP TABLE IF EXISTS $hydrographic_tmp, $water_graph, $subGraphTableNodes, $subGraphTableEdges, $subGraphBlocks".toString()

datasource.createIndex(water, "id")
datasource.createIndex(water, "ID_WATER")
datasource.createSpatialIndex(water, "THE_GEOM")
datasource.execute """
CREATE TABLE $water_graph (EDGE_ID SERIAL, START_NODE INT, END_NODE INT) AS
SELECT CAST((row_number() over()) as Integer), a.id as START_NODE, b.id AS END_NODE
SELECT CAST((row_number() over()) as Integer), a.ID_WATER as START_NODE, b.ID_WATER AS END_NODE
FROM $water AS a, $water AS b
WHERE a.id <>b.id AND a.the_geom && b.the_geom
WHERE a.ID_WATER <>b.ID_WATER AND a.the_geom && b.the_geom
AND ST_INTERSECTS(b.the_geom,a.the_geom) and a.ZINDEX=0;
""".toString()

Expand All @@ -292,14 +292,14 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin
CREATE INDEX ON $subGraphTableNodes (NODE_ID);
CREATE TABLE $subGraphBlocks AS SELECT ST_ToMultiLine(ST_UNION(ST_ACCUM(A.THE_GEOM))) AS THE_GEOM
FROM $water A, $subGraphTableNodes B
WHERE a.id=b.NODE_ID GROUP BY B.CONNECTED_COMPONENT
WHERE a.ID_WATER=b.NODE_ID GROUP BY B.CONNECTED_COMPONENT
HAVING SUM(st_area(A.THE_GEOM)) >= $surface_hydro;""".toString()
debug "Creating the water block table..."
datasource """DROP TABLE IF EXISTS $hydrographic_tmp;
CREATE TABLE $hydrographic_tmp (THE_GEOM GEOMETRY)
AS SELECT the_geom FROM $subGraphBlocks
UNION ALL SELECT ST_ToMultiLine(a.the_geom) as the_geom FROM $water a
LEFT JOIN $subGraphTableNodes b ON a.id = b.NODE_ID WHERE b.NODE_ID IS NULL and
LEFT JOIN $subGraphTableNodes b ON a.ID_WATER = b.NODE_ID WHERE b.NODE_ID IS NULL and
st_area(a.the_geom)>=$surface_hydro;
DROP TABLE $subGraphTableNodes,$subGraphTableEdges, $water_graph, $subGraphBlocks ;""".toString()

Expand All @@ -311,7 +311,7 @@ String prepareTSUData(JdbcDataSource datasource, String zone, String road, Strin
if (road && datasource.hasTable(road)) {
debug "Preparing road..."
if(datasource.getColumnNames(road).size()>0) {
queryCreateOutputTable += [road_tmp: "(SELECT ST_ToMultiLine(THE_GEOM) FROM $road where (zindex=0 or crossing = 'bridge') " +
queryCreateOutputTable += [road_tmp: "(SELECT ST_ToMultiLine(THE_GEOM) FROM $road where (zindex=0 or crossing in ('bridge', 'crossing')) " +
"and type not in ('track','service', 'path', 'cycleway', 'steps'))"]
}
}
Expand Down
Loading

0 comments on commit 1942638

Please sign in to comment.