Skip to content

Commit

Permalink
2024 Fuel Grid (#3606)
Browse files Browse the repository at this point in the history
- Updates to 2024 fuel grid
- polygonize excludes NoData values
  • Loading branch information
brettedw authored May 10, 2024
1 parent db46b70 commit 4933335
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/alembic/versions/cb626d81dec9_load_fuel_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

def upgrade():
# Iterate through the fuel types and insert them.
for fuel_type_id, geom in fuel_type_iterator():
for fuel_type_id, geom in fuel_type_iterator('fbp2021.tif'):
statement = fuel_type_table.insert().values(fuel_type_id=fuel_type_id, geom=geom)
op.execute(statement)

Expand Down
48 changes: 48 additions & 0 deletions api/alembic/versions/f9ab4e6a7eeb_2024_fuel_grid_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""2024 fuel grid update
Revision ID: f9ab4e6a7eeb
Revises: 2b3755392ad8
Create Date: 2024-05-08 10:25:04.121562
"""
from alembic import op
import sqlalchemy as sa
from app.db.models.auto_spatial_advisory import SFMSFuelType, FuelType
from app.auto_spatial_advisory.fuel_type_layer import fuel_type_iterator

# revision identifiers, used by Alembic.
revision = 'f9ab4e6a7eeb'
down_revision = '2b3755392ad8'
branch_labels = None
depends_on = None

m3_fuel_type = {
'fuel_type_id': 13,
'fuel_type_code': 'M-3',
'description': 'Dead Balsam Fir Mixedwood'
}

def upgrade():
# add missing fuel type to SFMSFuelType table
stmt = sa.insert(SFMSFuelType).values(fuel_type_id=m3_fuel_type['fuel_type_id'], fuel_type_code=m3_fuel_type['fuel_type_code'], description=m3_fuel_type['description'])
op.execute(stmt)

# Delete all the fuel types.
op.execute(sa.delete(FuelType))

# Iterate through the fuel types and insert them.
for fuel_type_id, geom in fuel_type_iterator('fbp2024.tif'):
statement = sa.insert(FuelType).values(fuel_type_id=fuel_type_id, geom=geom)
op.execute(statement)


def downgrade():
stmt = sa.delete(SFMSFuelType).where(SFMSFuelType.fuel_type_id == m3_fuel_type['fuel_type_id'])
op.execute(stmt)

# Delete all the fuel types.
op.execute(sa.delete(FuelType))

for fuel_type_id, geom in fuel_type_iterator('fbp2021.tif'):
statement = sa.insert(FuelType).values(fuel_type_id=fuel_type_id, geom=geom)
op.execute(statement)
6 changes: 3 additions & 3 deletions api/app/auto_spatial_advisory/fuel_type_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
logger = logging.getLogger(__name__)


def fuel_type_iterator() -> Generator[Tuple[int, str], None, None]:
def fuel_type_iterator(fuel_grid_filename: str) -> Generator[Tuple[int, str], None, None]:
"""
Yields fuel type id and geom by polygonizing fuel type layer raster stored in S3, and then
iterating over feature from the resultant layer.
Expand All @@ -31,7 +31,7 @@ def fuel_type_iterator() -> Generator[Tuple[int, str], None, None]:
bucket = config.get('OBJECT_STORE_BUCKET')
# Hard coded for a geotiff on our S3 server, but this could be replaced by any raster file
# that gdal is able to read.
filename = f'/vsis3/{bucket}/sfms/static/fbp2021.tif'
filename = f'/vsis3/{bucket}/sfms/static/{fuel_grid_filename}'
logger.info('Polygonizing %s...', filename)
with polygonize_in_memory(filename, 'fuel', 'fuel') as layer:

Expand Down Expand Up @@ -61,7 +61,7 @@ async def inject_ftl_into_database():
"""
logger.info('save to database')
async with get_async_write_session_scope() as session:
for fuel_type_id, geom in fuel_type_iterator():
for fuel_type_id, geom in fuel_type_iterator('fbp2024.tif'):
fuel_type = FuelType(fuel_type_id=fuel_type_id, geom=geom)
await save_fuel_type(session, fuel_type)

Expand Down
3 changes: 2 additions & 1 deletion api/app/utils/polygonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def polygonize_in_memory(geotiff_filename, layer, field) -> ogr.Layer:
source: gdal.Dataset = gdal.Open(geotiff_filename, gdal.GA_ReadOnly)

source_band = source.GetRasterBand(1)
nodata_value = source_band.GetNoDataValue()
source_data = source.ReadAsArray()
# https://gdal.org/api/python/osgeo.osr.html#osgeo.osr.SpatialReference
spatial_reference: osr.SpatialReference = source.GetSpatialRef()

# generate mask data
mask_data = np.where(source_data == 0, False, True)
mask_data = np.where(source_data == nodata_value, False, True)
mask_ds, mask_band = _create_in_memory_band(
mask_data, source_band.XSize, source_band.YSize, source.GetProjection(),
source.GetGeoTransform())
Expand Down

0 comments on commit 4933335

Please sign in to comment.