Skip to content

Commit 4d3d74c

Browse files
committedOct 4, 2021
updated max
1 parent 8b80e7d commit 4d3d74c

File tree

3 files changed

+85
-130
lines changed

3 files changed

+85
-130
lines changed
 

‎src/max/index.js

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

36
/**
47
* The max function takes a raster as an input and an optional geometry.
@@ -10,6 +13,6 @@ import max from './max.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 maxs for each band
1215
* @example
13-
* const maxs = geoblaze.max(georaster, geometry);
16+
* const maxs = await geoblaze.max(georaster, geometry);
1417
*/
1518
export default max;

‎src/max/max.module.js

+32-85
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,49 @@
1-
import _ from 'underscore';
2-
import fastMax from 'fast-max';
3-
import get from '../get';
4-
import load from '../load';
5-
import utils from '../utils';
6-
import convertGeometry from '../convert-geometry';
7-
import intersectPolygon from '../intersect-polygon';
8-
1+
/**
2+
* @prettier
3+
*/
4+
import _ from "underscore";
5+
import fastMax from "fast-max";
6+
import get from "../get";
7+
import wrap from "../wrap-func";
8+
import utils from "../utils";
9+
import convertGeometry from "../convert-geometry";
10+
import intersectPolygon from "../intersect-polygon";
911

