Skip to content

Commit

Permalink
# This is a combination of 6 commits.
Browse files Browse the repository at this point in the history
# The first commit's message is:
build glfw on travis, close #41

# This is the 2nd commit message:

install gflw deps

# This is the 3rd commit message:

include <memory>

# This is the 4th commit message:

consistently install c++ libs

# This is the 5th commit message:

Constructor from geojson-cpp variant (#50)

* Add constructor from geojson parse variant

* Simplify 2nd constructor

* fix formatting

# This is the 6th commit message:

split geometry collections into separate features (#51)
  • Loading branch information
mourner authored and Dane Springmeyer committed Jul 20, 2016
1 parent 0e5df63 commit 2a9a78f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 74 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ matrix:
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-5' ]
packages: [ 'libstdc++-5-dev', 'libstdc++6', 'g++-5', 'libxi-dev','libglu1-mesa-dev','x11proto-randr-dev','x11proto-xext-dev','libxrandr-dev','x11proto-xf86vidmode-dev','libxxf86vm-dev','libxcursor-dev','libxinerama-dev' ]
- os: linux
env: CXX=clang++-3.8 BUILDTYPE=Debug ASAN_OPTIONS=detect_leaks=1 CXXFLAGS="-fsanitize=address" LDFLAGS="-fsanitize=address"
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ libstdc++-5-dev', 'libstdc++6', 'g++-5' ]
packages: [ 'libstdc++-5-dev', 'libstdc++6', 'g++-5', 'libxi-dev','libglu1-mesa-dev','x11proto-randr-dev','x11proto-xext-dev','libxrandr-dev','x11proto-xf86vidmode-dev','libxxf86vm-dev','libxcursor-dev','libxinerama-dev' ]
- os: linux
env: CXX=clang++-3.8 BUILDTYPE=Release
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', ]
packages: [ libstdc++-5-dev', 'libstdc++6', 'g++-5' ]
packages: [ 'libstdc++-5-dev', 'libstdc++6', 'g++-5', 'libxi-dev','libglu1-mesa-dev','x11proto-randr-dev','x11proto-xext-dev','libxrandr-dev','x11proto-xf86vidmode-dev','libxxf86vm-dev','libxcursor-dev','libxinerama-dev']
- os: osx
osx_image: xcode7.3
env: BUILDTYPE=Release
Expand All @@ -39,7 +39,8 @@ install:
script:
- make test -j2 V=1
- make bench V=1
- make build/bench V=1
- make build/debug V=1

notifications:
slack:
Expand Down
2 changes: 1 addition & 1 deletion debug/debug.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <GLFW/glfw3.h>

#include <memory>
#include "../bench/util.hpp"
#include <mapbox/geojson.hpp>
#include <mapbox/geojsonvt.hpp>
Expand Down
22 changes: 22 additions & 0 deletions include/mapbox/geojsonvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
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 geometry_collection = mapbox::geometry::geometry_collection<double>;
using geojson = mapbox::util::variant<geometry, feature, feature_collection>;

struct ToFeatureCollection {
feature_collection operator()(const feature_collection& value) const {
return value;
}
feature_collection operator()(const feature& value) const {
return { value };
}
feature_collection operator()(const geometry& value) const {
return { { value } };
}
};

struct Options {
// max zoom to preserve detail on
uint8_t maxZoom = 18;
Expand Down Expand Up @@ -58,6 +76,10 @@ class GeoJSONVT {
splitTile(features, 0, 0, 0);
}

GeoJSONVT(const geojson& geojson_, const Options& options_ = Options())
: GeoJSONVT(geojson::visit(geojson_, ToFeatureCollection{}), options_) {
}

std::map<uint8_t, uint32_t> stats;
uint32_t total = 0;

Expand Down
51 changes: 26 additions & 25 deletions include/mapbox/geojsonvt/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class InternalTile {

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

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

void addFeature(geometry::point<int16_t>&& point, const property_map& props) {
tile.features.push_back({ std::move(point), props });
void addFeature(const vt_point& point, const property_map& props) {
tile.features.push_back({ transform(point), props });
}

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

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

void addFeature(const vt_geometry_collection& collection, const property_map& props) {
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);
});
}
}

template <class T>
void addFeature(T&& multi, const property_map& props) {
switch (multi.size()) {
void addFeature(const T& multi, const property_map& props) {
const auto new_multi = transform(multi);

switch (new_multi.size()) {
case 0:
break;
case 1:
tile.features.push_back({ std::move(multi[0]), props });
tile.features.push_back({ std::move(new_multi[0]), props });
break;
default:
tile.features.push_back({ std::move(multi), props });
tile.features.push_back({ std::move(new_multi), props });
break;
}
}
Expand Down Expand Up @@ -185,18 +198,6 @@ class InternalTile {
}
return result;
}

mapbox::geometry::geometry_collection<int16_t>
transform(const vt_geometry_collection& geometries) {
mapbox::geometry::geometry_collection<int16_t> result;
for (const auto& geometry : geometries) {
vt_geometry::visit(geometry, [&](const auto& g) {
// `this->` is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636
result.push_back(this->transform(g));
});
}
return result;
}
};

} // namespace detail
Expand Down
57 changes: 13 additions & 44 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,39 +204,25 @@ TEST(GetTile, Projection) {
options.extent = 8192;
options.tolerance = 0;

GeoJSONVT index { geojson.get<mapbox::geojson::feature_collection>(), options };
GeoJSONVT index{ geojson, options };

struct TileCoordinate {
uint8_t z;
uint32_t x;
uint32_t y;
};

std::vector<TileCoordinate> tileCoordinates {
{ 0, 0, 0 },
{ 1, 0, 0 },
{ 2, 0, 1 },
{ 3, 1, 3 },
{ 4, 2, 6 },
{ 5, 5, 12 },
{ 6, 10, 24 },
{ 7, 20, 49 },
{ 8, 40, 98 },
{ 9, 81, 197 },
{ 10, 163, 395 },
{ 11, 327, 791 },
{ 12, 655, 1583 },
{ 13, 1310, 3166 },
{ 14, 2620, 6332 },
{ 15, 5241, 12664 },
{ 16, 10482, 25329 },
{ 17, 20964, 50660 },
{ 18, 41929, 101320 },
{ 19, 83859, 202640 },
{ 20, 167719, 405281 },
std::vector<TileCoordinate> tileCoordinates{
{ 0, 0, 0 }, { 1, 0, 0 }, { 2, 0, 1 },
{ 3, 1, 3 }, { 4, 2, 6 }, { 5, 5, 12 },
{ 6, 10, 24 }, { 7, 20, 49 }, { 8, 40, 98 },
{ 9, 81, 197 }, { 10, 163, 395 }, { 11, 327, 791 },
{ 12, 655, 1583 }, { 13, 1310, 3166 }, { 14, 2620, 6332 },
{ 15, 5241, 12664 }, { 16, 10482, 25329 }, { 17, 20964, 50660 },
{ 18, 41929, 101320 }, { 19, 83859, 202640 }, { 20, 167719, 405281 },
};

for (const auto tileCoordinate: tileCoordinates) {
for (const auto tileCoordinate : tileCoordinates) {
auto tile = index.getTile(tileCoordinate.z, tileCoordinate.x, tileCoordinate.y);
ASSERT_EQ(tile.num_points, tile.num_simplified);
ASSERT_EQ(tile.features.size(), 1);
Expand All @@ -247,12 +233,12 @@ TEST(GetTile, Projection) {

const double totalFeatures = (1u << tileCoordinate.z) * 8192.0;

const auto toWebMercatorLon = [&](const mapbox::geometry::point<int16_t> &point) -> double {
const auto toWebMercatorLon = [&](const mapbox::geometry::point<int16_t>& point) -> double {
const double x0 = 8192.0 * tileCoordinate.x;
return (x0 + point.x) * 360.0 / totalFeatures - 180.0;
};

const auto toWebMercatorLat = [&](const mapbox::geometry::point<int16_t> &point) -> double {
const auto toWebMercatorLat = [&](const mapbox::geometry::point<int16_t>& point) -> double {
const double y0 = 8192.0 * tileCoordinate.y;
const double y2 = 180.0 - (y0 + point.y) * 360.0 / totalFeatures;
return 360.0 / M_PI * std::atan(std::exp(y2 * M_PI / 180.0)) - 90.0;
Expand All @@ -276,24 +262,7 @@ genTiles(const std::string& data, uint8_t maxZoom = 0, uint32_t maxPoints = 1000
options.indexMaxPoints = maxPoints;

const auto geojson = mapbox::geojson::parse(data);
mapbox::geojson::feature_collection features;
if (geojson.is<mapbox::geojson::feature_collection>()) {
features = geojson.get<mapbox::geojson::feature_collection>();
} else if (geojson.is<mapbox::geojson::feature>()) {
features.emplace_back(geojson.get<mapbox::geojson::feature>());
} else if (geojson.is<mapbox::geojson::geometry>()) {
const auto& geom = geojson.get<mapbox::geojson::geometry>();
if (geom.is<mapbox::geojson::geometry_collection>()) {
for (const auto& item : geom.get<mapbox::geojson::geometry_collection>()) {
mapbox::geometry::feature<double> feat{ item };
features.emplace_back(feat);
}
} else {
mapbox::geometry::feature<double> feat{ geom };
features.emplace_back(feat);
}
}
GeoJSONVT index{ features, options };
GeoJSONVT index{ geojson, options };

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

Expand Down

0 comments on commit 2a9a78f

Please sign in to comment.