summaryrefslogtreecommitdiff
path: root/tagstats/geodistribution.hpp
blob: d5d1c7850a88f1dd27517f6a3555c97717c446bb (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#ifndef TAGSTATS_GEODISTRIBUTION_HPP
#define TAGSTATS_GEODISTRIBUTION_HPP

/*

  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 <stdexcept>
#include <limits>

#include <gd.h>

/**
 * Functor class defining the () operator as a function that limits a
 * Osmium::OSM::Position to a bounding box, reduces the resolution
 * of the coordinates and returns an integer.
 *
 * If the position is outside the bounding box, std::numeric_limits<T>::max()
 * is returned.
 *
 * @tparam T Result type after conversion. Must be an unsigned integer type.
 */
template <typename T>
class MapToInt {

    double m_minx;
    double m_miny;
    double m_maxx;
    double m_maxy;

    unsigned int m_width;
    unsigned int m_height;

    double m_dx;
    double m_dy;

public:

    MapToInt(double minx = -180, double miny = -90, double maxx = 180, double maxy = 90, unsigned int width = 360, unsigned int height = 180) :
        m_minx(minx), m_miny(miny), m_maxx(maxx), m_maxy(maxy),
        m_width(width), m_height(height),
        m_dx(maxx - minx), m_dy(maxy - miny) {
        if (size() >= std::numeric_limits<T>::max()) {
            throw std::range_error("width*height must be smaller than MAXINT for type T");
        }
    }

    T operator()(const Osmium::OSM::Position& p) const {
        if (p.lon() < m_minx || p.lat() < m_miny || p.lon() >= m_maxx || p.lat() >= m_maxy) {
            // if the position is out of bounds we return MAXINT for type T
            return std::numeric_limits<T>::max();
        }
        int x = (p.lon() - m_minx) / m_dx * m_width;
        int y = (m_maxy - p.lat()) / m_dy * m_height;

        if (x < 0) {
            x = 0;
        } else if (static_cast<unsigned int>(x) >= m_width) {
            x = m_width-1;
        }
        if (y < 0) {
            y = 0;
        } else if (static_cast<unsigned int>(y) >= m_height) {
            y = m_height-1;
        }

        return y * m_width + x;
    }

    unsigned int width() const {
        return m_width;
    }

    unsigned int height() const {
        return m_height;
    }

    unsigned int size() const {
        return m_width * m_height;
    }

};

/**
 * Stores the geographical distribution of something in a space efficient way.
 */
class GeoDistribution {

    typedef std::vector<bool> geo_distribution_t;

    /**
     * Contains a pointer to a bitset that gives us the distribution.
     * If only one grid cell is used so far, this pointer is NULL. Only
     * if more than one grid cell is used, we dynamically create an
     * object for this.
     */
    geo_distribution_t* m_distribution;

    /**
     * Number of set grid cells.
     */
    unsigned int m_cells;

    /// If there is only one grid cell location, this is where its kept.
    rough_position_t m_location;

    /// Overall distribution
    static geo_distribution_t c_distribution_all;

    static int c_width;
    static int c_height;

public:

    GeoDistribution() : m_distribution(NULL), m_cells(0), m_location(0) {
    }

    ~GeoDistribution() {
        delete m_distribution;
    }

    void clear() {
        delete m_distribution;
        m_distribution = NULL;
        m_cells = 0;
        m_location = 0;
    }

    static void set_dimensions(int width, int height) {
        c_width = width;
        c_height = height;
        c_distribution_all.resize(c_width * c_height);
    }

    /**
     * Add the given coordinate to the distribution store.
     */
    void add_coordinate(rough_position_t n) {
        if (n == std::numeric_limits<rough_position_t>::max()) {
            // ignore positions that are out of bounds
            return;
        }
        if (m_cells == 0) {
            m_location = n;
            m_cells++;
            c_distribution_all[n] = true;
        } else if (m_cells == 1 && m_location != n) {
            m_distribution = new geo_distribution_t(c_width*c_height);
            (*m_distribution)[m_location] = true;
            c_distribution_all[m_location] = true;
            (*m_distribution)[n] = true;
            c_distribution_all[n] = true;
            m_cells++;
        } else if (m_cells == 1 && m_location == n) {
            // nothing to do
        } else if (! (*m_distribution)[n]) {
            m_cells++;
            (*m_distribution)[n] = true;
            c_distribution_all[n] = true;
        }
    }

    /**
     * Create PNG image.
     * You have to call free_png() to free the memory allocated for the
     * PNG image once you are done with it.
     *
     * @param size Pointer to integer thats set to the size of the created
     *        image.
     * @returns Pointer to memory area with PNG image.
     */
    void* create_png(int* size) {
        gdImagePtr im = gdImageCreate(c_width, c_height);
        int bgColor = gdImageColorAllocate(im, 0, 0, 0);
        gdImageColorTransparent(im, bgColor);
        int fgColor = gdImageColorAllocate(im, 180, 0, 0);

        if (m_cells == 1) {
            int y = m_location / c_width;
            int x = m_location - (y * c_width);
            gdImageSetPixel(im, x, y, fgColor);
        } else if (m_cells >= 2) {
            int n=0;
            for (int y=0; y < c_height; y++) {
                for (int x=0; x < c_width; x++) {
                    if ((*m_distribution)[n]) {
                        gdImageSetPixel(im, x, y, fgColor);
                    }
                    n++;
                }
            }
        }

        void* png = gdImagePngPtr(im, size);
        gdImageDestroy(im);

        return png;
    }

    /**
     * Call this to free the pointer returned by create_png().
     */
    void free_png(void *png) {
        gdFree(png);
    }

    static void* create_empty_png(int* size) {
        gdImagePtr im = gdImageCreate(c_width, c_height);
        int bgColor = gdImageColorAllocate(im, 0, 0, 0);
        gdImageColorTransparent(im, bgColor);

        void* png = gdImagePngPtr(im, size);
        gdImageDestroy(im);

        return png;
    }

    /**
     * Return the number of cells set.
     */
    unsigned int cells() const {
        return m_cells;
    }

    /**
     * Return the number of cells that are set in at least one GeoDistribution
     * object.
     */
    static unsigned int count_all_set_cells() {
        int c=0;
        for (int n=0; n < c_width*c_height; ++n) {
            if (c_distribution_all[n]) {
                c++;
            }
        }
        return c;
    }

};

#endif // TAGSTATS_GEODISTRIBUTION_HPP