File: ogr.cpp

package info (click to toggle)
mapnik 4.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 18,548 kB
  • sloc: cpp: 163,861; python: 1,190; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (128 lines) | stat: -rw-r--r-- 5,108 bytes parent folder | download
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
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 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 <cstdlib>
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/datasource_cache.hpp>
#include "../../../plugins/input/gdal+ogr/ogr_utils.hpp"

inline std::vector<ogr_utils::option_ptr> split_options(std::string const& options)
{
    return ogr_utils::split_open_options(options);
}

void assert_option(std::vector<ogr_utils::option_ptr> const& options, size_t const& index, std::string const expected)
{
    auto const* opt = options.at(index).get();
    REQUIRE(opt != nullptr);
    REQUIRE_FALSE(strcmp(opt, expected.c_str()));
}

TEST_CASE("ogr")
{
    bool const have_ogr_plugin = mapnik::datasource_cache::instance().plugin_registered("ogr");
    if (have_ogr_plugin)
    {
        SECTION("ogr point feature")
        {
            mapnik::Map m(256, 256);
            mapnik::load_map(m, "./test/data/good_maps/point_json.xml");
            std::string fontdir("fonts/");
            REQUIRE(m.register_fonts(fontdir, true));
            m.zoom_all();
            mapnik::image_rgba8 im(256, 256);
            mapnik::agg_renderer<mapnik::image_rgba8> ren(m, im);
            ren.apply();
            std::string filename("./test/data/images/point_json.png");
            if (std::getenv("UPDATE") != nullptr)
            {
                mapnik::save_to_file(im, filename);
            }
            std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename, "png"));
            mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
            mapnik::image_rgba8 expected = mapnik::util::get<mapnik::image_rgba8>(data);
            REQUIRE(mapnik::compare(expected, im) == 0);
        }
    }
}

TEST_CASE("ogr open_options")
{
    bool const have_ogr_plugin = mapnik::datasource_cache::instance().plugin_registered("ogr");
    if (have_ogr_plugin)
    {
        SECTION("splitting open_options string")
        {
            std::string opts = "ZOOM=5";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, opts);
        }
        SECTION("splitting open_options string with multiple components")
        {
            std::string opts = "ZOOM=8 USE_BOUNDS=YES";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "ZOOM=8");
            assert_option(v, 1, "USE_BOUNDS=YES");
        }
        SECTION("open_options string with escaped character")
        {
            std::string opts = "MY_KEY=THIS\\ VALUE OTHER_KEY=SOMETHING";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "MY_KEY=THIS VALUE");
            assert_option(v, 1, "OTHER_KEY=SOMETHING");
        }
        SECTION("splitting open_options string with escaping")
        {
            std::string opts = "ZOOM=14 FANCY_OPTION=WITH\\ SPACE_VALUE";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "ZOOM=14");
            assert_option(v, 1, "FANCY_OPTION=WITH SPACE_VALUE");
        }
        SECTION("splitting open_options string, starts with space")
        {
            std::string opts = " ZOOM=14 K23=V45";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "ZOOM=14");
            assert_option(v, 1, "K23=V45");
        }
        SECTION("splitting open_options string, ends with space")
        {
            std::string opts = "ZOOM=14 K23=V46 ";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "ZOOM=14");
            assert_option(v, 1, "K23=V46");
        }
        SECTION("splitting open_options string, doubled space")
        {
            std::string opts = "ZOOM=14  K23=V47";
            std::vector<ogr_utils::option_ptr> v = split_options(opts);
            assert_option(v, 0, "ZOOM=14");
            assert_option(v, 1, "K23=V47");
        }
    }
}