Commit 1831ed36 authored by Biersteker, Levi's avatar Biersteker, Levi
Browse files

added buffer raster function

parent ecaee21d
......@@ -148,7 +148,6 @@ def colrow2xy(colrow, rio_object=None, affine_in=None):
return x, y
def progress_bar(iterable, prefix='', suffix='', decimals=1, length=100, fill='#', printEnd="\r"):
"""
Call in a loop to create terminal progress bar
......@@ -230,4 +229,3 @@ def file_of_type(x, ext):
raise argparse.ArgumentTypeError('{} is not a file'.format(x))
if not ext_match:
raise argparse.ArgumentTypeError('{} is not a correct file'.format(x))
import rasterio as rio
import numpy as np
import os
def buffer_raster(raster_path: str, crs, padding: int = 100):
"""
Parameters
----------
raster_path : str
filepath for raster to buffer
crs
crs specification
padding : int
amount of cells to buffer around raster
Returns
-------
buffered raster : rasterio.io.DatasetReader
"""
raster = rio.open(raster_path)
raster_buffered = raster.read(1)
for i in range(padding):
raster_buffered = np.insert(raster_buffered, [0], -9999, axis=1)
raster_buffered = np.insert(raster_buffered, [0], -9999, axis=0)
raster_buffered = np.insert(raster_buffered, [-1], -9999, axis=1)
raster_buffered = np.insert(raster_buffered, [-1], -9999, axis=0)
raster_buffered = np.where(raster_buffered == -9999, np.nan, raster_buffered)
origin_x, origin_y = raster.xy(-padding, -padding)
outputpath = os.path.splitext(raster_path)[0] + ".tif"
with rio.open(
outputpath,
"w",
driver="GTiff",
height=raster_buffered.shape[0],
width=raster_buffered.shape[1],
count=1,
dtype=raster_buffered.dtype,
crs=crs,
transform=rio.transform.from_origin(origin_x - 12.5, origin_y + 12.5, 25, 25),
) as outputraster:
outputraster.write(raster_buffered, 1)
return rio.open(outputpath)
......@@ -9,7 +9,7 @@ from mpl_toolkits.axes_grid1 import make_axes_locatable
import os
def mnp_hokaggregation(hok_csv, hokken_polygon_path, background_raster_path, outputdir):
def mnp_hokaggregation(hok_csv, hokken_polygon_path, background_raster_path, outputdir, gridcell_count=False):
"""
make aggregationplots for use in manual validation of mnp runs. can both aggregate validation data and species
model gridcells per hok.
......@@ -46,19 +46,26 @@ def mnp_hokaggregation(hok_csv, hokken_polygon_path, background_raster_path, out
extend = "max"
ylabel_text = "Number of observations in SOVON"
elif "COUNT_KEY_VALUE" in colnames:
elif "OPS_COUNT" in colnames:
bounds = np.array([1, 10, 50, 100, 500, 1000])
explabel = "_validation"
valuecolumn = "OPS_COUNT"
extend = "max"
ylabel_text = "number of observations in NDFF"
elif gridcell_count:
bounds = np.array([1, 10, 100, 1000, 10000, 40000])
explabel = "_"
valuecolumn = "COUNT_KEY_VALUE"
extend = "neither"
ylabel_text = "Number of gridcells in Hok"
elif "OPS_COUNT" in colnames:
bounds = np.array([1, 10, 50, 100, 500, 1000])
explabel = "_validation"
valuecolumn = "OPS_COUNT"
elif "MAX_KEY_VALUE" in colnames:
bounds = np.array([0, 0.1, 0.5, 1, 1.5, 2])
explabel = "_"
valuecolumn = "MAX_KEY_VALUE"
extend = "max"
ylabel_text = "number of observations in NDFF"
ylabel_text = "Largest key population in cell"
else:
raise Exception("no value column in columns")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment