6

I managed to automate the creation of terrain profiles, using the beautiful "Highcharts" Javascript framework. The data source of the profiles is stored in an attribute called "elev_data" of my linestring Features [EPSG:31287]:

wkt_geom;id;name;elev_data
LINESTRING(485337.03520647069672123 478687.21718103648163378, 477190.07880394282983616 482190.81527377443853766, 473768.4297775769373402 482082.93272007041377947, 462918.97458666400052607 475541.86418546421919018, 451969.3244225507369265 462052.78583500615786761, 446844.90212881547631696 458318.85324375209165737, 442245.01766366267111152 456208.73519689653767273, 435203.69195721752475947 455812.453897203609813, 428364.42688080196967348 450196.91061822243500501, 414995.14512734755408019 450542.12914763967273757, 412394.10736548586282879 446756.6119861145853065, 408804.01940666820155457 445495.35169308050535619, 407646.22162218159064651 437909.94463149970397353, 404443.36516060709254816 437601.71908963553141803, 400806.6649368109065108 440459.61497568729100749, 396378.01719814090756699 438191.49543189530959353);1;A1;[[0.0,245.941207886],[8871.44300349,265.862884521],[12295.9257906,287.396118164],[24968.7534062,360.676086426],[42348.2448833,414.400756836],[48690.8296423,437.362426758],[53753.2690799,444.892730713],[60808.0140225,433.137115479],[69660.1386933,503.99710083],[83038.1227898,573.612243652],[87632.5614131,562.628051758],[91438.9659096,579.682617188],[99114.6946959,539.605651855],[102333.395008,523.157836914],[106960.166689,533.281494141],[111937.445945,552.722900391]]
LINESTRING(452570.17855033132946119 462858.20528277161065489, 448153.72135915257968009 472998.9959175722906366, 448777.70450415066443384 475698.70112533023348078, 444283.12003721366636455 476841.77734390797559172, 441221.06307075585937127 475500.74522358615649864, 427836.54942135507008061 475199.05964523466536775, 419276.20701565645867959 481462.8514012906816788, 414083.17948533577146009 483164.60927838657516986, 410799.12681691453326494 488773.7393510842230171, 407004.37368545326171443 490271.97727407095953822, 405591.05425128655042499 492672.73204009979963303, 406898.42652144364546984 501322.36894592072349042);2;A8;[[0.0,411.077331543],[11064.2873644,325.955474854],[13836.0080073,329.704528809],[18475.0544097,347.914459229],[21818.8756345,356.44442749],[35210.6469056,454.271972656],[45820.8459402,471.487640381],[51287.0361958,428.268829346],[57788.483018,392.171966553],[61869.2968371,369.296386719],[64655.8414778,349.66619873],[73405.7160233,315.235839844]]
LINESTRING(452570.17855033132946119 462858.20528277161065489, 456561.00999457266880199 454684.48283911606995389, 458414.6864655296667479 442124.18234631378436461, 462614.20490059169242159 438749.37014489434659481, 463207.38116095447912812 430740.31667187571292743, 472424.92870949127245694 425989.60370636364677921, 475973.2473294916562736 414063.74508341425098479);3;A9;[[0.0,411.077331543],[9099.02673088,475.123352051],[21799.8779514,471.613616943],[27189.3737689,465.936584473],[35223.3869064,504.245849609],[45597.2097169,578.050720215],[58044.7839276,1934.05322266]]

While enabling the Atlas Feature, I can export nice terrain profiles after manually refreshing the HTML Frame for every single page print: enter image description here

But unfortunately a complete Atlas Export doesn't work. It seems like the QGIS expression inside the HTML won't update or there is a timing problem:

<html>
<head>
<style type="text/css">
    #container {
    max-width: 1000px;
    height: 250px;
    margin: 1em auto;
}
</style>
</head>
<body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
<div id="container"></div>
<script type="text/javascript">
var elevationData = [% "elev_data" %];
Highcharts.chart('container', {
    chart: {
        type: 'area',
        zoomType: 'x',
        panning: true,
        panKey: 'shift'
    },
    title: {
        text: 'Highway [% name %]'
    },
    subtitle: {
        text: 'Upper Austria'
    },
    xAxis: {
        labels: {
            format: '{value} m'
        },
        minRange: 5,
        title: {
            text: 'Distance'
        }
    },
    yAxis: {
        startOnTick: true,
        endOnTick: false,
        maxPadding: 0.35,
        title: {
            text: null
        },
        labels: {
            format: '{value} m'
        }
    },
    legend: {
        enabled: false
    },
    series: [{
        data: elevationData,
        lineColor: Highcharts.getOptions().colors[1],
        color: Highcharts.getOptions().colors[2],
        fillOpacity: 0.5,
        name: 'Elevation',
        marker: {
            enabled: false
        },
        threshold: null
    }]
});
</script>
</body>
</html>

The result is an empty profile or only a small part of the profile is drawn: enter image description here

I get the same results in QGIS 2.18.28 and QGIS 3.x.

What can I do, to automate the Atlas Export and refresh the HTML frame during each page generation?

0

If someone ist interested, I found a solution for this timing Problem!

While trying to find a workaround, I stumbled across the command line tool "wkhtmltopdf" (https://wkhtmltopdf.org) where I got same results like with QGIS Atlas print. After checking the commandline parameters I found the parameter 'javascript-delay' that did what I was hoping for. A quick inspection of the source code showed the use of QTimer.singleShot() for the Javascript delay.

After this insight, a little bit wiser, I came up with following solution:

1) I created an Expression function and put the following code into the Python macro section of my QGIS Project:

from qgis.core import *
from  qgis.utils import iface
from PyQt4.QtCore import *


@qgsfunction(args=1, group='RAG')
def wait(values, feature, parent):
    loop = QEventLoop()
    QTimer.singleShot(int(values[0]),loop.quit)
    loop.exec_()
    return 1 # return 1 for 1mm line width

2) Then I configured a rule based styling for my linestring layer "Highways"

Label                 Rule
Current Highway       "name" = @atlas_pagename
Highways              ELSE

and set the Expression function "wait(300)" for defining the width of the current Highway (returning 1 for 1mm).

Atlas Export works like a charm now, displaying the dynamic HTML pages correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.