diff --git a/plugins/input/ogr/build.py b/plugins/input/ogr/build.py index acf487311..06d9fbe6f 100644 --- a/plugins/input/ogr/build.py +++ b/plugins/input/ogr/build.py @@ -31,6 +31,7 @@ plugin_sources = Split( """ %(PLUGIN_NAME)s_converter.cpp %(PLUGIN_NAME)s_datasource.cpp + %(PLUGIN_NAME)s_utils.cpp %(PLUGIN_NAME)s_featureset.cpp %(PLUGIN_NAME)s_index_featureset.cpp """ % locals() diff --git a/plugins/input/ogr/ogr_datasource.cpp b/plugins/input/ogr/ogr_datasource.cpp index 9663fb096..7cfa0b8de 100644 --- a/plugins/input/ogr/ogr_datasource.cpp +++ b/plugins/input/ogr/ogr_datasource.cpp @@ -127,16 +127,23 @@ void ogr_datasource::init(mapnik::parameters const& params) } std::string driver = *params.get("driver", ""); + std::vector open_options_map = ogr_utils::split_open_options(*params.get("open_options", "")); + char** open_options = ogr_utils::open_options_for_ogr(open_options_map); if (!driver.empty()) { unsigned int nOpenFlags = GDAL_OF_READONLY | GDAL_OF_VECTOR; const char* papszAllowedDrivers[] = {driver.c_str(), nullptr}; + dataset_ = reinterpret_cast( - GDALOpenEx(dataset_name_.c_str(), nOpenFlags, papszAllowedDrivers, nullptr, nullptr)); + GDALOpenEx(dataset_name_.c_str(), nOpenFlags, papszAllowedDrivers, open_options, nullptr)); } else { + if (open_options[0] != nullptr) + { + throw datasource_exception(" parameter provided but is missing"); + } // open ogr driver dataset_ = reinterpret_cast(OGROpen(dataset_name_.c_str(), false, nullptr)); } diff --git a/plugins/input/ogr/ogr_datasource.hpp b/plugins/input/ogr/ogr_datasource.hpp index bde5829b0..819353421 100644 --- a/plugins/input/ogr/ogr_datasource.hpp +++ b/plugins/input/ogr/ogr_datasource.hpp @@ -47,6 +47,7 @@ MAPNIK_DISABLE_WARNING_PUSH #include MAPNIK_DISABLE_WARNING_POP #include "ogr_layer_ptr.hpp" +#include "ogr_utils.hpp" DATASOURCE_PLUGIN_DEF(ogr_datasource_plugin, ogr); diff --git a/plugins/input/ogr/ogr_utils.cpp b/plugins/input/ogr/ogr_utils.cpp new file mode 100644 index 000000000..04a3ec0f9 --- /dev/null +++ b/plugins/input/ogr/ogr_utils.cpp @@ -0,0 +1,85 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2023 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 "ogr_utils.hpp" +#include + +std::vector ogr_utils::split_open_options(const std::string& options) +{ + size_t i; + bool escaped = false; // if previous character was a backslash to escape a backslash or space + std::vector opts; + std::string unescaped_str; // copy of input string but unescaped + for (i = 0; i < options.size(); ++i) + { + char current = options.at(i); + if (current == '\\') + { + if (escaped) + { + unescaped_str.push_back(current); + } + escaped = !escaped; + } + else if (current != ' ') { + unescaped_str.push_back(current); + } + if (current == ' ' || i + 1 == options.size()) + { + if (!escaped) { + size_t count = unescaped_str.size(); + if (count > 0) + { + option_ptr opt (new char[count + 1], [](char* arr) { delete[] arr; }); + unescaped_str.copy(opt.get(), count); + opt[count] = '\0'; + opts.push_back(std::move(opt)); + } + unescaped_str = ""; + } + else + { + escaped = false; + unescaped_str.push_back(current); + } + } + } + if (escaped) + { + throw mapnik::datasource_exception(" parameter ends with single backslash"); + } + opts.emplace_back(nullptr); + return opts; +} + + +char** ogr_utils::open_options_for_ogr(std::vector& options) +{ + char** for_ogr = new char*[options.size() + 1]; + for (size_t i = 0; i < options.size(); ++i) + { + for_ogr[i] = options.at(i).get(); + } + for_ogr[options.size()] = nullptr; + return for_ogr; +} + diff --git a/plugins/input/ogr/ogr_utils.hpp b/plugins/input/ogr/ogr_utils.hpp new file mode 100644 index 000000000..af306925d --- /dev/null +++ b/plugins/input/ogr/ogr_utils.hpp @@ -0,0 +1,43 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2023 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 PLUGINS_INPUT_OGR_OGR_UTILS_HPP_ +#define PLUGINS_INPUT_OGR_OGR_UTILS_HPP_ + +#include +#include +#include +#include + +namespace ogr_utils { + +using option_ptr = std::unique_ptr>; + +std::vector split_open_options(const std::string& options); + +char** open_options_for_ogr(std::vector& options); + +} // namespace ogr_utils + + + +#endif /* PLUGINS_INPUT_OGR_OGR_UTILS_HPP_ */ diff --git a/test/build.py b/test/build.py index 6771bae1f..1a784569a 100644 --- a/test/build.py +++ b/test/build.py @@ -35,6 +35,7 @@ else: # unit tests sources = glob.glob('./unit/*/*.cpp') sources.extend(glob.glob('./unit/*.cpp')) + sources.append('../plugins/input/ogr/ogr_utils.cpp') test_program = test_env_local.Program("./unit/run", source=sources) Depends(test_program, env.subst('../src/%s' % env['MAPNIK_LIB_NAME'])) Depends(test_program, env.subst('../src/json/libmapnik-json${LIBSUFFIX}')) diff --git a/test/unit/datasource/ogr.cpp b/test/unit/datasource/ogr.cpp index 8441ecc55..a25548b55 100644 --- a/test/unit/datasource/ogr.cpp +++ b/test/unit/datasource/ogr.cpp @@ -29,6 +29,19 @@ #include #include #include +#include "../../../plugins/input/ogr/ogr_utils.hpp" + +inline std::vector split_options(std::string const& options) +{ + return ogr_utils::split_open_options(options); +} + +void assert_option(std::vector 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") { @@ -57,3 +70,59 @@ TEST_CASE("ogr") } } } + +TEST_CASE("ogr open_options") +{ + const bool 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 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 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 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 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 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 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 v = split_options(opts); + assert_option(v, 0, "ZOOM=14"); + assert_option(v, 1, "K23=V47"); + } + } +}