initial svg commit

This commit is contained in:
Matt Walsh committed 2021-11-02 22:33:50 -05:00
1 parent 395e3fc78e
commit 4246240ee4
6 files changed
+1982 -1415

No files matched your search

+7 -7
View File
@@ -3,16 +3,16 @@ const { Level2Radar } = require('nexrad-level-2-data');
const glob = require('glob');
const fs = require('fs');
const { plot, writePngToFile } = require('../src');
const { plot, writeSvgToFile } = require('../src');
// list files
// const files = glob.sync('./data/KLOT/34/*');
// const files = glob.sync('./data/KLOT/381/*');
const files = glob.sync('./data/KLOT/381/*');
// const files = glob.sync('./data/KLOT/548/*');
// const files = glob.sync('./data/KLOT/940/*');
// const files = glob.sync('./data/KSRX/804/*');
// const files = glob.sync('./data/TORD/767/*');
const files = glob.sync('./data/KLOT/KLOT20210812_171451_V*'); // ref palette tuning
// const files = glob.sync('./data/KLOT/KLOT20210812_171451_V*'); // ref palette tuning
// const files = glob.sync('./data/KLOT/KLOT20210621_041151_V06'); // vel palette tuning
// const store each file's data
@@ -33,7 +33,7 @@ const plots = [];
// default to all plots unless single is specificed
const single = process.argv.includes('single');
let sizes = [3600, 1800, 900, 800, 600, 450, 100];
let sizes = [900, 450, 100];
let elevations = radarData.listElevations();
if (single) {
sizes = sizes.slice(0, 1);
@@ -51,13 +51,13 @@ if (single) {
elevation,
size,
palettize: true,
cropTo: size / 2,
// cropTo: size / 2,
});
// write files to disk
await Promise.allSettled([
writePngToFile(`./output/REF-${elevation}-${size}.png`, plots[elevation].REF),
writePngToFile(`./output/VEL-${elevation}-${size}.png`, plots[elevation].VEL),
writeSvgToFile(`./output/REF-${elevation}-${size}.svg`, plots[elevation].REF),
writeSvgToFile(`./output/VEL-${elevation}-${size}.svg`, plots[elevation].VEL),
]);
}));
}));
+1908 -1371
View File
File diff suppressed because it is too large. Load diff
+3 -2
View File
@@ -24,8 +24,9 @@
},
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
"dependencies": {
"canvas": "^2.8.0",
"nexrad-level-2-data": "file:../nexrad-level-2-data"
"@svgdotjs/svg.js": "^3.1.1",
"nexrad-level-2-data": "file:../nexrad-level-2-data",
"svgdom": "^0.1.8"
},
"devDependencies": {
"eslint": "^7.32.0",
+50 -34
View File
@@ -1,9 +1,8 @@
const canvasObj = require('canvas');
const { createSVGWindow } = require('svgdom');
const { createCanvas } = canvasObj;
const { SVG, registerWindow } = require('@svgdotjs/svg.js');
const Palette = require('./palettes');
const palettizeImage = require('./palettize');
// names of data structures keyed to product name
const dataNames = {
@@ -42,7 +41,25 @@ const DEFAULT_OPTIONS = {
const RAD45 = (45 * Math.PI / 180);
const RAD90 = RAD45 * 2;
// round to specified number of decimal points
const roundTo = (decimals, value) => Math.round(value * 10 ** decimals) / 10 ** decimals;
// polar to cartesean conversion
const polarToCartesean = (r, theta) => {
const x = roundTo(2, r * Math.cos(theta));
const y = roundTo(2, r * Math.sin(theta));
return {
x, y,
};
};
const draw = (data, _options) => {
// create a new window
const window = createSVGWindow();
const { document } = window;
// register window and document
registerWindow(window, document);
// combine options with defaults
const options = {
...DEFAULT_OPTIONS,
@@ -58,19 +75,10 @@ const draw = (data, _options) => {
const cropTo = Math.min(options.size, options.cropTo);
if (options.cropTo < 1) throw new Error('Provide options.cropTo > 0');
// create the canvas and context
const canvas = createCanvas(cropTo, cropTo);
const ctx = canvas.getContext('2d');
// fill background with black
ctx.fillStyle = options.background;
ctx.fillRect(0, 0, cropTo, cropTo);
// canvas settings
ctx.imageSmoothingEnabled = true;
ctx.lineWidth = options.lineWidth;
ctx.translate(cropTo / 2, cropTo / 2);
ctx.rotate(-Math.PI / 2);
// create the canvas
const canvas = SVG(document.documentElement);
canvas.size(cropTo, cropTo);
canvas.viewbox(-cropTo / 2, -cropTo / 2, cropTo, cropTo);
// get the palette
const palette = palettes[options.product];
@@ -99,6 +107,8 @@ const draw = (data, _options) => {
// check for data for this product
if (headers[0][dataName] === undefined) return false;
const paths = {};
// loop through data
headers.forEach((header) => {
// get correct data
@@ -153,34 +163,40 @@ const draw = (data, _options) => {
// see if there's a sample to plot
if (thisSample && !palette.inDeadband(thisSample)) {
ctx.beginPath();
ctx.strokeStyle = palette.findColorRgba(thisSample);
ctx.arc(0, 0, (idx + deadZone) / scale, startAngle, endAngle);
ctx.stroke();
// get color
const color = palette.findColorRgba(thisSample);
if (!paths[color]) paths[color] = [];
// calculate radius
const r = (idx + deadZone) / scale;
const rRounded = roundTo(2, r);
// calculate start of arc
const arcStartCartesean = polarToCartesean(r, startAngle);
paths[color].push(['M', arcStartCartesean.x, arcStartCartesean.y]);
// calculate end of arc
const arcEndCartesean = polarToCartesean(r, endAngle);
// end of arc and flags
paths[color].push(['A', rRounded, rRounded, 0, 0, 0, arcEndCartesean.x, arcEndCartesean.y]);
}
}
}
});
// draw each path
Object.entries(paths).forEach(([color, path]) => {
path.push('z');
canvas.path(path.flat()).stroke({ color });
});
if (!options.palettize) {
// return the palette and canvas
return {
canvas,
};
}
// palettize image
const palettized = palettizeImage(canvas.getContext('2d'), palette);
// return palettized image
return {
canvas: palettized,
palette: palette.getPalette(),
canvas,
};
};
module.exports = {
draw,
DEFAULT_OPTIONS,
canvas: canvasObj,
};
+2 -1
View File
@@ -1,5 +1,5 @@
const { draw, canvas } = require('./draw');
const { writePngToFile } = require('./utils/file');
const { writePngToFile, writeSvgToFile } = require('./utils/file');
/**
* Plot level 2 data
* @param {Level2Data} data output from the nexrad-level-2-data library
@@ -44,5 +44,6 @@ const plot = (data, _products, options) => {
module.exports = {
plot,
writePngToFile,
writeSvgToFile,
canvas,
};
+12
View File
@@ -26,6 +26,18 @@ const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
writeStream.on('error', (e) => reject(e));
});
const writeSvgToFile = async (fileName, data) => {
// the draw function will return false if no data was found, short circuit the output here
if (!data.canvas) {
return;
}
// write to the stream
await fs.promises.writeFile(fileName, data.canvas.svg());
console.log(fileName);
};
module.exports = {
writePngToFile,
writeSvgToFile,
};