summaryrefslogtreecommitdiff
path: root/tagstats/statistics_handler.hpp
blob: 2188629653422442e37c9d57ada91f12be01cc65 (plain)
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
218
219
220
#ifndef TAGSTATS_HANDLER_STATISTICS_HPP
#define TAGSTATS_HANDLER_STATISTICS_HPP

/*

Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).

This file is part of Taginfo (https://github.com/joto/taginfo).

Osmium is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License or (at your option) the GNU
General Public License as published by the Free Software Foundation, either
version 3 of the Licenses, or (at your option) any later version.

Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
General Public License for more details.

You should have received a copy of the Licenses along with Osmium. If not, see
<http://www.gnu.org/licenses/>.

*/

#include "sqlite.hpp"

/**
 * Osmium handler that collects basic statistics from OSM data and
 * writes it to a Sqlite database.
 */
class StatisticsHandler : public Osmium::Handler::Base {

public:

    StatisticsHandler(Sqlite::Database& database) :
        Base(),
        m_database(database) {
        // if you change anything in this array, also change the corresponding struct below
        static const char *sn[] = {
            "nodes",
            "nodes_without_tags",
            "node_tags",
            "max_node_id",
            "max_tags_on_node",
            "ways",
            "way_tags",
            "way_nodes",
            "max_way_id",
            "max_tags_on_way",
            "max_nodes_on_way",
            "closed_ways",
            "relations",
            "relation_tags",
            "relation_members",
            "max_relation_id",
            "max_tags_on_relation",
            "max_members_on_relation",
            "max_user_id",
            "anon_user_objects",
            "max_node_version",
            "max_way_version",
            "max_relation_version",
            "sum_node_version",
            "sum_way_version",
            "sum_relation_version",
            "max_changeset_id",
            0    // last element (sentinel) must always be 0
        };
        m_stat_names = sn;

        // initialize all statistics to zero
        for (int i=0; m_stat_names[i]; ++i) {
            ((uint64_t *) &m_stats)[i] = 0;
        }
    }

    void node(const shared_ptr<Osmium::OSM::Node const>& node) {
        update_common_stats(*node);
        m_stats.nodes++;
        if (m_tag_count == 0) {
            m_stats.nodes_without_tags++;
        }
        if (m_id > (int64_t) m_stats.max_node_id) {
            m_stats.max_node_id = m_id;
        }
        m_stats.node_tags += m_tag_count;
        if (m_tag_count > (int64_t) m_stats.max_tags_on_node) {
            m_stats.max_tags_on_node = m_tag_count;
        }
        if (m_version > (int64_t) m_stats.max_node_version) {
            m_stats.max_node_version = m_version;
        }
        m_stats.sum_node_version += m_version;
    }

    void way(const shared_ptr<Osmium::OSM::Way const>& way) {
        update_common_stats(*way);
        m_stats.ways++;
        if (way->is_closed()) {
            m_stats.closed_ways++;
        }
        if (m_id > (int64_t) m_stats.max_way_id) {
            m_stats.max_way_id = m_id;
        }
        m_stats.way_tags += m_tag_count;
        m_stats.way_nodes += way->nodes().size();
        if (m_tag_count > (int64_t) m_stats.max_tags_on_way) {
            m_stats.max_tags_on_way = m_tag_count;
        }
        if (way->nodes().size() > (int64_t) m_stats.max_nodes_on_way) {
            m_stats.max_nodes_on_way = way->nodes().size();
        }
        if (m_version > (int64_t) m_stats.max_way_version) {
            m_stats.max_way_version = m_version;
        }
        m_stats.sum_way_version += m_version;
    }

    void relation(const shared_ptr<Osmium::OSM::Relation const>& relation) {
        update_common_stats(*relation);
        m_stats.relations++;
        if (m_id > (int64_t) m_stats.max_relation_id) {
            m_stats.max_relation_id = m_id;
        }
        m_stats.relation_tags += m_tag_count;
        osm_sequence_id_t member_count = relation->members().size();
        m_stats.relation_members += member_count;
        if (m_tag_count > (int64_t) m_stats.max_tags_on_relation) {
            m_stats.max_tags_on_relation = m_tag_count;
        }
        if (member_count > (int64_t) m_stats.max_members_on_relation) {
            m_stats.max_members_on_relation = member_count;
        }
        if (m_version > (int64_t) m_stats.max_relation_version) {
            m_stats.max_relation_version = m_version;
        }
        m_stats.sum_relation_version += m_version;
    }

    void final() {
        Sqlite::Statement statement_insert_into_main_stats(m_database, "INSERT INTO stats (key, value) VALUES (?, ?);");
        m_database.begin_transaction();

        for (int i=0; m_stat_names[i]; ++i) {
            statement_insert_into_main_stats
            .bind_text(m_stat_names[i])
            .bind_int64( ((uint64_t *) &m_stats)[i] )
            .execute();
        }
        statement_insert_into_main_stats
        .bind_text("nodes_with_tags")
        .bind_int64( ((uint64_t *) &m_stats)[0] - ((uint64_t *) &m_stats)[1] )
        .execute();

        m_database.commit();
    }

private:

    // if you change anything in this struct, also change the corresponding array above
    struct statistics {
        uint64_t nodes;
        uint64_t nodes_without_tags;
        uint64_t node_tags;
        uint64_t max_node_id;
        uint64_t max_tags_on_node;
        uint64_t ways;
        uint64_t way_tags;
        uint64_t way_nodes;
        uint64_t max_way_id;
        uint64_t max_tags_on_way;
        uint64_t max_nodes_on_way;
        uint64_t closed_ways;
        uint64_t relations;
        uint64_t relation_tags;
        uint64_t relation_members;
        uint64_t max_relation_id;
        uint64_t max_tags_on_relation;
        uint64_t max_members_on_relation;
        uint64_t max_user_id;
        uint64_t anon_user_objects;
        uint64_t max_node_version;
        uint64_t max_way_version;
        uint64_t max_relation_version;
        uint64_t sum_node_version;
        uint64_t sum_way_version;
        uint64_t sum_relation_version;
        uint64_t max_changeset_id;
    } m_stats;

    const char **m_stat_names;

    Sqlite::Database& m_database;

    osm_object_id_t m_id;
    osm_version_t   m_version;
    int             m_tag_count;

    void update_common_stats(const Osmium::OSM::Object& object) {
        m_id        = object.id();
        m_version   = object.version();
        m_tag_count = object.tags().size();

        osm_user_id_t uid = object.uid();
        if (uid == 0) {
            m_stats.anon_user_objects++;
        }
        if (uid > (int64_t) m_stats.max_user_id) {
            m_stats.max_user_id = uid;
        }

        osm_changeset_id_t changeset = object.changeset();
        if (changeset > (int64_t) m_stats.max_changeset_id) {
            m_stats.max_changeset_id = changeset;
        }
    }

}; // class StatisticsHandler

#endif // TAGSTATS_HANDLER_STATISTICS_HPP