1012
const getMaxForRaster = (georaster, geom) => {
11-
1213
try {
13-
1414
const { noDataValue } = georaster;
1515

16-
if (geom === null || geom === undefined) {
17-
18-
if (georaster.values) {
19-
return georaster.values.map(band => {
20-
let band_max;
21-
band.forEach(row => {
22-
let row_max = fastMax(row, { no_data: noDataValue });
23-
if (row_max !== undefined && (band_max === undefined || row_max > band_max)) band_max = row_max;
24-
})
25-
return band_max;
26-
});
27-
} else {
28-
const options = { height: georaster.height, left: 0, right: georaster.width, bottom: georaster.height, top: 0, width: georaster.width };
29-
return georaster.getValues(options).then(values => {
30-
return values.map(band => {
31-
let band_max;
32-
band.forEach(row => {
33-
let row_max = fastMax(row, { no_data: noDataValue });
34-
if (row_max !== undefined && (band_max === undefined || row_max > band_max)) band_max = row_max;
35-
})
36-
return band_max;
37-
});
38-
});
39-
}
16+
const calcMax = values => values.map(band => fastMax(band, { no_data: noDataValue }));
4017

18+
if (geom === null || geom === undefined) {
19+
const values = get(georaster, undefined, true);
20+
return utils.callAfterResolveArgs(calcMax, values);
4121
} else if (utils.isBbox(geom)) {
42-
geom = convertGeometry('bbox', geom);
43-
44-
// grab array of values;
45-
const flat = true;
46-
if (georaster.values) {
47-
const values = get(georaster, geom, flat);
48-
49-
// get max value
50-
return values.map(band => fastMax(band, { no_data: noDataValue }));
51-
} else {
52-
return get(georaster, geom, flat).then(values => (
53-
values.map(band => fastMax(band, { no_data: noDataValue })
54-
)));
55-
}
56-
22+
geom = convertGeometry("bbox", geom);
23+
const values = get(georaster, geom, true);
24+
return utils.callAfterResolveArgs(calcMax, values);
5725
} else if (utils.isPolygon(geom)) {
58-
geom = convertGeometry('polygon', geom);
26+
geom = convertGeometry("polygon", geom);
5927
const values = [];
60-
61-
if (georaster.values) {
62-
intersectPolygon(georaster, geom, (value, bandIndex) => {
63-
if (!values[bandIndex]) {
64-
values[bandIndex] = value;
65-
} else if (value > values[bandIndex]) {
66-
values[bandIndex] = value;
67-
}
68-
});
69-
28+
const done = intersectPolygon(georaster, geom, (value, bandIndex) => {
29+
if (!values[bandIndex]) {
30+
values[bandIndex] = value;
31+
} else if (value > values[bandIndex]) {
32+
values[bandIndex] = value;
33+
}
34+
});
35+
36+
return utils.callAfterResolveArgs(() => {
7037
if (values) return values;
71-
else throw 'No Values were found in the given geometry';
72-
} else {
73-
return intersectPolygon(georaster, geom, (value, bandIndex) => {
74-
if (!values[bandIndex]) {
75-
values[bandIndex] = value;
76-
} else if (value > values[bandIndex]) {
77-
values[bandIndex] = value;
78-
}
79-
}).then(() => {
80-
if (values) return values;
81-
else throw 'No Values were found in the given geometry';
82-
});
83-
}
84-
38+
else throw "No Values were found in the given geometry";
39+
}, done);
8540
} else {
86-
throw 'Non-Bounding Box geometries are currently not supported.';
41+
throw "Non-Bounding Box geometries are currently not supported.";
8742
}
88-
} catch(e) {
43+
} catch (e) {
8944
console.error(e);
9045
throw e;
9146
}
9247
};
9348

94-
const getMaxForRasterWrapper = (georaster, geom) => {
95-
if (typeof georaster === "string") {
96-
return load(georaster).then(loaded => getMaxForRaster(loaded, geom));
97-
} else {
98-
return getMaxForRaster(georaster, geom);
99-
}
100-
}
101-
102-
export default getMaxForRasterWrapper;
49+
export default wrap(getMaxForRaster);

‎src/max/max.test.js

+48-43
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
1-
import test from 'flug';
2-
import fetch from 'cross-fetch';
3-
import load from '../load';
4-
import max from './max.module';
5-
6-
const url = 'http://localhost:3000/data/test.tiff';
7-
8-
const bbox = [80.63, 7.42, 84.21, 10.10];
9-
const expectedBboxValue = 5166.70;
10-
11-
const polygon = [[
12-
[83.12255859375, 22.49225722008518], [82.96875, 21.57571893245848], [81.58447265624999, 1.207458730482642],
13-
[83.07861328125, 20.34462694382967], [83.8037109375, 19.497664168139053], [84.814453125, 19.766703551716976],
14-
[85.078125, 21.166483858206583], [86.044921875, 20.838277806058933], [86.98974609375, 22.49225722008518],
15-
[85.58349609375, 24.54712317973075], [84.6826171875, 23.36242859340884], [83.12255859375, 22.49225722008518]
16-
]];
17-
const expectedPolygonValue = 7807.40;
18-
19-
test('Got Correct Get Max from Bounding Box', async ({ eq }) => {
20-
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
1+
/**
2+
* @prettier
3+
*/
4+
import test from "flug";
5+
import { serve } from "srvd";
6+
import load from "../load";
7+
import max from "./max.module";
8+
9+
if (require.main === module) serve({ debug: true, port: 3000, wait: 60 });
10+
11+
const url = "http://localhost:3000/data/test.tiff";
12+
13+
const bbox = [80.63, 7.42, 84.21, 10.1];
14+
const expectedBboxValue = 5166.7;
15+
16+
const polygon = [
17+
[
18+
[83.12255859375, 22.49225722008518],
19+
[82.96875, 21.57571893245848],
20+
[81.58447265624999, 1.207458730482642],
21+
[83.07861328125, 20.34462694382967],
22+
[83.8037109375, 19.497664168139053],
23+
[84.814453125, 19.766703551716976],
24+
[85.078125, 21.166483858206583],
25+
[86.044921875, 20.838277806058933],
26+
[86.98974609375, 22.49225722008518],
27+
[85.58349609375, 24.54712317973075],
28+
[84.6826171875, 23.36242859340884],
29+
[83.12255859375, 22.49225722008518]
30+
]
31+
];
32+
const expectedPolygonValue = 7807.4;
33+
34+
test("(Legacy) Max without Geometry", async ({ eq }) => {
35+
const georaster = await load(url);
36+
const value = max(georaster)[0];
37+
eq(value, 8131.2);
38+
});
39+
40+
test("(Legacy) Max with Bounding Box", async ({ eq }) => {
41+
const georaster = await load(url);
2142
const value = Number(max(georaster, bbox)[0].toFixed(2));
2243
eq(value, expectedBboxValue);
2344
});
2445

25-
test('Get Max from Polygon', async ({ eq }) => {
26-
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
46+
test("(Legacy) Max with Polygon", async ({ eq }) => {
47+
const georaster = await load(url);
2748
const value = Number(max(georaster, polygon)[0].toFixed(2));
2849
eq(value, expectedPolygonValue);
2950
});
3051

31-
test('Get Max from Raster without polygon', async ({ eq }) => {
32-
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
33-
const value = max(georaster)[0];
34-
eq(value, 8131.2);
52+
test("(Modern) Max without Geometry", async ({ eq }) => {
53+
eq(await max(url), [8131.2]);
3554
});
3655

37-
38-
// async
39-
test('Got Correct Get Max from Bounding Box', async ({ eq }) => {
40-
const georaster = await load(url);
41-
const result = await max(georaster, bbox);
56+
test("(Modern) Got Correct Get Max from Bounding Box", async ({ eq }) => {
57+
const result = await max(url, bbox);
4258
const value = Number(result[0].toFixed(2));
4359
eq(value, expectedBboxValue);
4460
});
4561

46-
test('Get Max from Polygon', async ({ eq }) => {
62+
test("(Modern) Get Max from Polygon", async ({ eq }) => {
4763
const result = await max(url, polygon);
4864
const value = Number(result[0].toFixed(2));
4965
eq(value, expectedPolygonValue);
5066
});
51-
52-
test('Get Max from Raster without polygon from async obj', async ({ eq }) => {
53-
const georaster = await load(url);
54-
const value = await max(georaster);
55-
eq(value[0], 8131.2);
56-
});
57-
58-
test('Get Max from Raster without polygon directly from url', async ({ eq }) => {
59-
const value = await max(url);
60-
eq(value[0], 8131.2);
61-
});

0 commit comments

Comments
 (0)
Please sign in to comment.