Skip to content

Commit

Permalink
Add get_green_areas
Browse files Browse the repository at this point in the history
  • Loading branch information
fmcclean committed May 5, 2021
1 parent faaf5d1 commit a7fd596
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
32 changes: 29 additions & 3 deletions citycatpg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import os
import subprocess
import warnings
from typing import Tuple


@dataclass
Expand Down Expand Up @@ -159,19 +158,25 @@ def get_model(self, con: connection, open_boundaries: bool = True):
else:
open_boundaries = None

if self.buildings_table:
if self.buildings_table is not None:
buildings = self.get_buildings(con)
else:
buildings = None

if self.green_areas_table is not None:
green_areas = self.get_green_areas(con)
else:
green_areas = None

self.model = Model(
dem=self.get_dem(con),
rainfall=rainfall,
rainfall_polygons=rainfall_polygons,
duration=self.run_duration,
output_interval=self.output_frequency,
open_boundaries=open_boundaries,
buildings=buildings
buildings=buildings,
green_areas=green_areas
)

def get_dem(self, con: connection):
Expand Down Expand Up @@ -296,6 +301,27 @@ def get_buildings(self, con: connection):
domain_table=sql.Identifier(self.domain_table),
).as_string(con), con=con)

def get_green_areas(self, con: connection):
"""Get green areas from postgres
Args:
con: Postgres connection
Returns:
geopandas.GeoDataFrame: Buildings polygons
"""

return gpd.GeoDataFrame.from_postgis(sql.SQL("""
SELECT {green_areas_table}.geom
FROM {green_areas_table}, {domain_table}
WHERE ST_Intersects({green_areas_table}.geom, {domain_table}.geom)
AND {domain_table}.gid={domain_id}
""").format(
domain_id=sql.Literal(self.domain_id),
green_areas_table=sql.Identifier(self.green_areas_table),
domain_table=sql.Identifier(self.domain_table),
).as_string(con), con=con)

def execute(self, run_path: str, out_path: str):
"""Execute model using current configuration
Expand Down
18 changes: 12 additions & 6 deletions tests/setup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

dem_file.seek(0)

r = Run(100, rain_table='rain', buildings_table='buildings')
r = Run(100, rain_table='rain', buildings_table='buildings', green_areas_table='green_areas')

geom = Polygon([[x_min, y_min], [x_min, y_max], [x_max / 2, y_max / 2], [x_max, y_min], [x_min, y_min]])

with con:
with con.cursor() as cursor:
Expand Down Expand Up @@ -60,16 +62,20 @@
DROP TABLE IF EXISTS {buildings_table};
CREATE TABLE {buildings_table} (gid serial PRIMARY KEY, geom geometry);
INSERT INTO {buildings_table} (gid, geom) VALUES (500, ST_GeomFromText(%(buildings)s, 27700));
DROP TABLE IF EXISTS {green_areas_table};
CREATE TABLE {green_areas_table} (gid serial PRIMARY KEY, geom geometry);
INSERT INTO {green_areas_table} (gid, geom) VALUES (500, ST_GeomFromText(%(green_areas)s, 27700));
""").format(
domain_table=sql.Identifier(r.domain_table),
dem_table=sql.Identifier(r.dem_table),
rain_table=sql.Identifier(r.rain_table),
metadata_table=sql.Identifier(r.metadata_table),
buildings_table=sql.Identifier(r.buildings_table)
buildings_table=sql.Identifier(r.buildings_table),
green_areas_table=sql.Identifier(r.green_areas_table)
),
dict(
geom=str(Polygon(
[[x_min, y_min], [x_min, y_max], [x_max / 2, y_max / 2], [x_max, y_min], [x_min, y_min]])),
buildings=str(Polygon(
[[x_min, y_min], [x_min, y_max], [x_max / 2, y_max / 2], [x_max, y_min], [x_min, y_min]]).buffer(-70)),
geom=str(geom),
buildings=str(geom.buffer(-70)),
green_areas=str(geom.buffer(-100)),
rast=psycopg2.Binary(dem_file.read())))
4 changes: 2 additions & 2 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_add_run(self):
with con.cursor() as cur:
cur.execute('DROP TABLE IF EXISTS runs')
run = Run(run_duration=120, rain_total=100, rain_duration=120, run_name='test',
output_frequency=60, domain_id=500, buildings_table='buildings')
output_frequency=60, domain_id=500, buildings_table='buildings', green_areas_table='green_areas')
run.add(con)
return run

Expand All @@ -35,7 +35,7 @@ def test_generate_rainfall(self):
def test_get_model(self):
run = Run(100, rain_table='rain',
rain_start=datetime.datetime(2000, 1, 1), rain_end=datetime.datetime(2000, 1, 2), domain_id=500,
buildings_table='buildings')
buildings_table='buildings', green_areas_table='green_areas')

run.get_model(con)
run.model.write('tests/test_model')
Expand Down

0 comments on commit a7fd596

Please sign in to comment.