2015-11-24 11:50:09 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2024-07-22 11:20:47 +02:00
|
|
|
* Copyright (C) 2024 Artem Pavlenko
|
2015-11-24 11:50:09 +01:00
|
|
|
*
|
|
|
|
* 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 <mapnik/datasource.hpp>
|
|
|
|
#include <mapnik/datasource_cache.hpp>
|
2016-02-19 11:02:12 +01:00
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
2015-11-24 11:50:09 +01:00
|
|
|
#include <mapnik/util/fs.hpp>
|
|
|
|
#include <cstdlib>
|
2016-02-18 17:36:18 +01:00
|
|
|
#include <fstream>
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-24 11:50:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2022-01-21 00:10:17 +01:00
|
|
|
#include <mapnik/util/mapped_memory_file.hpp>
|
2015-11-24 11:50:09 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::size_t count_shapefile_features(std::string const& filename)
|
|
|
|
{
|
2016-02-19 11:02:12 +01:00
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
|
|
|
mapnik::mapped_memory_cache::instance().clear();
|
|
|
|
#endif
|
2015-11-24 11:50:09 +01:00
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = "shape";
|
|
|
|
params["file"] = filename;
|
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
2016-01-30 01:30:03 +01:00
|
|
|
REQUIRE(ds != nullptr);
|
2015-11-24 11:50:09 +01:00
|
|
|
CHECK(ds->type() == mapnik::datasource::datasource_t::Vector);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
mapnik::query query(ds->envelope());
|
|
|
|
for (auto const& field : fields)
|
|
|
|
{
|
|
|
|
query.add_property_name(field.get_name());
|
|
|
|
}
|
|
|
|
auto features = ds->features(query);
|
2016-01-30 01:30:03 +01:00
|
|
|
REQUIRE(features != nullptr);
|
2015-11-24 11:50:09 +01:00
|
|
|
|
|
|
|
std::size_t feature_count = 0;
|
|
|
|
auto feature = features->next();
|
|
|
|
while (feature)
|
|
|
|
{
|
|
|
|
++feature_count;
|
|
|
|
feature = features->next();
|
|
|
|
}
|
2016-02-18 17:36:18 +01:00
|
|
|
|
2015-11-24 11:50:09 +01:00
|
|
|
return feature_count;
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:22:09 +01:00
|
|
|
int create_shapefile_index(std::string const& filename, bool index_parts, bool silent = true)
|
2015-11-24 11:50:09 +01:00
|
|
|
{
|
|
|
|
std::string cmd;
|
|
|
|
if (std::getenv("DYLD_LIBRARY_PATH") != nullptr)
|
|
|
|
{
|
2016-01-04 15:22:09 +01:00
|
|
|
cmd += std::string("DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " ";
|
2015-11-24 11:50:09 +01:00
|
|
|
}
|
2021-10-09 13:46:55 +02:00
|
|
|
cmd += "shapeindex";
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2021-10-09 13:55:32 +02:00
|
|
|
cmd += ".exe";
|
|
|
|
#endif
|
2021-10-09 13:46:55 +02:00
|
|
|
cmd += " ";
|
2022-01-26 23:25:53 +01:00
|
|
|
if (index_parts)
|
|
|
|
cmd += "--index-parts ";
|
2016-01-04 15:22:09 +01:00
|
|
|
cmd += filename;
|
2015-11-24 11:50:09 +01:00
|
|
|
if (silent)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifndef _WIN32
|
2015-11-24 11:50:09 +01:00
|
|
|
cmd += " 2>/dev/null";
|
|
|
|
#else
|
|
|
|
cmd += " 2> nul";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return std::system(cmd.c_str());
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
} // namespace
|
2015-11-24 11:50:09 +01:00
|
|
|
|
2016-02-18 17:36:18 +01:00
|
|
|
TEST_CASE("invalid shapeindex")
|
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
const bool have_shape_plugin = mapnik::datasource_cache::instance().plugin_registered("shape");
|
|
|
|
if (have_shape_plugin)
|
2016-02-18 17:36:18 +01:00
|
|
|
{
|
|
|
|
SECTION("Invalid index")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto val :
|
|
|
|
{std::make_tuple(true, std::string("mapnik-invalid-index.................")), // invalid header
|
|
|
|
std::make_tuple(false, std::string("mapnik-index................."))}) // valid header + invalid index
|
2016-02-18 17:36:18 +01:00
|
|
|
{
|
2016-02-19 11:25:27 +01:00
|
|
|
std::string path = "test/data/shp/boundaries.shp";
|
|
|
|
std::string index_path = path.substr(0, path.rfind(".")) + ".index";
|
|
|
|
// remove *.index if present
|
2022-01-21 00:10:17 +01:00
|
|
|
mapnik::util::mapped_memory_file::deleteFile(index_path);
|
2016-02-19 11:25:27 +01:00
|
|
|
// count features
|
2016-02-18 17:36:18 +01:00
|
|
|
|
2016-02-19 11:25:27 +01:00
|
|
|
std::size_t feature_count = count_shapefile_features(path);
|
2016-02-18 17:36:18 +01:00
|
|
|
|
2016-02-19 11:25:27 +01:00
|
|
|
// create index
|
|
|
|
std::ofstream index(index_path.c_str(), std::ios::binary);
|
|
|
|
index.write(std::get<1>(val).c_str(), std::get<1>(val).size());
|
|
|
|
index.close();
|
2016-02-18 17:36:18 +01:00
|
|
|
|
2016-02-19 11:25:27 +01:00
|
|
|
// count features
|
|
|
|
std::size_t feature_count_indexed = count_shapefile_features(path);
|
|
|
|
if (std::get<0>(val)) // fallback to un-indexed access
|
|
|
|
{
|
|
|
|
// ensure number of features are the same
|
|
|
|
CHECK(feature_count == feature_count_indexed);
|
|
|
|
}
|
2022-01-26 23:25:53 +01:00
|
|
|
else // the header is valid but index file itself is not - expect datasource to fail and return 0
|
|
|
|
// features.
|
2016-02-19 11:25:27 +01:00
|
|
|
{
|
|
|
|
CHECK(feature_count_indexed == 0);
|
|
|
|
}
|
|
|
|
// remove *.index if present
|
2022-01-21 00:10:17 +01:00
|
|
|
mapnik::util::mapped_memory_file::deleteFile(index_path);
|
2016-02-18 17:36:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 11:50:09 +01:00
|
|
|
TEST_CASE("shapeindex")
|
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
const bool have_shape_plugin = mapnik::datasource_cache::instance().plugin_registered("shape");
|
|
|
|
if (have_shape_plugin)
|
2015-11-24 11:50:09 +01:00
|
|
|
{
|
2016-02-18 17:36:18 +01:00
|
|
|
SECTION("Index")
|
2015-11-24 11:50:09 +01:00
|
|
|
{
|
|
|
|
for (auto const& path : mapnik::util::list_directory("test/data/shp/"))
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
if (boost::iends_with(path, ".shp"))
|
2015-11-24 11:50:09 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (bool index_parts : {false, true})
|
2015-11-24 11:50:09 +01:00
|
|
|
{
|
2016-02-04 18:54:21 +01:00
|
|
|
CAPTURE(path);
|
|
|
|
CAPTURE(index_parts);
|
|
|
|
|
2016-01-04 15:22:09 +01:00
|
|
|
std::string index_path = path.substr(0, path.rfind(".")) + ".index";
|
|
|
|
// remove *.index if present
|
2022-01-21 00:10:17 +01:00
|
|
|
mapnik::util::mapped_memory_file::deleteFile(index_path);
|
2016-01-04 15:22:09 +01:00
|
|
|
// count features
|
|
|
|
std::size_t feature_count = count_shapefile_features(path);
|
|
|
|
// create *.index
|
2017-06-01 15:27:17 +02:00
|
|
|
if (feature_count > 0)
|
2016-01-04 15:22:09 +01:00
|
|
|
{
|
2017-06-01 15:27:17 +02:00
|
|
|
REQUIRE(create_shapefile_index(path, index_parts) == EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
REQUIRE(create_shapefile_index(path, index_parts) != EXIT_SUCCESS);
|
2016-01-04 15:22:09 +01:00
|
|
|
REQUIRE(!mapnik::util::exists(index_path)); // index won't be created if there's no features
|
|
|
|
}
|
|
|
|
// count features
|
|
|
|
std::size_t feature_count_indexed = count_shapefile_features(path);
|
|
|
|
// ensure number of features are the same
|
|
|
|
REQUIRE(feature_count == feature_count_indexed);
|
|
|
|
// remove *.index if present
|
2022-01-21 00:10:17 +01:00
|
|
|
mapnik::util::mapped_memory_file::deleteFile(index_path);
|
2015-11-24 11:50:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|