1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
|
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2025 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 "catch.hpp"
#include "ds_test_util.hpp"
#include <mapnik/unicode.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/geometry/geometry_type.hpp>
#include <mapnik/geometry/is_empty.hpp>
#include <mapnik/json/geometry_parser.hpp>
#include <mapnik/util/geometry_to_geojson.hpp>
#include <mapnik/util/fs.hpp>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <locale>
#include <string_view>
#include <array>
#include <utility>
/*
Compile and run just this test:
clang++ -o test-geojson -g -I./test/ test/unit/run.cpp test/unit/datasource/geojson.cpp `mapnik-config --all-flags`
./test-geojson -d yes
*/
namespace {
std::pair<mapnik::datasource_ptr, mapnik::feature_ptr> fetch_first_feature(std::string const& filename,
bool cache_features)
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = filename;
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
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);
auto feature = features->next();
return std::make_pair(ds, feature);
}
void iterate_over_features(mapnik::featureset_ptr features)
{
auto feature = features->next();
while (feature != nullptr)
{
feature = features->next();
}
}
} // namespace
TEST_CASE("geojson")
{
bool const have_geojson_plugin = mapnik::datasource_cache::instance().plugin_registered("geojson");
if (have_geojson_plugin)
{
SECTION("GeoJSON I/O errors")
{
std::string filename = "does_not_exist.geojson";
for (auto create_index : {true, false})
{
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"));
}
for (auto cache_features : {true, false})
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = filename;
params["cache_features"] = cache_features;
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
}
}
}
SECTION("GeoJSON an empty FeatureCollection")
{
for (auto cache_features : {true, false})
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = "./test/data/json/empty_featurecollection.json";
params["cache_features"] = cache_features;
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
}
}
SECTION("GeoJSON empty Geometries handling")
{
auto valid_empty_geometries = {"null", // Point can't be empty
"{ \"type\": \"LineString\", \"coordinates\":[]}",
"{ \"type\": \"Polygon\", \"coordinates\":[]} ",
"{ \"type\": \"Polygon\", \"coordinates\":[[]]} ",
"{ \"type\": \"MultiPoint\", \"coordinates\":[]}",
"{ \"type\": \"MultiLineString\", \"coordinates\":[]}",
"{ \"type\": \"MultiLineString\", \"coordinates\":[[]]}",
"{ \"type\": \"MultiPolygon\", \"coordinates\":[]}",
"{ \"type\": \"MultiPolygon\", \"coordinates\":[[]]}",
"{ \"type\": \"MultiPolygon\", \"coordinates\": [[[]]] }"};
for (auto const& in : valid_empty_geometries)
{
std::string json(in);
mapnik::geometry::geometry<double> geom;
CHECK(mapnik::json::from_geojson(json, geom));
REQUIRE(mapnik::geometry::is_empty(geom));
// round trip
std::string json_out;
CHECK(mapnik::util::to_geojson(json_out, geom));
json.erase(std::remove_if(std::begin(json),
std::end(json),
[l = std::locale{}](auto ch) { return std::isspace(ch, l); }),
std::end(json));
REQUIRE(json == json_out);
}
constexpr std::array<std::string_view, 6> invalid_empty_geometries{
"{ \"type\": \"Point\", \"coordinates\": [] }",
"{ \"type\": \"LineString\", \"coordinates\": [[]] }",
"{ \"type\": \"Polygon\", \"coordinates\": [[[]]] }",
"{ \"type\": \"MultiPoint\", \"coordinates\": [[]] }",
"{ \"type\": \"MultiLineString\", \"coordinates\": [[[]]] }",
"{ \"type\": \"MultiPolygon\", \"coordinates\": [[[[]]]] }",
};
for (auto&& json : invalid_empty_geometries)
{
mapnik::geometry::geometry<double> geom;
CHECK(!mapnik::json::from_geojson(std::string{json}, geom));
}
}
SECTION("GeoJSON num_features_to_query")
{
std::string filename = "./test/data/json/featurecollection-multipleprops.geojson";
for (mapnik::value_integer num_features_to_query : {mapnik::value_integer(-1),
mapnik::value_integer(0),
mapnik::value_integer(1),
mapnik::value_integer(2),
mapnik::value_integer(3),
std::numeric_limits<mapnik::value_integer>::max()})
{
for (auto create_index : {true, false})
{
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"));
}
for (auto cache_features : {true, false})
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = filename;
params["cache_features"] = cache_features;
params["num_features_to_query"] = num_features_to_query;
auto ds = mapnik::datasource_cache::instance().create(params);
CHECK(ds != nullptr);
auto fields = ds->get_descriptor().get_descriptors();
if (!create_index && cache_features)
{
// when there's no index and caching is enabled descriptor is always fully initialised
REQUIRE(fields.size() == 2);
}
else
{
// at least 1 feature should be queried
REQUIRE(std::cmp_equal(fields.size(),
std::min(std::max(mapnik::value_integer(1), num_features_to_query),
mapnik::value_integer(2))));
}
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
}
SECTION("GeoJSON attribute descriptors are alphabetically ordered")
{
for (auto cache_features : {true, false})
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = "./test/data/json/properties.json";
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
CHECK(ds != nullptr);
std::vector<std::string> expected_names = {"a", "b", "c", "d", "e"};
auto fields = ds->get_descriptor().get_descriptors();
std::size_t index = 0;
for (auto const& field : fields)
{
REQUIRE(field.get_name() == expected_names[index++]);
}
}
}
SECTION("GeoJSON invalid Point")
{
for (auto cache_features : {true, false})
{
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = "./test/data/json/point-invalid.json";
params["cache_features"] = cache_features;
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
}
}
SECTION("GeoJSON Point")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/point.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Point);
auto const& pt = mapnik::util::get<mapnik::geometry::point<double>>(geometry);
REQUIRE(pt.x == 100);
REQUIRE(pt.y == 0);
}
}
SECTION("GeoJSON LineString")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/linestring.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::LineString);
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::LineString);
auto const& line = mapnik::util::get<mapnik::geometry::line_string<double>>(geometry);
REQUIRE(line.size() == 2);
REQUIRE(mapnik::geometry::envelope(line) == mapnik::box2d<double>(100, 0, 101, 1));
}
}
SECTION("GeoJSON Polygon")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/polygon.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Polygon);
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Polygon);
auto const& poly = mapnik::util::get<mapnik::geometry::polygon<double>>(geometry);
REQUIRE(poly.size() == 2);
REQUIRE(poly[0].size() == 5);
REQUIRE(poly[1].size() == 5);
REQUIRE(mapnik::geometry::envelope(poly) == mapnik::box2d<double>(100, 0, 101, 1));
}
}
SECTION("GeoJSON MultiPoint")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/multipoint.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPoint);
auto const& multi_pt = mapnik::util::get<mapnik::geometry::multi_point<double>>(geometry);
REQUIRE(multi_pt.size() == 2);
REQUIRE(mapnik::geometry::envelope(multi_pt) == mapnik::box2d<double>(100, 0, 101, 1));
}
}
SECTION("GeoJSON MultiLineString")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/multilinestring.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::LineString);
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiLineString);
auto const& multi_line = mapnik::util::get<mapnik::geometry::multi_line_string<double>>(geometry);
REQUIRE(multi_line.size() == 2);
REQUIRE(multi_line[0].size() == 2);
REQUIRE(multi_line[1].size() == 2);
REQUIRE(mapnik::geometry::envelope(multi_line) == mapnik::box2d<double>(100, 0, 103, 3));
}
}
SECTION("GeoJSON MultiPolygon")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/json/multipolygon.json", cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Polygon);
// test
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPolygon);
auto const& multi_poly = mapnik::util::get<mapnik::geometry::multi_polygon<double>>(geometry);
REQUIRE(multi_poly.size() == 2);
REQUIRE(multi_poly[0].size() == 1);
REQUIRE(multi_poly[1].size() == 2);
REQUIRE(mapnik::geometry::envelope(multi_poly) == mapnik::box2d<double>(100, 0, 103, 3));
}
}
SECTION("GeoJSON GeometryCollection")
{
std::string filename("./test/data/json/geometrycollection.json");
for (auto create_index : {true, false})
{
if (create_index)
{
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
// index will not exist because this is not a featurecollection
CHECK(!mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature(filename, cache_features);
auto feature = result.second;
auto ds = result.first;
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Collection);
// test
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::GeometryCollection);
auto const& collection = mapnik::util::get<mapnik::geometry::geometry_collection<double>>(geometry);
REQUIRE(collection.size() == 2);
REQUIRE(mapnik::geometry::geometry_type(collection[0]) == mapnik::geometry::Point);
REQUIRE(mapnik::geometry::geometry_type(collection[1]) == mapnik::geometry::LineString);
REQUIRE(mapnik::geometry::envelope(collection) == mapnik::box2d<double>(100, 0, 102, 1));
}
}
}
SECTION("GeoJSON Feature")
{
// Create datasource
mapnik::parameters params;
params["type"] = "geojson";
std::string base("./test/data/json/");
std::string file("feature.json");
params["base"] = base;
params["file"] = file;
std::string filename = base + file;
for (auto create_index : {true, false})
{
if (create_index)
{
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
// index will not exist because this is not a featurecollection
CHECK(!mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
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);
auto features2 = ds->features_at_point(ds->envelope().center(), 0);
auto feature = features->next();
auto feature2 = features2->next();
REQUIRE(feature != nullptr);
REQUIRE(feature2 != nullptr);
CHECK(feature->id() == 1);
CHECK(feature2->id() == 1);
mapnik::value val = feature->get("name");
CHECK(val.to_string() == "Dinagat Islands");
mapnik::value val2 = feature2->get("name");
CHECK(val2.to_string() == "Dinagat Islands");
REQUIRE(features->next() == nullptr);
REQUIRE(features2->next() == nullptr);
}
}
}
SECTION("GeoJSON ensure empty and null properties are handled")
{
mapnik::parameters params;
params["type"] = "geojson";
for (auto const& c_str :
{"./test/data/json/feature-null-properties.json", "./test/data/json/feature-empty-properties.json"})
{
std::string filename(c_str);
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(!mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
auto fields = ds->get_descriptor().get_descriptors();
CHECK(fields.size() == 0);
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
}
SECTION("GeoJSON FeatureCollection")
{
std::string filename("./test/data/json/featurecollection.json");
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
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"));
}
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = filename;
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Collection);
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);
auto features2 = ds->features_at_point(ds->envelope().center(), 10);
auto bounding_box = ds->envelope();
mapnik::box2d<double> bbox;
mapnik::value_integer count = 0;
while (true)
{
auto feature = features->next();
auto feature2 = features2->next();
if (!feature || !feature2)
break;
if (!bbox.valid())
bbox = feature->envelope();
else
bbox.expand_to_include(feature->envelope());
++count;
REQUIRE(feature->id() == count);
REQUIRE(feature2->id() == count);
}
REQUIRE(count == 3);
REQUIRE(bounding_box == bbox);
}
if (mapnik::util::exists(filename + ".index"))
{
CHECK(mapnik::util::remove(filename + ".index"));
}
}
}
SECTION("GeoJSON extra properties")
{
// Create datasource
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/feature_collection_extra_properties.json");
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
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"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
REQUIRE(bool(ds));
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);
auto feature = features->next();
REQUIRE(feature != nullptr);
REQUIRE(feature->envelope() == mapnik::box2d<double>(123, 456, 123, 456));
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
SECTION("GeoJSON \"type\":\"FeatureCollection\" in Feature properties (#4140)")
{
// Create datasource
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/feature_collection_issue_4140.json");
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
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"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
REQUIRE(bool(ds));
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);
auto feature1 = features->next();
REQUIRE(feature1 != nullptr);
REQUIRE(feature1->envelope() == mapnik::box2d<double>(-122.0, 48.0, -122.0, 48.0));
auto feature2 = features->next();
REQUIRE(feature2 != nullptr);
REQUIRE(feature2->envelope() == mapnik::box2d<double>(0.0, 51.0, 0.0, 51.0));
REQUIRE(ds->envelope() == mapnik::box2d<double>(-122.0, 48.0, 0.0, 51.0));
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
SECTION("GeoJSON ensure mapnik::datasource_cache::instance().create() throws on malformed input")
{
mapnik::parameters params;
params["type"] = "geojson";
for (auto const& c_str : {"./test/data/json/feature-malformed-1.geojson",
"./test/data/json/feature-malformed-2.geojson",
"./test/data/json/feature-malformed-3.geojson"})
{
std::string filename(c_str);
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(!mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
}
SECTION("GeoJSON ensure mapnik::featureset::next() throws on malformed input")
{
std::string filename{"./test/data/json/featurecollection-malformed.json"};
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
auto fields = ds->get_descriptor().get_descriptors();
mapnik::query query(ds->envelope());
auto features = ds->features(query);
REQUIRE_THROWS(iterate_over_features(features));
}
// cleanup
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
SECTION("GeoJSON ensure input fully consumed and throw exception otherwise")
{
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/points-malformed.geojson");
params["file"] = filename; // mismatched parentheses
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
// unfortunately when using an index or not
// caching features we use the bbox grammar
// which is not strict (and would be a perf hit if it were strict).
// So this is one known hole where invalid data may silently parse okay
// refs https://github.com/mapnik/mapnik/issues/3125
if (!create_index && cache_features == true)
{
std::stringstream msg;
msg << "testcase: create index " << create_index << " cache_features " << cache_features;
params["cache_features"] = cache_features;
INFO(msg.str());
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
}
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
SECTION("GeoJSON ensure original feature ordering is preserved")
{
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/ordered.json");
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
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);
auto feature = features->next();
mapnik::value_integer count = 0;
while (feature != nullptr)
{
// ids are in ascending order, starting from 1
mapnik::value val = feature->get("id");
REQUIRE(val.get<mapnik::value_integer>() == ++count);
feature = features->next();
}
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
SECTION("GeoJSON descriptor returns all field names")
{
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/featurecollection-multipleprops.geojson");
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors();
std::initializer_list<std::string> names = {"one", "two"};
REQUIRE_FIELD_NAMES(fields, names);
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
SECTION("GeoJSON properties are properly expressed")
{
mapnik::transcoder tr("utf8");
mapnik::parameters params;
params["type"] = "geojson";
std::string filename("./test/data/json/escaped.geojson");
params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
for (auto create_index : {true, false})
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = create_disk_index(filename);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache_features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors();
std::initializer_list<std::string> names = {"NOM_FR",
"array",
"boolean",
"description",
"double",
"empty_array",
"empty_object",
"int",
"name",
"object",
"spaces"};
REQUIRE_FIELD_NAMES(fields, names);
auto fs = all_features(ds);
std::initializer_list<attr> attrs = {
attr{"name", tr.transcode("Test")},
attr{"NOM_FR", tr.transcode("Québec")},
attr{"boolean", mapnik::value_bool(true)},
attr{"description", tr.transcode("Test: \u005C")},
attr{"double", mapnik::value_double(1.1)},
attr{"int", mapnik::value_integer(1)},
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\"}}")},
attr{"spaces", tr.transcode("this has spaces")},
attr{"array",
tr.transcode("[\"string\",\"value with spaces\",3,1.1,null,true,"
"\"Québec\",{\"name\":\"object within an array\"},"
"[\"array\",\"within\",\"an\",\"array\"]]")},
attr{"empty_array", tr.transcode("[]")},
attr{"empty_object", tr.transcode("{}")},
};
auto feature = fs->next();
REQUIRE(bool(feature));
REQUIRE_ATTRIBUTES(feature, attrs);
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
mapnik::util::remove(filename + ".index");
}
}
}
}
}
|