Compare commits

...
1 Commits
Author SHA1 Message Date
Matt Walsh 4246240ee4 initial svg commit 2021-11-02 22:33:50 -05:00
6 changed files with 1982 additions and 1415 deletions

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 glob = require('glob');
const fs = require('fs'); const fs = require('fs');
const { plot, writePngToFile } = require('../src'); const { plot, writeSvgToFile } = require('../src');
// list files // list files
// const files = glob.sync('./data/KLOT/34/*'); // 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/548/*');
// const files = glob.sync('./data/KLOT/940/*'); // const files = glob.sync('./data/KLOT/940/*');
// const files = glob.sync('./data/KSRX/804/*'); // const files = glob.sync('./data/KSRX/804/*');
// const files = glob.sync('./data/TORD/767/*'); // 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 files = glob.sync('./data/KLOT/KLOT20210621_041151_V06'); // vel palette tuning
// const store each file's data // const store each file's data
@@ -33,7 +33,7 @@ const plots = [];
// default to all plots unless single is specificed // default to all plots unless single is specificed
const single = process.argv.includes('single'); const single = process.argv.includes('single');
let sizes = [3600, 1800, 900, 800, 600, 450, 100]; let sizes = [900, 450, 100];
let elevations = radarData.listElevations(); let elevations = radarData.listElevations();
if (single) { if (single) {
sizes = sizes.slice(0, 1); sizes = sizes.slice(0, 1);
@@ -51,13 +51,13 @@ if (single) {
elevation, elevation,
size, size,
palettize: true, palettize: true,
cropTo: size / 2, // cropTo: size / 2,
}); });
// write files to disk // write files to disk
await Promise.allSettled([ await Promise.allSettled([
writePngToFile(`./output/REF-${elevation}-${size}.png`, plots[elevation].REF), writeSvgToFile(`./output/REF-${elevation}-${size}.svg`, plots[elevation].REF),
writePngToFile(`./output/VEL-${elevation}-${size}.png`, plots[elevation].VEL), 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", "homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
"dependencies": { "dependencies": {
"canvas": "^2.8.0", "@svgdotjs/svg.js": "^3.1.1",
"nexrad-level-2-data": "file:../nexrad-level-2-data" "nexrad-level-2-data": "file:../nexrad-level-2-data",
"svgdom": "^0.1.8"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.32.0", "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 Palette = require('./palettes');
const palettizeImage = require('./palettize');
// names of data structures keyed to product name // names of data structures keyed to product name
const dataNames = { const dataNames = {
@@ -42,7 +41,25 @@ const DEFAULT_OPTIONS = {
const RAD45 = (45 * Math.PI / 180); const RAD45 = (45 * Math.PI / 180);
const RAD90 = RAD45 * 2; 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) => { 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 // combine options with defaults
const options = { const options = {
...DEFAULT_OPTIONS, ...DEFAULT_OPTIONS,
@@ -58,19 +75,10 @@ const draw = (data, _options) => {
const cropTo = Math.min(options.size, options.cropTo); const cropTo = Math.min(options.size, options.cropTo);
if (options.cropTo < 1) throw new Error('Provide options.cropTo > 0'); if (options.cropTo < 1) throw new Error('Provide options.cropTo > 0');
// create the canvas and context // create the canvas
const canvas = createCanvas(cropTo, cropTo); const canvas = SVG(document.documentElement);
const ctx = canvas.getContext('2d'); canvas.size(cropTo, cropTo);
canvas.viewbox(-cropTo / 2, -cropTo / 2, cropTo, cropTo);
// 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);
// get the palette // get the palette
const palette = palettes[options.product]; const palette = palettes[options.product];
@@ -99,6 +107,8 @@ const draw = (data, _options) => {
// check for data for this product // check for data for this product
if (headers[0][dataName] === undefined) return false; if (headers[0][dataName] === undefined) return false;
const paths = {};
// loop through data // loop through data
headers.forEach((header) => { headers.forEach((header) => {
// get correct data // get correct data
@@ -153,34 +163,40 @@ const draw = (data, _options) => {
// see if there's a sample to plot // see if there's a sample to plot
if (thisSample && !palette.inDeadband(thisSample)) { if (thisSample && !palette.inDeadband(thisSample)) {
ctx.beginPath(); // get color
ctx.strokeStyle = palette.findColorRgba(thisSample); const color = palette.findColorRgba(thisSample);
ctx.arc(0, 0, (idx + deadZone) / scale, startAngle, endAngle);
ctx.stroke(); 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 { return {
canvas: palettized, canvas,
palette: palette.getPalette(),
}; };
}; };
module.exports = { module.exports = {
draw, draw,
DEFAULT_OPTIONS, DEFAULT_OPTIONS,
canvas: canvasObj,
}; };
+2 -1
View File
@@ -1,5 +1,5 @@
const { draw, canvas } = require('./draw'); const { draw, canvas } = require('./draw');
const { writePngToFile } = require('./utils/file'); const { writePngToFile, writeSvgToFile } = require('./utils/file');
/** /**
* Plot level 2 data * Plot level 2 data
* @param {Level2Data} data output from the nexrad-level-2-data library * @param {Level2Data} data output from the nexrad-level-2-data library
@@ -44,5 +44,6 @@ const plot = (data, _products, options) => {
module.exports = { module.exports = {
plot, plot,
writePngToFile, writePngToFile,
writeSvgToFile,
canvas, canvas,
}; };
+12
View File
@@ -26,6 +26,18 @@ const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
writeStream.on('error', (e) => reject(e)); 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 = { module.exports = {
writePngToFile, writePngToFile,
writeSvgToFile,
}; };