From bc67dc9a517d988f77b6f9c6e2b3bffd5f466ce3 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 19 Nov 2019 12:51:17 -0500 Subject: [PATCH] Formula tests --- app/api/formulas.py | 41 ++++++++++++++++++++-------- app/static/app/js/components/Map.jsx | 2 +- app/tests/test_formulas.py | 18 ++++++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 app/tests/test_formulas.py diff --git a/app/api/formulas.py b/app/api/formulas.py index d57fe574..3e74c963 100644 --- a/app/api/formulas.py +++ b/app/api/formulas.py @@ -1,25 +1,44 @@ # Algos from https://github.com/dirceup/tiled-vegetation-indices/blob/master/app/lib/vegetation_index.rb import re +from functools import lru_cache + +# Functions can use all of the supported functions and operators from +# https://numexpr.readthedocs.io/en/latest/user_guide.html#supported-operators algos = { - 'vari': { + 'VARI': { 'bands': 'RGB', - 'expr': '(G-R)/(G+R-B)' + 'expr': '(G - R) / (G + R - B)', + 'help': 'Visual Atmospheric Resistance Index shows the areas of vegetation.' + }, + 'NDVI': { + 'bands': 'RGN', + 'expr': '(N - R) / (N + R)', + 'help': 'Normalized Difference Vegetation Index shows the amount of green vegetation.' + }, + 'BAI': { + 'bands': 'RGN', + 'expr': '1.0 / (((0.1 - R) ** 2) + ((0.06 - N) ** 2))', + 'help': 'Burn Area Index hightlights burned land in the red to near-infrared spectrum.' + }, + 'GLI': { + 'bands': 'RGB', + 'expr': '((G * 2) - R - B) / ((G * 2) + R + B)', + 'help': 'Green Leaf Index shows greens leaves and stems.' + }, + 'GNDVI':{ + 'bands': 'RGN', + 'expr': '(N - G) / (N + G)', + 'help': 'Green Normalized Difference Vegetation Index is similar to NDVI, but measures the green spectrum instead of red.' }, - 'TEST': { + '_TESTRB': { 'bands': 'RGB', 'expr': 'B+R' } } -band_map = { - 'RGB': (0, 1, 2), - - - 'BGR': (2, 1, 0), -} - +#@lru_cache(max_size=20) def lookup_formula(algo, band_order = 'RGB'): if algo is None: return None @@ -38,4 +57,4 @@ def lookup_formula(algo, band_order = 'RGB'): b = matches.group(1) return 'b' + str(input_bands.index(algo_bands.index(b)) + 1) - return re.sub("([A-Z]+?[a-z]*)", repl, algos[algo]['expr']) + return re.sub("([A-Z]+?[a-z]*)", repl, re.sub("\s+", "", algos[algo]['expr'])) diff --git a/app/static/app/js/components/Map.jsx b/app/static/app/js/components/Map.jsx index 6de8219f..37b0853d 100644 --- a/app/static/app/js/components/Map.jsx +++ b/app/static/app/js/components/Map.jsx @@ -94,7 +94,7 @@ class Map extends React.Component { const { url, meta, type } = tile; let metaUrl = url + "metadata"; - if (type == "plant") metaUrl += "?formula=vari&bands=RGB&rescale=0.02,0.1&color_map=rdylgn"; + if (type == "plant") metaUrl += "?formula=GLI&bands=RGB&rescale=-1,1&color_map=rdylgn"; if (type == "dsm") metaUrl += "?rescale=156%2C165&hillshade=3&color_map=jet_r"; console.log(type, metaUrl); diff --git a/app/tests/test_formulas.py b/app/tests/test_formulas.py new file mode 100644 index 00000000..45fc1b18 --- /dev/null +++ b/app/tests/test_formulas.py @@ -0,0 +1,18 @@ +from django.test import TestCase +from app.api.formulas import lookup_formula + +class TestFormulas(TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_formulas(self): + # Original + self.assertTrue(lookup_formula("_TESTRB", "RGB") == "b3+b1") + + # Swap bands + self.assertTrue(lookup_formula("_TESTRB", "BGR") == "b1+b3") + self.assertTrue(lookup_formula("_TESTRB", "NRB") == "b2+b3") +