open-vector-tile
Version:
This library reads/writes Open Vector Tiles
108 lines • 3.32 kB
JavaScript
import { Pbf as Protobuf } from 'pbf-ts';
/** Elevation object to read from */
export class ImageData {
#pbf;
name = 'default';
imagePos = 0;
type = 'png';
width = 512;
height = 512;
/**
* @param pbf - the pbf protocol we are reading from
* @param end - the position to stop at
*/
constructor(pbf, end) {
this.#pbf = pbf;
pbf.readFields(this.#readImage, this, end);
}
/**
* Reads the image data
* @returns - the image data
*/
image() {
this.#pbf.pos = this.imagePos;
return this.#pbf.readBytes();
}
/**
* @param tag - the tag to read
* @param imageData - the image data to mutate
* @param pbf - the Protobuf to pull the appropriate data from
*/
#readImage(tag, imageData, pbf) {
if (tag === 1)
imageData.type = fromImageType(pbf.readVarint());
else if (tag === 2)
imageData.width = pbf.readVarint();
else if (tag === 3)
imageData.height = pbf.readVarint();
else if (tag === 4)
imageData.imagePos = pbf.pos;
else if (tag === 5)
imageData.name = pbf.readString();
}
}
/**
* @param input - the image data & metadata to encode
* @returns - the encoded tile data
*/
export function writeImageData(input) {
const pbf = new Protobuf();
const { type, width, height, image } = input;
pbf.writeVarintField(1, toImageType(type));
pbf.writeVarintField(2, width);
pbf.writeVarintField(3, height);
pbf.writeBytesField(4, image);
pbf.writeStringField(5, input.name);
return pbf.commit();
}
/**
* @param imageType - the enum to convert
* @returns - the string
*/
export function fromImageType(imageType) {
if (imageType === 0 /* ImageType.PNG */)
return 'png';
else if (imageType === 1 /* ImageType.JPG */)
return 'jpg';
else if (imageType === 2 /* ImageType.WEBP */)
return 'webp';
else if (imageType === 3 /* ImageType.GIF */)
return 'gif';
else if (imageType === 4 /* ImageType.AVIF */)
return 'avif';
else if (imageType === 5 /* ImageType.SVG */)
return 'svg';
else if (imageType === 6 /* ImageType.BMP */)
return 'bmp';
else if (imageType === 7 /* ImageType.RAW */)
return 'raw';
else if (imageType === 8 /* ImageType.UNKNOWN */)
return 'unknown';
throw new Error('Invalid image type');
}
/**
* @param imageType - the string to convert
* @returns - the enum
*/
export function toImageType(imageType) {
if (imageType === 'png')
return 0 /* ImageType.PNG */;
else if (imageType === 'jpg')
return 1 /* ImageType.JPG */;
else if (imageType === 'webp')
return 2 /* ImageType.WEBP */;
else if (imageType === 'gif')
return 3 /* ImageType.GIF */;
else if (imageType === 'avif')
return 4 /* ImageType.AVIF */;
else if (imageType === 'svg')
return 5 /* ImageType.SVG */;
else if (imageType === 'bmp')
return 6 /* ImageType.BMP */;
else if (imageType === 'raw')
return 7 /* ImageType.RAW */;
else if (imageType === 'unknown')
return 8 /* ImageType.UNKNOWN */;
throw new Error('Invalid image type');
}
//# sourceMappingURL=imageLayer.js.map