niViz

niViz Git Source Tree

Root/convert-native.js

1/*
2 * niViz -- snow profiles visualization
3 * Copyright (C) 2015 WSL/SLF - Fluelastrasse 11 - 7260 Davos Dorf - Switzerland.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19// NOTE: you must run make dep && make dist before running this script:
20// node convert-native.js
21
22const fs = require('fs');
23const path = require('path');
24const moment = require('moment');
25const jsdom = require('jsdom');
26const { JSDOM } = jsdom;
27const { convert } = require('convert-svg-to-png');
28
29let window = initEnv();
30
31let niviz = require('./dist/lib/niviz-latest.js'); // initEnv() needs to be run beforehand
32
33// grab the SVG element from the window and resize it to 900x900px
34let canvas = jQuery('#canvas1').width(900).height(900);
35
36let data = fs.readFileSync('test/fixtures/fz_snowprofile_53930_de.caaml', 'utf-8');
37
38// HACK: Unfortunatly the jsdom DOMParser does not deal well with namespaces
39// the solution is to strip it of namespaces altogether
40data = data.replace(/<[a-z]+:/g, '<');
41data = data.replace(/<\/[a-z]+:/g, '</');
42
43// niviz.parse() parses the data with the respective parser (second argument)
44// and returns a station object, that contains profiles and meta info
45let station = niviz.parse(data, 'caaml', function (error, station) {
46
47 // niviz.draw() lets you draw whichever profile you want to draw:
48 // SimpleProfile, MobileProfile, SLFProfile, StructureProfile
49 let graph = niviz.draw('MobileProfile', station, canvas, {
50 miniature: false,
51 margin_top: 50
52 });
53
54 // niviz.convert() converts your graph object into an SVG
55 niviz.convert('SVG', graph, function (data, error) {
56
57 fs.writeFile('out.svg', data, function () {
58
59 // Adding 10px to the top of the SVG before converting it into a PNG
60 // data = data.replace('<svg ', '<svg style="margin-top: 10px;" ');
61
62 // Using the convert-svg-to-png library saves us some hassle with fonts
63 const png = convert(data, { background: 'white', width: 1000 }).then(function (png) {
64 fs.writeFile('out.png', png, function () {
65 process.exit(1);
66 });
67 });
68 });
69
70 });
71});
72
73
74// Set up the jsdom environment so that niViz can thrive
75function initEnv () {
76
77 let dom = new JSDOM('<!DOCTYPE html><body><svg id="canvas1"></svg></body></html>', {
78 runScripts: 'dangerously',
79 url: 'http://localhost'
80 });
81
82 let window = dom.window;
83 window.ignoreLibs = true;
84 global.window = window;
85 global.document = window.document;
86 global.moment = moment;
87 global.canvg = require('canvg');
88 global.Snap = require('snapsvg');
89 global.NodeList = window.NodeList;
90 global.navigator = window.navigator;
91 global.DOMParser = window.DOMParser;
92 global.Element = window.Element;
93
94 // mute any output on console.dir()
95 global.console.dir = function () {};
96
97 // to initialize jQuery we require a window with a document
98 global.jQuery = require('jquery');
99
100 return window;
101}

Archive Download this file

Branches