Skip to content

Commit 5b32e41

Browse files
committedOct 4, 2021
modernized mean
1 parent 4d3d74c commit 5b32e41

File tree

3 files changed

+91
-39
lines changed

3 files changed

+91
-39
lines changed
 

‎src/mean/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import mean from './mean.module';
1+
/**
2+
* @prettier
3+
*/
4+
import mean from "./mean.module";
25

36
/**
47
* The mean function takes a raster as an input and an optional geometry.
@@ -10,6 +13,6 @@ import mean from './mean.module';
1013
* @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.
1114
* @returns {Object} array of means for each band
1215
* @example
13-
* const means = geoblaze.mean(georaster, geometry);
16+
* const means = await geoblaze.mean(georaster, geometry);
1417
*/
1518
export default mean;

‎src/mean/mean.module.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import get from '../get';
2-
import utils from '../utils';
3-
import convertGeometry from '../convert-geometry';
4-
import intersectPolygon from '../intersect-polygon';
1+
/**
2+
* @prettier
3+
*/
4+
import get from "../get";
5+
import utils from "../utils";
6+
import wrap from "../wrap-func";
7+
import convertGeometry from "../convert-geometry";
8+
import intersectPolygon from "../intersect-polygon";
59

610
const mean = (georaster, geom) => {
7-
811
try {
9-
1012
const geomIsBbox = utils.isBbox(geom);
1113

1214
if (geom === null || geom === undefined || geomIsBbox) {
13-
1415
if (geomIsBbox) {
15-
geom = convertGeometry('bbox', geom);
16+
geom = convertGeometry("bbox", geom);
1617
}
1718

18-
const values = get(georaster, geom);
19-
2019
const { noDataValue } = georaster;
2120

21+
const values = get(georaster, geom);
22+
2223
// sum values
2324
const sums = [];
2425
for (let bandIndex = 0; bandIndex < values.length; bandIndex++) {
@@ -40,8 +41,9 @@ const mean = (georaster, geom) => {
4041
sums.push(sumForBand / cellsWithValues);
4142
}
4243
return sums;
43-
} else if (utils.isPolygon(geom)) { // if geometry is a polygon
44-
geom = convertGeometry('polygon', geom);
44+
} else if (utils.isPolygon(geom)) {
45+
// if geometry is a polygon
46+
geom = convertGeometry("polygon", geom);
4547
const sums = [];
4648
const numValues = [];
4749

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

6971
if (results) return results;
70-
else throw 'No Values were found in the given geometry';
71-
72+
else throw "No Values were found in the given geometry";
7273
} else {
73-
throw 'Only Bounding Box and Polygon geometries are currently supported.';
74+
throw "Only Bounding Box and Polygon geometries are currently supported.";
7475
}
75-
} catch(e) {
76+
} catch (e) {
7677
console.error(e);
7778
throw e;
7879
}
7980
};
8081

81-
export default mean;
82+
export default wrap(mean);

‎src/mean/mean.test.js

+68-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
import test from 'flug';
2-
import load from '../load';
3-
import mean from './mean.module';
1+
/**
2+
* @prettier
3+
*/
4+
import test from "flug";
5+
import { serve } from "srvd";
6+
import load from "../load";
7+
import mean from "./mean.module";
8+
import utils from "../utils";
49

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

7-
const bbox = [80.63, 7.42, 84.21, 10.10];
12+
const { round } = utils;
13+
14+
const url = "http://localhost:3000/data/test.tiff";
15+
16+
const bbox = [80.63, 7.42, 84.21, 10.1];
817
const expectedBboxValue = 1232.47;
918

10-
const polygon = [[
11-
[83.12255859375, 22.49225722008518], [82.96875, 21.57571893245848], [81.58447265624999, 1.207458730482642],
12-
[83.07861328125, 20.34462694382967], [83.8037109375, 19.497664168139053], [84.814453125, 19.766703551716976],
13-
[85.078125, 21.166483858206583], [86.044921875, 20.838277806058933], [86.98974609375, 22.49225722008518],
14-
[85.58349609375, 24.54712317973075], [84.6826171875, 23.36242859340884], [83.12255859375, 22.49225722008518]
15-
]];
19+
const polygon = [
20+
[
21+
[83.12255859375, 22.49225722008518],
22+
[82.96875, 21.57571893245848],
23+
[81.58447265624999, 1.207458730482642],
24+
[83.07861328125, 20.34462694382967],
25+
[83.8037109375, 19.497664168139053],
26+
[84.814453125, 19.766703551716976],
27+
[85.078125, 21.166483858206583],
28+
[86.044921875, 20.838277806058933],
29+
[86.98974609375, 22.49225722008518],
30+
[85.58349609375, 24.54712317973075],
31+
[84.6826171875, 23.36242859340884],
32+
[83.12255859375, 22.49225722008518]
33+
]
34+
];
1635
const expectedPolygonValue = 1826.74;
1736

1837
const polygonGeojson = `{
@@ -35,28 +54,57 @@ const polygonGeojson = `{
3554
}
3655
]
3756
}`;
38-
const expectedPolygonGeojsonValue = 1826.74 ;
57+
const expectedPolygonGeojsonValue = 1826.74;
3958

40-
test('Mean: Get Mean from Whole Raster', async ({ eq }) => {
59+
test("(Legacy) Mean without Geometry", async ({ eq }) => {
4160
const georaster = await load(url);
42-
const value = Number(mean(georaster)[0].toFixed(2));
61+
const results = mean(georaster);
62+
const value = round(results[0]);
4363
eq(value, 132.04);
4464
});
4565

46-
test('Get Mean from Bounding Box', async ({ eq }) => {
66+
test("(Legacy) Mean with Bounding Box", async ({ eq }) => {
4767
const georaster = await load(url);
48-
const value = Number(mean(georaster, bbox)[0].toFixed(2));
68+
const results = mean(georaster, bbox);
69+
const value = round(results[0]);
4970
eq(value, expectedBboxValue);
5071
});
5172

52-
test('Get Mean from Polygon', async ({ eq }) => {
73+
test("(Legacy) Mean with Polygon", async ({ eq }) => {
5374
const georaster = await load(url);
54-
const value = Number(mean(georaster, polygon)[0].toFixed(2));
75+
const results = mean(georaster, polygon);
76+
const value = round(results[0]);
5577
eq(value, expectedPolygonValue);
5678
});
5779

58-
test('Get Mean from Polygon (GeoJSON)', async ({ eq }) => {
80+
test("(Legacy) Mean from GeoJSON", async ({ eq }) => {
5981
const georaster = await load(url);
60-
const value = Number(mean(georaster, polygonGeojson)[0].toFixed(2));
82+
const results = mean(georaster, polygonGeojson);
83+
const value = round(results[0]);
84+
eq(value, expectedPolygonGeojsonValue);
85+
});
86+
87+
// Modernized Async
88+
test("(Modern) Mean without Geometry", async ({ eq }) => {
89+
const results = await mean(url);
90+
const value = round(results[0]);
91+
eq(value, 132.04);
92+
});
93+
94+
test("(Modern) Mean with Bounding Box", async ({ eq }) => {
95+
const results = await mean(url, bbox);
96+
const value = round(results[0]);
97+
eq(value, expectedBboxValue);
98+
});
99+
100+
test("(Modern) Mean with Polygon", async ({ eq }) => {
101+
const results = await mean(url, polygon);
102+
const value = round(results[0]);
103+
eq(value, expectedPolygonValue);
104+
});
105+
106+
test("(Modern) Mean from GeoJSON", async ({ eq }) => {
107+
const results = await mean(url, polygonGeojson);
108+
const value = round(results[0]);
61109
eq(value, expectedPolygonGeojsonValue);
62110
});

0 commit comments

Comments
 (0)
Please sign in to comment.