Skip to content

Commit

Permalink
initial commit as place to keep project creation bash scripts for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
NewGraphEnvironment committed Feb 21, 2024
1 parent 9bc9e0f commit f4dcb66
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 0 deletions.
45 changes: 45 additions & 0 deletions dev/rfp_source_bcdata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Load bcdata layers to a geopackage.
#'
#' This function loads bcdata layers to a `background_layers.gpkg` with the specified watershed groups. The update
#' option can be set to update an existing geopackage if it exists.
#'
#' @param watershed_groups A character vector of watershed group codes. Double quoted list of single quoted values. Default is NULL.
#' @param update A logical indicating whether to update the GeoPackage file. Default is FALSE.
#'
#' @return Invisible NULL. The function is called for its side effects.
#' @importFrom reticulate use_condaenv
#' @export
#'
#' @examples
#' \dontrun{
#' rfp_source_bcdata(watershed_groups = c("ADMS", "BULK"))
#' }
rfp_source_bcdata <- function(watershed_groups = NULL, update = FALSE) {
# Construct the path to the script within the installed package
scriptPath <- system.file("extdata", "rfp_source_bcdata.sh", package = "rfp")

# Check if the script exists to avoid errors
if(!file.exists(scriptPath)) {
stop("Script 'rfp_source_bcdata.sh' not found in the package.")
}

if(update){
update <- "update"
}else{
update <- ""
}

# Convert watershed_groups to a single, comma-separated string
watershed_groups <- paste(shQuote(watershed_groups, type = "sh"), collapse = " ")

# Prepare the arguments for the script
args <- c(watershed_groups, update)

# Call the bash script with arguments
system2("bash", args = c(scriptPath, watershed_groups, update))

invisible(NULL)
}



27 changes: 27 additions & 0 deletions inst/extdata/rfp_qgis_create_bcfishpass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -euxo pipefail


