diff --git a/src/FACT_bldgs.jl b/src/FACT_bldgs.jl index 6b76ac0aef98cea6f8727efe0774e9f7907ffe09..32a2f687441ac9ca651b1368612af9c9d3c1bf8c 100644 --- a/src/FACT_bldgs.jl +++ b/src/FACT_bldgs.jl @@ -29,8 +29,8 @@ function footprint_avg_height(conn_dict::Dict, selection::Dict) for (id, shape) in zip(geo_id, shape_obj) # prepare query # given the CRS of the shape, the ST_Area will return squared degrees, hence here the max_footptin and avg_footprint are in millions of squared degrees. - query = "SELECT IF(ST_Area(ST_GeomFromText('"*shape*"')) = 0, 0, SUM(ST_Area(SHAPE))/ST_Area(ST_GeomFromText('"*shape*"'))) as perc_footprint, MAX(ST_Area(SHAPE))*1000000 as max_footprint, AVG(ST_Area(SHAPE))*1000000 as avg_footprint FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" - query2 = "SELECT CASE WHEN SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) = 0 THEN NULL ELSE SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN JSON_EXTRACT(properties, '\$.height') * ST_Area(SHAPE) ELSE 0 END) / SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) END as avg_height FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" + query = "SELECT IF(ST_Area(ST_GeomFromText('"*shape*"')) = 0, 0, SUM(ST_Area(SHAPE))/ST_Area(ST_GeomFromText('"*shape*"'))) as perc_footprint, MAX(ST_Area(SHAPE))*1000000 as max_footprint, AVG(ST_Area(SHAPE))*1000000 as avg_footprint FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE) AND part_id = LEFT('"*id*"', 2);" + query2 = "SELECT CASE WHEN SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) = 0 THEN NULL ELSE SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN JSON_EXTRACT(properties, '\$.height') * ST_Area(SHAPE) ELSE 0 END) / SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) END as avg_height FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE) AND part_id = LEFT('"*id*"', 2);" # create connection conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute