-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1bc9ac0
Showing
7 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "citycatio"] | ||
path = citycatio | ||
branch = citycatio | ||
url = https://github.com/nclwater/citycat-output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Citycatpg | ||
|
||
Citycatpg is a Python package that runs CityCAT models from a PostgreSQL database. | ||
|
||
## Tests | ||
`python -m unittests` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import read_postgis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from citycatio import Model | ||
import rasterio as rio | ||
import pandas as pd | ||
|
||
|
||
def read_postgis(con, domain_table='domain', domain_id=1, rainfall_table='rainfall', dem_table='dem'): | ||
with con.cursor() as cursor: | ||
cursor.execute(""" | ||
SELECT ST_AsGDALRaster(ST_Union(ST_Clip(rast, geom)), 'GTiff') | ||
FROM {}, {} WHERE ST_Intersects(rast, geom) and gid={} | ||
""".format(dem_table, domain_table, domain_id)) | ||
|
||
dem = rio.MemoryFile(cursor.fetchone()[0].tobytes()) | ||
|
||
return Model(dem=dem, rainfall=pd.DataFrame([0])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
channels: | ||
- conda-forge | ||
|
||
dependencies: | ||
- geopandas | ||
- rasterio | ||
- netCDF4 | ||
- psycopg2 | ||
- sqlalchemy |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
from shapely.geometry import Polygon | ||
from citycatpg import read_postgis | ||
import rasterio as rio | ||
from rasterio.transform import Affine | ||
import numpy as np | ||
import psycopg2 | ||
|
||
dem_file = rio.MemoryFile() | ||
x_min, y_max = 100, 500 | ||
res = 5 | ||
height, width = 100, 200 | ||
x_max = x_min + width * res | ||
y_min = y_max - height * res | ||
array = np.round(np.random.random((height, width)), 3) | ||
transform = Affine.translation(x_min, y_max) * Affine.scale(res, -res) | ||
|
||
with rio.open( | ||
dem_file, | ||
'w', | ||
driver='GTiff', | ||
height=height, | ||
width=width, | ||
count=1, | ||
dtype=array.dtype, | ||
transform=transform, | ||
nodata=-9999 | ||
) as dst: | ||
dst.write(array, 1) | ||
|
||
|
||
class TestReadPostgis(unittest.TestCase): | ||
def test_read_postgis(self): | ||
con = psycopg2.connect(database='postgres', user='postgres', password='password', host='localhost') | ||
dem_file.seek(0) | ||
with con.cursor() as cursor: | ||
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis_raster") | ||
cursor.execute("SET postgis.gdal_enabled_drivers TO 'GTiff'") | ||
|
||
cursor.execute("DROP TABLE IF EXISTS domain") | ||
cursor.execute("CREATE TABLE domain (gid serial PRIMARY KEY, geom geometry)") | ||
cursor.execute("INSERT INTO domain(geom) VALUES (ST_GeomFromText('{}'))".format( | ||
Polygon([[x_min, y_min], [x_min, y_max], [x_max/2, y_max/2], [x_max, y_min], [x_min, y_min]]))) | ||
|
||
cursor.execute("DROP TABLE IF EXISTS dem") | ||
cursor.execute("CREATE TABLE dem (rast raster)") | ||
cursor.execute("INSERT INTO dem(rast) VALUES (ST_FromGDALRaster({}))".format(psycopg2.Binary(dem_file.read()))) | ||
|
||
cursor.execute("DROP TABLE IF EXISTS rainfall") | ||
|
||
model = read_postgis(con) | ||
model.write('tests/test_model') |