Merge branch 'geofabrik-ogr-open-options-pr'

This commit is contained in:
Artem Pavlenko 2023-11-17 16:31:13 +00:00
commit 7d2029165e
7 changed files with 208 additions and 1 deletions

View file

@ -31,6 +31,7 @@ plugin_sources = Split(
""" """
%(PLUGIN_NAME)s_converter.cpp %(PLUGIN_NAME)s_converter.cpp
%(PLUGIN_NAME)s_datasource.cpp %(PLUGIN_NAME)s_datasource.cpp
%(PLUGIN_NAME)s_utils.cpp
%(PLUGIN_NAME)s_featureset.cpp %(PLUGIN_NAME)s_featureset.cpp
%(PLUGIN_NAME)s_index_featureset.cpp %(PLUGIN_NAME)s_index_featureset.cpp
""" % locals() """ % locals()

View file

@ -127,16 +127,23 @@ void ogr_datasource::init(mapnik::parameters const& params)
} }
std::string driver = *params.get<std::string>("driver", ""); std::string driver = *params.get<std::string>("driver", "");
std::vector<ogr_utils::option_ptr> open_options_map = ogr_utils::split_open_options(*params.get<std::string>("open_options", ""));
char** open_options = ogr_utils::open_options_for_ogr(open_options_map);
if (!driver.empty()) if (!driver.empty())
{ {
unsigned int nOpenFlags = GDAL_OF_READONLY | GDAL_OF_VECTOR; unsigned int nOpenFlags = GDAL_OF_READONLY | GDAL_OF_VECTOR;
const char* papszAllowedDrivers[] = {driver.c_str(), nullptr}; const char* papszAllowedDrivers[] = {driver.c_str(), nullptr};
dataset_ = reinterpret_cast<gdal_dataset_type>( dataset_ = reinterpret_cast<gdal_dataset_type>(
GDALOpenEx(dataset_name_.c_str(), nOpenFlags, papszAllowedDrivers, nullptr, nullptr)); GDALOpenEx(dataset_name_.c_str(), nOpenFlags, papszAllowedDrivers, open_options, nullptr));
} }
else else
{ {
if (open_options[0] != nullptr)
{
throw datasource_exception("<open_options> parameter provided but <driver> is missing");
}
// open ogr driver // open ogr driver
dataset_ = reinterpret_cast<gdal_dataset_type>(OGROpen(dataset_name_.c_str(), false, nullptr)); dataset_ = reinterpret_cast<gdal_dataset_type>(OGROpen(dataset_name_.c_str(), false, nullptr));
} }

View file

@ -47,6 +47,7 @@ MAPNIK_DISABLE_WARNING_PUSH
#include <ogrsf_frmts.h> #include <ogrsf_frmts.h>
MAPNIK_DISABLE_WARNING_POP MAPNIK_DISABLE_WARNING_POP
#include "ogr_layer_ptr.hpp" #include "ogr_layer_ptr.hpp"
#include "ogr_utils.hpp"
DATASOURCE_PLUGIN_DEF(ogr_datasource_plugin, ogr); DATASOURCE_PLUGIN_DEF(ogr_datasource_plugin, ogr);

View file

@ -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 <mapnik/datasource.hpp>
std::vector<ogr_utils::option_ptr> 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<ogr_utils::option_ptr> 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("<open_options> parameter ends with single backslash");
}
opts.emplace_back(nullptr);
return opts;
}
char** ogr_utils::open_options_for_ogr(std::vector<ogr_utils::option_ptr>& 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;
}

View file

@ -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 <functional>
#include <memory>
#include <string>
#include <vector>
namespace ogr_utils {
using option_ptr = std::unique_ptr<char[], std::function<void(char*)>>;
std::vector<option_ptr> split_open_options(const std::string& options);
char** open_options_for_ogr(std::vector<ogr_utils::option_ptr>& options);
} // namespace ogr_utils
#endif /* PLUGINS_INPUT_OGR_OGR_UTILS_HPP_ */

View file

@ -35,6 +35,7 @@ else:
# unit tests # unit tests
sources = glob.glob('./unit/*/*.cpp') sources = glob.glob('./unit/*/*.cpp')
sources.extend(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) 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/%s' % env['MAPNIK_LIB_NAME']))
Depends(test_program, env.subst('../src/json/libmapnik-json${LIBSUFFIX}')) Depends(test_program, env.subst('../src/json/libmapnik-json${LIBSUFFIX}'))

View file

@ -29,6 +29,19 @@
#include <mapnik/image_reader.hpp> #include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp> #include <mapnik/image_util.hpp>
#include <mapnik/datasource_cache.hpp> #include <mapnik/datasource_cache.hpp>
#include "../../../plugins/input/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") 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<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");
}
}
}