arso-rainfall-intensity
Version:
!!!Only for slovenia!!! Provides simple api for fetching radar images of rainfall intensity and parsing from pixels and converting pixel coordinates to pairs of latitude and longitude or vice versa.
79 lines • 3.49 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const proj4_1 = __importDefault(require("proj4"));
const pngjs_1 = require("pngjs");
const assertions_1 = require("./assertions");
class RadarImageProjection {
constructor(degreeUnitProjectionName, meterUnitProjectionName, degBboxSW, degBboxNE) {
this.degreeP = proj4_1.default.Proj(degreeUnitProjectionName);
this.meterP = proj4_1.default.Proj(meterUnitProjectionName);
this.mBoundSW = this.projectDegrees2MeterUnits(degBboxSW);
this.mBoundNE = this.projectDegrees2MeterUnits(degBboxNE);
this.dBoundSW = degBboxSW;
this.dBoundNE = degBboxNE;
this.image = null;
}
get width() {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
return this.image.width;
}
get height() {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
return this.image.height;
}
loadImageFromBuffer(buffer) {
this.image = pngjs_1.PNG.sync.read(buffer, { checkCRC: false });
}
getImageBuffer() {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
return pngjs_1.PNG.sync.write(this.image);
}
getPixelInfo({ x, y }) {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
const idx = (this.image.width * y + x) << 2;
return {
r: this.image.data[idx],
g: this.image.data[idx + 1],
b: this.image.data[idx + 2],
a: this.image.data[idx + 3]
};
}
projectImagePixelToDegreeUnit({ x, y }) {
const pt = this.projectImagePixelToMeterUnit({ x, y });
return this.projectMetersToDegreeUnits(pt);
}
projectImagePixelToMeterUnit({ x, y }) {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
const pxMeters = this.mBoundSW.x + (this.mBoundNE.x - this.mBoundSW.x) * x / this.image.width;
const pyMeters = this.mBoundNE.y + (this.mBoundSW.y - this.mBoundNE.y) * y / this.image.height;
return { x: pxMeters, y: pyMeters };
}
projectDegreeUnitToImagePixel({ x, y }) {
const pt = this.projectDegrees2MeterUnits({ x, y });
return this.projectMeterUnitToImagePixel(pt);
}
projectMeterUnitToImagePixel({ x, y }) {
assertions_1.assertNotNull(this.image, 'Image not loaded.');
const mInput = { x, y };
const rX = (mInput.x - this.mBoundSW.x) / (this.mBoundNE.x - this.mBoundSW.x);
const rY = (mInput.y - this.mBoundNE.y) / (this.mBoundSW.y - this.mBoundNE.y);
if (rX < 0 || rX > 1 || rY < 0 || rY > 1) {
throw new Error('given units are not inside defined bounds.');
}
return {
x: Math.min(Math.floor(rX * this.image.width), this.image.width - 1),
y: Math.min(Math.floor(rY * this.image.height), this.image.height - 1)
};
}
projectDegrees2MeterUnits({ x, y }) {
return proj4_1.default.transform(this.degreeP, this.meterP, { x, y });
}
projectMetersToDegreeUnits({ x, y }) {
return proj4_1.default.transform(this.meterP, this.degreeP, { x, y });
}
}
exports.default = RadarImageProjection;
//# sourceMappingURL=radar-image-projection.js.map