source: subversion/applications/utils/import/srtm2wayinfo/osm-parse-old.cpp

Last change on this file was 17101, checked in by herm, 8 years ago

Support different storage systems for ways.

File size: 2.6 KB
Line 
1/* Copyright (c) 2009 Hermann Kraus
2 * This software is available under a "MIT Style" license
3 * (see COPYING).
4 */
5/** \file
6  * Minimalistic OSM parser (old and slow).
7  * Only handles the attributes required for this project and ignores everything else.
8  * \note This is a old version of the parser which uses QXmlStreamReader. It is therefore
9  * standard compliant, but also takes much longer to parse the data (5 to 10 times slower).
10  */
11#include "osm-parse.h"
12
13#include <QString>
14#include <QFile>
15#include <QDebug>
16#include <QXmlStreamReader>
17
18/** Parse an OSM-XML file.
19  * This function is just a wrapper that creates an QFile object
20  * and calls parse(QFile *).
21  */
22void OsmData::parse(QString filename)
23{
24    QFile f(filename);
25    f.open(QIODevice::ReadOnly);
26    parse(&f);
27    f.close();
28}
29
30/** Parse an OSM-XML file.
31  * Stores information about the parsed data in the "nodes" and "ways" arrays.
32  */
33void OsmData::parse(QFile *file)
34{
35    bool keep = false;
36    int i = 0;
37    int kept = 0, discarded = 0, nodes_referenced = 0;
38    QXmlStreamReader xml(file);
39    while (!xml.atEnd()) {
40        xml.readNext();
41        if (xml.isEndElement() && xml.name() == "way") {
42            if (keep) {
43                ways->append(currentWay);
44                foreach(OsmNodeId nodeid, currentWay.nodes) {
45                    (*nodes)[nodeid].incOrder();
46                }
47                nodes_referenced += currentWay.nodes.count();
48                kept++;
49            } else {
50                discarded++;
51            }
52            continue;
53        }
54
55        if (!xml.isStartElement()) continue;
56
57        i++;
58        if ((i & 65535) == 0) qDebug() << i;
59        if (xml.name() == "tag") {
60            if (wayTags.contains(xml.attributes().value("k").toString())) {
61                keep = true;
62            }
63            continue;
64        }
65
66        if (xml.name() == "node") {
67            nodes_total++;
68            OsmNodeId nodeid = xml.attributes().value("id").toString().toInt();
69            (*nodes)[nodeid] = OsmNode(
70                xml.attributes().value("lat").toString().toFloat(),
71                xml.attributes().value("lon").toString().toFloat());
72            continue;
73        }
74
75        if (xml.name() == "way") {
76            keep = false;
77            currentWay.clear();
78            currentWay.setId(xml.attributes().value("id").toString().toInt());
79        }
80
81        if (xml.name() == "nd") {
82            currentWay.addNode(xml.attributes().value("ref").toString().toInt());
83        }
84    }
85    qDebug() << "Kept:" << kept << "Discarded:" << discarded <<  "Noderefs:" << nodes_referenced << "Nodes:" << nodes_total;
86}
Note: See TracBrowser for help on using the repository browser.