Skip to content

Commit 5090261

Browse files
committedOct 1, 2021
improved max
1 parent 9c47d0e commit 5090261

File tree

2 files changed

+96
-45
lines changed

2 files changed

+96
-45
lines changed
 

‎src/max/max.module.js

+66-42
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
import _ from 'underscore';
2+
import fastMax from 'fast-max';
23
import get from '../get';
4+
import load from '../load';
35
import utils from '../utils';
46
import convertGeometry from '../convert-geometry';
57
import intersectPolygon from '../intersect-polygon';
68

7-
const getMax = (values, noDataValue) => {
8-
const numberOfValues = values.length;
9-
if (numberOfValues > 0) {
10-
let max = null;
11-
for (let i = 0; i < numberOfValues; i++) {
12-
const value = values[i];
13-
if (value !== noDataValue) {
14-
15-
/* We first compare the current value to the stored maximum.
16-
If the new value is greater than the stored minimum, replace the
17-
stored minimum with the new value. When checking a greater than
18-
comparison aganist a null value, like in the first comparison,
19-
the statement resolves as true. */
20-
21-
if (value > max) {
22-
max = value;
23-
}
24-
}
25-
}
26-
return max;
27-
} else {
28-
throw 'No values were provided';
29-
}
30-
};
319

3210
const getMaxForRaster = (georaster, geom) => {
3311

@@ -37,34 +15,72 @@ const getMaxForRaster = (georaster, geom) => {
3715

3816
if (geom === null || geom === undefined) {
3917

40-
return georaster.values.map(band => {
41-
return _.max(band.map(row => getMax(row, noDataValue)));
42-
});
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+
}
4340

4441
} else if (utils.isBbox(geom)) {
4542
geom = convertGeometry('bbox', geom);
4643

4744
// grab array of values;
4845
const flat = true;
49-
const values = get(georaster, geom, flat);
50-
51-
// get max value
52-
return values.map(band => getMax(band, noDataValue));
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+
}
5356

5457
} else if (utils.isPolygon(geom)) {
5558
geom = convertGeometry('polygon', geom);
5659
const values = [];
5760

58-
intersectPolygon(georaster, geom, (value, bandIndex) => {
59-
if (!values[bandIndex]) {
60-
values[bandIndex] = value;
61-
} else if (value > values[bandIndex]) {
62-
values[bandIndex] = value;
63-
}
64-
});
65-
66-
if (values) return values;
67-
else throw 'No Values were found in the given geometry';
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+
70+
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+
}
6884

6985
} else {
7086
throw 'Non-Bounding Box geometries are currently not supported.';
@@ -75,4 +91,12 @@ const getMaxForRaster = (georaster, geom) => {
7591
}
7692
};
7793

78-
export default getMaxForRaster;
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;

‎src/max/max.test.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'flug';
2+
import fetch from 'cross-fetch';
23
import load from '../load';
34
import max from './max.module';
45

@@ -16,19 +17,45 @@ const polygon = [[
1617
const expectedPolygonValue = 7807.40;
1718

1819
test('Got Correct Get Max from Bounding Box', async ({ eq }) => {
19-
const georaster = await load(url);
20+
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
2021
const value = Number(max(georaster, bbox)[0].toFixed(2));
2122
eq(value, expectedBboxValue);
2223
});
2324

2425
test('Get Max from Polygon', async ({ eq }) => {
25-
const georaster = await load(url);
26+
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
2627
const value = Number(max(georaster, polygon)[0].toFixed(2));
2728
eq(value, expectedPolygonValue);
2829
});
2930

3031
test('Get Max from Raster without polygon', async ({ eq }) => {
31-
const georaster = await load(url);
32+
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
3233
const value = max(georaster)[0];
3334
eq(value, 8131.2);
3435
});
36+
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);
42+
const value = Number(result[0].toFixed(2));
43+
eq(value, expectedBboxValue);
44+
});
45+
46+
test('Get Max from Polygon', async ({ eq }) => {
47+
const result = await max(url, polygon);
48+
const value = Number(result[0].toFixed(2));
49+
eq(value, expectedPolygonValue);
50+
});
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.