From 4b5bbe9446e65b225da969676eab4a9f9762da96 Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 19 Oct 2015 12:23:43 +0100 Subject: [PATCH] unit test(geojson) - add tests reading all geometry primitives --- test/unit/datasource/geojson.cpp | 230 ++++++++++++++++++++++++++++++- 1 file changed, 226 insertions(+), 4 deletions(-) diff --git a/test/unit/datasource/geojson.cpp b/test/unit/datasource/geojson.cpp index 6363cd185..3218a28f9 100644 --- a/test/unit/datasource/geojson.cpp +++ b/test/unit/datasource/geojson.cpp @@ -26,13 +26,235 @@ #include #include #include +#include #include + +namespace detail { + +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); + 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 feature; +} + +} + TEST_CASE("geojson") { std::string geojson_plugin("./plugins/input/geojson.input"); if (mapnik::util::exists(geojson_plugin)) { + SECTION("GeoJSON Point") + { + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/point.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Point); + auto const& pt = mapnik::util::get >(geometry); + REQUIRE(pt.x == 100); + REQUIRE(pt.y == 0); + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/point.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Point); + auto const& pt = mapnik::util::get >(geometry); + REQUIRE(pt.x == 100); + REQUIRE(pt.y == 0); + } + } + + SECTION("GeoJSON LineString") + { + mapnik::parameters params; + params["type"] = "geojson"; + params["file"] = "./test/data/json/linestring.json"; + + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/linestring.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::LineString); + auto const& line = mapnik::util::get >(geometry); + REQUIRE(line.size() == 2); + REQUIRE(mapnik::geometry::envelope(line) == mapnik::box2d(100,0,101,1)); + + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/linestring.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::LineString); + auto const& line = mapnik::util::get >(geometry); + REQUIRE(line.size() == 2); + REQUIRE(mapnik::geometry::envelope(line) == mapnik::box2d(100,0,101,1)); + } + } + + SECTION("GeoJSON Polygon") + { + mapnik::parameters params; + params["type"] = "geojson"; + params["file"] = "./test/data/json/polygon.json"; + + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/polygon.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Polygon); + auto const& poly = mapnik::util::get >(geometry); + REQUIRE(poly.num_rings() == 2); + REQUIRE(poly.exterior_ring.size() == 5); + REQUIRE(poly.interior_rings.size() == 1); + REQUIRE(poly.interior_rings[0].size() == 5); + REQUIRE(mapnik::geometry::envelope(poly) == mapnik::box2d(100,0,101,1)); + + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/polygon.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::Polygon); + auto const& poly = mapnik::util::get >(geometry); + REQUIRE(poly.num_rings() == 2); + REQUIRE(poly.exterior_ring.size() == 5); + REQUIRE(poly.interior_rings.size() == 1); + REQUIRE(poly.interior_rings[0].size() == 5); + REQUIRE(mapnik::geometry::envelope(poly) == mapnik::box2d(100,0,101,1)); + } + } + + SECTION("GeoJSON MultiPoint") + { + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/multipoint.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPoint); + auto const& multi_pt = mapnik::util::get >(geometry); + REQUIRE(multi_pt.size() == 2); + REQUIRE(mapnik::geometry::envelope(multi_pt) == mapnik::box2d(100,0,101,1)); + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/multipoint.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPoint); + auto const& multi_pt = mapnik::util::get >(geometry); + REQUIRE(multi_pt.size() == 2); + REQUIRE(mapnik::geometry::envelope(multi_pt) == mapnik::box2d(100,0,101,1)); + } + } + + SECTION("GeoJSON MultiLineString") + { + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/multilinestring.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiLineString); + auto const& multi_line = mapnik::util::get >(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(100,0,103,3)); + + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/multilinestring.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiLineString); + auto const& multi_line = mapnik::util::get >(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(100,0,103,3)); + } + } + + SECTION("GeoJSON MultiPolygon") + { + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/multipolygon.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPolygon); + auto const& multi_poly = mapnik::util::get >(geometry); + REQUIRE(multi_poly.size() == 2); + REQUIRE(multi_poly[0].num_rings() == 1); + REQUIRE(multi_poly[1].num_rings() == 2); + REQUIRE(mapnik::geometry::envelope(multi_poly) == mapnik::box2d(100,0,103,3)); + + } + { + // on-fly in-memory r-tree index + read features from disk + auto feature = detail::fetch_first_feature("./test/data/json/multipolygon.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::MultiPolygon); + auto const& multi_poly = mapnik::util::get >(geometry); + REQUIRE(multi_poly.size() == 2); + REQUIRE(multi_poly[0].num_rings() == 1); + REQUIRE(multi_poly[1].num_rings() == 2); + REQUIRE(mapnik::geometry::envelope(multi_poly) == mapnik::box2d(100,0,103,3)); + } + } + + SECTION("GeoJSON GeometryCollection") + { + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/geometrycollection.json", true); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::GeometryCollection); + auto const& collection = mapnik::util::get >(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(100,0,102,1)); + } + { + // cache features in memory + auto feature = detail::fetch_first_feature("./test/data/json/geometrycollection.json", false); + // test + auto const& geometry = feature->get_geometry(); + REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::GeometryCollection); + auto const& collection = mapnik::util::get >(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(100,0,102,1)); + } + } + SECTION("json feature cache-feature=\"true\"") { // Create datasource @@ -44,7 +266,7 @@ TEST_CASE("geojson") { REQUIRE(bool(ds)); auto fields = ds->get_descriptor().get_descriptors(); mapnik::query query(ds->envelope()); - for (auto const &field : fields) + for (auto const& field : fields) { query.add_property_name(field.get_name()); } @@ -64,7 +286,7 @@ TEST_CASE("geojson") { REQUIRE(bool(ds)); auto fields = ds->get_descriptor().get_descriptors(); mapnik::query query(ds->envelope()); - for (auto const &field : fields) + for (auto const& field : fields) { query.add_property_name(field.get_name()); } @@ -85,7 +307,7 @@ TEST_CASE("geojson") { REQUIRE(bool(ds)); auto fields = ds->get_descriptor().get_descriptors(); mapnik::query query(ds->envelope()); - for (auto const &field : fields) + for (auto const& field : fields) { query.add_property_name(field.get_name()); } @@ -107,7 +329,7 @@ TEST_CASE("geojson") { REQUIRE(bool(ds)); auto fields = ds->get_descriptor().get_descriptors(); mapnik::query query(ds->envelope()); - for (auto const &field : fields) + for (auto const& field : fields) { query.add_property_name(field.get_name()); }