Skip to content

Commit 9474c3d

Browse files
committedOct 3, 2021
added parse
1 parent 6103a1f commit 9474c3d

File tree

5 files changed

+161
-36
lines changed

5 files changed

+161
-36
lines changed
 

‎src/index.js

+26-36
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import bandArithmetic from './band-arithmetic';
2-
import cache from './cache';
3-
import get from './get';
4-
import histogram from './histogram';
5-
import identify from './identify';
6-
import load from './load';
7-
import max from './max';
8-
import mean from './mean';
9-
import median from './median';
10-
import min from './min';
11-
import mode from './mode';
12-
import rasterCalculator from './raster-calculator';
13-
import sum from './sum';
1+
/**
2+
* @prettier
3+
*/
4+
import bandArithmetic from "./band-arithmetic";
5+
import cache from "./cache";
6+
import get from "./get";
7+
import histogram from "./histogram";
8+
import identify from "./identify";
9+
import load from "./load";
10+
import max from "./max";
11+
import mean from "./mean";
12+
import median from "./median";
13+
import min from "./min";
14+
import mode from "./mode";
15+
import parse from "./parse";
16+
import rasterCalculator from "./raster-calculator";
17+
import sum from "./sum";
1418

1519
const geoblaze = {
1620
cache,
@@ -24,40 +28,26 @@ const geoblaze = {
2428
median,
2529
min,
2630
mode,
31+
parse,
2732
rasterCalculator,
28-
sum,
33+
sum
2934
};
3035

3136
export default geoblaze;
3237

33-
export {
34-
cache,
35-
bandArithmetic,
36-
get,
37-
histogram,
38-
identify,
39-
load,
40-
max,
41-
mean,
42-
median,
43-
min,
44-
mode,
45-
rasterCalculator,
46-
sum
47-
};
48-
38+
export { cache, bandArithmetic, get, histogram, identify, load, max, mean, median, min, mode, parse, rasterCalculator, sum };
4939

5040
/* set window.geoblaze in the browser */
51-
if (typeof window !== 'undefined') {
52-
window['geoblaze'] = geoblaze;
41+
if (typeof window !== "undefined") {
42+
window["geoblaze"] = geoblaze;
5343
}
5444

5545
/* set self.geoblaze in a web worker */
56-
if (typeof self !== 'undefined') {
57-
self['geoblaze'] = geoblaze;
46+
if (typeof self !== "undefined") {
47+
self["geoblaze"] = geoblaze;
5848
}
5949

6050
/* set global.geoblaze in node */
61-
if (typeof global !== 'undefined') {
62-
global['geoblaze'] = geoblaze;
51+
if (typeof global !== "undefined") {
52+
global["geoblaze"] = geoblaze;
6353
}

‎src/parse/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @prettier
3+
*/
4+
import parse from "./parse.module";
5+
6+
/**
7+
* The parse function takes a url to a geotiff or geotiff file as an input
8+
* and returns a promise. The promise resolves as a georaster, which
9+
* can be used as input in other geoblaze methods, such as identify, sum,
10+
* or histogram.
11+
* @name parse
12+
* @param {Object|string} urlOrFile - a string representation of a url or a geotiff file
13+
* @example
14+
* const georaster = await geoblaze.parse(urlOrFile);
15+
* const sums = sum(georaster, geometry);
16+
*/
17+
export default parse;

‎src/parse/parse.module.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @prettier
3+
*/
4+
import parseGeoRaster from "georaster";
5+
import nodeUrl from "url";
6+
import { ERROR_PARSING_GEOTIFF } from "../error-constants";
7+
import cache from "../cache";
8+
import utils from "../utils";
9+
10+
const inBrowser = typeof window === "object";
11+
12+
const URL = inBrowser ? window.URL : nodeUrl.parse;
13+
14+
async function parseGeorasterWithErrorHandling(it, { logErrors }) {
15+
try {
16+
return await parseGeoRaster(it);
17+
} catch (error) {
18+
if (logErrors) console.error(error);
19+
throw new Error(ERROR_PARSING_GEOTIFF);
20+
}
21+
}
22+
23+
export default async function parse(it, { logErrors = true } = { logErrors: true }) {
24+
if (["Blob", "File"].includes(utils.getConstructorName(it))) it = URL.createObjectURL(it);
25+
26+
if (typeof it === "string") {
27+
if (!cache[it]) cache[it] = parseGeorasterWithErrorHandling(it, { logErrors });
28+
return await cache[it];
29+
} else {
30+
return await parseGeorasterWithErrorHandling(it);
31+
}
32+
}

‎src/parse/parse.test.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @prettier
3+
*/
4+
import test from "flug";
5+
import fetch from "cross-fetch";
6+
import parse from "./index";
7+
import { serve } from "srvd";
8+
9+
import {
10+
// ERROR_BAD_URL
11+
ERROR_PARSING_GEOTIFF
12+
} from "../error-constants";
13+
14+
const path = "http://localhost:3000/data/test.tiff";
15+
const incorrectPath = "http://localhost:3000/data/this-is-not-a-real-dataset.tiff";
16+
const incorrectPath2 = "this-is-a-fake-path";
17+
//const incorrectPath3 = 'http://localhost:3000/data/random-file';
18+
19+
const properties = [
20+
"projection",
21+
"xmin"
22+
// 'values'
23+
];
24+
25+
if (require.main === module) serve({ debug: true, port: 3000, wait: 15 });
26+
27+
test("Loaded tiff export from geonode", async ({ eq }) => {
28+
const url = "https://s3.amazonaws.com/georaster/geonode_atlanteil.tif";
29+
const georaster = await parse(url, { logErrors: false });
30+
properties.forEach(property => {
31+
eq(property in georaster, true);
32+
});
33+
});
34+
35+
test("Loaded tiff file", async ({ eq }) => {
36+
const georaster = await parse(path, { logErrors: false });
37+
properties.forEach(property => {
38+
eq(property in georaster, true);
39+
});
40+
});
41+
42+
test("Error from invalid URL", async ({ eq }) => {
43+
let message;
44+
try {
45+
await parse(incorrectPath, { logErrors: false });
46+
} catch (error) {
47+
({ message } = error);
48+
}
49+
eq(message, ERROR_PARSING_GEOTIFF);
50+
});
51+
52+
test("Load: Error from another invalid URL tiff file", async ({ eq }) => {
53+
let message;
54+
try {
55+
await parse(incorrectPath2, { logErrors: false });
56+
} catch (error) {
57+
({ message } = error);
58+
}
59+
eq(message, ERROR_PARSING_GEOTIFF);
60+
});
61+
62+
/*
63+
// commenting out this test because failing because Unhandled Promise Rejection
64+
// in georaster
65+
describe('Error from an invalid file', function () {
66+
this.timeout(10);
67+
it('Loaded tiff file', () => {
68+
try {
69+
return parse(incorrectPath3).then(null, error => {
70+
expect(error.message).to.equal(ERROR_PARSING_GEOTIFF);
71+
});
72+
} catch(error) {
73+
expect(error.message).to.equal(ERROR_PARSING_GEOTIFF);
74+
}
75+
});
76+
});
77+
*/
78+
79+
// test('Loaded from tiff file', async ({ eq }) => {
80+
// const blob = await fetch(path);
81+
// const tiff = await parse(blob);
82+
// let image = tiff.getImage();
83+
// properties.forEach(property => {
84+
// eq(property in image);
85+
// })
86+
// });
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.