Skip to content

Commit cd4194e

Browse files
committedOct 4, 2021
modernized identify
1 parent abf63e2 commit cd4194e

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed
 

‎src/identify/identify.module.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import convertGeometry from '../convert-geometry';
2-
import load from "../load";
3-
4-
const identifySync = (georaster, geometry) => {
1+
/**
2+
* @prettier
3+
*/
4+
import convertGeometry from "../convert-geometry";
5+
import wrap from "../wrap-func";
56

7+
const identify = (georaster, geometry) => {
68
// The convertGeometry function takes the input
79
// geometry and converts it to a standard format.
8-
const [xInCrs, yInCrs] = convertGeometry('point', geometry);
10+
const [xInCrs, yInCrs] = convertGeometry("point", geometry);
911

1012
// By normalizing the difference in latitude and longitude between the image
1113
// origin and the point geometry by the cell height and width respectively,
@@ -17,7 +19,6 @@ const identifySync = (georaster, geometry) => {
1719
const y = Math.floor((georaster.ymax - yInCrs) / georaster.pixelHeight);
1820

1921
try {
20-
2122
// iterate through the bands
2223
// get the row and then the column of the pixel that you want
2324
if (x > 0 && x < georaster.width && y > 0 && y < georaster.height) {
@@ -30,19 +31,10 @@ const identifySync = (georaster, geometry) => {
3031
} else {
3132
return null;
3233
}
33-
34-
} catch(e) {
34+
} catch (e) {
3535
console.error(e);
3636
throw e;
3737
}
3838
};
3939

40-
function identify (georaster, geometry) {
41-
if (typeof georaster === "string") {
42-
return load(georaster).then(georasterLoaded => identifySync(georasterLoaded, geometry));
43-
} else {
44-
return identifySync(georaster, geometry);
45-
}
46-
}
47-
48-
export default identify;
40+
export default wrap(identify);

‎src/identify/identify.test.js

+42-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
1-
import test from 'flug';
2-
import fetch from 'cross-fetch';
3-
import load from '../load';
4-
import identify from './identify.module';
1+
/**
2+
* @prettier
3+
*/
4+
import test from "flug";
5+
import { serve } from "srvd";
6+
import load from "../load";
7+
import identify from "./identify.module";
58

6-
const url = 'http://localhost:3000/data/test.tiff';
9+
const url = "http://localhost:3000/data/test.tiff";
710
const point = [80.63, 7.42];
811
const expectedValue = 350.7;
912

10-
test('Identified Point Correctly from file', async ({ eq }) => {
11-
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
13+
if (require.main === module) serve({ debug: true, port: 3000, wait: 3 });
14+
15+
test("(Legacy) Identified Point Correctly from file", async ({ eq }) => {
16+
const georaster = await load(url);
1217
const values = identify(georaster, point)[0];
1318
eq(values, expectedValue);
1419
});
1520

16-
test('Try to identify point outside raster and correctly returned null from file', async ({ eq }) => {
17-
const georaster = await fetch(url).then(r => r.arrayBuffer()).then(load);
18-
const value = identify(georaster, [-200, 7.42]);
19-
eq(value, null);
21+
test("(Legacy) Try to identify point outside raster and correctly returned null from file", async ({ eq }) => {
22+
const georaster = await load(url);
23+
const values = identify(georaster, [-200, 7.42]);
24+
eq(values, null);
2025
});
2126

22-
test('Identified Point Correctly from URL', async ({ eq }) => {
27+
test("(Legacy) Identified Point Correctly from URL", async ({ eq }) => {
2328
const georaster = await load(url);
24-
const values = await identify(georaster, point);
29+
const values = identify(georaster, point);
2530
eq(values[0], expectedValue);
2631
});
2732

28-
test('Try to identify point outside raster and correctly returned null from URL', async ({ eq }) => {
33+
test("(Legacy) Try to identify point outside raster and correctly returned null from URL", async ({ eq }) => {
2934
const georaster = await load(url);
30-
const value = await identify(georaster, [-200, 7.42]);
31-
eq(value, null);
35+
const values = identify(georaster, [-200, 7.42]);
36+
eq(values, null);
37+
});
38+
39+
// modern
40+
test("(Modern) Identified Point Correctly from file", async ({ eq }) => {
41+
const values = await identify(url, point);
42+
eq(values, [expectedValue]);
43+
});
44+
45+
test("(Modern) Try to identify point outside raster and correctly returned null from file", async ({ eq }) => {
46+
const values = await identify(url, [-200, 7.42]);
47+
eq(values, null);
48+
});
49+
50+
test("(Modern) Identified Point Correctly from URL", async ({ eq }) => {
51+
const values = await identify(url, point);
52+
eq(values, [expectedValue]);
53+
});
54+
55+
test("(Modern) Try to identify point outside raster and correctly returned null from URL", async ({ eq }) => {
56+
const values = await identify(url, [-200, 7.42]);
57+
eq(values, null);
3258
});

‎src/identify/index.js

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

36
/**
47
Given a raster and a point geometry,

0 commit comments

Comments
 (0)
Please sign in to comment.