-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patharray.test.ts
More file actions
170 lines (159 loc) · 4.62 KB
/
Copy patharray.test.ts
File metadata and controls
170 lines (159 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { Affine } from "@developmentseed/affine";
import { describe, expect, it } from "vitest";
import type {
RasterArrayBandSeparate,
RasterArrayPixelInterleaved,
} from "../src/array.js";
import {
packBandsToRGBA,
reorderBands,
toBandSeparate,
toPixelInterleaved,
} from "../src/array.js";
import type { GeographicCRS } from "../src/crs.js";
const EPSG_4326: GeographicCRS = {
$schema: "https://proj.org/schemas/v0.7/projjson.schema.json",
type: "GeographicCRS",
name: "WGS 84",
datum_ensemble: {
name: "World Geodetic System 1984 ensemble",
members: [
{
name: "World Geodetic System 1984 (Transit)",
id: { authority: "EPSG", code: 1166 },
},
{
name: "World Geodetic System 1984 (G730)",
id: { authority: "EPSG", code: 1152 },
},
{
name: "World Geodetic System 1984 (G873)",
id: { authority: "EPSG", code: 1153 },
},
{
name: "World Geodetic System 1984 (G1150)",
id: { authority: "EPSG", code: 1154 },
},
{
name: "World Geodetic System 1984 (G1674)",
id: { authority: "EPSG", code: 1155 },
},
{
name: "World Geodetic System 1984 (G1762)",
id: { authority: "EPSG", code: 1156 },
},
{
name: "World Geodetic System 1984 (G2139)",
id: { authority: "EPSG", code: 1309 },
},
{
name: "World Geodetic System 1984 (G2296)",
id: { authority: "EPSG", code: 1383 },
},
],
ellipsoid: {
name: "WGS 84",
semi_major_axis: 6378137,
inverse_flattening: 298.257223563,
},
accuracy: "2.0",
id: { authority: "EPSG", code: 6326 },
},
coordinate_system: {
subtype: "ellipsoidal",
axis: [
{
name: "Geodetic latitude",
abbreviation: "Lat",
direction: "north",
unit: "degree",
},
{
name: "Geodetic longitude",
abbreviation: "Lon",
direction: "east",
unit: "degree",
},
],
},
};
function baseMetadata() {
return {
width: 2,
height: 2,
transform: [1, 0, 0, 0, -1, 0] as Affine,
crs: EPSG_4326,
nodata: null,
mask: null,
};
}
describe("RasterArray helpers", () => {
it("converts band-separate data to pixel-interleaved", () => {
const raster: RasterArrayBandSeparate = {
...baseMetadata(),
layout: "band-separate",
count: 3,
bands: [
new Uint16Array([1, 2, 3, 4]),
new Uint16Array([10, 20, 30, 40]),
new Uint16Array([100, 200, 300, 400]),
],
};
const interleaved = toPixelInterleaved(raster);
expect(interleaved.layout).toBe("pixel-interleaved");
expect(Array.from(interleaved.data)).toEqual([
1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 40, 400,
]);
});
it("converts pixel-interleaved data to band-separate", () => {
const raster: RasterArrayPixelInterleaved = {
...baseMetadata(),
layout: "pixel-interleaved",
count: 3,
data: new Uint16Array([1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 40, 400]),
};
const planar = toBandSeparate(raster);
expect(planar.layout).toBe("band-separate");
expect(Array.from(planar.bands[0]!)).toEqual([1, 2, 3, 4]);
expect(Array.from(planar.bands[1]!)).toEqual([10, 20, 30, 40]);
expect(Array.from(planar.bands[2]!)).toEqual([100, 200, 300, 400]);
});
it("reorders bands without repacking to pixel layout", () => {
const raster: RasterArrayBandSeparate = {
...baseMetadata(),
layout: "band-separate",
count: 3,
bands: [
new Uint8Array([1, 2, 3, 4]),
new Uint8Array([10, 20, 30, 40]),
new Uint8Array([100, 110, 120, 130]),
],
};
const reordered = reorderBands(raster, [2, 0, 1]);
expect(reordered.count).toBe(3);
expect(Array.from(reordered.bands[0]!)).toEqual([100, 110, 120, 130]);
expect(Array.from(reordered.bands[1]!)).toEqual([1, 2, 3, 4]);
expect(Array.from(reordered.bands[2]!)).toEqual([10, 20, 30, 40]);
});
it("packs selected bands to RGBA", () => {
const raster: RasterArrayBandSeparate = {
...baseMetadata(),
layout: "band-separate",
count: 3,
bands: [
new Uint8Array([1, 2, 3, 4]),
new Uint8Array([10, 20, 30, 40]),
new Uint8Array([100, 110, 120, 130]),
],
};
const packed = packBandsToRGBA(raster, {
order: [2, 1, 0, null],
fillValue: 255,
});
expect(packed.layout).toBe("pixel-interleaved");
expect(packed.count).toBe(4);
expect(Array.from(packed.data)).toEqual([
100, 10, 1, 255, 110, 20, 2, 255, 120, 30, 3, 255, 130, 40, 4, 255,
]);
});
});