File: ds_test_util.hpp

package info (click to toggle)
mapnik 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 20,208 kB
  • sloc: cpp: 144,467; python: 30,152; sh: 797; makefile: 166; xml: 140; lisp: 13
file content (211 lines) | stat: -rw-r--r-- 6,235 bytes parent folder | download | duplicates (3)
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
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/


#ifndef MAPNIK_UNIT_DATSOURCE_UTIL
#define MAPNIK_UNIT_DATSOURCE_UTIL

#include "catch.hpp"

#include <mapnik/datasource.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/geometry_types.hpp>
#include <mapnik/geometry_type.hpp>

namespace {

template <typename T>
std::string vector_to_string(T const& vec)
{
    std::stringstream s;
    for (auto const& item : vec)
    {
        s << "  " << item << "\n";
    }
    return s.str();
}

template <>
std::string vector_to_string(std::vector<mapnik::attribute_descriptor> const& vec)
{
    std::stringstream s;
    for (auto const& item : vec)
    {
        s << "  " << item.get_name() << "\n";
    }
    return s.str();
}

#define REQUIRE_FIELD_NAMES(fields, names) \
    INFO("fields:\n" + vector_to_string(fields) + "names:\n" +  vector_to_string(names)); \
    REQUIRE(fields.size() == names.size()); \
    auto itr_a = fields.begin(); \
    auto const end_a = fields.end(); \
    auto itr_b = names.begin(); \
    for (; itr_a != end_a; ++itr_a, ++itr_b) \
    { \
        CHECK(itr_a->get_name() == *itr_b); \
    } \

inline void require_field_names(std::vector<mapnik::attribute_descriptor> const &fields,
                         std::initializer_list<std::string> const &names)
{
    REQUIRE_FIELD_NAMES(fields,names);
}

#define REQUIRE_FIELD_TYPES(fields, types) \
    REQUIRE(fields.size() == types.size()); \
    auto itr_a = fields.begin(); \
    auto const end_a = fields.end(); \
    auto itr_b = types.begin(); \
    for (; itr_a != end_a; ++itr_a, ++itr_b) { \
        CHECK(itr_a->get_type() == *itr_b); \
    } \

inline void require_field_types(std::vector<mapnik::attribute_descriptor> const &fields,
                         std::initializer_list<mapnik::eAttributeType> const &types)
{
    REQUIRE_FIELD_TYPES(fields, types);
}

inline mapnik::featureset_ptr all_features(mapnik::datasource_ptr ds) {
    auto fields = ds->get_descriptor().get_descriptors();
    mapnik::query query(ds->envelope());
    for (auto const &field : fields) {
        query.add_property_name(field.get_name());
    }
    return ds->features(query);
}

inline std::size_t count_features(mapnik::featureset_ptr features) {
    std::size_t count = 0;
    while (features->next()) {
        ++count;
    }
    return count;
}

using attr = std::tuple<std::string, mapnik::value>;

#define REQUIRE_ATTRIBUTES(feature, ...) \
    do { \
        auto const& _feat = (feature); /* evaluate feature only once */ \
        REQUIRE(_feat != nullptr); \
        for (auto const& kv : __VA_ARGS__) { \
            auto& key = std::get<0>(kv); \
            auto& val = std::get<1>(kv); \
            CAPTURE(key); \
            CHECKED_IF(_feat->has_key(key)) { \
                CHECK(_feat->get(key) == val); \
                CHECK(_feat->get(key).which() == val.which()); \
            } \
        } \
    } while (0)

namespace detail {
struct feature_count {
    template <typename T>
    std::size_t operator()(T const &geom) const {
        return mapnik::util::apply_visitor(*this, geom);
    }

    std::size_t operator()(mapnik::geometry::geometry_empty const &) const {
        return 0;
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::point<T> const &) const {
        return 1;
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::line_string<T> const &) const {
        return 1;
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::polygon<T> const &) const {
        return 1;
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::multi_point<T> const &mp) const {
        return mp.size();
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::multi_line_string<T> const &mls) const {
        return mls.size();
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::multi_polygon<T> const &mp) const {
        return mp.size();
    }

    template <typename T>
    std::size_t operator()(mapnik::geometry::geometry_collection<T> const &col) const {
        std::size_t sum = 0;
        for (auto const &geom : col) {
            sum += operator()(geom);
        }
        return sum;
    }
};
} // namespace detail

template <typename T>
inline std::size_t feature_count(mapnik::geometry::geometry<T> const &g) {
    return detail::feature_count()(g);
}

inline void require_geometry(mapnik::feature_ptr feature,
                      std::size_t num_parts,
                      mapnik::geometry::geometry_types type) {
    REQUIRE(bool(feature));
    CHECK(mapnik::geometry::geometry_type(feature->get_geometry()) == type);
    CHECK(feature_count(feature->get_geometry()) == num_parts);
}

inline int create_disk_index(std::string const& filename, bool silent = true)
{
    std::string cmd;
    if (std::getenv("DYLD_LIBRARY_PATH") != nullptr)
    {
        cmd += std::string("DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " ";
    }
    cmd += "mapnik-index " + filename;
    if (silent)
    {
#ifndef _WINDOWS
        cmd += " 2>/dev/null";
#else
        cmd += " 2> nul";
#endif
    }
    return std::system(cmd.c_str());
}

}

#endif // MAPNIK_UNIT_DATSOURCE_UTIL