File: postgis.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (340 lines) | stat: -rw-r--r-- 14,208 bytes parent folder | download | duplicates (4)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*****************************************************************************
 *
 * 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
 *
 *****************************************************************************/

#include "catch.hpp"
#include "ds_test_util.hpp"

#include <mapnik/datasource.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry_type.hpp>
#include <mapnik/util/fs.hpp>

/*
  Compile and run just this test:
  clang++ -o test-postgis -g -I./test/ test/unit/run.cpp test/unit/datasource/postgis.cpp `mapnik-config --all-flags` && ./test-postgis -d yes
*/

#include <boost/optional/optional_io.hpp>

namespace {

bool run(std::string const& command, bool okay_to_fail = false)
{
    std::string cmd;
    if (std::getenv("DYLD_LIBRARY_PATH") != nullptr)
    {
        cmd += std::string("DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " && ";
    }
    cmd += command;
    // silence output unless MAPNIK_TEST_DEBUG is defined
    if (std::getenv("MAPNIK_TEST_DEBUG") == nullptr)
    {
#ifndef _WINDOWS
        cmd += " 2>/dev/null";
#else
        cmd += " 2> nul";
#endif
    }
    else
    {
        std::clog << "Running " << cmd << "\n";
    }
    bool worked = (std::system(cmd.c_str()) == 0);
    if (okay_to_fail == true) return true;
    return worked;
}


std::string const dbname("mapnik-tmp-postgis-test-db");
bool status = false;

bool ping_postmaster()
{
    return (run("psql --version")
            && run("dropdb --if-exists " + dbname)
            && run("createdb -T template_postgis " + dbname));
}

}

TEST_CASE("postgis") {

    SECTION("Ping Postmaster (check if server is runnging and accessible")
    {
        if (!ping_postmaster())
        {
            WARN("Can't run postgis.input tests - check postmaster is running and accessible");
            return;
        }
        else
        {
            status = true;
        }
    }
    if (status)
    {
        SECTION("Postgis data initialization")
        {
            //don't add 'true' here, to get error message, when drop fails. If it works nothing is output
            REQUIRE(run("dropdb --if-exists " + dbname));
            REQUIRE(run("createdb -T template_postgis " + dbname));
            //REQUIRE(run("createdb " + dbname));
            // Breaks when raster support is missing (unfortunately this is common)
            //REQUIRE(run("psql -c 'CREATE EXTENSION postgis;' " + dbname, true));
            REQUIRE(run("psql -q -f ./test/data/sql/postgis-create-db-and-tables.sql " + dbname));
        }

        mapnik::parameters base_params;
        base_params["type"] = "postgis";
        base_params["dbname"] = dbname;

        SECTION("Postgis should throw without 'table' parameter")
        {
            mapnik::parameters params(base_params);
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis should throw with 'max_async_connection' greater than 'max_size'")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test";
            params["max_async_connection"] = "2";
            params["max_size"] = "1";
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis should throw with invalid metadata query")
        {
            mapnik::parameters params(base_params);
            params["table"] = "does_not_exist";
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis should throw with invalid key field")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test_invalid_id";
            params["key_field"] = "id";
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis should throw with multicolumn primary key")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test_invalid_multi_col_pk";
            params["autodetect_key_field"] = "true";
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis should throw without geom column")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test_no_geom_col";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            CHECK_THROWS(all_features(ds));
        }

        SECTION("Postgis should throw with invalid credentials")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test";
            params["user"] = "not_a_valid_user";
            params["password"] = "not_a_valid_pwd";
            CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
        }

        SECTION("Postgis initialize dataset with persist_connection, schema, extent, geometry field, autodectect key field, simplify_geometries, row_limit")
        {
            mapnik::parameters params(base_params);
            params["persist_connection"] = "false";
            params["table"] = "public.test";
            params["geometry_field"] = "geom";
            params["autodetect_key_field"] = "true";
            params["extent"] = "-1 -1, -1 2, 4 3, 3 -1, -1 -1";
            params["simplify_geometries"] = "true";
            params["row_limit"] = "1";
            auto ds = mapnik::datasource_cache::instance().create(params);
        }

        SECTION("Postgis dataset geometry type")
        {
            mapnik::parameters params(base_params);
            params["table"] = "(SELECT * FROM test WHERE gid=1) as data";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
        }

        SECTION("Postgis query field names")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            REQUIRE(ds->type() == mapnik::datasource::datasource_t::Vector);
            auto fields = ds->get_descriptor().get_descriptors();
            require_field_names(fields, { "gid", "colbigint", "col_text", "col-char", "col+bool", "colnumeric", "colsmallint", "colfloat4", "colfloat8", "colcharacter" });
            require_field_types(fields, { mapnik::Integer, mapnik::Integer, mapnik::String, mapnik::String, mapnik::Boolean, mapnik::Double, mapnik::Integer, mapnik::Double, mapnik::Double, mapnik::String });
        }

        SECTION("Postgis iterate features")
        {
            mapnik::parameters params(base_params);
            params["table"] = "test";
            params["key_field"] = "gid";
            params["max_async_connection"] = "2";
            //params["cursor_size"] = "2";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);

            auto featureset = ds->features_at_point(mapnik::coord2d(1, 1));
            mapnik::feature_ptr feature;
            while ((bool(feature = featureset->next()))) {
                REQUIRE(feature->get(2).to_string() == feature->get("col_text").to_string());
                REQUIRE(feature->get(4).to_bool() == feature->get("col+bool").to_bool());
                REQUIRE(feature->get(5).to_double() == feature->get("colnumeric").to_double());
                REQUIRE(feature->get(5).to_string() == feature->get("colnumeric").to_string());
            }

            featureset = all_features(ds);
            feature = featureset->next();
            //deactivate char tests for now: not yet implemented.
            //add at postgis_datasource.cpp:423
            //case 18:    // char
            //REQUIRE("A" == feature->get("col-char").to_string());
            feature = featureset->next();
            //REQUIRE("B" == feature->get("col-char").to_string());
            feature = featureset->next();
            REQUIRE(false == feature->get("col+bool").to_bool());
        }

        SECTION("Postgis cursorresultest")
        {
            mapnik::parameters params(base_params);
            params["table"] = "(SELECT * FROM test) as data";
            params["cursor_size"] = "2";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            auto featureset = all_features(ds);
            CHECK(count_features(featureset) == 8);

            featureset = all_features(ds);
            mapnik::feature_ptr feature;
            while (bool(feature = featureset->next())) {
                CHECK(feature->size() == 10);
            }

            featureset = all_features(ds);
            require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Point);
            require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Point);
            require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiPoint);
            require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::LineString);
            require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiLineString);
            require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Polygon);
            require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiPolygon);
            require_geometry(featureset->next(), 3, mapnik::geometry::geometry_types::GeometryCollection);
        }

        SECTION("Postgis bbox query")
        {
            mapnik::parameters params(base_params);
            params["table"] = "(SELECT * FROM public.test) as data WHERE geom && !bbox!";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            mapnik::box2d<double> ext = ds->envelope();
            CAPTURE(ext);
            INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
            REQUIRE(ext.minx() == -2);
            REQUIRE(ext.miny() == -2);
            REQUIRE(ext.maxx() == 5);
            REQUIRE(ext.maxy() == 4);
        }

        SECTION("Postgis query extent: full dataset")
        {
            //include schema to increase coverage
            mapnik::parameters params(base_params);
            params["table"] = "(SELECT * FROM public.test) as data";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            mapnik::box2d<double> ext = ds->envelope();
            CAPTURE(ext);
            INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
            REQUIRE(ext.minx() == -2);
            REQUIRE(ext.miny() == -2);
            REQUIRE(ext.maxx() == 5);
            REQUIRE(ext.maxy() == 4);
        }
