-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_elevation_data.js
More file actions
217 lines (192 loc) · 6.34 KB
/
plot_elevation_data.js
File metadata and controls
217 lines (192 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// plot elevation data from elevation_results.json using Plotly and save as PNG
const fs = require('fs');
const puppeteer = require('puppeteer');
// Read and parse the elevation data
let elevationData = JSON.parse(fs.readFileSync('elevation_results.json', 'utf8'));
// Moving average smoothing function
function movingAverage(arr, windowSize) {
const result = [];
for (let i = 0; i < arr.length; i++) {
let start = Math.max(0, i - Math.floor(windowSize / 2));
let end = Math.min(arr.length, i + Math.ceil(windowSize / 2));
let window = arr.slice(start, end);
let avg = window.reduce((sum, val) => sum + val, 0) / window.length;
result.push(avg);
}
return result;
}
const windowSize = 10; // You can adjust this for more/less smoothing
const elevationsSmoothed = movingAverage(elevations, windowSize);
// Create the plot data (original and smoothed)
const trace = {
x: distances,
y: elevations,
type: 'scatter',
mode: 'lines+markers',
name: 'Original Elevation',
line: {
color: '#1f77b4',
width: 2,
dash: 'dot'
},
marker: {
size: 4,
color: '#1f77b4'
}
};
const traceSmoothed = {
x: distances,
y: elevationsSmoothed,
type: 'scatter',
mode: 'lines',
name: 'Smoothed Elevation',
line: {
color: '#ff7f0e',
width: 3
}
};
const layout = {
title: 'Elevation Profile vs Distance',
xaxis: {
title: 'Distance (km)',
showgrid: true,
gridcolor: '#f0f0f0'
},
yaxis: {
title: 'Elevation (m)',
showgrid: true,
gridcolor: '#f0f0f0'
},
plot_bgcolor: 'white',
paper_bgcolor: 'white',
hovermode: 'closest',
width: 1200,
height: 600
};
const config = {
displayModeBar: false,
displaylogo: false
};
// Create HTML content
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>Elevation Profile</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: white;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 5px;
}
.stat-item {
text-align: center;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #1f77b4;
}
.stat-label {
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<h1>Elevation Profile</h1>
<div class="stats">
<div class="stat-item">
<div class="stat-value">${cumulativeDistance.toFixed(2)}</div>
<div class="stat-label">Total Distance (km)</div>
</div>
<div class="stat-item">
<div class="stat-value">${Math.min(...elevations)}</div>
<div class="stat-label">Min Elevation (m)</div>
</div>
<div class="stat-item">
<div class="stat-value">${Math.max(...elevations)}</div>
<div class="stat-label">Max Elevation (m)</div>
</div>
<div class="stat-item">
<div class="stat-value">${(Math.max(...elevations) - Math.min(...elevations)).toFixed(0)}</div>
<div class="stat-label">Elevation Gain (m)</div>
</div>
</div>
<div id="plot"></div>
</div>
<script>
var data = ${JSON.stringify([trace, traceSmoothed])};
var layout = ${JSON.stringify(layout)};
var config = ${JSON.stringify(config)};
Plotly.newPlot('plot', data, layout, config);
</script>
</body>
</html>`;
async function generatePNG() {
try {
// Write HTML file temporarily
fs.writeFileSync('temp_elevation_plot.html', htmlContent);
// Launch browser
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
// Set viewport for consistent sizing
await page.setViewport({ width: 1280, height: 800 });
// Load the HTML file
await page.goto(`file://${process.cwd()}/temp_elevation_plot.html`);
// Wait for Plotly to render by checking if the plot element exists and has content
await page.waitForFunction(() => {
const plotElement = document.getElementById('plot');
return plotElement && plotElement.children.length > 0;
}, { timeout: 10000 });
// Additional wait to ensure rendering is complete
await new Promise(resolve => setTimeout(resolve, 1000));
// Take screenshot
await page.screenshot({
path: 'elevation_profile.png',
fullPage: true,
type: 'png'
});
await browser.close();
// Clean up temporary HTML file
fs.unlinkSync('temp_elevation_plot.html');
console.log('Elevation profile saved as elevation_profile.png');
console.log(`Total distance: ${cumulativeDistance.toFixed(2)} km`);
console.log(`Elevation range: ${Math.min(...elevations)}m - ${Math.max(...elevations)}m`);
console.log(`Elevation gain: ${Math.max(...elevations) - Math.min(...elevations)}m`);
} catch (error) {
console.error('Error generating PNG:', error);
}
}
// Create a simple console output for quick visualization
console.log('\nElevation Profile Summary:');
console.log('Distance (km) | Elevation (m)');
console.log('-------------|-------------');
for (let i = 0; i < Math.min(distances.length, 10); i++) {
console.log(`${distances[i].toFixed(2).padStart(12)} | ${elevations[i].toString().padStart(12)}`);
}
if (distances.length > 10) {
console.log('...');
console.log(`${distances[distances.length-1].toFixed(2).padStart(12)} | ${elevations[elevations.length-1].toString().padStart(12)}`);
}
// Generate the PNG
generatePNG();