Comparing version 0.0.1 to 1.0.0-0
{ | ||
"name": "xdim", | ||
"version": "0.0.1", | ||
"version": "1.0.0-0", | ||
"description": "Multi-Dimensional Functions. Create, Query, and Transform Multi-Dimensional Data.", | ||
@@ -5,0 +5,0 @@ "main": "xdim.js", |
@@ -89,6 +89,5 @@ # xdim | ||
## clip | ||
The `clip` function is almost the same as [select](#select) except that it returns an array of numbers instead of just one. | ||
You also pass in a rect instead of a point. The "rect" is a [hyperrectangle](https://en.wikipedia.org/wiki/Hyperrectangle) (i.e. multi-dimensional rectangle) defined by an object with dimension name keys and start and end to the clipping range. | ||
The `clip` function is used to pull out a subsection of the data within a [hyperrectangle](https://en.wikipedia.org/wiki/Hyperrectangle) (i.e. multi-dimensional rectangle), which we call "rect". The "rect" is defined by an object with dimension name keys and a numerical range. The range is "inclusive", including the first and last numbers provided. | ||
```javascript | ||
import { select } from 'xdim'; | ||
import { clip } from 'xdim'; | ||
@@ -102,5 +101,10 @@ // satellite imagery data broken down by band | ||
const result = select({ | ||
const result = clip({ | ||
data, | ||
// if you don't care about the structure of the returned data | ||
// or want to receive your results more quickly, | ||
// you can set flat to true, and it will return a flat array | ||
flat: false, | ||
// each band is a separate array | ||
@@ -116,5 +120,5 @@ // the values in a band are in row-major order | ||
rect: { | ||
band: 2, // 3rd band (blue), where band index starts at zero | ||
row: { start: 55, end: 74 }, // from the 56th to the 75th row (counting from the top) | ||
column: { start: 55, end: 63 } // from the 56th to the 64th column (counting from the left) | ||
band: [2, 2], // 3rd band (blue), where band index starts at zero | ||
row: [55, 74], // from the 56th to the 75th row (counting from the top) | ||
column: [60, 62] // from the 61st to the 63rd column (counting from the left) | ||
} | ||
@@ -126,4 +130,10 @@ }); | ||
{ | ||
// the actual values found within the clipping region | ||
values: [64, 27, 19, 91, 62, ...] | ||
data: [ | ||
// only one band was selected, so we only have one sub-array | ||
// because the original data combined all the rows in the same array | ||
// the result has the same structure | ||
// all the values in band 2 that fall within row 55 to row 74 and column 60 to 62 | ||
[64, 27, 19, 23, 45, 82 ... ] | ||
] | ||
} | ||
@@ -130,0 +140,0 @@ ``` |
37
xdim.js
@@ -107,5 +107,3 @@ const layoutCache = {}; | ||
// clip a hyperrectangle (a multi-dimensional rectangle) | ||
// returns an array of numbers | ||
function clip({ useLayoutCache = true, data, layout, rect, sizes = {} }) { | ||
function clip({ useLayoutCache = true, data, layout, rect, sizes = {}, flat = false }) { | ||
if (typeof layout === "string") layout = parse(layout, { useLayoutCache }); | ||
@@ -119,3 +117,3 @@ | ||
if (arr.type === "Vector") { | ||
const { start, end } = rect[arr.dim]; | ||
const [start, end] = rect[arr.dim]; | ||
new_datas = new_datas.concat(data.slice(start, end + 1)); | ||
@@ -131,3 +129,3 @@ } else { | ||
const { dim } = part; | ||
const { start, end } = rect[dim]; | ||
const [start, end] = rect[dim]; | ||
const new_offsets = []; | ||
@@ -150,3 +148,30 @@ for (let n = start; n <= end; n++) { | ||
return { values: datas }; | ||
if (flat) { | ||
return { | ||
data: datas | ||
}; | ||
} | ||
// prepareResult | ||
const out_sizes = Object.fromEntries(Object.entries(rect).map(([dim, [start, end]]) => [dim, end - start + 1])); | ||
const { data: out_data } = prepareData({ | ||
layout, | ||
sizes: out_sizes | ||
}); | ||
const max_depth = layout.dims.length; | ||
const step = (arr, depth) => { | ||
if (depth === max_depth) { | ||
for (let i = 0; i < arr.length; i++) { | ||
arr[i] = datas.shift(); | ||
} | ||
} else { | ||
arr.forEach(sub => step(sub, depth + 1)); | ||
} | ||
}; | ||
step(out_data, 1); | ||
return { data: out_data }; | ||
} | ||
@@ -153,0 +178,0 @@ |
24638
273
272