Skip to content

Commit

Permalink
blm_context project xml augmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
KellyMWhitehead authored and philipbaileynar committed Aug 7, 2024
1 parent 505f831 commit 15feb5a
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 27 deletions.
25 changes: 18 additions & 7 deletions packages/blm_context/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"id": "HUC",
"description": "What HUC?",
// "default": "17070202" // North Fork John Day
"default": "1706030607" // Really small HUC
"default": "1002000107" // Really small HUC
// "default": "17060103" // Asotin
},
{
Expand Down Expand Up @@ -39,19 +39,30 @@
},
{
"name": "BLM Context Metrics",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "blm_context.blm_context_metrics",
"console": "integratedTerminal",
"args": [
"{env:DATA_ROOT}/rs_context/${input:HUC}"
]
},
{
"name": "Augment XML",
"type": "debugpy",
"request": "launch",
"module": "blm_context.blm_context_rs",
"console": "integratedTerminal",
"args": [
"{env:DATA_ROOT}/blm_context/${input:HUC}/project.rs.xml",
"{env:DATA_ROOT}/rs_context/${input:HUC}/project.rs.xml,{env:DATA_ROOT}/blm_context/national/project.rs.xml,{env:DATA_ROOT}/vbet/${input:HUC}/project.rs.xml"
]
},
{
"name": "Write report",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "rscontext.rs_context_report",
"module": "blm_context.blm_context_report",
"console": "integratedTerminal",
"args": [
"{env:DATA_ROOT}/rs_context/${input:HUC}/project.rs.xml",
Expand All @@ -60,7 +71,7 @@
},
{
"name": "Calculate Metrics",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "rscommons.classes.project_metrics",
"console": "integratedTerminal",
Expand All @@ -71,10 +82,10 @@
},
{
"name": "Version Bump",
"type": "python",
"type": "debugpy",
"module": "rscommons.version",
"args": [
"${workspaceFolder}/rscontext/__version__.py"
"${workspaceFolder}/blm_context/__version__.py"
],
"request": "launch",
"console": "integratedTerminal"
Expand Down
21 changes: 8 additions & 13 deletions packages/blm_context/blm_context/blm_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
# Date: 29 Jul 2024
# -------------------------------------------------------------------------------
import argparse
import glob
import json
import os
import sys
import shutil
import time
import traceback
import uuid
from typing import Dict

from osgeo import ogr
Expand All @@ -25,7 +21,7 @@

from rscommons.util import (parse_metadata, pretty_duration, safe_makedirs, safe_remove_dir)
from rscommons.vector_ops import copy_feature_class
from rscommons.augment_lyr_meta import augment_layermeta, add_layer_descriptions, raster_resolution_meta
from rscommons.augment_lyr_meta import augment_layermeta, add_layer_descriptions

from blm_context.__version__ import __version__

Expand All @@ -38,7 +34,6 @@
os.path.dirname(__file__), 'layer_descriptions.json')
LayerTypes = {
# key: (name, id, tag, relpath)
# 'DEM': RSLayer('NED 10m DEM', 'DEM', 'Raster', 'topography/dem.tif'),
'HILLSHADE': RSLayer('DEM Hillshade', 'HILLSHADE', 'Raster', 'topography/dem_hillshade.tif'),
# NHD Geopackage Layers
'NHDPLUSHR': RSLayer('NHD HR Plus', 'NHDPLUSHR', 'Geopackage', 'hydrology/nhdplushr.gpkg', {
Expand Down Expand Up @@ -82,7 +77,7 @@
'USFWS_CRITICAL_HABITAT_A': RSLayer('USFWS Critical Habitat A', 'USFWS_CRITICAL_HABITAT_A', 'Vector', 'USFWS_Critical_Habitat_A')
}),
'USFWS_CRITICAL_HABITAT_L': RSLayer('USFWS Critical Habitat L', 'USFWS_CRITICAL_HABITAT_L', 'Geopackage', 'Habitat_Designations/USFWS_Critical_Habitat_L.gpkg', {
'USFWS_CRITICAL_HABITAT_L': RSLayer('USFWS Critical Habitat L', 'USFWS_CRITICAL_HABITAT_L', 'Vector', 'USFWS_Critical_Habitat_A')
'USFWS_CRITICAL_HABITAT_L': RSLayer('USFWS Critical Habitat L', 'USFWS_CRITICAL_HABITAT_L', 'Vector', 'USFWS_Critical_Habitat_L')
}),
# 'Land Use Planning'
'NIFC_FUEL_POLYS': RSLayer('NIFC Fuel Polys', 'NIFC_FUEL_POLYS', 'Geopackage', 'Land_Use_Planning/NIFC_Fuel_Polys.gpkg', {
Expand Down Expand Up @@ -122,7 +117,7 @@
'BLM_RESTORATION_LANDSCAPES_A': RSLayer('BLM Restoration Landscapes A', 'BLM_RESTORATION_LANDSCAPES_A', 'Geopackage', 'BLM_National_Priority_Areas/BLM_Restoration_Landscapes_A.gpkg', {
'BLM_RESTORATION_LANDSCAPES_A': RSLayer('BLM Restoration Landscapes A', 'BLM_RESTORATION_LANDSCAPES_A', 'Vector', 'BLM_Restoration_Landscapes_A')
}),
'DOI_KEYSTONE_INITIATIVES_A': RSLayer('DOI Keystone Initiatives A', 'DOI_KEYSTONE_INITIATIVES_A', 'Geopackage', 'National_Priority_Areas/DOI_Keystone_Initiatives_A.gpkg', {
'DOI_KEYSTONE_INITIATIVES_A': RSLayer('DOI Keystone Initiatives A', 'DOI_KEYSTONE_INITIATIVES_A', 'Geopackage', 'BLM_National_Priority_Areas/DOI_Keystone_Initiatives_A.gpkg', {
'DOI_KEYSTONE_INITIATIVES_A': RSLayer('DOI Keystone Initiatives A', 'DOI_KEYSTONE_INITIATIVES_A', 'Vector', 'DOI_Keystone_Initiatives_A')
}),
# 'National Landscape Conservation
Expand All @@ -143,7 +138,7 @@ def blm_context(huc: int, blm_context_folder: str, rsc_folder: str, vbet_folder:
log = Logger("BLM Context")

# Add the layer metadata immediately before we write anything
augment_layermeta('rscontext', LYR_DESCRIPTIONS_JSON, LayerTypes)
# augment_layermeta('rscontext', LYR_DESCRIPTIONS_JSON, LayerTypes)

log.info(f'Starting BLM Context v. {__version__}')

Expand All @@ -156,11 +151,11 @@ def blm_context(huc: int, blm_context_folder: str, rsc_folder: str, vbet_folder:

safe_makedirs(output_folder)

project_name = f'Riverscapes Context for HUC {huc}'
project_name = f'BLM Context for HUC {huc}'
project = RSProject(cfg, output_folder)
project.create(project_name, 'RSContext', [
RSMeta('Model Documentation', 'https://tools.riverscapes.net/rscontext',
RSMetaTypes.URL, locked=True),
project.create(project_name, 'BLMContext', [
# RSMeta('Model Documentation', 'https://tools.riverscapes.net/blm_context',
# RSMetaTypes.URL, locked=True),
RSMeta('HUC', str(huc), RSMetaTypes.HIDDEN, locked=True),
RSMeta('Hydrologic Unit Code', str(huc), locked=True)
])
Expand Down
6 changes: 3 additions & 3 deletions packages/blm_context/blm_context/blm_context_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# from rscommons.util import safe_makedirs, sizeof_fmt
# from rscommons.plotting import xyscatter, box_plot

from rscontext.__version__ import __version__
from blm_context.__version__ import __version__


class RSContextReport(RSReport):
class BLMContextReport(RSReport):

def __init__(self, report_path, rs_project, project_root):
super().__init__(rs_project, report_path)
Expand Down Expand Up @@ -38,5 +38,5 @@ def report_intro(self):

cfg = ModelConfig('https://xml.riverscapes.net/Projects/XSD/V2/RiverscapesProject.xsd', __version__)
project = RSProject(cfg, args.projectxml)
report = RSContextReport(args.report_path, project, os.path.dirname(args.projectxml))
report = BLMContextReport(args.report_path, project, os.path.dirname(args.projectxml))
report.write()
113 changes: 113 additions & 0 deletions packages/blm_context/blm_context/blm_context_rs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Augment BLM Context XML with RS Meta data from input projects
"""
import argparse
import traceback
import sys
import os
from rscommons import RSProject, dotenv, Logger
from blm_context.blm_context_report import BLMContextReport

lyrs_in_out = {
# BLM_CONTEXT_ID: INPUT_ID
'NHDFlowline': 'NHDFlowline',
'vw_NHDFlowlineVAA': 'vw_NHDFlowlineVAA',
'vw_NHDPlusCatchmentVAA': 'vw_NHDPlusCatchmentVAA',
'NHDArea': 'NHDArea',
'NHDPlusCatchment': 'NHDPlusCatchment',
'NHDWaterbody': 'NHDWaterbody',
'NHDPlusFlowlineVAA': 'NHDPlusFlowlineVAA',
'WBDHU2': 'WBDHU2',
'WBDHU4': 'WBDHU4',
'WBDHU6': 'WBDHU6',
'WBDHU8': 'WBDHU8',
'WBDHU10': 'WBDHU10',
'WBDHU12': 'WBDHU12',
'buffered_clip100m': 'buffered_clip100m',
'buffered_clip500m': 'buffered_clip500m',
'network_crossings': 'network_crossings',
'network_intersected': 'network_intersected',
'network_segmented': 'network_segmented',
'catchments': 'catchments',
'processing_extent': 'processing_extent',
'NHDAreaSplit': 'NHDAreaSplit',
'NHDWaterbodySplit': 'NHDWaterbodySplit',
'vbet_full': 'vbet_full',
'low_lying_valley_bottom': 'low_lying_valley_bottom',
'low_lying_floodplain': 'low_lying_floodplain',
'elevated_floodplain': 'elevated_floodplain',
'floodplain': 'floodplain',
'vbet_centerlines': 'vbet_centerlines',
'vbet_igos': 'vbet_igos',
'HILLSHADE': 'HILLSHADE',
'USFWS_Critical_Habitat_A': 'USFWS_Critical_Habitat_A',
'USFWS_Critical_Habitat_L': 'USFWS_Critical_Habitat_L',
'NIFC_Fuel_Polys': 'NIFC_Fuel_Polys',
'BLM_Natl_Fire_Perimeters_P': 'BLM_Natl_Fire_Perimeters_P',
'BLM_Natl_Visual_Resource_Inventory_Classes_Polygon_A': 'BLM_Natl_Visual_Resource_Inventory_Classes_Polygon_A',
'BLM_Natl_Area_Critical_Env_Concern_A': 'BLM_Natl_Area_Critical_Env_Concern_A',
'BLM_Natl_Wild_Horse_and_Burro_Herd_Mgmt_Area_A': 'BLM_Natl_Wild_Horse_and_Burro_Herd_Mgmt_Area_A',
'BLM_Natl_Grazing_Allotment_P': 'BLM_Natl_Grazing_Allotment_P',
'BLM_Natl_WesternUS_GRSG_ROD_Habitat_Mgmt_Areas_Aug22_A"': 'BLM_Natl_WesternUS_GRSG_ROD_Habitat_Mgmt_Areas_Aug22_A"',
'BLM_Natl_Land_Use_Plans_2022_A': 'BLM_Natl_Land_Use_Plans_2022_A',
'BLM_Natl_Revision_Development_Land_Use_Plans_A': 'BLM_Natl_Revision_Development_Land_Use_Plans_A',
'BLM_ES_SO_Natl_Scenic_Historic_Trails_NLCS_L': 'BLM_ES_SO_Natl_Scenic_Historic_Trails_NLCS_L',
'BLM_Natl_Recreation_Site_Polygons': 'BLM_Natl_Recreation_Site_Polygons',
'BLM_Restoration_Landscapes_A': 'BLM_Restoration_Landscapes_A',
'DOI_Keystone_Initiatives_A': 'DOI_Keystone_Initiatives_A',
'BLM_Natl_NLCS_Wilderness_Areas_A': 'BLM_Natl_NLCS_Wilderness_Areas_A',
'BLM_Natl_NLCS_Wilderness_Study_Areas_A': 'BLM_Natl_NLCS_Wilderness_Study_Areas_A',
'BLM_NLCS_Natl_Monuments_Cons_Areas_A': 'BLM_NLCS_Natl_Monuments_Cons_Areas_A'
}


def main():
"""Augment BLM Context XML with RS Meta data from input projects
"""

parser = argparse.ArgumentParser(
description='BLM Context XML Augmenter',
# epilog="This is an epilog"
)
parser.add_argument('out_project_xml', help='Input XML file', type=str)
parser.add_argument('in_xmls', help='Comma-separated list of XMLs in decreasing priority', type=str)
parser.add_argument('--verbose', help='(optional) a little extra logging ', action='store_true', default=False)

args = dotenv.parse_args_env(parser)

# Initiate the log file
log = Logger('XML Augmenter')
log.setup(verbose=args.verbose)
log.title(f'XML Augmenter: {args.out_project_xml}')

try:
out_prj = RSProject(None, args.out_project_xml)
out_prj.rs_meta_augment(
args.in_xmls.split(','),
lyrs_in_out
)

in_xml = args.in_xmls.split(',')[0]
out_prj.rs_copy_project_extents(in_xml)

# if watershed in meta, change the project name
watershed_node = out_prj.XMLBuilder.find('MetaData').find('Meta[@name="Watershed"]')
if watershed_node is not None:
name_node = out_prj.XMLBuilder.find('Name')
name_node.text = f"BLM Context for {watershed_node.text}"

out_prj.XMLBuilder.write()
# report_path = out_prj.XMLBuilder.find('.//HTMLFile[@id="REPORT"]/Path').text
# report = BLMContextReport(os.path.join(out_prj.project_dir, report_path), out_prj)
# report.write()

except Exception as err:
log.error(err)
traceback.print_exc(file=sys.stdout)
sys.exit(1)

sys.exit(0)


if __name__ == '__main__':
main()
8 changes: 4 additions & 4 deletions scripts/automation/FargateBLMContext.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ try() {
cd /usr/local/src/riverscapes-tools/packages/blm_context
python3 -m blm_context.blm_context_rs \
$BLM_CONTEXT_DIR/project.rs.xml \
$NATIONAL_BLM_CONTEXT_DIR/project.rs.xml,$RS_CONTEXT_DIR/project.rs.xml,$VBET_DIR/project.rs.xml
$RS_CONTEXT_DIR/project.rs.xml,$NATIONAL_BLM_CONTEXT_DIR/project.rs.xml,$VBET_DIR/project.rs.xml

python3 -m blm_context.blm_context_metrics \
$VBET_DIR \
$BLM_CONTEXT_DIR
# python3 -m blm_context.blm_context_metrics \
# $VBET_DIR \
# $BLM_CONTEXT_DIR

echo "====================== Final Disk space usage ======================="
df -h
Expand Down

0 comments on commit 15feb5a

Please sign in to comment.