Skip to content

Commit

Permalink
modernized mean
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Oct 4, 2021
1 parent 4d3d74c commit 5b32e41
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 39 deletions.
7 changes: 5 additions & 2 deletions src/mean/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import mean from './mean.module';
/**
* @prettier
*/
import mean from "./mean.module";

/**
* The mean function takes a raster as an input and an optional geometry.
Expand All @@ -10,6 +13,6 @@ import mean from './mean.module';
* @param {Object} geometry - geometry can be an [xmin, ymin, xmax, ymax] array for a bounding box, [[[x00,y00],...,[x0n,y0n],[x00,y00]]...] for a polygon, a GeoJSON polygon object, or a string representation of a GeoJSON polygon object.
* @returns {Object} array of means for each band
* @example
* const means = geoblaze.mean(georaster, geometry);
* const means = await geoblaze.mean(georaster, geometry);
*/
export default mean;
35 changes: 18 additions & 17 deletions src/mean/mean.module.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import get from '../get';
import utils from '../utils';
import convertGeometry from '../convert-geometry';
import intersectPolygon from '../intersect-polygon';
/**
* @prettier
*/
import get from "../get";
import utils from "../utils";
import wrap from "../wrap-func";
import convertGeometry from "../convert-geometry";
import intersectPolygon from "../intersect-polygon";

const mean = (georaster, geom) => {

try {

const geomIsBbox = utils.isBbox(geom);

if (geom === null || geom === undefined || geomIsBbox) {

if (geomIsBbox) {
geom = convertGeometry('bbox', geom);
geom = convertGeometry("bbox", geom);
}

const values = get(georaster, geom);

const { noDataValue } = georaster;

const values = get(georaster, geom);

// sum values
const sums = [];
for (let bandIndex = 0; bandIndex < values.length; bandIndex++) {
Expand All @@ -40,8 +41,9 @@ const mean = (georaster, geom) => {
sums.push(sumForBand / cellsWithValues);
}
return sums;
} else if (utils.isPolygon(geom)) { // if geometry is a polygon
geom = convertGeometry('polygon', geom);
} else if (utils.isPolygon(geom)) {
// if geometry is a polygon
geom = convertGeometry("polygon", geom);
const sums = [];
const numValues = [];

Expand All @@ -67,15 +69,14 @@ const mean = (georaster, geom) => {
});

if (results) return results;
else throw 'No Values were found in the given geometry';

else throw "No Values were found in the given geometry";
} else {
throw 'Only Bounding Box and Polygon geometries are currently supported.';
throw "Only Bounding Box and Polygon geometries are currently supported.";
}
} catch(e) {
} catch (e) {
console.error(e);
throw e;
}
};

export default mean;
export default wrap(mean);
88 changes: 68 additions & 20 deletions src/mean/mean.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import test from 'flug';
import load from '../load';
import mean from './mean.module';
/**
* @prettier
*/
import test from "flug";
import { serve } from "srvd";
import load from "../load";
import mean from "./mean.module";
import utils from "../utils";

const url = 'http://localhost:3000/data/test.tiff';
if (require.main === module) serve({ debug: true, port: 3000, wait: 3 });

const bbox = [80.63, 7.42, 84.21, 10.10];
const { round } = utils;

const url = "http://localhost:3000/data/test.tiff";

const bbox = [80.63, 7.42, 84.21, 10.1];
const expectedBboxValue = 1232.47;

const polygon = [[
[83.12255859375, 22.49225722008518], [82.96875, 21.57571893245848], [81.58447265624999, 1.207458730482642],
[83.07861328125, 20.34462694382967], [83.8037109375, 19.497664168139053], [84.814453125, 19.766703551716976],
[85.078125, 21.166483858206583], [86.044921875, 20.838277806058933], [86.98974609375, 22.49225722008518],
[85.58349609375, 24.54712317973075], [84.6826171875, 23.36242859340884], [83.12255859375, 22.49225722008518]
]];
const polygon = [
[
[83.12255859375, 22.49225722008518],
[82.96875, 21.57571893245848],
[81.58447265624999, 1.207458730482642],
[83.07861328125, 20.34462694382967],
[83.8037109375, 19.497664168139053],
[84.814453125, 19.766703551716976],
[85.078125, 21.166483858206583],
[86.044921875, 20.838277806058933],
[86.98974609375, 22.49225722008518],
[85.58349609375, 24.54712317973075],
[84.6826171875, 23.36242859340884],
[83.12255859375, 22.49225722008518]
]
];
const expectedPolygonValue = 1826.74;

const polygonGeojson = `{
Expand All @@ -35,28 +54,57 @@ const polygonGeojson = `{
}
]
}`;
const expectedPolygonGeojsonValue = 1826.74 ;
const expectedPolygonGeojsonValue = 1826.74;

test('Mean: Get Mean from Whole Raster', async ({ eq }) => {
test("(Legacy) Mean without Geometry", async ({ eq }) => {
const georaster = await load(url);
const value = Number(mean(georaster)[0].toFixed(2));
const results = mean(georaster);
const value = round(results[0]);
eq(value, 132.04);
});

test('Get Mean from Bounding Box', async ({ eq }) => {
test("(Legacy) Mean with Bounding Box", async ({ eq }) => {
const georaster = await load(url);
const value = Number(mean(georaster, bbox)[0].toFixed(2));
const results = mean(georaster, bbox);
const value = round(results[0]);
eq(value, expectedBboxValue);
});

test('Get Mean from Polygon', async ({ eq }) => {
test("(Legacy) Mean with Polygon", async ({ eq }) => {
const georaster = await load(url);
const value = Number(mean(georaster, polygon)[0].toFixed(2));
const results = mean(georaster, polygon);
const value = round(results[0]);
eq(value, expectedPolygonValue);
});

test('Get Mean from Polygon (GeoJSON)', async ({ eq }) => {
test("(Legacy) Mean from GeoJSON", async ({ eq }) => {
const georaster = await load(url);
const value = Number(mean(georaster, polygonGeojson)[0].toFixed(2));
const results = mean(georaster, polygonGeojson);
const value = round(results[0]);
eq(value, expectedPolygonGeojsonValue);
});

// Modernized Async
test("(Modern) Mean without Geometry", async ({ eq }) => {
const results = await mean(url);
const value = round(results[0]);
eq(value, 132.04);
});

test("(Modern) Mean with Bounding Box", async ({ eq }) => {
const results = await mean(url, bbox);
const value = round(results[0]);
eq(value, expectedBboxValue);
});

test("(Modern) Mean with Polygon", async ({ eq }) => {
const results = await mean(url, polygon);
const value = round(results[0]);
eq(value, expectedPolygonValue);
});

test("(Modern) Mean from GeoJSON", async ({ eq }) => {
const results = await mean(url, polygonGeojson);
const value = round(results[0]);
eq(value, expectedPolygonGeojsonValue);
});

0 comments on commit 5b32e41

Please sign in to comment.