Skip to content

Commit d0f3607

Browse files
committedOct 3, 2021
updated get
1 parent 9474c3d commit d0f3607

File tree

3 files changed

+161
-61
lines changed

3 files changed

+161
-61
lines changed
 

‎src/get/get.module.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import utils from '../utils';
2-
import convertGeometry from '../convert-geometry';
3-
import load from "../load";
1+
/**
2+
* @prettier
3+
*/
4+
import utils from "../utils";
5+
import convertGeometry from "../convert-geometry";
6+
import wrap from "../wrap-func";
47

5-
const getSync = (georaster, geom, flat) => {
6-
let cropTop; let cropLeft; let cropRight; let cropBottom;
8+
const get = (georaster, geom, flat) => {
9+
let cropTop;
10+
let cropLeft;
11+
let cropRight;
12+
let cropBottom;
713
if (geom === null || geom === undefined) {
814
try {
915
if (flat) {
@@ -12,15 +18,19 @@ const getSync = (georaster, geom, flat) => {
1218
cropRight = georaster.width;
1319
cropTop = 0;
1420
} else {
15-
return georaster.values || georaster.getValues();
21+
return (
22+
georaster.values ||
23+
georaster.getValues({ height: georaster.height, left: 0, right: georaster.width, bottom: georaster.height, top: 0, width: georaster.width })
24+
);
1625
}
1726
} catch (error) {
1827
console.error(error);
1928
throw error;
2029
}
21-
} else if (utils.isBbox(geom)) { // bounding box
30+
} else if (utils.isBbox(geom)) {
31+
// bounding box
2232
try {
23-
const geometry = convertGeometry('bbox', geom);
33+
const geometry = convertGeometry("bbox", geom);
2434

2535
// use a utility function that converts from the lat/long coordinate
2636
// space to the image coordinate space
@@ -35,13 +45,12 @@ const getSync = (georaster, geom, flat) => {
3545
cropLeft = Math.max(bboxLeft, 0);
3646
cropRight = Math.min(bboxRight, georaster.width);
3747
cropBottom = Math.min(bboxBottom, georaster.height);
38-
3948
} catch (error) {
4049
console.error(error);
4150
throw error;
4251
}
4352
} else {
44-
throw 'Geometry is not a bounding box - please make sure to send a bounding box when using geoblaze.get';
53+
throw "Geometry is not a bounding box - please make sure to send a bounding box when using geoblaze.get";
4554
}
4655

4756
try {
@@ -53,20 +62,22 @@ const getSync = (georaster, geom, flat) => {
5362
values = values.concat(Array.prototype.slice.call(band[rowIndex].slice(cropLeft, cropRight)));
5463
}
5564
return values;
56-
});
65+
});
5766
} else {
58-
return georaster.getValues({
59-
height: cropBottom - cropTop,
60-
width: cropRight - cropLeft,
61-
left: cropLeft,
62-
top: cropTop,
63-
right: cropRight,
64-
bottom: cropBottom
65-
}).then(bands => {
66-
return bands.map(rows => {
67-
return rows.reduce((acc, row) => acc.concat(Array.from(row)), []);
67+
return georaster
68+
.getValues({
69+
height: cropBottom - cropTop,
70+
width: cropRight - cropLeft,
71+
left: cropLeft,
72+
top: cropTop,
73+
right: cropRight,
74+
bottom: cropBottom
75+
})
76+
.then(bands => {
77+
return bands.map(rows => {
78+
return rows.reduce((acc, row) => acc.concat(Array.from(row)), []);
79+
});
6880
});
69-
});
7081
}
7182
} else {
7283
if (georaster.values) {
@@ -76,7 +87,7 @@ const getSync = (georaster, geom, flat) => {
7687
table.push(band[rowIndex].slice(cropLeft, cropRight));
7788
}
7889
return table;
79-
});
90+
});
8091
} else {
8192
return georaster.getValues({
8293
height: cropBottom - cropTop,
@@ -93,14 +104,4 @@ const getSync = (georaster, geom, flat) => {
93104
}
94105
};
95106

96-
const get = (georaster, geom, flat) => {
97-
if (typeof georaster === "string") {
98-
return load(georaster).then(georasterLoaded => {
99-
return getSync(georasterLoaded, geom, flat);
100-
});
101-
} else {
102-
return getSync(georaster, geom, flat);
103-
}
104-
}
105-
106-
export default get;
107+
export default wrap(get);

‎src/get/get.test.js

+121-25
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,151 @@
1-
import test from 'flug';
2-
import fetch from 'cross-fetch';
3-
import load from '../load';
4-
import get from './get.module';
1+
/**
2+
* @prettier
3+
*/
4+
import test from "flug";
5+
import { serve } from "srvd";
6+
import load from "../load";
7+
import parse from "../parse";
8+
import get from "./get.module";
59

6-
const urlRwanda = 'http://localhost:3000/data/RWA_MNH_ANC.tif';
7-
const bboxRwanda = require('../../data/RwandaBufferedBoundingBox.json');
10+
const urlRwanda = "http://localhost:3000/data/RWA_MNH_ANC.tif";
11+
const bboxRwanda = require("../../data/RwandaBufferedBoundingBox.json");
812

9-
test('Got Correct Flat Values when Geom bigger than Raster', async ({ eq }) => {
10-
const georaster = await fetch(urlRwanda).then(r => r.arrayBuffer()).then(load);
13+
const EXPECTED_HEIGHT = 712;
14+
const EXPECTED_WIDTH = 995;
15+
const EXPECTED_AREA = 708440;
16+
17+
if (require.main === module) serve({ debug: true, port: 3000, wait: 15 });
18+
19+
test("(Legacy) Got Correct Flat Values when Geom bigger than Raster", async ({ eq }) => {
20+
const georaster = await load(urlRwanda);
1121
const actualValues = get(georaster, bboxRwanda, true);
1222
eq(actualValues.length, 1);
13-
console.log(actualValues[0].filter(it => it === undefined).length);
14-
eq(actualValues[0].length, georaster.height * georaster.width);
23+
eq(actualValues[0].length, EXPECTED_AREA);
1524
});
1625

17-
test('Got Correct 2-D Values when Geom bigger than Raster', async ({ eq }) => {
18-
const georaster = await fetch(urlRwanda).then(r => r.arrayBuffer()).then(load);
26+
test("(Legacy) Got Correct 2-D Values when Geom bigger than Raster", async ({ eq }) => {
27+
const georaster = await load(urlRwanda);
1928
const actualValues = get(georaster, bboxRwanda, false);
2029
eq(actualValues.length, 1);
21-
eq(actualValues[0].length, georaster.height);
22-
eq(actualValues[0][0].length, georaster.width);
30+
eq(actualValues[0].length, EXPECTED_HEIGHT);
31+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
2332
});
2433

25-
test('Got Correct flat values for whole raster', async ({ eq }) => {
26-
const georaster = await fetch(urlRwanda).then(r => r.arrayBuffer()).then(load);
34+
test("(Legacy) Got Correct flat values for whole raster", async ({ eq }) => {
35+
const georaster = await load(urlRwanda);
2736
const flat = true;
2837
const actualValues = get(georaster, null, flat);
2938
eq(actualValues.length, 1);
30-
eq(actualValues[0].length, georaster.height * georaster.width);
39+
eq(actualValues[0].length, EXPECTED_AREA);
3140
});
3241

42+
test("(Legacy) Got Correct Flat Values when Geom bigger than Raster (from URL)", async ({ eq }) => {
43+
const georaster = await load(urlRwanda);
44+
const actualValues = get(georaster, bboxRwanda, true);
45+
eq(actualValues.length, 1);
46+
eq(actualValues[0].length, EXPECTED_AREA);
47+
});
3348

34-
test('Got Correct Flat Values when Geom bigger than Raster (from URL)', async ({ eq }) => {
49+
test("(Legacy) Got Correct 2-D Values when Geom bigger than Raster (from URL)", async ({ eq }) => {
3550
const georaster = await load(urlRwanda);
36-
const actualValues = await get(georaster, bboxRwanda, true);
51+
const actualValues = get(georaster, bboxRwanda, false);
3752
eq(actualValues.length, 1);
38-
eq(actualValues[0].length, georaster.height * georaster.width);
53+
eq(actualValues[0].length, EXPECTED_HEIGHT);
54+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
3955
});
4056

41-
test('Got Correct 2-D Values when Geom bigger than Raster (from URL)', async ({ eq }) => {
57+
test("(Legacy) Got Correct flat values for whole raster (from URL)", async ({ eq }) => {
4258
const georaster = await load(urlRwanda);
59+
const flat = true;
60+
const actualValues = get(georaster, null, flat);
61+
eq(actualValues.length, 1);
62+
eq(actualValues[0].length, EXPECTED_AREA);
63+
});
64+
65+
// modern
66+
test("(Modern) Got Correct Flat Values when Geom bigger than Raster", async ({ eq }) => {
67+
const georaster = await parse(urlRwanda);
68+
const actualValues = await get(georaster, bboxRwanda, true);
69+
eq(actualValues.length, 1);
70+
eq(actualValues[0].length, EXPECTED_AREA);
71+
});
72+
73+
test("(Modern) Got Correct 2-D Values when Geom bigger than Raster", async ({ eq }) => {
74+
const georaster = await parse(urlRwanda);
4375
const actualValues = await get(georaster, bboxRwanda, false);
4476
eq(actualValues.length, 1);
45-
eq(actualValues[0].length, georaster.height);
46-
eq(actualValues[0][0].length, georaster.width);
77+
eq(actualValues[0].length, EXPECTED_HEIGHT);
78+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
4779
});
4880

49-
test('Got Correct flat values for whole raster (from URL)', async ({ eq }) => {
81+
test("(Modern) Got Correct flat values for whole raster", async ({ eq }) => {
82+
const georaster = await parse(urlRwanda);
83+
const flat = true;
84+
const actualValues = await get(georaster, null, flat);
85+
eq(actualValues.length, 1);
86+
eq(actualValues[0].length, EXPECTED_AREA);
87+
});
88+
89+
test("(Modern) Got Correct Flat Values when Geom bigger than Raster (from URL)", async ({ eq }) => {
90+
const georaster = await parse(urlRwanda);
91+
const actualValues = await get(georaster, bboxRwanda, true);
92+
eq(actualValues.length, 1);
93+
eq(actualValues[0].length, EXPECTED_AREA);
94+
});
95+
96+
test("(Modern) Got Correct 2-D Values when Geom bigger than Raster (from URL)", async ({ eq }) => {
97+
const georaster = await parse(urlRwanda);
98+
const actualValues = await get(georaster, bboxRwanda, false);
99+
eq(actualValues.length, 1);
100+
eq(actualValues[0].length, EXPECTED_HEIGHT);
101+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
102+
});
103+
104+
test("(Modern) Got Correct flat values for whole raster (from URL)", async ({ eq }) => {
50105
const georaster = await load(urlRwanda);
51106
const flat = true;
52107
const actualValues = await get(georaster, null, flat);
53108
eq(actualValues.length, 1);
54-
eq(actualValues[0].length, georaster.height * georaster.width);
109+
eq(actualValues[0].length, EXPECTED_AREA);
110+
});
111+
112+
// GeoRaster URL
113+
test("(Modern) Got Correct Flat Values when Geom bigger than Raster [url source]", async ({ eq }) => {
114+
const actualValues = await get(urlRwanda, bboxRwanda, true);
115+
eq(actualValues.length, 1);
116+
eq(actualValues[0].length, EXPECTED_AREA);
117+
});
118+
119+
test("(Modern) Got Correct 2-D Values when Geom bigger than Raster [url source]", async ({ eq }) => {
120+
const actualValues = await get(urlRwanda, bboxRwanda, false);
121+
eq(actualValues.length, 1);
122+
eq(actualValues[0].length, EXPECTED_HEIGHT);
123+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
124+
});
125+
126+
test("(Modern) Got Correct flat values for whole raster [url source]", async ({ eq }) => {
127+
const flat = true;
128+
const actualValues = await get(urlRwanda, null, flat);
129+
eq(actualValues.length, 1);
130+
eq(actualValues[0].length, EXPECTED_AREA);
131+
});
132+
133+
test("(Modern) Got Correct Flat Values when Geom bigger than Raster [url source]", async ({ eq }) => {
134+
const actualValues = await get(urlRwanda, bboxRwanda, true);
135+
eq(actualValues.length, 1);
136+
eq(actualValues[0].length, EXPECTED_AREA);
137+
});
138+
139+
test("(Modern) Got Correct 2-D Values when Geom bigger than Raster [url source]", async ({ eq }) => {
140+
const actualValues = await get(urlRwanda, bboxRwanda, false);
141+
eq(actualValues.length, 1);
142+
eq(actualValues[0].length, EXPECTED_HEIGHT);
143+
eq(actualValues[0][0].length, EXPECTED_WIDTH);
144+
});
145+
146+
test("(Modern) Got Correct flat values for whole raster [url source]", async ({ eq }) => {
147+
const flat = true;
148+
const actualValues = await get(urlRwanda, null, flat);
149+
eq(actualValues.length, 1);
150+
eq(actualValues[0].length, EXPECTED_AREA);
55151
});

‎src/get/index.js

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

36
/**
47
* The get function takes a raster and a bounding box as input. It returns
@@ -11,6 +14,6 @@ import get from './get.module';
1114
* @param {Boolean} flat - whether or not the resulting output should be flattened into a single array
1215
* @returns {Object} array of pixel values
1316
* @example
14-
* const pixels = geoblaze.get(georaster, geometry);
17+
* const pixels = await geoblaze.get(georaster, geometry);
1518
*/
1619
export default get;

0 commit comments

Comments
 (0)
Please sign in to comment.