Skip to content

Commit

Permalink
mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jun 27, 2020
1 parent e6d892b commit 2d524a9
Show file tree
Hide file tree
Showing 7 changed files with 2,675 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ dist

# TernJS port file
.tern-port

*.tif
*.tiff
test/data/*.json
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# geotiff-palette
Get the Palette (aka Color Map) for a GeoTIFF

# install
```bash
npm install geotiff-palette
```

# usage
## downloaded GeoTIFF
```
const { readFileSync } = require('fs');
const { fromArrayBuffer } = require('geotiff');
const { getPalette } = require('geotiff-palette');
const data = readFileSync('image.tif');
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
const geotiff = await fromArrayBuffer(arrayBuffer);
const image = await geotiff.getImage();
const palette = await getPalette(image);
console.log(palette);
```
palette will look something like this:
```
[
[ 112, 108, 96, 255 ], [ 112, 104, 80, 255 ], [ 104, 104, 104, 255 ],
[ 96, 88, 52, 255 ], [ 104, 104, 112, 255 ], [ 128, 120, 108, 255 ],
[ 160, 160, 160, 255 ], [ 152, 152, 144, 255 ], [ 104, 96, 72, 255 ],
[ 96, 80, 44, 255 ], [ 40, 48, 56, 255 ], [ 152, 144, 144, 255 ],
[ 32, 48, 56, 255 ], [ 120, 120, 96, 255 ], [ 112, 120, 104, 255 ],
... 256 total items
]
```

# support
Post an issue at https://github.com/GeoTIFF/geotiff-palette or email the package author at daniel.j.dufour@gmail.com
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const getPalette = (image, { debug = false } = { debug: false }) => {
if (debug) console.log("starting getPalette with image", image);
const { fileDirectory } = image;
const {
BitsPerSample,
ColorMap,
ImageLength,
ImageWidth,
PhotometricInterpretation,
SampleFormat,
SamplesPerPixel
} = fileDirectory;

if (!ColorMap) {
throw new Error("[geotiff-palette]: the image does not contain a color map, so we can't make a palette.");
}

const count = Math.pow(2, BitsPerSample);
if (debug) console.log("[geotiff-palette]: count:", count);

const bandSize = ColorMap.length / 3;
if (debug) console.log("[geotiff-palette]: bandSize:", bandSize);

if (bandSize !== count) {
throw new Error("[geotiff-palette]: can't handle situations where the color map has more or less values than the number of possible values in a raster");
}

const greenOffset = bandSize;
const redOffset = greenOffset + bandSize;

const result = [];
for (let i = 0; i < count; i++) {
// colorMap[mapIndex] / 65536 * 256 equals colorMap[mapIndex] / 256
// because (1 / 2^16) * (2^8) equals 1 / 2^8
result.push([
Math.floor(ColorMap[i] / 256), // red
Math.floor(ColorMap[greenOffset + i] / 256), // green
Math.floor(ColorMap[redOffset + i] / 256), // blue
255 // alpha value is always 255
]);
}
if (debug) console.log("[geotiff-palette]: result is ", result);
return result;
}

module.exports = { getPalette };
Loading

0 comments on commit 2d524a9

Please sign in to comment.