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('SimpleProfile', station, canvas, {
50 miniature: false,
51 margin_top: 50,
52 other_parameters: [{ name: 'comments' }],
53 use_hhindex_as_primary_axis: true
54 });
55
56 // niviz.convert() converts your graph object into an SVG
57 niviz.convert('SVG', graph, function (data, error) {
58
59 fs.writeFile('out.svg', data, function () {
60
61 // Adding 10px to the top of the SVG before converting it into a PNG
62 // data = data.replace('<svg ', '<svg style="margin-top: 10px;" ');
63
64 // Using the convert-svg-to-png library saves us some hassle with fonts
65 const png = convert(data, { background: 'white', width: 1000 }).then(function (png) {
66 fs.writeFile('out.png', png, function () {
67 process.exit(1);
68 });
69 });
70 });
71
72 });
73});
74
75
76// Set up the jsdom environment so that niViz can thrive
77function initEnv () {
78
79 let dom = new JSDOM('<!DOCTYPE html><body><svg id="canvas1"></svg></body></html>', {
80 runScripts: 'dangerously',
81 url: 'http://localhost'
82 });
83
84 let window = dom.window;
85 window.ignoreLibs = true;
86 global.window = window;
87 global.document = window.document;
88 global.moment = moment;
89 global.canvg = require('canvg');
90 global.Snap = require('snapsvg');
91 global.NodeList = window.NodeList;
92 global.navigator = window.navigator;
93 global.DOMParser = window.DOMParser;
94 global.Element = window.Element;
95
96 // mute any output on console.dir()
97 global.console.dir = function () {};
98
99 // to initialize jQuery we require a window with a document
100 global.jQuery = require('jquery');
101
102 return window;
103}

Archive Download this file

Branches