/* deactivated for merging: still investigating a proper fix
   SECTION("Postgis query extent from subquery")
   {
   mapnik::parameters params(base_params);
   params["table"] = "(SELECT * FROM test where gid=4) as data";
   auto ds = mapnik::datasource_cache::instance().create(params);
   REQUIRE(ds != nullptr);
   mapnik::box2d<double> ext = ds->envelope();
   CAPTURE(ext);
   INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
   REQUIRE(ext.minx() == 0);
   REQUIRE(ext.miny() == 0);
   REQUIRE(ext.maxx() == 1);
   REQUIRE(ext.maxy() == 2);
   }
*/
        SECTION("Postgis query extent: from subquery with 'extent_from_subquery=true'")
        {
            mapnik::parameters params(base_params);
            params["table"] = "(SELECT * FROM test where gid=4) as data";
            params["extent_from_subquery"] = "true";
            auto ds = mapnik::datasource_cache::instance().create(params);
            REQUIRE(ds != nullptr);
            mapnik::box2d<double> ext = ds->envelope();
            CAPTURE(ext);
            INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
            REQUIRE(ext.minx() == 0);
            REQUIRE(ext.miny() == 0);
            REQUIRE(ext.maxx() == 1);
            REQUIRE(ext.maxy() == 2);
        }
/* deactivated for merging: still investigating a proper fix
   SECTION("Postgis query extent: subset with 'extent_from_subquery=true' and 'scale_denominator'")
   {
   mapnik::parameters params(base_params);
   // !!!! postgis-vt-util::z() returns 'null' when 'scale_denominator > 600000000'
   // https://github.com/mapbox/postgis-vt-util/blob/559f073877696a6bfea41baf3e1065f9cf4d18d1/postgis-vt-util.sql#L615-L617
   params["table"] = "(SELECT * FROM test where gid=4 AND z(!scale_denominator!) BETWEEN 0 AND 22) as data";
   params["extent_from_subquery"] = "true";
   auto ds = mapnik::datasource_cache::instance().create(params);
   REQUIRE(ds != nullptr);
   mapnik::box2d<double> ext = ds->envelope();
   CAPTURE(ext);
   INFO("" << std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
   REQUIRE(ext.minx() == 0);
   REQUIRE(ext.miny() == 0);
   REQUIRE(ext.maxx() == 1);
   REQUIRE(ext.maxy() == 2);
   }
*/

    }
}