Skip to content

Commit

Permalink
Add feature id support
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 7, 2017
1 parent 957069f commit 7d50bfc
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
3 changes: 2 additions & 1 deletion include/mapbox/geojsonvt/clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ inline vt_features clip(const vt_features& features,
for (const auto& feature : features) {
const auto& geom = feature.geometry;
const auto& props = feature.properties;
const auto& id = feature.id;

const double min = get<I>(feature.bbox.min);
const double max = get<I>(feature.bbox.max);
Expand All @@ -238,7 +239,7 @@ inline vt_features clip(const vt_features& features,
continue;

} else {
clipped.emplace_back(vt_geometry::visit(geom, clipper<I>{ k1, k2 }), props);
clipped.emplace_back(vt_geometry::visit(geom, clipper<I>{ k1, k2 }), props, id);
}
}

Expand Down
3 changes: 2 additions & 1 deletion include/mapbox/geojsonvt/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ inline vt_features convert(const geometry::feature_collection<double>& features,
for (const auto& feature : features) {
projected.emplace_back(
geometry::geometry<double>::visit(feature.geometry, project{ tolerance }),
feature.properties);
feature.properties,
feature.id);
}
return projected;
}
Expand Down
25 changes: 13 additions & 12 deletions include/mapbox/geojsonvt/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ class InternalTile {
for (const auto& feature : source) {
const auto& geom = feature.geometry;
const auto& props = feature.properties;
const auto& id = feature.id;

tile.num_points += feature.num_points;

vt_geometry::visit(geom, [&](const auto& g) {
// `this->` is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636
this->addFeature(g, props);
this->addFeature(g, props, id);
});

bbox.min.x = std::min(feature.bbox.min.x, bbox.min.x);
Expand Down Expand Up @@ -94,43 +95,43 @@ class InternalTile {
return true;
}

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

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

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

void addFeature(const vt_geometry_collection& collection, const property_map& props) {
void addFeature(const vt_geometry_collection& collection, const property_map& props, const std::experimental::optional<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
this->addFeature(g, props);
this->addFeature(g, props, id);
});
}
}

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

