summaryrefslogtreecommitdiff
path: root/tagstats/tagstats.cpp
blob: 91b63ed74655497492dbcac80f5a0dc786e22c03 (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
/*

  Copyright 2012 Jochen Topf <jochen@topf.org>.

  This file is part of Tagstats.

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

  Tagstats 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Tagstats.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <getopt.h>

#define OSMIUM_WITH_PBF_INPUT
#define OSMIUM_WITH_XML_INPUT

#include <osmium.hpp>

#include "statistics_handler.hpp"

/**
 * Positions are stored in this type of integer for the distribution images.
 * TAGSTATS_GEODISTRIBUTION_INT must be set in Makefile, typically to uint16_t
 * or uint32_t (for higher resolution but needs twice as much memory).
 */
typedef TAGSTATS_GEODISTRIBUTION_INT rough_position_t;

// Set BYID in Makefile to SparseTable, MmapFile, or MmapAnon
#ifdef TAGSTATS_GEODISTRIBUTION_FOR_WAYS
# include TAGSTATS_GEODISTRIBUTION_INCLUDE
typedef Osmium::Storage::ById::TAGSTATS_GEODISTRIBUTION_FOR_WAYS<rough_position_t> storage_t;
#endif // TAGSTATS_GEODISTRIBUTION_FOR_WAYS

#include "geodistribution.hpp"

GeoDistribution::geo_distribution_t GeoDistribution::c_distribution_all;
int GeoDistribution::c_width;
int GeoDistribution::c_height;

#include "tagstats_handler.hpp"


/* ================================================== */

void print_help() {
    std::cout << "tagstats [OPTIONS] OSMFILE DATABASE\n\n" \
              << "This program is part of Taginfo. It calculates statistics\n" \
              << "on OSM tags from OSMFILE and puts them into DATABASE (an SQLite database).\n" \
              << "\nOptions:\n" \
              << "  -H, --help                    This help message\n";
#ifdef TAGSTATS_COUNT_TAG_COMBINATIONS
    std::cout << "  -T, --tags=FILENAME           File with tags we are interested in\n" \
              << "  -m, --min-tag-combination-count=N  Tag combinations not appearing this often\n" \
              << "                                     are not written to database\n";
#endif // TAGSTATS_COUNT_TAG_COMBINATIONS
    std::cout << "  -R, --relation-types=FILENAME File with relation types we are interested in\n" \
              << "  -t, --top=NUMBER              Top of bounding box for distribution images\n" \
              << "  -r, --right=NUMBER            Right of bounding box for distribution images\n" \
              << "  -b, --bottom=NUMBER           Bottom of bounding box for distribution images\n" \
              << "  -l, --left=NUMBER             Left of bounding box for distribution images\n" \
              << "  -w, --width=NUMBER            Width of distribution images (default: 360)\n" \
              << "  -h, --height=NUMBER           Height of distribution images (default: 180)\n" \
              << "\nDefault for bounding box is: (-180, -90, 180, 90).\n";
}

int main(int argc, char *argv[]) {
    static struct option long_options[] = {
        {"help",           no_argument, 0, 'H'},
#ifdef TAGSTATS_COUNT_TAG_COMBINATIONS
        {"tags",                      required_argument, 0, 'T'},
        {"min-tag-combination-count", required_argument, 0, 'm'},
#endif // TAGSTATS_COUNT_TAG_COMBINATIONS
        {"relation-types", required_argument, 0, 'R'},
        {"top",            required_argument, 0, 't'},
        {"right",          required_argument, 0, 'r'},
        {"bottom",         required_argument, 0, 'b'},
        {"left",           required_argument, 0, 'l'},
        {"width",          required_argument, 0, 'w'},
        {"height",         required_argument, 0, 'h'},
        {0, 0, 0, 0}
    };

    std::string tags_list;
    std::string relation_type_list;

    double top    =   90;
    double right  =  180;
    double bottom =  -90;
    double left   = -180;

    unsigned int width  = 360;
    unsigned int height = 180;

    unsigned int min_tag_combination_count = 1000;

    while (true) {
        int c = getopt_long(argc, argv,
#ifdef TAGSTATS_COUNT_TAG_COMBINATIONS
                            "dHR:t:r:b:l:w:h:T:m:",
#else
                            "dHR:t:r:b:l:w:h:",
#endif // TAGSTATS_COUNT_TAG_COMBINATIONS
                            long_options, 0);
        if (c == -1) {
            break;
        }

        switch (c) {
            case 'H':
                print_help();
                exit(0);
#ifdef TAGSTATS_COUNT_TAG_COMBINATIONS
            case 'T':
                tags_list = optarg;
                break;
            case 'm':
                min_tag_combination_count = atoi(optarg);
                break;
#endif // TAGSTATS_COUNT_TAG_COMBINATIONS
            case 'R':
                relation_type_list = optarg;
                break;
            case 't':
                top = atof(optarg);
                break;
            case 'r':
                right = atof(optarg);
                break;
            case 'b':
                bottom = atof(optarg);
                break;
            case 'l':
                left = atof(optarg);
                break;
            case 'w':
                width = atoi(optarg);
                break;
            case 'h':
                height = atoi(optarg);
                break;
            default:
                exit(1);
        }
    }

    if (argc - optind != 2) {
        std::cerr << "Usage: " << argv[0] << " [OPTIONS] OSMFILE DATABASE" << std::endl;
        exit(1);
    }

    GeoDistribution::set_dimensions(width, height);
    Osmium::OSMFile infile(argv[optind]);
    Sqlite::Database db(argv[optind+1]);
    MapToInt<rough_position_t> map_to_int(left, bottom, right, top, width, height);
    TagStatsHandler handler(db, tags_list, relation_type_list, map_to_int, min_tag_combination_count);
    Osmium::Input::read(infile, handler);
}