Skip to content

Commit

Permalink
Update to geometry v1.0.0 & geojson v0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Nov 8, 2018
1 parent 37ebe04 commit 32020bd
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .mason
Submodule .mason updated 138 files
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ DEBUG_FLAGS ?= -g -O0 -DDEBUG
MASON ?= .mason/mason

VARIANT = variant 1.1.5
GEOMETRY = geometry 0.9.3
GEOJSON = geojson 0.4.2
GEOMETRY = geometry 1.0.0
GEOJSON = geojson 0.4.3
GLFW = glfw 3.1.2
GTEST = gtest 1.8.0
RAPIDJSON = rapidjson 1.1.0
Expand Down
5 changes: 3 additions & 2 deletions debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ void drawLine(const T points) {
}

struct DrawFeature {
void operator()(const mapbox::geometry::point<int16_t>&) {
}
void operator()(const mapbox::geometry::empty&) {}

void operator()(const mapbox::geometry::point<int16_t>&) {}

void operator()(const mapbox::geometry::line_string<int16_t>& points) {
drawLine(points);
Expand Down
8 changes: 5 additions & 3 deletions include/mapbox/geojsonvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <mapbox/geojsonvt/types.hpp>
#include <mapbox/geojsonvt/wrap.hpp>

#include <mapbox/feature.hpp>

#include <chrono>
#include <cmath>
#include <map>
Expand All @@ -14,8 +16,8 @@ namespace mapbox {
namespace geojsonvt {

using geometry = mapbox::geometry::geometry<double>;
using feature = mapbox::geometry::feature<double>;
using feature_collection = mapbox::geometry::feature_collection<double>;
using feature = mapbox::feature::feature<double>;
using feature_collection = mapbox::feature::feature_collection<double>;
using geometry_collection = mapbox::geometry::geometry_collection<double>;
using geojson = mapbox::util::variant<geometry, feature, feature_collection>;

Expand Down Expand Up @@ -93,7 +95,7 @@ class GeoJSONVT {
public:
const Options options;

GeoJSONVT(const mapbox::geometry::feature_collection<double>& features_,
GeoJSONVT(const mapbox::feature::feature_collection<double>& features_,
const Options& options_ = Options())
: options(options_) {

Expand Down
4 changes: 4 additions & 0 deletions include/mapbox/geojsonvt/clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class clipper {
const double k2;
const bool lineMetrics;

vt_geometry operator()(const vt_empty& empty) const {
return empty;
}

vt_geometry operator()(const vt_point& point) const {
return point;
}
Expand Down
9 changes: 7 additions & 2 deletions include/mapbox/geojsonvt/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <mapbox/geojsonvt/simplify.hpp>
#include <mapbox/geojsonvt/types.hpp>
#include <mapbox/geometry.hpp>
#include <mapbox/feature.hpp>

#include <algorithm>
#include <cmath>
Expand All @@ -15,6 +16,10 @@ struct project {
const double tolerance;
using result_type = vt_geometry;

vt_empty operator()(const geometry::empty& empty) {
return empty;
}

vt_point operator()(const geometry::point<double>& p) {
const double sine = std::sin(p.y * M_PI / 180);
const double x = p.x / 360 + 0.5;
Expand Down Expand Up @@ -93,13 +98,13 @@ struct project {
}
};

inline vt_features convert(const geometry::feature_collection<double>& features,
inline vt_features convert(const feature::feature_collection<double>& features,
const double tolerance, bool generateId) {
vt_features projected;
projected.reserve(features.size());
uint64_t genId = 0;
for (const auto& feature : features) {
optional<identifier> featureId = feature.id;
identifier featureId = feature.id;
if (generateId) {
featureId = { uint64_t {genId++} };
}
Expand Down
20 changes: 14 additions & 6 deletions include/mapbox/geojsonvt/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace mapbox {
namespace geojsonvt {

struct Tile {
mapbox::geometry::feature_collection<int16_t> features;
mapbox::feature::feature_collection<int16_t> features;
uint32_t num_points = 0;
uint32_t num_simplified = 0;
};
Expand Down Expand Up @@ -68,14 +68,18 @@ class InternalTile {
}

private:
void addFeature(const vt_empty& empty, const property_map& props, const identifier& id) {
tile.features.push_back({ transform(empty), props, id });
}

void
addFeature(const vt_point& point, const property_map& props, const optional<identifier>& id) {
addFeature(const vt_point& point, const property_map& props, const identifier& id) {
tile.features.push_back({ transform(point), props, id });
}

void addFeature(const vt_line_string& line,
const property_map& props,
const optional<identifier>& id) {
const identifier& id) {
const auto new_line = transform(line);
if (!new_line.empty()) {
if (lineMetrics) {
Expand All @@ -90,15 +94,15 @@ class InternalTile {

void addFeature(const vt_polygon& polygon,
const property_map& props,
const optional<identifier>& id) {
const identifier& id) {
const auto new_polygon = transform(polygon);
if (!new_polygon.empty())
tile.features.push_back({ std::move(new_polygon), props, id });
}

void addFeature(const vt_geometry_collection& collection,
const property_map& props,
const optional<identifier>& id) {
const identifier& id) {
for (const auto& geom : collection) {
vt_geometry::visit(geom, [&](const auto& g) {
// `this->` is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636
Expand All @@ -108,7 +112,7 @@ class InternalTile {
}

template <class T>
void addFeature(const T& multi, const property_map& props, const optional<identifier>& id) {
void addFeature(const T& multi, const property_map& props, const identifier& id) {
const auto new_multi = transform(multi);

switch (new_multi.size()) {
Expand All @@ -123,6 +127,10 @@ class InternalTile {
}
}

mapbox::geometry::empty transform(const vt_empty& empty) {
return empty;
}

mapbox::geometry::point<int16_t> transform(const vt_point& p) {
++tile.num_simplified;
return { static_cast<int16_t>(::round((p.x * z2 - x) * extent)),
Expand Down
22 changes: 14 additions & 8 deletions include/mapbox/geojsonvt/types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <mapbox/geometry.hpp>
#include <mapbox/feature.hpp>
#include <mapbox/variant.hpp>

#include <algorithm>
Expand All @@ -12,6 +13,8 @@ namespace mapbox {
namespace geojsonvt {
namespace detail {

using vt_empty = mapbox::geometry::empty;

struct vt_point : mapbox::geometry::point<double> {
double z = 0.0; // simplification tolerance

Expand Down Expand Up @@ -97,7 +100,8 @@ using vt_multi_polygon = std::vector<vt_polygon>;

struct vt_geometry_collection;

using vt_geometry = mapbox::util::variant<vt_point,
using vt_geometry = mapbox::util::variant<vt_empty,
vt_point,
vt_line_string,
vt_polygon,
vt_multi_point,
Expand All @@ -107,15 +111,17 @@ using vt_geometry = mapbox::util::variant<vt_point,

struct vt_geometry_collection : std::vector<vt_geometry> {};

using property_map = mapbox::geometry::property_map;
using identifier = mapbox::geometry::identifier;

template <class T>
using optional = std::experimental::optional<T>;
using null_value = mapbox::feature::null_value_t;
using property_map = mapbox::feature::property_map;
using identifier = mapbox::feature::identifier;

template <class T>
struct vt_geometry_type;

template <>
struct vt_geometry_type<geometry::empty> {
using type = vt_empty;
};
template <>
struct vt_geometry_type<geometry::point<double>> {
using type = vt_point;
Expand Down Expand Up @@ -152,12 +158,12 @@ struct vt_geometry_type<geometry::geometry_collection<double>> {
struct vt_feature {
vt_geometry geometry;
property_map properties;
optional<identifier> id;
identifier id;

mapbox::geometry::box<double> bbox = { { 2, 1 }, { -1, 0 } };
uint32_t num_points = 0;

vt_feature(const vt_geometry& geom, const property_map& props, const optional<identifier>& id_)
vt_feature(const vt_geometry& geom, const property_map& props, const identifier& id_)
: geometry(geom), properties(props), id(id_) {

mapbox::geometry::for_each_point(geom, [&](const vt_point& p) {
Expand Down
4 changes: 2 additions & 2 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ TEST(GetTile, Projection) {
}
}

std::map<std::string, mapbox::geometry::feature_collection<int16_t>>
std::map<std::string, mapbox::feature::feature_collection<int16_t>>
genTiles(const std::string& data, uint8_t maxZoom = 0, uint32_t maxPoints = 10000, bool lineMetrics = false) {
Options options;
options.maxZoom = 14;
Expand All @@ -348,7 +348,7 @@ genTiles(const std::string& data, uint8_t maxZoom = 0, uint32_t maxPoints = 1000
const auto geojson = mapbox::geojson::parse(data);
GeoJSONVT index{ geojson, options };

std::map<std::string, mapbox::geometry::feature_collection<int16_t>> output;
std::map<std::string, mapbox::feature::feature_collection<int16_t>> output;

for (const auto& pair : index.getInternalTiles()) {
auto& tile = pair.second;
Expand Down
37 changes: 19 additions & 18 deletions test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mapbox/geojsonvt/tile.hpp>
#include <mapbox/geojsonvt/types.hpp>
#include <mapbox/geometry.hpp>
#include <mapbox/geometry_io.hpp>
#include <mapbox/variant_io.hpp>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
Expand Down Expand Up @@ -50,12 +51,12 @@ inline void compareValues(const T& a, const T& b) {
EXPECT_TRUE(a == b);
}

void compareValues(const mapbox::geometry::value& a,
const mapbox::geometry::value& b) {
void compareValues(const mapbox::feature::value& a,
const mapbox::feature::value& b) {
if (a == b) return;

double a_as_double = mapbox::geometry::value::visit(a, ToDouble{});
double b_as_double = mapbox::geometry::value::visit(b, ToDouble{});
double a_as_double = mapbox::feature::value::visit(a, ToDouble{});
double b_as_double = mapbox::feature::value::visit(b, ToDouble{});
EXPECT_DOUBLE_EQ(a_as_double, b_as_double);
}

Expand All @@ -74,8 +75,8 @@ void compareMaps(const MapType& a, const MapType& b) {

} // namespace

bool operator==(const mapbox::geometry::feature<short>& a,
const mapbox::geometry::feature<short>& b) {
bool operator==(const mapbox::feature::feature<short>& a,
const mapbox::feature::feature<short>& b) {
// EXPECT_EQ(a.geometry, b.geometry);
EXPECT_EQ(typeid(a.geometry), typeid(b.geometry));
compareMaps(a.properties, b.properties);
Expand All @@ -84,8 +85,8 @@ bool operator==(const mapbox::geometry::feature<short>& a,
return true;
}

bool operator==(const mapbox::geometry::feature_collection<short>& a,
const mapbox::geometry::feature_collection<short>& b) {
bool operator==(const mapbox::feature::feature_collection<short>& a,
const mapbox::feature::feature_collection<short>& b) {
EXPECT_EQ(a.size(), b.size());
if (a.size() == b.size()) {
unsigned i = 0;
Expand All @@ -96,10 +97,10 @@ bool operator==(const mapbox::geometry::feature_collection<short>& a,
return true;
}

bool operator==(const std::map<std::string, mapbox::geometry::feature_collection<short>>& a,
const std::map<std::string, mapbox::geometry::feature_collection<short>>& b) {
bool operator==(const std::map<std::string, mapbox::feature::feature_collection<short>>& a,
const std::map<std::string, mapbox::feature::feature_collection<short>>& b) {
EXPECT_EQ(a.size(), b.size());
typedef std::map<std::string, mapbox::geometry::feature_collection<short>>::const_iterator
typedef std::map<std::string, mapbox::feature::feature_collection<short>>::const_iterator
it_type;
for (it_type it = a.begin(); it != a.end(); it++) {
if (b.find(it->first) != b.end()) {
Expand All @@ -125,22 +126,22 @@ bool operator==(const mapbox::geojsonvt::Tile& a, const mapbox::geojsonvt::Tile&
return true;
}

mapbox::geometry::feature_collection<int16_t>
mapbox::feature::feature_collection<int16_t>
parseJSONTile(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& tile) {
mapbox::geometry::feature_collection<int16_t> features;
mapbox::feature::feature_collection<int16_t> features;
EXPECT_TRUE(tile.IsArray());
for (rapidjson::SizeType k = 0; k < tile.Size(); ++k) {
const auto& feature = tile[k];

mapbox::geometry::feature<short> feat{ mapbox::geometry::point<short>() };
mapbox::feature::feature<short> feat{ mapbox::geometry::point<short>() };

if (feature.HasMember("tags") && feature["tags"].IsObject()) {
const auto& tags = feature["tags"];
for (auto jt = tags.MemberBegin(); jt != tags.MemberEnd(); jt++) {
const std::string tagKey{ jt->name.GetString(), jt->name.GetStringLength() };
switch (jt->value.GetType()) {
case rapidjson::kNullType:
feat.properties.emplace(tagKey, mapbox::geometry::null_value);
feat.properties.emplace(tagKey, mapbox::feature::null_value);
break;
case rapidjson::kFalseType:
feat.properties.emplace(tagKey, false);
Expand Down Expand Up @@ -247,7 +248,7 @@ parseJSONTile(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAll
return features;
}

mapbox::geometry::feature_collection<int16_t> parseJSONTile(const std::string& data) {
mapbox::feature::feature_collection<int16_t> parseJSONTile(const std::string& data) {
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
d.Parse<0>(data.c_str());

Expand All @@ -260,9 +261,9 @@ mapbox::geometry::feature_collection<int16_t> parseJSONTile(const std::string& d
return parseJSONTile(d);
}

std::map<std::string, mapbox::geometry::feature_collection<int16_t>>
std::map<std::string, mapbox::feature::feature_collection<int16_t>>
parseJSONTiles(const std::string& data) {
std::map<std::string, mapbox::geometry::feature_collection<int16_t>> result;
std::map<std::string, mapbox::feature::feature_collection<int16_t>> result;
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
d.Parse<0>(data.c_str());

Expand Down
16 changes: 8 additions & 8 deletions test/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace mapbox {
namespace geojsonvt {

std::string loadFile(const std::string& filename);
mapbox::geometry::feature_collection<int16_t> parseJSONTile(const std::string& data);
std::map<std::string, mapbox::geometry::feature_collection<int16_t>>
mapbox::feature::feature_collection<int16_t> parseJSONTile(const std::string& data);
std::map<std::string, mapbox::feature::feature_collection<int16_t>>
parseJSONTiles(const std::string& data);
bool operator==(const mapbox::geometry::feature<short>& a,
const mapbox::geometry::feature<short>& b);
bool operator==(const mapbox::geometry::feature_collection<short>& a,
const mapbox::geometry::feature_collection<short>& b);
bool operator==(const std::map<std::string, mapbox::geometry::feature_collection<short>>& a,
const std::map<std::string, mapbox::geometry::feature_collection<short>>& b);
bool operator==(const mapbox::feature::feature<short>& a,
const mapbox::feature::feature<short>& b);
bool operator==(const mapbox::feature::feature_collection<short>& a,
const mapbox::feature::feature_collection<short>& b);
bool operator==(const std::map<std::string, mapbox::feature::feature_collection<short>>& a,
const std::map<std::string, mapbox::feature::feature_collection<short>>& b);
bool operator==(const mapbox::geojsonvt::Tile& a, const mapbox::geojsonvt::Tile& b);

namespace detail {
Expand Down

0 comments on commit 32020bd

Please sign in to comment.