-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6d892b
commit 2d524a9
Showing
7 changed files
with
2,675 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,7 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
*.tif | ||
*.tiff | ||
test/data/*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.