2016-03-17 11:52:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2016-03-17 11:52:35 +00: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"
|
2016-05-17 10:53:07 +00:00
|
|
|
#include "ds_test_util.hpp"
|
2016-03-17 11:52:35 +00:00
|
|
|
|
|
|
|
#include <mapnik/util/fs.hpp>
|
|
|
|
#include <mapnik/util/file_io.hpp>
|
|
|
|
#include <mapnik/json/topology.hpp>
|
2016-12-19 15:18:19 +00:00
|
|
|
#include <mapnik/json/topojson_grammar_x3.hpp>
|
2016-03-17 11:52:35 +00:00
|
|
|
#include <mapnik/json/topojson_utils.hpp>
|
|
|
|
|
2018-08-10 18:53:20 +00:00
|
|
|
#define HEREDOC(...) #__VA_ARGS__
|
|
|
|
|
2016-03-17 11:52:35 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-08-10 18:53:20 +00:00
|
|
|
bool parse_topology_string(std::string const& buffer, mapnik::topojson::topology & topo);
|
|
|
|
|
2016-03-17 11:52:35 +00:00
|
|
|
bool parse_topology(std::string const& filename, mapnik::topojson::topology & topo)
|
|
|
|
{
|
|
|
|
mapnik::util::file file(filename);
|
|
|
|
std::string buffer;
|
|
|
|
buffer.resize(file.size());
|
|
|
|
std::fread(&buffer[0], buffer.size(), 1, file.get());
|
|
|
|
if (!file) return false;
|
2018-08-10 18:53:20 +00:00
|
|
|
return parse_topology_string(buffer, topo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_topology_string(std::string const& buffer)
|
|
|
|
{
|
|
|
|
mapnik::topojson::topology topo;
|
|
|
|
return parse_topology_string(buffer, topo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_topology_string(std::string const& buffer, mapnik::topojson::topology & topo)
|
|
|
|
{
|
2016-12-19 15:18:19 +00:00
|
|
|
using space_type = boost::spirit::x3::standard::space_type;
|
|
|
|
char const* itr = buffer.c_str();
|
|
|
|
char const* end = itr + buffer.length();
|
|
|
|
try
|
|
|
|
{
|
2019-07-01 17:38:11 +00:00
|
|
|
boost::spirit::x3::phrase_parse(itr, end, mapnik::json::grammar::topology, space_type() , topo);
|
2016-12-19 15:18:19 +00:00
|
|
|
}
|
|
|
|
catch (boost::spirit::x3::expectation_failure<char const*> const& ex)
|
|
|
|
{
|
|
|
|
std::cerr << "failed to parse TopoJSON..." << std::endl;
|
|
|
|
std::cerr << ex.what() << std::endl;
|
|
|
|
std::cerr << "Expected: " << ex.which();
|
2018-08-10 19:30:50 +00:00
|
|
|
std::cerr << "\nGot: \"";
|
|
|
|
auto ctx = ex.where();
|
|
|
|
std::streamsize len = 0;
|
|
|
|
// stop before NUL terminator or after 200 bytes, whichever comes first
|
|
|
|
for (; ctx[len] && len < 200; ++len)
|
|
|
|
;
|
|
|
|
// extend to UTF-8 character boundary or 210 bytes, whichever comes first
|
|
|
|
for (; len < 210 && ((unsigned char)ctx[len] & 0xC0) == 0x80; ++len)
|
|
|
|
;
|
|
|
|
std::cerr.write(ctx, len);
|
|
|
|
std::cerr << (ctx[len] ? "..." : "") << '"' << std::endl;
|
2016-12-19 15:18:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (itr == end);
|
2016-03-17 11:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:53:20 +00:00
|
|
|
TEST_CASE("TopoJSON")
|
2016-03-17 11:52:35 +00:00
|
|
|
{
|
2018-08-10 18:53:20 +00:00
|
|
|
SECTION("Minimal Topology")
|
|
|
|
{
|
|
|
|
// + A topology must have a member with the name “objects” whose value is another object.
|
|
|
|
// + A topology must have a member with the name “arcs” whose value is an array of arcs.
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"type": "Topology", "objects": {}, "arcs": []
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"type": "Topology", "arcs": [], "objects": {}
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"objects": {}, "type": "Topology", "arcs": []
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"objects": {}, "arcs": [], "type": "Topology"
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"arcs": [], "type": "Topology", "objects": {}
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
CHECK(parse_topology_string(HEREDOC(
|
|
|
|
{
|
|
|
|
"arcs": [], "objects": {}, "type": "Topology"
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2016-03-17 11:52:35 +00:00
|
|
|
SECTION("geometry parsing")
|
|
|
|
{
|
2016-03-17 14:10:29 +00:00
|
|
|
mapnik::value_integer feature_id = 0;
|
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
|
|
|
mapnik::transcoder tr("utf8");
|
2016-03-17 11:52:35 +00:00
|
|
|
for (auto const& path : mapnik::util::list_directory("test/data/topojson/"))
|
|
|
|
{
|
|
|
|
mapnik::topojson::topology topo;
|
|
|
|
REQUIRE(parse_topology(path, topo));
|
|
|
|
for (auto const& geom : topo.geometries)
|
|
|
|
{
|
2016-03-17 14:10:29 +00:00
|
|
|
mapnik::box2d<double> bbox = mapnik::util::apply_visitor(mapnik::topojson::bounding_box_visitor(topo), geom);
|
|
|
|
CHECK(bbox.valid());
|
|
|
|
mapnik::topojson::feature_generator<mapnik::context_ptr> visitor(ctx, tr, topo, feature_id++);
|
|
|
|
mapnik::feature_ptr feature = mapnik::util::apply_visitor(visitor, geom);
|
|
|
|
CHECK(feature);
|
|
|
|
CHECK(feature->envelope() == bbox);
|
2016-03-17 11:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 10:53:07 +00:00
|
|
|
|
|
|
|
SECTION("TopoJSON properties are properly expressed")
|
|
|
|
{
|
|
|
|
std::string filename("./test/data/topojson/escaped.topojson");
|
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
|
|
|
mapnik::transcoder tr("utf8");
|
|
|
|
mapnik::topojson::topology topo;
|
|
|
|
REQUIRE(parse_topology(filename, topo));
|
|
|
|
mapnik::value_integer feature_id = 0;
|
|
|
|
for (auto const& geom : topo.geometries)
|
|
|
|
{
|
|
|
|
mapnik::box2d<double> bbox = mapnik::util::apply_visitor(mapnik::topojson::bounding_box_visitor(topo), geom);
|
|
|
|
CHECK(bbox.valid());
|
|
|
|
mapnik::topojson::feature_generator<mapnik::context_ptr> visitor(ctx, tr, topo, feature_id);
|
|
|
|
mapnik::feature_ptr feature = mapnik::util::apply_visitor(visitor, geom);
|
|
|
|
CHECK(feature);
|
|
|
|
CHECK(feature->envelope() == bbox);
|
|
|
|
std::initializer_list<attr> attrs = {
|
2016-05-24 10:16:48 +00:00
|
|
|
attr{"name", tr.transcode("Test")},
|
|
|
|
attr{"NOM_FR", tr.transcode("Québec")},
|
2018-06-30 13:26:17 +00:00
|
|
|
attr{"boolean", mapnik::value_bool(true)},
|
2016-05-24 10:16:48 +00:00
|
|
|
attr{"description", tr.transcode("Test: \u005C")},
|
2016-05-17 10:53:07 +00:00
|
|
|
attr{"double", mapnik::value_double(1.1)},
|
|
|
|
attr{"int", mapnik::value_integer(1)},
|
2016-08-12 10:05:50 +00:00
|
|
|
attr{"object", tr.transcode("{\"name\":\"waka\",\"spaces\":\"value with spaces\",\"int\":1,\"double\":1.1,\"boolean\":false"
|
|
|
|
",\"NOM_FR\":\"Québec\",\"array\":[\"string\",\"value with spaces\",3,1.1,null,true"
|
|
|
|
",\"Québec\"],\"another_object\":{\"name\":\"nested object\"}}")},
|
2016-05-24 10:16:48 +00:00
|
|
|
attr{"spaces", tr.transcode("this has spaces")},
|
|
|
|
attr{"array", tr.transcode("[\"string\",\"value with spaces\",3,1.1,null,true,"
|
2016-08-12 10:05:50 +00:00
|
|
|
"\"Québec\",{\"name\":\"object within an array\"},"
|
2016-05-24 10:16:48 +00:00
|
|
|
"[\"array\",\"within\",\"an\",\"array\"]]")},
|
|
|
|
attr{"empty_array", tr.transcode("[]")},
|
|
|
|
attr{"empty_object", tr.transcode("{}")},
|
2016-05-17 10:53:07 +00:00
|
|
|
};
|
|
|
|
REQUIRE_ATTRIBUTES(feature, attrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 11:52:35 +00:00
|
|
|
}
|