2015-10-22 19:31:57 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2015-10-22 19:31:57 +02: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-05-18 23:14:37 +02:00
|
|
|
#include "catch.hpp"
|
2015-10-29 17:23:54 +01:00
|
|
|
#include "ds_test_util.hpp"
|
2015-05-18 23:14:37 +02:00
|
|
|
|
|
|
|
#include <mapnik/map.hpp>
|
2016-03-12 05:07:54 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
2015-05-18 23:14:37 +02:00
|
|
|
#include <mapnik/datasource.hpp>
|
|
|
|
#include <mapnik/datasource_cache.hpp>
|
|
|
|
#include <mapnik/geometry.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/geometry_types.hpp>
|
|
|
|
#include <mapnik/geometry/geometry_type.hpp>
|
2015-05-18 23:14:37 +02:00
|
|
|
#include <mapnik/expression.hpp>
|
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
|
|
|
#include <mapnik/debug.hpp>
|
2021-05-17 17:06:34 +02:00
|
|
|
#include <mapnik/util/from_u8string.hpp>
|
2015-07-01 02:04:48 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2015-05-18 23:14:37 +02:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-09 18:51:35 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2015-10-23 19:39:34 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2015-10-23 19:39:34 +02:00
|
|
|
|
2015-05-18 23:14:37 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
2015-10-23 19:39:34 +02:00
|
|
|
|
|
|
|
bool is_csv(std::string const& filename)
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
return boost::iends_with(filename, ".csv") || boost::iends_with(filename, ".tsv");
|
2015-10-23 19:39:34 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
void add_csv_files(std::string dir, std::vector<std::string>& csv_files)
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
for (auto const& path : mapnik::util::list_directory(dir))
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
if (is_csv(path))
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
|
|
|
csv_files.emplace_back(path);
|
|
|
|
}
|
2015-05-18 23:14:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::datasource_ptr get_csv_ds(std::string const& file_name, bool strict = true, std::string const& base = "")
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
params["file"] = file_name;
|
2016-01-06 00:18:02 +01:00
|
|
|
if (!base.empty())
|
|
|
|
{
|
|
|
|
params["base"] = base;
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
params["strict"] = mapnik::value_bool(strict);
|
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
|
|
|
// require a non-null pointer returned
|
|
|
|
REQUIRE(ds != nullptr);
|
|
|
|
return ds;
|
2015-05-18 23:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
TEST_CASE("csv")
|
|
|
|
{
|
2021-05-17 17:06:34 +02:00
|
|
|
using mapnik::util::from_u8string;
|
2022-02-14 10:15:09 +01:00
|
|
|
|
|
|
|
// check the CSV datasource is loaded
|
|
|
|
const bool have_csv_plugin = mapnik::datasource_cache::instance().plugin_registered("csv");
|
|
|
|
if (have_csv_plugin)
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2015-10-29 17:23:54 +01:00
|
|
|
// make the tests silent since we intentionally test error conditions that are noisy
|
2015-08-19 12:04:45 +02:00
|
|
|
auto const severity = mapnik::logger::instance().get_severity();
|
|
|
|
mapnik::logger::instance().set_severity(mapnik::logger::none);
|
|
|
|
|
2015-10-23 15:11:17 +02:00
|
|
|
SECTION("CSV I/O errors")
|
|
|
|
{
|
|
|
|
std::string filename = "does_not_exist.csv";
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {true, false})
|
2015-10-23 15:11:17 +02:00
|
|
|
{
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
// index wont be created
|
|
|
|
CHECK(!mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = "csv";
|
|
|
|
params["file"] = filename;
|
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
2015-11-09 19:47:07 +01:00
|
|
|
params["base"] = "";
|
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
|
|
|
params["base"] = "/";
|
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
2015-10-23 15:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("broken files")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
std::vector<std::string> broken;
|
|
|
|
add_csv_files("test/data/csv/fails", broken);
|
|
|
|
add_csv_files("test/data/csv/warns", broken);
|
|
|
|
broken.emplace_back("test/data/csv/fails/does_not_exist.csv");
|
2015-10-23 14:11:08 +02:00
|
|
|
|
2022-02-14 10:15:09 +01:00
|
|
|
for (auto const& path : broken)
|
|
|
|
{
|
|
|
|
bool require_fail = true;
|
|
|
|
if (create_index)
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
int ret = create_disk_index(path);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
require_fail = (boost::iends_with(path, "feature_id_counting.csv")) ? false : true;
|
|
|
|
if (!require_fail)
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
REQUIRE(mapnik::util::exists(path + ".index"));
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 10:15:09 +01:00
|
|
|
INFO(path);
|
|
|
|
if (require_fail)
|
|
|
|
{
|
|
|
|
REQUIRE_THROWS(get_csv_ds(path));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK(bool(get_csv_ds(path)));
|
|
|
|
}
|
|
|
|
if (mapnik::util::exists(path + ".index"))
|
|
|
|
{
|
|
|
|
CHECK(mapnik::util::remove(path + ".index"));
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("good files")
|
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
std::vector<std::string> good;
|
|
|
|
add_csv_files("test/data/csv", good);
|
|
|
|
add_csv_files("test/data/csv/warns", good);
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2022-02-14 10:15:09 +01:00
|
|
|
for (auto const& path : good)
|
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(path + ".index"))
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
mapnik::util::remove(path + ".index");
|
|
|
|
}
|
|
|
|
for (auto create_index : {false, true})
|
|
|
|
{
|
|
|
|
if (create_index)
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
int ret = create_disk_index(path);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
if (!boost::iends_with(path,
|
|
|
|
"more_headers_than_column_values.csv")) // mapnik-index won't create
|
|
|
|
// *.index for 0 features
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-02-14 10:15:09 +01:00
|
|
|
CHECK(mapnik::util::exists(path + ".index"));
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 10:15:09 +01:00
|
|
|
auto ds = get_csv_ds(path, false);
|
|
|
|
// require a non-null pointer returned
|
|
|
|
REQUIRE(bool(ds));
|
|
|
|
if (mapnik::util::exists(path + ".index"))
|
|
|
|
{
|
|
|
|
CHECK(mapnik::util::remove(path + ".index"));
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
|
|
|
SECTION("lon/lat detection")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2015-10-23 14:11:08 +02:00
|
|
|
for (auto const& lon_name : {std::string("lon"), std::string("lng")})
|
|
|
|
{
|
|
|
|
std::string filename = (boost::format("test/data/csv/%1%_lat.csv") % lon_name).str();
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {lon_name, "lat"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer});
|
|
|
|
|
|
|
|
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
|
|
|
|
|
|
|
mapnik::query query(ds->envelope());
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& field : fields)
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
query.add_property_name(field.get_name());
|
|
|
|
}
|
|
|
|
auto features = ds->features(query);
|
|
|
|
auto feature = features->next();
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(
|
|
|
|
feature,
|
|
|
|
{attr{lon_name, mapnik::value_integer(0)}, attr{"lat", mapnik::value_integer(0)}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END SECTION
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("type detection")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2015-11-09 19:47:07 +01:00
|
|
|
std::string base = "test/data/csv/";
|
|
|
|
std::string filename = "nypd.csv";
|
|
|
|
std::string filepath = base + filename;
|
2015-10-23 14:11:08 +02:00
|
|
|
// cleanup in the case of a failed previous run
|
2015-11-09 19:47:07 +01:00
|
|
|
if (mapnik::util::exists(filepath + ".index"))
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
2015-11-09 19:47:07 +01:00
|
|
|
mapnik::util::remove(filepath + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
2015-11-09 19:47:07 +01:00
|
|
|
int ret = create_disk_index(filepath);
|
2015-10-23 14:11:08 +02:00
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
2015-11-09 19:47:07 +01:00
|
|
|
CHECK(mapnik::util::exists(filepath + ".index"));
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
2022-01-26 23:25:53 +01:00
|
|
|
auto ds = get_csv_ds(filename, true, base);
|
2015-11-09 19:47:07 +01:00
|
|
|
CHECK(ds->type() == mapnik::datasource::datasource_t::Vector);
|
2015-10-23 14:11:08 +02:00
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
2022-01-26 23:25:53 +01:00
|
|
|
require_field_names(
|
|
|
|
fields,
|
|
|
|
{"Precinct", "Phone", "Address", "City", "geo_longitude", "geo_latitude", "geo_accuracy"});
|
|
|
|
require_field_types(fields,
|
|
|
|
{mapnik::String,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::Double,
|
|
|
|
mapnik::Double,
|
|
|
|
mapnik::String});
|
2015-10-23 14:11:08 +02:00
|
|
|
|
|
|
|
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
|
|
|
CHECK(count_features(all_features(ds)) == 2);
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-11-09 19:47:07 +01:00
|
|
|
auto fs = all_features(ds);
|
2022-01-26 23:25:53 +01:00
|
|
|
auto fs2 = ds->features_at_point(ds->envelope().center(), 10000);
|
2015-11-09 19:47:07 +01:00
|
|
|
REQUIRE(fs != nullptr);
|
|
|
|
auto feature = fs->next();
|
|
|
|
auto feature2 = fs2->next();
|
|
|
|
REQUIRE(feature != nullptr);
|
|
|
|
REQUIRE(feature2 != nullptr);
|
2022-01-26 23:25:53 +01:00
|
|
|
auto expected_attr = {attr{"City", mapnik::value_unicode_string("New York, NY")},
|
|
|
|
attr{"geo_accuracy", mapnik::value_unicode_string("house")},
|
|
|
|
attr{"Phone", mapnik::value_unicode_string("(212) 334-0711")},
|
|
|
|
attr{"Address", mapnik::value_unicode_string("19 Elizabeth Street")},
|
|
|
|
attr{"Precinct", mapnik::value_unicode_string("5th Precinct")},
|
|
|
|
attr{"geo_longitude", mapnik::value_double(-70.0)},
|
|
|
|
attr{"geo_latitude", mapnik::value_double(40.0)}};
|
2018-06-30 15:09:49 +02:00
|
|
|
REQUIRE_ATTRIBUTES(feature, expected_attr);
|
|
|
|
REQUIRE_ATTRIBUTES(feature2, expected_attr);
|
2015-11-09 19:47:07 +01:00
|
|
|
if (mapnik::util::exists(filepath + ".index"))
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2015-11-09 19:47:07 +01:00
|
|
|
mapnik::util::remove(filepath + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("skipping blank rows")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/blank_rows.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "name"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::String});
|
|
|
|
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
|
|
|
CHECK(count_features(all_features(ds)) == 2);
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("empty rows")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/empty_rows.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
2022-01-26 23:25:53 +01:00
|
|
|
require_field_names(
|
|
|
|
fields,
|
|
|
|
{"x", "y", "text", "date", "integer", "boolean", "float", "time", "datetime", "empty_column"});
|
|
|
|
require_field_types(fields,
|
|
|
|
{mapnik::Integer,
|
|
|
|
mapnik::Integer,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::Integer,
|
|
|
|
mapnik::Boolean,
|
|
|
|
mapnik::Double,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::String,
|
|
|
|
mapnik::String});
|
2015-10-23 14:11:08 +02:00
|
|
|
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
|
|
|
CHECK(count_features(all_features(ds)) == 4);
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
auto featureset = all_features(ds);
|
|
|
|
auto feature = featureset->next();
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(feature,
|
|
|
|
{attr{"x", mapnik::value_integer(0)},
|
|
|
|
attr{"empty_column", mapnik::value_unicode_string("")},
|
|
|
|
attr{"text", mapnik::value_unicode_string("a b")},
|
|
|
|
attr{"float", mapnik::value_double(1.0)},
|
|
|
|
attr{"datetime", mapnik::value_unicode_string("1971-01-01T04:14:00")},
|
|
|
|
attr{"y", mapnik::value_integer(0)},
|
|
|
|
attr{"boolean", mapnik::value_bool(true)},
|
|
|
|
attr{"time", mapnik::value_unicode_string("04:14:00")},
|
|
|
|
attr{"date", mapnik::value_unicode_string("1971-01-01")},
|
|
|
|
attr{"integer", mapnik::value_integer(40)}});
|
|
|
|
|
|
|
|
while (bool(feature = featureset->next()))
|
|
|
|
{
|
2015-10-23 14:11:08 +02:00
|
|
|
CHECK(feature->size() == 10);
|
|
|
|
CHECK(feature->get("empty_column") == mapnik::value_unicode_string(""));
|
|
|
|
}
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("slashes")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/has_attributes_with_slashes.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "name"});
|
|
|
|
// NOTE: y column is integer, even though a double value is used below in the test?
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::String});
|
|
|
|
|
|
|
|
auto featureset = all_features(ds);
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"name", mapnik::value_unicode_string("a/a")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 1}, attr{"y", 4}, attr{"name", mapnik::value_unicode_string("b/b")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 10}, attr{"y", 2.5}, attr{"name", mapnik::value_unicode_string("c/c")}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("wkt field")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/wkt.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
using mapnik::geometry::geometry_types;
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"type"});
|
|
|
|
require_field_types(fields, {mapnik::String});
|
|
|
|
|
|
|
|
auto featureset = all_features(ds);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Point);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::LineString);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Polygon);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Polygon);
|
|
|
|
require_geometry(featureset->next(), 4, geometry_types::MultiPoint);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiLineString);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiPolygon);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiPolygon);
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("handling of missing header")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/missing_header.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
// TODO: does this mean 'missing_header.csv' should be in the warnings
|
|
|
|
// subdirectory, since it doesn't work in strict mode?
|
|
|
|
auto ds = get_csv_ds(filename, false);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"one", "two", "x", "y", "_4", "aftermissing"});
|
|
|
|
auto feature = all_features(ds)->next();
|
|
|
|
REQUIRE(feature);
|
|
|
|
REQUIRE(feature->has_key("_4"));
|
|
|
|
CHECK(feature->get("_4") == mapnik::value_unicode_string("missing"));
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("handling of headers that are numbers")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/numbers_for_headers.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "1990", "1991", "1992"});
|
|
|
|
auto feature = all_features(ds)->next();
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(feature,
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"1990", 1}, attr{"1991", 2}, attr{"1992", 3}});
|
2015-10-23 14:11:08 +02:00
|
|
|
auto expression = mapnik::parse_expression("[1991]=2");
|
|
|
|
REQUIRE(bool(expression));
|
|
|
|
auto value = mapnik::util::apply_visitor(
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::evaluate<mapnik::feature_impl, mapnik::value_type, mapnik::attributes>(*feature,
|
|
|
|
mapnik::attributes()),
|
|
|
|
*expression);
|
2015-10-23 14:11:08 +02:00
|
|
|
CHECK(value == true);
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("quoted numbers")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/quoted_numbers.csv";
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "label"});
|
|
|
|
auto featureset = all_features(ds);
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 0}, attr{"y", 0}, attr{"label", ustring("0,0")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 5}, attr{"y", 5}, attr{"label", ustring("5,5")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 0}, attr{"y", 5}, attr{"label", ustring("0,5")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 5}, attr{"y", 0}, attr{"label", ustring("5,0")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 2.5}, attr{"y", 2.5}, attr{"label", ustring("2.5,2.5")}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("reading newlines")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
for (auto const& platform : {std::string("windows"), std::string("mac")})
|
|
|
|
{
|
|
|
|
std::string filename = (boost::format("test/data/csv/%1%_newlines.csv") % platform).str();
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "z"});
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(all_features(ds)->next(), {attr{"x", 1}, attr{"y", 10}, attr{"z", 9999.9999}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("mixed newlines")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& filename :
|
|
|
|
{std::string("test/data/csv/mac_newlines_with_unix_inline.csv"),
|
|
|
|
std::string("test/data/csv/mac_newlines_with_unix_inline_escaped.csv"),
|
|
|
|
std::string("test/data/csv/windows_newlines_with_unix_inline.csv"),
|
|
|
|
std::string("test/data/csv/windows_newlines_with_unix_inline_escaped.csv")})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "line"});
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(all_features(ds)->next(),
|
|
|
|
{attr{"x", 0},
|
|
|
|
attr{"y", 0},
|
|
|
|
attr{"line", ustring("many\n lines\n of text\n with unix newlines")}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END SECTION
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("tabs")
|
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/tabs_in_csv.csv";
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
2015-08-19 12:04:45 +02:00
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
2015-10-23 14:11:08 +02:00
|
|
|
require_field_names(fields, {"x", "y", "z"});
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(all_features(ds)->next(), {attr{"x", -122}, attr{"y", 48}, attr{"z", 0}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("separators")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& filename : {std::string("test/data/csv/pipe_delimiters.csv"),
|
|
|
|
std::string("test/data/csv/semicolon_delimiters.csv")})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "z"});
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(all_features(ds)->next(),
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"z", ustring("hello")}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("null and bool keywords are empty strings")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
2015-10-23 14:11:08 +02:00
|
|
|
std::string filename = "test/data/csv/nulls_and_booleans_as_strings.csv";
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "null", "boolean"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::String, mapnik::Boolean});
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
auto featureset = all_features(ds);
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"null", ustring("null")}, attr{"boolean", true}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(),
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"null", ustring("")}, attr{"boolean", false}});
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("nonexistent query fields throw")
|
|
|
|
{
|
|
|
|
std::string filename = "test/data/csv/lon_lat.csv";
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"lon", "lat"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer});
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
mapnik::query query(ds->envelope());
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& field : fields)
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
query.add_property_name(field.get_name());
|
|
|
|
}
|
|
|
|
// also add an invalid one, triggering throw
|
|
|
|
query.add_property_name("bogus");
|
|
|
|
REQUIRE_THROWS(ds->features(query));
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("leading zeros mean strings")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
2015-10-23 14:11:08 +02:00
|
|
|
std::string filename = "test/data/csv/leading_zeros.csv";
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto create_index : {false, true})
|
2015-10-23 14:11:08 +02:00
|
|
|
{
|
|
|
|
// cleanup in the case of a failed previous run
|
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
if (create_index)
|
|
|
|
{
|
|
|
|
int ret = create_disk_index(filename);
|
|
|
|
int ret_posix = (ret >> 8) & 0x000000ff;
|
|
|
|
INFO(ret);
|
|
|
|
INFO(ret_posix);
|
|
|
|
CHECK(mapnik::util::exists(filename + ".index"));
|
|
|
|
}
|
|
|
|
auto ds = get_csv_ds(filename);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "fips"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::String});
|
|
|
|
|
|
|
|
auto featureset = all_features(ds);
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 0}, attr{"y", 0}, attr{"fips", ustring("001")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 0}, attr{"y", 0}, attr{"fips", ustring("003")}});
|
|
|
|
REQUIRE_ATTRIBUTES(featureset->next(), {attr{"x", 0}, attr{"y", 0}, attr{"fips", ustring("005")}});
|
2015-10-23 14:11:08 +02:00
|
|
|
if (mapnik::util::exists(filename + ".index"))
|
|
|
|
{
|
2015-10-23 19:39:34 +02:00
|
|
|
mapnik::util::remove(filename + ".index");
|
2015-10-23 14:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("advanced geometry detection")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using row = std::pair<std::string, mapnik::datasource_geometry_t>;
|
2022-01-26 23:25:53 +01:00
|
|
|
for (row r : {row{"point", mapnik::datasource_geometry_t::Point},
|
|
|
|
row{"poly", mapnik::datasource_geometry_t::Polygon},
|
|
|
|
row{"multi_poly", mapnik::datasource_geometry_t::Polygon},
|
|
|
|
row{"line", mapnik::datasource_geometry_t::LineString}})
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
std::string file_name = (boost::format("test/data/csv/%1%_wkt.csv") % r.first).str();
|
|
|
|
auto ds = get_csv_ds(file_name);
|
|
|
|
CHECK(ds->get_geometry_type() == r.second);
|
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-23 14:11:08 +02:00
|
|
|
SECTION("creation of CSV from in-memory strings")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& name : {std::string("Winthrop, WA"), from_u8string(u8"Qu\u00e9bec")})
|
|
|
|
{
|
|
|
|
std::string csv_string = (boost::format("wkt,Name\n"
|
|
|
|
"\"POINT (120.15 48.47)\",\"%1%\"\n") %
|
|
|
|
name)
|
|
|
|
.str();
|
2015-08-19 12:04:45 +02:00
|
|
|
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
params["inline"] = csv_string;
|
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
|
|
|
REQUIRE(bool(ds));
|
|
|
|
|
|
|
|
auto feature = all_features(ds)->next();
|
|
|
|
REQUIRE(bool(feature));
|
|
|
|
REQUIRE(feature->has_key("Name"));
|
2016-03-12 05:07:54 +01:00
|
|
|
std::string utf8;
|
2016-03-12 17:59:54 +01:00
|
|
|
mapnik::transcoder tr("utf-8");
|
|
|
|
ustring expected_string = tr.transcode(name.c_str());
|
2016-03-12 05:07:54 +01:00
|
|
|
mapnik::value val(expected_string);
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::to_utf8(expected_string, utf8);
|
2016-03-12 05:07:54 +01:00
|
|
|
INFO(feature->get("Name"));
|
|
|
|
INFO(utf8);
|
|
|
|
INFO(val);
|
|
|
|
CHECK(feature->get("Name") == val);
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2015-10-22 16:11:18 +02:00
|
|
|
SECTION("creation of CSV from in-memory strings with bogus headers")
|
|
|
|
{
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
|
|
|
|
// should throw
|
|
|
|
params["inline"] = "latitude, longtitude, Name\n" // misspellt (!)
|
2022-01-26 23:25:53 +01:00
|
|
|
"120.15,48.47,Winhrop";
|
2015-10-22 16:11:18 +02:00
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
|
|
|
|
|
|
|
// should throw
|
|
|
|
params["strict"] = true;
|
|
|
|
params["inline"] = "latitude, longitude\n" // -- missing header
|
2022-01-26 23:25:53 +01:00
|
|
|
"120.15,48.47,Winhrop";
|
2015-10-22 16:11:18 +02:00
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
|
|
|
|
|
|
|
// should not throw
|
|
|
|
params["strict"] = false;
|
|
|
|
params["inline"] = "latitude, longitude,Name\n"
|
2022-01-26 23:25:53 +01:00
|
|
|
"0,0,Unknown, extra bogus field\n"
|
|
|
|
"120.15,48.47,Winhrop\n";
|
2015-10-22 16:11:18 +02:00
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
|
|
|
REQUIRE(bool(ds));
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE(ds->envelope() == mapnik::box2d<double>(48.47, 120.15, 48.47, 120.15));
|
2015-10-22 16:11:18 +02:00
|
|
|
auto feature = all_features(ds)->next();
|
|
|
|
REQUIRE(bool(feature));
|
|
|
|
REQUIRE(feature->has_key("Name"));
|
|
|
|
|
|
|
|
// should throw
|
|
|
|
params["strict"] = false;
|
|
|
|
params["inline"] = "x, Name\n" // -- missing required *geometry* header
|
2022-01-26 23:25:53 +01:00
|
|
|
"120.15,Winhrop";
|
2015-10-22 16:11:18 +02:00
|
|
|
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
|
|
|
|
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("geojson quoting")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using mapnik::geometry::geometry_types;
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& file : {std::string("test/data/csv/geojson_double_quote_escape.csv"),
|
|
|
|
std::string("test/data/csv/geojson_single_quote.csv"),
|
|
|
|
std::string("test/data/csv/geojson_2x_double_quote_filebakery_style.csv")})
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
auto ds = get_csv_ds(file);
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"type"});
|
|
|
|
require_field_types(fields, {mapnik::String});
|
|
|
|
|
|
|
|
auto featureset = all_features(ds);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Point);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::LineString);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Polygon);
|
|
|
|
require_geometry(featureset->next(), 1, geometry_types::Polygon);
|
|
|
|
require_geometry(featureset->next(), 4, geometry_types::MultiPoint);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiLineString);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiPolygon);
|
|
|
|
require_geometry(featureset->next(), 2, geometry_types::MultiPolygon);
|
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("fewer headers than rows throws")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
REQUIRE_THROWS(get_csv_ds("test/data/csv/more_column_values_than_headers.csv"));
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("feature ID only incremented for valid rows")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
auto ds = get_csv_ds("test/data/csv/warns/feature_id_counting.csv", false);
|
|
|
|
auto fs = all_features(ds);
|
|
|
|
|
|
|
|
// first
|
|
|
|
auto feature = fs->next();
|
|
|
|
REQUIRE(bool(feature));
|
|
|
|
CHECK(feature->id() == 1);
|
|
|
|
|
|
|
|
// second, should have skipped bogus one
|
|
|
|
feature = fs->next();
|
|
|
|
REQUIRE(bool(feature));
|
|
|
|
CHECK(feature->id() == 2);
|
|
|
|
|
|
|
|
feature = fs->next();
|
|
|
|
CHECK(!feature);
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("dynamically defining headers")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
using ustring = mapnik::value_unicode_string;
|
|
|
|
using row = std::pair<std::string, std::size_t>;
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
for (auto const& r : {row{"test/data/csv/fails/needs_headers_two_lines.csv", 2},
|
|
|
|
row{"test/data/csv/fails/needs_headers_one_line.csv", 1},
|
|
|
|
row{"test/data/csv/fails/needs_headers_one_line_no_newline.csv", 1}})
|
2015-08-19 12:04:45 +02:00
|
|
|
{
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
params["file"] = r.first;
|
|
|
|
params["headers"] = "x,y,name";
|
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
|
|
|
REQUIRE(bool(ds));
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "name"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::String});
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(all_features(ds)->next(),
|
|
|
|
{attr{"x", 0}, attr{"y", 0}, attr{"name", ustring("data_name")}});
|
2015-08-19 12:04:45 +02:00
|
|
|
REQUIRE(count_features(all_features(ds)) == r.second);
|
2015-08-27 13:02:17 +02:00
|
|
|
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
2015-08-19 12:04:45 +02:00
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
|
|
|
MAPNIK_DISABLE_LONG_LONG
|
|
|
|
SECTION("64bit int fields work")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
auto ds = get_csv_ds("test/data/csv/64bit_int.csv");
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "bigint"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::Integer});
|
|
|
|
|
|
|
|
auto fs = all_features(ds);
|
|
|
|
auto feature = fs->next();
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(feature, {attr{"x", 0}, attr{"y", 0}, attr{"bigint", 2147483648}});
|
2015-08-19 12:04:45 +02:00
|
|
|
|
|
|
|
feature = fs->next();
|
2022-01-26 23:25:53 +01:00
|
|
|
REQUIRE_ATTRIBUTES(feature, {attr{"x", 0}, attr{"y", 0}, attr{"bigint", 9223372036854775807ll}});
|
|
|
|
REQUIRE_ATTRIBUTES(feature, {attr{"x", 0}, attr{"y", 0}, attr{"bigint", 0x7FFFFFFFFFFFFFFFll}});
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
2022-01-26 23:25:53 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2015-08-19 12:04:45 +02:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("various number types")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
auto ds = get_csv_ds("test/data/csv/number_types.csv");
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {"x", "y", "floats"});
|
|
|
|
require_field_types(fields, {mapnik::Integer, mapnik::Integer, mapnik::Double});
|
|
|
|
auto fs = all_features(ds);
|
2022-01-26 23:25:53 +01:00
|
|
|
for (double d : {.0, +.0, 1e-06, -1e-06, 0.000001, 1.234e+16, 1.234e+16})
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
auto feature = fs->next();
|
|
|
|
REQUIRE(bool(feature));
|
|
|
|
CHECK(feature->get("floats").get<mapnik::value_double>() == Approx(d));
|
|
|
|
}
|
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("manually supplied extent")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
std::string csv_string("wkt,Name\n");
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
params["inline"] = csv_string;
|
|
|
|
params["extent"] = "-180,-90,180,90";
|
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
2015-07-01 02:04:48 +02:00
|
|
|
REQUIRE(bool(ds));
|
2015-08-19 12:04:45 +02:00
|
|
|
auto box = ds->envelope();
|
|
|
|
CHECK(box.minx() == -180);
|
2022-01-26 23:25:53 +01:00
|
|
|
CHECK(box.miny() == -90);
|
|
|
|
CHECK(box.maxx() == 180);
|
|
|
|
CHECK(box.maxy() == 90);
|
2015-08-19 12:04:45 +02:00
|
|
|
} // END SECTION
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("inline geojson")
|
|
|
|
{
|
2015-08-19 12:04:45 +02:00
|
|
|
std::string csv_string = "geojson\n'{\"coordinates\":[-92.22568,38.59553],\"type\":\"Point\"}'";
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = std::string("csv");
|
|
|
|
params["inline"] = csv_string;
|
2015-10-02 13:20:54 +02:00
|
|
|
params["quote"] = "'";
|
2015-08-19 12:04:45 +02:00
|
|
|
auto ds = mapnik::datasource_cache::instance().create(params);
|
|
|
|
REQUIRE(bool(ds));
|
|
|
|
|
|
|
|
auto fields = ds->get_descriptor().get_descriptors();
|
|
|
|
require_field_names(fields, {});
|
|
|
|
|
|
|
|
// TODO: this originally had the following comment:
|
|
|
|
// - re-enable after https://github.com/mapnik/mapnik/issues/2319 is fixed
|
|
|
|
// but that seems to have been merged and tested separately?
|
|
|
|
auto fs = all_features(ds);
|
|
|
|
auto feat = fs->next();
|
|
|
|
CHECK(feature_count(feat->get_geometry()) == 1);
|
|
|
|
} // END SECTION
|
|
|
|
mapnik::logger::instance().set_severity(severity);
|
|
|
|
}
|
2015-05-18 23:14:37 +02:00
|
|
|
} // END TEST CASE
|