Skip to content

Commit

Permalink
Imports zone units to advisory_shapes table (#3183)
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad authored Oct 25, 2023
1 parent ba0f5f9 commit 5d18f97
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
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 ###
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')
1 change: 1 addition & 0 deletions api/app/db/models/auto_spatial_advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ShapeTypeEnum(enum.Enum):
"Incident"/"Fire", "Custom" etc. etc. """
fire_centre = 1
fire_zone = 2
fire_zone_unit = 3


class RunTypeEnum(enum.Enum):
Expand Down
25 changes: 25 additions & 0 deletions api/app/utils/zone_units.py
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

0 comments on commit 5d18f97

Please sign in to comment.