switch (new_multi.size()) {
case 0:
break;
case 1:
tile.features.push_back({ std::move(new_multi[0]), props });
tile.features.push_back({ std::move(new_multi[0]), props, id });
break;
default:
tile.features.push_back({ std::move(new_multi), props });
tile.features.push_back({ std::move(new_multi), props, id });
break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions include/mapbox/geojsonvt/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ using vt_geometry = mapbox::util::variant<vt_point,

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

using property_map = std::unordered_map<std::string, mapbox::geometry::value>;
using property_map = mapbox::geometry::property_map;
using identifier = mapbox::geometry::identifier;

template <class T>
struct vt_geometry_type;
Expand Down Expand Up @@ -127,11 +128,13 @@ struct vt_geometry_type<geometry::geometry_collection<double>> {
struct vt_feature {
vt_geometry geometry;
property_map properties;
std::experimental::optional<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)
: geometry(geom), properties(props) {
vt_feature(const vt_geometry& geom, const property_map& props, const std::experimental::optional<identifier>& id_)
: geometry(geom), properties(props), id(id_) {

mapbox::geometry::for_each_point(geom, [&](const vt_point& p) {
bbox.min.x = std::min(p.x, bbox.min.x);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/feature-tiles.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"z0-0-0":[{"geometry":[[[3186,2048],[3197,2048],[3197,2037],[3186,2037],[3186,2048]]],"type":3,"tags":{"prop0":"value0"}}]}
{"z0-0-0":[{"geometry":[[[3186,2048],[3197,2048],[3197,2037],[3186,2037],[3186,2048]]],"type":3,"tags":{"prop0":"value0"},"id":"id"}]}
1 change: 1 addition & 0 deletions test/fixtures/feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[100.0, 1.0], [100.0, 0.0] ]
]
},
"id": "id",
"properties": {
"prop0": "value0"
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/us-states-square.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"geometry":[[[-64,4160],[-64,-64],[4160,-64],[4160,4160],[-64,4160]]],"type":3,"tags":{"name":"Pennsylvania","density":284.3}}]
[{"geometry":[[[-64,4160],[-64,-64],[4160,-64],[4160,4160],[-64,4160]]],"type":3,"tags":{"name":"Pennsylvania","density":284.3},"id":"42"}]
2 changes: 1 addition & 1 deletion test/fixtures/us-states-tiles.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/us-states-z7-37-48.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"geometry":[[[3415,-64],[3323,-15],[3300,-64],[3415,-64]]],"type":3,"tags":{"name":"Connecticut","density":739.1}},{"geometry":[[[762,2248],[627,2476],[475,2600],[507,2900],[722,3179],[778,3642],[1089,4124],[1233,4144],[1234,4160],[321,4160],[220,2403],[467,2196],[762,2248]]],"type":3,"tags":{"name":"Delaware","density":464.3}},{"geometry":[[[-64,2403],[220,2403],[321,4160],[-64,4160],[-64,2829],[-51,2714],[-64,2717],[-64,2403]]],"type":3,"tags":{"name":"Maryland","density":596.3}},{"geometry":[[[2914,-64],[2964,-36],[2788,523],[2549,649],[2421,943],[2820,1090],[2852,1310],[2677,2331],[2222,3086],[1927,3303],[1664,3775],[1528,3467],[1105,3313],[587,2900],[547,2580],[627,2476],[762,2248],[1153,2092],[1177,1946],[1624,1634],[1696,1467],[1281,1080],[1265,838],[1081,775],[1065,555],[1289,218],[1169,17],[1243,-64],[2914,-64]]],"type":3,"tags":{"name":"New Jersey","density":1189}},{"geometry":[[[3300,-64],[3323,-15],[3945,144],[4072,28],[4160,28],[4160,594],[3929,681],[3458,765],[3147,744],[2916,838],[2788,523],[2964,-36],[2914,-64],[3300,-64]]],"type":3,"tags":{"name":"New York","density":412.3}},{"geometry":[[[1243,-64],[1169,17],[1289,218],[1065,555],[1081,775],[1265,838],[1281,1080],[1696,1467],[1624,1634],[1177,1946],[1153,2092],[762,2248],[467,2196],[220,2403],[-64,2403],[-64,-64],[1243,-64]]],"type":3,"tags":{"name":"Pennsylvania","density":284.3}}]
[{"geometry":[[[3415,-64],[3323,-15],[3300,-64],[3415,-64]]],"type":3,"tags":{"name":"Connecticut","density":739.1},"id":"09"},{"geometry":[[[762,2248],[627,2476],[475,2600],[507,2900],[722,3179],[778,3642],[1089,4124],[1233,4144],[1234,4160],[321,4160],[220,2403],[467,2196],[762,2248]]],"type":3,"tags":{"name":"Delaware","density":464.3},"id":"10"},{"geometry":[[[-64,2403],[220,2403],[321,4160],[-64,4160],[-64,2829],[-51,2714],[-64,2717],[-64,2403]]],"type":3,"tags":{"name":"Maryland","density":596.3},"id":"24"},{"geometry":[[[2914,-64],[2964,-36],[2788,523],[2549,649],[2421,943],[2820,1090],[2852,1310],[2677,2331],[2222,3086],[1927,3303],[1664,3775],[1528,3467],[1105,3313],[587,2900],[547,2580],[627,2476],[762,2248],[1153,2092],[1177,1946],[1624,1634],[1696,1467],[1281,1080],[1265,838],[1081,775],[1065,555],[1289,218],[1169,17],[1243,-64],[2914,-64]]],"type":3,"tags":{"name":"New Jersey","density":1189},"id":"34"},{"geometry":[[[3300,-64],[3323,-15],[3945,144],[4072,28],[4160,28],[4160,594],[3929,681],[3458,765],[3147,744],[2916,838],[2788,523],[2964,-36],[2914,-64],[3300,-64]]],"type":3,"tags":{"name":"New York","density":412.3},"id":"36"},{"geometry":[[[1243,-64],[1169,17],[1289,218],[1065,555],[1081,775],[1265,838],[1281,1080],[1696,1467],[1624,1634],[1177,1946],[1153,2092],[762,2248],[467,2196],[220,2403],[-64,2403],[-64,-64],[1243,-64]]],"type":3,"tags":{"name":"Pennsylvania","density":284.3},"id":"42"}]
6 changes: 6 additions & 0 deletions test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ bool operator==(const mapbox::geometry::feature<short>& a,
// EXPECT_EQ(a.geometry, b.geometry);
EXPECT_EQ(typeid(a.geometry), typeid(b.geometry));
EXPECT_EQ(a.properties, b.properties);
EXPECT_EQ(a.id, b.id);
return true;
}

Expand Down Expand Up @@ -126,6 +127,11 @@ parseJSONTile(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAll
}
}

if (feature.HasMember("id")) {
EXPECT_TRUE(feature["id"].IsString());
feat.id = { std::string{ feature["id"].GetString(), feature["id"].GetStringLength() } };
}

if (feature.HasMember("type") && feature.HasMember("geometry")) {
const auto& geometry = feature["geometry"];
const auto& type = feature["type"];
Expand Down

0 comments on commit 7d50bfc

Please sign in to comment.