# check that the name of the new directory for the project is provided as argument
if [ $# -eq 0 ]
then
echo "No arguments supplied - provide name of new project directory to be placed in ~Projects/gis/"
exit 1
fi

echo 'Generating QGIS project in Projects/gis/: '$1


# ---------------
# create directory for project and move the files in and copy in a qlr template (works relative to location dragged from when we reference gpkg) along with the directories for info ignored on phones

mkdir ~/Projects/gis/$1
mv background_layers.gpkg* ~/Projects/gis/$1/
mv habitat_lateral.tif ~/Projects/gis/$1/
cp ../../data/qgis/bcfishpass_mobile.qgs ~/Projects/gis/$1/$1.qgs
cp ../../data/qgis/form_pscis.gpkg ~/Projects/gis/$1/
cp ../../data/qgis/form_fiss_site.gpkg ~/Projects/gis/$1/
mkdir -p ~/Projects/gis/$1/ignore_mobile/photos
touch ~/Projects/gis/$1/ignore_mobile/photos/photos.txt
cp -R ../../data/methods ~/Projects/gis/$1/ignore_mobile/methods
cp -R ../../data/templates ~/Projects/gis/$1/ignore_mobile/templates
141 changes: 141 additions & 0 deletions inst/extdata/rfp_source_aws.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash
set -euxo pipefail

# ---------------
# extract layers from aws to load to background_layers.gpkg within digital field projects

# usage: specify watershed groups of interest as a comma separated, single quoted string
# eg
# test with ./rfp_sources_aws.sh "'PARS'" &>log.txt
# ---------------


# check that watershed group code is provided as argument
if [ $# -eq 0 ]
then
echo "No arguments supplied - provide list of watershed_group_code values for watersheds of interest"
exit 1
fi


# Name of the file
GPKG="background_layers.gpkg"

# name the sources file
SOURCES="rfp_source_aws.txt"

# Check if the GPKG IS NOT there
if [[ ! -f $GPKG ]]; then
# If the file exists, ask the user if they wish to remove it
echo "There should already be a file called $GPKG. Please provide."
exit 1
else
echo 'Updateing project area from watershed group boundaries: '$1
rm -f aoi.geojson
bcdata dump WHSE_BASEMAPPING.FWA_WATERSHED_GROUPS_POLY \
--query "WATERSHED_GROUP_CODE in ($1)" -l > aoi.geojson
ogr2ogr -f GPKG background_layers.gpkg \
-update \
-overwrite \
-t_srs EPSG:3005 \
-nln fwa_watershed_groups_poly \
aoi.geojson
fi

# get bounding box of project area in BC Albers and WGS84 (lon/lat)
BOUNDS=$(fio info background_layers.gpkg --layer fwa_watershed_groups_poly --bounds)
BOUNDS_LL=$(echo "[$BOUNDS]" | tr ' ', ',' | rio transform --src_crs EPSG:3005 --dst_crs EPSG:4326 | tr -d '[] ')


# ---------------
# lateral habitat
# ---------------
# should be able to pipe $(fio cat aoi.geojson --dst_crs EPSG:3005) to rio mask for a one liner,
# but this works fine
echo 'Clipping lateral habitat tiff to project area'
ogr2ogr -f GeoJSON aoi_alb.geojson -t_srs EPSG:3005 aoi.geojson

rio mask /vsicurl/https://bcfishpass.s3.us-west-2.amazonaws.com/habitat_lateral.tif \
habitat_lateral.tif \
--crop \
--geojson-mask aoi_alb.geojson

rm aoi_alb.geojson

# ---------------
# bcfishpass releated data sources archived to flatgeobuf on s3 for fast retrieval
# (and with watershed group code included, no spatial query for clipping required)
# ---------------
echo 'Getting bcfishpass and supporting layers from s3'

# get a list of the aws layers to update
FGB_SOURCES=$(grep -v '^#' $SOURCES)

for layer in $FGB_SOURCES; do
ogr2ogr \
-f GPKG background_layers.gpkg \
-update \
-overwrite \
-nln $layer \
-t_srs EPSG:3005 \
-dim XY \
-spat $BOUNDS \
-spat_srs EPSG:3005 \
-where "watershed_group_code in ($1)" \
/vsicurl/https://newgraph.s3.us-west-2.amazonaws.com/$layer.fgb \
$layer
done

# ---------------
# model parameters
# ---------------
echo 'Getting model parameter info from aws'

# update the model info used to create the crossings and streams layers
CSVS=("parameters_habitat_method" "parameters_habitat_thresholds")

for layer in "${CSVS[@]}"; do
ogr2ogr \
-f GPKG background_layers.gpkg \
-update \
-overwrite \
-nln $layer \
/vsicurl/https://newgraph.s3.us-west-2.amazonaws.com/$layer.csv
done

# ---------------
# named streams
# ---------------
echo 'Getting named streams from fwapg feature service'
ogr2ogr -f GPKG background_layers.gpkg \
-update \
-overwrite \
-t_srs EPSG:3005 \
-nln fwa_named_streams \
-clipsrc aoi.geojson \
-clipsrclayer aoi \
"https://features.hillcrestgeo.ca/fwa/collections/whse_basemapping.fwa_named_streams/items.json?bbox=$BOUNDS_LL"
# "http://www.a11s.one:9000/collections/whse_basemapping.fwa_named_streams/items.json?bbox=$BOUNDS_LL"


echo 'Creating a record of the layers that were loaded'
# create a record of the layers that were loaded
# Get the current date and time
TIMESTAMP=$(date '+%Y-%m-%d %H:%M')

# Header for the CSV file
echo "timestamp,content,watershed_groups,source" > temp.csv

# Use awk to process the text file line by line
awk -v timestamp="$TIMESTAMP" -v watershed_groups="$1" -v source="$SOURCES" '!/^#/ && NF > 0 {print timestamp "," $0 "," watershed_groups "," source}' $SOURCES >> temp.csv

# Append the temporary CSV file to the GeoPackage
ogr2ogr -append -f "GPKG" $GPKG temp.csv -nln rfp_tracking

# Remove the temporary CSV file
rm temp.csv





Loading

0 comments on commit f4dcb66

Please sign in to comment.