-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imports zone units to advisory_shapes table (#3183)
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
api/alembic/versions/505a3f03ba75_add_fire_zone_units_to_advisory_shapes.py
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,71 @@ | ||
"""add fire zone units to advisory shapes | ||
Revision ID: 505a3f03ba75 | ||
Revises: 8a05bc230ad7 | ||
Create Date: 2023-10-24 12:41:11.578847 | ||
""" | ||
import json | ||
from alembic import op | ||
import geoalchemy2 | ||
import sqlalchemy as sa | ||
from shapely import from_geojson | ||
from app.utils.zone_units import get_zone_units_geojson | ||
from sqlalchemy.orm.session import Session | ||
from sqlalchemy.dialects.postgresql import insert | ||
from shapely import wkb | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '505a3f03ba75' | ||
down_revision = '8a05bc230ad7' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
shape_type_table = sa.Table('advisory_shape_types', sa.MetaData(), | ||
sa.Column('id', sa.Integer), | ||
sa.Column('name', sa.String)) | ||
|
||
shape_table = sa.Table('advisory_shapes', sa.MetaData(), | ||
sa.Column('id', sa.Integer), | ||
sa.Column('source_identifier', sa.String), | ||
sa.Column('shape_type', sa.Integer), | ||
sa.Column('geom', geoalchemy2.Geometry)) | ||
|
||
|
||
def upgrade(): | ||
session = Session(bind=op.get_bind()) | ||
|
||
statement = shape_type_table.insert().values(name='fire_zone_unit').returning(shape_type_table.c.id) | ||
result = session.execute(statement).fetchone() | ||
shape_type_id = result.id | ||
|
||
fire_zone_units = get_zone_units_geojson() | ||
for feature in fire_zone_units.get('features', []): | ||
properties = feature.get('properties', {}) | ||
# Each zone unit is uniquely identified by an OBJECTID. | ||
object_id = properties.get('OBJECTID') | ||
geometry = feature.get('geometry', {}) | ||
geom = from_geojson(json.dumps(geometry)) | ||
|
||
insert_statement = insert(shape_table).values( | ||
source_identifier=object_id, | ||
shape_type=shape_type_id, | ||
geom=wkb.dumps(geom, hex=True, srid=3005)) | ||
stmt = insert_statement.on_conflict_do_nothing( | ||
constraint="advisory_shapes_source_identifier_shape_type_key", | ||
) | ||
session.execute(stmt) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic ### | ||
session = Session(bind=op.get_bind()) | ||
statement = shape_type_table.select().where(shape_type_table.c.name == 'fire_zone_unit') | ||
result = session.execute(statement).fetchone() | ||
shape_type_id = result.id | ||
|
||
session.execute(shape_table.delete().where(shape_table.c.shape_type == shape_type_id)) | ||
session.execute(shape_type_table.delete().where(shape_type_table.c.name == 'fire_zone_unit')) | ||
# ### end Alembic commands ### |
43 changes: 43 additions & 0 deletions
43
api/alembic/versions/8a05bc230ad7_add_fire_zone_unit_to_shapetypeenum.py
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,43 @@ | ||
"""add fire zone unit to shapetypeenum | ||
Revision ID: 8a05bc230ad7 | ||
Revises: 2442f07d975c | ||
Create Date: 2023-10-24 11:04:30.622497 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '8a05bc230ad7' | ||
down_revision = '2442f07d975c' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
shape_type_table = sa.Table('advisory_shape_types', sa.MetaData(), | ||
sa.Column('id', sa.Integer), | ||
sa.Column('name', sa.String)) | ||
|
||
|
||
def upgrade(): | ||
# Create a new enum type | ||
shape_type_new = sa.Enum('fire_centre', 'fire_zone', 'fire_zone_unit', name='shapetypeenum_2', create_type=False) | ||
conn = op.get_bind() | ||
|
||
shape_type_new.create(bind=conn, checkfirst=True) | ||
|
||
# Alter the column to use the new enum type | ||
op.execute('ALTER TABLE advisory_shape_types ALTER COLUMN name TYPE shapetypeenum_2 USING name::text::shapetypeenum_2') | ||
op.execute('DROP TYPE shapetypeenum') | ||
|
||
|
||
def downgrade(): | ||
conn = op.get_bind() | ||
# Recreate old new enum type | ||
shape_type_old = sa.Enum('fire_centre', 'fire_zone', name='shapetypeenum', create_type=False) | ||
|
||
shape_type_old.create(bind=conn, checkfirst=True) | ||
|
||
# Alter the column to use the old enum type | ||
op.execute('ALTER TABLE advisory_shape_types ALTER COLUMN name TYPE shapetypeenum USING name::text::shapetypeenum') | ||
op.execute('DROP TYPE shapetypeenum_2') |
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
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,25 @@ | ||
|
||
import json | ||
from app import config | ||
from botocore.session import get_session | ||
|
||
|
||
def get_zone_units_geojson(): | ||
""" | ||
Fetches fire zone units geojson from S3 storage. | ||
""" | ||
|
||
server = config.get('OBJECT_STORE_SERVER') | ||
user_id = config.get('OBJECT_STORE_USER_ID') | ||
secret_key = config.get('OBJECT_STORE_SECRET') | ||
bucket = config.get('OBJECT_STORE_BUCKET') | ||
session = get_session() | ||
# Create an S3 client | ||
client = session.create_client('s3', | ||
endpoint_url=f'https://{server}', | ||
aws_secret_access_key=secret_key, | ||
aws_access_key_id=user_id) | ||
|
||
fire_zone_units = client.get_object(Bucket=bucket, Key='zone-units/fire_zone_units.geojson') | ||
data = json.loads(fire_zone_units['Body'].read().decode('utf-8')) | ||
return data |