Skip to content

Commit

Permalink
refactored stats
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Dec 10, 2023
1 parent 0533f85 commit be8f405
Show file tree
Hide file tree
Showing 27 changed files with 132 additions and 109 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import median from "./median";
import min from "./min";
import mode from "./mode";
import parse from "./parse";
import range from "./range";
import rasterCalculator from "./raster-calculator";
import sum from "./sum";
import stat from "./stat";
import stats from "./stats";

const geoblaze = {
Expand All @@ -27,9 +29,11 @@ const geoblaze = {
min,
mode,
parse,
range,
rasterCalculator,
sum,
stats
stat,
stats,
sum
};

export default geoblaze;
Expand Down
7 changes: 6 additions & 1 deletion src/max/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The max function takes a georaster and an optional geometry.
* If a geometry is included, the function returns the max of all the pixels
Expand All @@ -15,4 +17,7 @@
* // maxs is [red, green, blue, nir]
* [231, 242, 254, 255]
*/
export { default } from "./max.module";

export default function max(georaster, geometry, test) {
return stat(georaster, geometry, "max", test);
}
6 changes: 0 additions & 6 deletions src/max/max.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/max/max.module.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/mean/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The mean function takes a georaster and an optional geometry.
* If a geometry is included, the function returns the mean of all the pixels
Expand All @@ -15,4 +17,7 @@
* // means is [red, green, blue, nir]
* [42, 84, 92, 94]
*/
export { default } from "./mean.module";

export default function mean(georaster, geometry, test) {
return stat(georaster, geometry, "mean", test);
}
6 changes: 0 additions & 6 deletions src/mean/mean.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/mean/mean.module.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/median/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The median function takes a raster and an optional geometry.
* If a geometry is included, the function returns the median of all the pixels
Expand All @@ -15,4 +17,7 @@
* // medians is [red, green, blue, nir]
* [42, 84, 92, 94]
*/
export { default } from "./median.module";

export default function median(georaster, geometry, test) {
return stat(georaster, geometry, "median", test);
}
6 changes: 0 additions & 6 deletions src/median/median.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/median/median.module.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/min/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The min function takes a georaster as an input and an optional geometry.
* If a geometry is included, the function returns the min of all the pixels
Expand All @@ -15,4 +17,7 @@
* // mins is [red, green, blue, nir]
* [1, 4, 9, 4]
*/
export { default } from "./min.module";

export default function min(georaster, geometry, test) {
return stat(georaster, geometry, "min", test);
}
6 changes: 0 additions & 6 deletions src/min/min.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/min/min.module.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/mode/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The mode function takes a georaster and an optional geometry.
* If a geometry is included, the function returns the mode of all the pixels
Expand All @@ -16,4 +18,7 @@
* // results is [red, green, blue, nir]
* [42, 84, 92, 94]
*/
export { default } from "./mode.module";

export default function mode(georaster, geometry, test) {
return stat(georaster, geometry, "mode", test);
}
6 changes: 0 additions & 6 deletions src/mode/mode.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/mode/mode.module.js

This file was deleted.

6 changes: 5 additions & 1 deletion src/modes/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import modes from "../stat";

/**
* The modes function takes a georaster and an optional geometry.
* If a geometry is included, the function returns the modes of all the pixels
Expand All @@ -17,4 +19,6 @@
* [[ 42, 43] , [83, 82, 84], [92], [94]]
*/

export { default } from "./modes.module";
export default function modes(georaster, geometry, test) {
return stat(georaster, geometry, "modes", test);
}
6 changes: 0 additions & 6 deletions src/modes/modes.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/modes/modes.module.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/range/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import stat from "../stat";

/**
* The range function takes a georaster as an input and an optional geometry.
* If a geometry is included, the function returns the range of all the pixels
* in that area for each band. If no geometry is included, the function returns the range of
* all the pixels for each band in the raster.
* @name min
* @param {GeoRaster|ArrayBuffer|Blob|Buffer|File|String} georaster - geospatial gridded raster data
* @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.
* @returns {Number[]} array of ranges for each band
* @example
* // naip.tif has 4-bands: red, green, blue and near-infrared (nir)
* const url = "https://example.org/naif.tif";
*
* const ranges = await geoblaze.range(url, geometry);
* // ranges is [red, green, blue, nir]
* [231, 234, 229, 0]
*/

export default function range(georaster, geometry, test) {
return stat(georaster, geometry, "range", test);
}
43 changes: 43 additions & 0 deletions src/range/range.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test from "flug";
import { serve } from "srvd";
import range from "./index";

serve({ debug: true, max: 10, port: 3000 });

const url = "http://localhost:3000/data/test.tiff";
const bbox = [80.63, 7.42, 84.21, 10.1];
const expectedBboxValue = 5166.7;

const polygon = [
[
[83.12255859375, 22.49225722008518],
[82.96875, 21.57571893245848],
[81.58447265624999, 1.207458730482642],
[83.07861328125, 20.34462694382967],
[83.8037109375, 19.497664168139053],
[84.814453125, 19.766703551716976],
[85.078125, 21.166483858206583],
[86.044921875, 20.838277806058933],
[86.98974609375, 22.49225722008518],
[85.58349609375, 24.54712317973075],
[84.6826171875, 23.36242859340884],
[83.12255859375, 22.49225722008518]
]
];

test("(Modern) Get Range from Bounding Box", async ({ eq }) => {
const results = await range(url, bbox);
const value = Number(results[0].toFixed(2));
eq(value, expectedBboxValue);
});

test("(Modern) Get Range from Polygon", async ({ eq }) => {
const results = await range(url, polygon);
const value = Number(results[0].toFixed(2));
eq(value, 7807.4);
});

test("(Modern) Get Range from whole Raster", async ({ eq }) => {
const results = await range(url);
eq(results, [8131.2]);
});
14 changes: 14 additions & 0 deletions src/stat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* The stat function takes a georaster and an optional geometry.
* It then uses the <a href="https://github.com/DanielJDufour/calc-stats" target="_blank">calc-stats</a> library
* to calculate statistics for each band.
* @name stat
* @param {GeoRaster|ArrayBuffer|Blob|Buffer|File|String} georaster - geospatial gridded raster data
* @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.
* @param {String} stat - name of statistic according to <a href="https://github.com/DanielJDufour/calc-stats" target="_blank">calc-stats</a>
* @returns {Object} array of stat for each band
* @example
await geoblaze.stat("https://example.org/rgb.tif", geometry, "median");
[22, 32, 34]
*/
export { default } from "./stat.module";
6 changes: 6 additions & 0 deletions src/stat/stat.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import QuickPromise from "quick-promise";
import stats from "../stats";

export default function stat(georaster, geometry, stat, test) {
return QuickPromise.resolve(stats(georaster, geometry, { stats: [stat] }, test)).then(stats => stats.map(it => it[stat]));
}
6 changes: 5 additions & 1 deletion src/sum/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat from "../stat";

/**
* The sum function takes a georaster and an optional geometry.
* If a geometry is included, the function returns the sum of all the pixels
Expand All @@ -15,4 +17,6 @@
* // results is [red, green, blue, nir]
* [217461, 21375, 57312, 457125]
*/
export { default } from "./sum.module";
export default function sum(georaster, geometry, test) {
return stat(georaster, geometry, "sum", test);
}
6 changes: 0 additions & 6 deletions src/sum/sum.core.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/sum/sum.module.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/utils.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ const utils = {
}
},

resolve(it) {
if (typeof it === "object" && typeof it.then === "function") {
// it appears to be a promise
return it;
} else {
return { then: func => func(it) };
}
},

range(start, stop, step = 1) {
const results = [];
for (let i = start; step < 0 ? i > stop : i < stop; i += step) {
Expand Down

0 comments on commit be8f405

Please sign in to comment.