Compare commits

...

31 commits

Author SHA1 Message Date
bergwerkgis
7e642481af Merge branch 'master' of https://github.com/mapnik/mapnik into geobuf 2016-08-16 10:50:24 +02:00
Dane Springmeyer
d5e6052fe3 Add geobuf unit test 2015-12-02 12:48:33 -05:00
Dane Springmeyer
121fff3df4 update test data for geobuf fixtures 2015-12-02 11:54:07 -05:00
Dane Springmeyer
b31e027ec4 start updating for new mapnik geometry storage 2015-12-02 11:52:21 -05:00
Dane Springmeyer
3090111a06 Merge branch 'master' of github.com:mapnik/mapnik into geobuf 2015-12-01 17:32:22 -05:00
Dane Springmeyer
8f5e3c02f4 Merge branch 'master' of github.com:mapnik/mapnik into geobuf
Conflicts:
	SConstruct
2015-07-01 22:06:46 -07:00
artemp
601ef6d637 Merge branch 'master' into geobuf 2015-02-04 18:56:04 +01:00
artemp
bf2ea88be4 amend test 2015-02-04 18:26:31 +01:00
artemp
671c0046ee allow "stringified" nested objects and arrays as feature property 2015-02-04 18:23:43 +01:00
artemp
6beee4ad68 Merge branch 'master' into geobuf 2015-02-04 09:48:31 +01:00
artemp
def6c9239c geojson plugin
* use parse_geojson(Iterator start, Iterator end) signiture
* use Iterator = char const*
* remove unneeded I/O in 'initialise_index'
* use memory mapped file (currently #if/def)
2015-02-03 13:33:55 +01:00
artemp
eb0ae77b21 json - instantiate grammars using Iterator = char const* to avoid potentially expensive std::string allocations 2015-02-03 13:32:44 +01:00
artemp
3b2c6d958f Merge branch 'master' into geobuf
Conflicts:
	include/mapnik/json/topojson_grammar_impl.hpp
2015-02-03 09:54:06 +01:00
artemp
defdc6a9f3 fix bbox extractor 2015-02-02 18:44:17 +01:00
artemp
38a184a18b unicode string grammar via boost/libs/spirit/example/qi/json/json/parser/grammar.hpp
update json based grammars
2015-02-02 11:20:23 +01:00
artemp
822581977d Merge branch 'master' into geobuf 2015-01-30 19:17:36 +01:00
artemp
9d69416a27 Merge branch 'master' into geobuf 2015-01-30 11:22:57 +01:00
artemp
fd1a3a5fc7 remove stderr + don't throw on supported messages 2015-01-30 11:19:21 +01:00
artemp
f8decad01e use feature call back to stuff features into container 2015-01-30 11:02:53 +01:00
artemp
c6e681d244 template geobuf class on 'feature' functor + fix signed varint type + apply transform (scaling) 2015-01-30 11:02:00 +01:00
artemp
2600451072 remove copyleft as we're using this header 'as it is' at the moment 2015-01-30 11:00:15 +01:00
artemp
4e3ac8ff0c json - make feature_collection_grammar "on-feature" callback based 2015-01-29 12:21:01 +01:00
artemp
d60e2da44e Merge branch 'master' into geobuf 2015-01-29 09:50:47 +01:00
artemp
18dae530ce Merge branch 'master' into geobuf 2015-01-28 13:09:47 +01:00
artemp
ff56e3f7e1 update params_test - #2651 2015-01-28 12:54:33 +01:00
artemp
a8443df1a1 use perfect forwarding to resolve copy/move semantics of universal references - #2651 2015-01-28 12:54:18 +01:00
artemp
c1f6b12d73 Merge branch 'master' into geobuf
Conflicts:
	SConstruct
2015-01-28 11:21:31 +01:00
artemp
78a6a1c129 + add support for MultiPoint and MultiLineString 2015-01-27 18:18:42 +01:00
artemp
6faf7efa36 return actual features 2015-01-27 16:13:12 +01:00
artemp
863ae6a8b3 geobuf.input - initial barebones implementation 2015-01-27 15:51:11 +01:00
artemp
76df4a2117 move operator<< into separate header <mapnik/util/variant_io.hpp> 2015-01-27 15:50:32 +01:00
9 changed files with 1544 additions and 0 deletions

View file

@ -116,6 +116,7 @@ PLUGINS = { # plugins with external dependencies
'csv': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'csv': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'raster': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'raster': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'geojson': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'geojson': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'geobuf': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'topojson':{'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'} 'topojson':{'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}
} }

View file

@ -0,0 +1,64 @@
#
# This file is part of Mapnik (c++ mapping toolkit)
#
# Copyright (C) 2013 Artem Pavlenko
#
# Mapnik 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
#
#
Import ('env')
Import ('plugin_base')
PLUGIN_NAME = 'geobuf'
plugin_env = plugin_base.Clone()
plugin_sources = Split(
"""
%(PLUGIN_NAME)s_datasource.cpp
%(PLUGIN_NAME)s_featureset.cpp
""" % locals()
)
# Link Library to Dependencies
libraries = []
libraries.append(env['ICU_LIB_NAME'])
libraries.append('boost_system%s' % env['BOOST_APPEND'])
libraries.append('mapnik-json')
if env['PLUGIN_LINKING'] == 'shared':
libraries.append(env['MAPNIK_NAME'])
TARGET = plugin_env.SharedLibrary('../%s' % PLUGIN_NAME,
SHLIBPREFIX='',
SHLIBSUFFIX='.input',
source=plugin_sources,
LIBS=libraries)
# if the plugin links to libmapnik ensure it is built first
Depends(TARGET, env.subst('../../../src/%s' % env['MAPNIK_LIB_NAME']))
if 'uninstall' not in COMMAND_LINE_TARGETS:
env.Install(env['MAPNIK_INPUT_PLUGINS_DEST'], TARGET)
env.Alias('install', env['MAPNIK_INPUT_PLUGINS_DEST'])
plugin_obj = {
'LIBS': libraries,
'SOURCES': plugin_sources,
}
Return('plugin_obj')

View file

@ -0,0 +1,568 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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
*
*****************************************************************************/
#ifndef MAPNIK_UTIL_GEOBUF_HPP
#define MAPNIK_UTIL_GEOBUF_HPP
#include <mapnik/debug.hpp>
#include <mapnik/value.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/util/noncopyable.hpp>
#include <cmath>
#include <cassert>
#include <vector>
#include <boost/optional.hpp>
#include "pbf.hpp"
namespace mapnik { namespace util {
enum geometry_type_e
{
Unknown = -1,
Point = 0,
MultiPoint = 1,
LineString = 2,
MultiLineString = 3,
Polygon = 4,
MultiPolygon = 5,
GeometryCollection = 6
};
namespace detail {
struct value_visitor
{
value_visitor(mapnik::feature_impl & feature, mapnik::transcoder const& tr, std::string const& name)
: feature_(feature),
tr_(tr),
name_(name) {}
void operator() (std::string const& val)
{
feature_.put_new(name_, tr_.transcode(val.c_str()));
}
template <typename T>
void operator() (T val)
{
feature_.put_new(name_, val);
}
mapnik::feature_impl & feature_;
mapnik::transcoder const& tr_;
std::string const& name_;
};
}
template <typename FeatureCallback>
struct geobuf : mapnik::util::noncopyable
{
using value_type = mapnik::util::variant<bool, int, double, std::string>;
unsigned dim = 2;
double precision = std::pow(10,6);
bool transformed = false;
std::size_t lengths = 0;
std::vector<std::string> keys_;
std::vector<value_type> values_;
mbgl::pbf pbf_;
FeatureCallback & callback_;
mapnik::context_ptr ctx_;
const std::unique_ptr<mapnik::transcoder> tr_;
public:
geobuf (unsigned char const* buf, std::size_t size, FeatureCallback & callback)
: pbf_(buf, size),
callback_(callback),
ctx_(std::make_shared<mapnik::context_type>()),
tr_(new mapnik::transcoder("utf8")) {}
void read()
{
while (pbf_.next())
{
switch (pbf_.tag)
{
case 1:
{
keys_.push_back(pbf_.string());
break;
}
case 2:
{
dim = pbf_.varint();
break;
}
case 3:
{
precision = std::pow(10,pbf_.varint());
break;
}
case 4:
{
auto feature_collection = pbf_.message();
read_feature_collection(feature_collection);
break;
}
case 6:
{
auto feature = feature_factory::create(ctx_,1);
auto message = pbf_.message();
feature->set_geometry(std::move(read_geometry(message)));
callback_(feature);
break;
}
default:
MAPNIK_LOG_ERROR(geobuf) << "FIXME tag=" << pbf_.tag;
break;
}
}
}
private:
double transform(std::int64_t input)
{
return (transformed) ? ((double)input) : (input/precision);
}
template <typename T>
void read_value(T & pbf)
{
while (pbf.next())
{
switch (pbf.tag)
{
case 1:
{
values_.emplace_back(pbf.string());
break;
}
case 2:
{
values_.emplace_back(pbf.float64());
break;
}
case 3:
{
values_.emplace_back(static_cast<int>(pbf.varint()));
break;
}
case 4:
{
values_.emplace_back(-static_cast<int>(pbf.varint()));
break;
}
case 5:
{
values_.emplace_back(pbf.boolean());
break;
}
case 6:
{
values_.emplace_back(pbf.string());
break;
}
default:
break;
}
}
}
template <typename T, typename Feature>
void read_props(T & pbf, Feature & feature)
{
uint8_t const* end = pbf.data + pbf.varint();
while (pbf.data < end)
{
auto key_index = pbf.varint();
auto value_index = pbf.varint();
assert(key_index < keys_.size());
assert(value_index < values_.size());
std::string const& name = keys_[key_index];
mapnik::util::apply_visitor(detail::value_visitor(feature, *tr_, name), values_[value_index]);
}
values_.clear();
}
template <typename T>
void read_feature (T & pbf)
{
auto feature = feature_factory::create(ctx_,1);
while (pbf.next())
{
switch (pbf.tag)
{
case 1:
{
auto message = pbf.message();
feature->set_geometry(std::move(read_geometry(message)));
break;
}
case 11:
{
auto feature_id = pbf.string();
break;
}
case 12:
{
feature->set_id(pbf.template svarint<std::int64_t>());
break;
}
case 13:
{
auto message = pbf.message();
read_value(message);
break;
}
case 14:
{
read_props(pbf, *feature);
break;
}
case 15:
{
read_props(pbf, *feature);
break;
}
default:
MAPNIK_LOG_ERROR(geobuf) << "FAIL tag=" << pbf.tag;
break;
}
}
callback_(feature);
}
template <typename T>
void read_feature_collection(T & pbf)
{
while (pbf.next())
{
switch (pbf.tag)
{
case 1:
{
auto message = pbf.message();
read_feature(message);
break;
}
case 13:
{
auto message = pbf.message();
read_value(message);
break;
}
case 15:
{
pbf.skipBytes(pbf.varint());
break;
}
default:
break;
}
}
}
template <typename T>
mapnik::geometry::point<double> read_point(T & pbf)
{
mapnik::geometry::point<double> point;
auto size = pbf.varint();
uint8_t const* end = pbf.data + size;
std::size_t count = 0;
double x, y;
while (pbf.data < end)
{
auto p = pbf.svarint();
auto index = count % dim;
if ( index == 0)
{
x = transform(p);
}
else if (index == 1)
{
y = transform(p);
return mapnik::geometry::point<double>(x, y);
}
++count;
}
std::clog << "should not get here\n";
return point;
}
template <typename T>
mapnik::geometry::geometry<double> read_coords(T & pbf, geometry_type_e type, boost::optional<std::vector<int>> const& coord_lengths)
{
mapnik::geometry::geometry<double> geom;
switch (type)
{
case Point:
{
geom = std::move(read_point(pbf));
break;
}
case Polygon:
{
geom = std::move(read_polygon(pbf, coord_lengths));
break;
}
case LineString:
{
//geom = std::move(read_line_string(pbf));
break;
}
case MultiPoint:
{
//geom = std::move(read_multi_point(pbf));
break;
}
case MultiLineString:
{
//geom = std::move(read_multi_linestring(pbf, coord_lengths));
}
case MultiPolygon:
{
//geom = std::move(read_multi_polygon(pbf, coord_lengths));
break;
}
case GeometryCollection:
{
throw std::runtime_error("GeometryCollection is not supported");
}
default:
{
break;
}
}
return geom;
}
template <typename T, typename Geometry>
void read_linear_ring(T & pbf, int len, std::size_t size, Geometry & geom, bool polygon = false)
{
int i = 0;
double x = 0.0;
double y = 0.0;
uint8_t const* end = pbf.data + size;
while ( (len > 0) ? i < len : pbf.data < end)
{
for (int d = 0; d < dim; ++d)
{
std::int64_t delta = pbf.template svarint<std::int64_t>();
if (d == 0) x += delta;
else if (d == 1) y += delta;
}
if (i == 0)
geom.exterior_ring.add_coord(transform(x), transform(y));
else
geom.exterior_ring.add_coord(transform(x), transform(y));
++i;
}
/*
if (polygon)
{
geom.close_path();
}
*/
}
template <typename T>
mapnik::geometry::polygon<double> read_polygon(T & pbf, boost::optional<std::vector<int>> const& coord_lengths)
{
mapnik::geometry::polygon<double> poly;
auto size = pbf.varint();
if (!coord_lengths)
{
read_linear_ring(pbf, 0, size, poly, true);
}
else
{
for (auto len : *coord_lengths)
{
read_linear_ring(pbf, len, size, poly, true);
}
}
return poly;
}
/*
template <typename T>
void read_multi_point(T & pbf)
{
int x = 0.0;
int y = 0.0;
auto size = pbf.varint();
uint8_t const* end = pbf.data + size;
while (pbf.data < end)
{
auto point = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::Point);
for (int d = 0; d < dim; ++d)
{
if (d == 0) x += pbf.svarint();
else if (d == 1) y += pbf.svarint();
}
point->move_to(x, y);
paths.push_back(point.release());
}
}
template <typename T>
void read_line_string(T & pbf)
{
auto line = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::LineString);
auto size = pbf.varint();
read_linear_ring(pbf, 0, size, *line);
paths.push_back(line.release());
}
template <typename T>
void read_multi_linestring(T & pbf, boost::optional<std::vector<int>> const& lengths)
{
auto size = pbf.varint();
if (!lengths)
{
auto line = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::LineString);
read_linear_ring(pbf, 0, size, *line);
paths.push_back(line.release());
}
else
{
for (auto len : *lengths)
{
auto line = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::LineString);
read_linear_ring(pbf, len, size, *line);
paths.push_back(line.release());
}
}
}
template <typename T>
void read_multi_polygon(T & pbf, boost::optional<std::vector<int>> const& lengths)
{
auto size = pbf.varint();
if (!lengths)
{
auto poly = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::Polygon);
read_linear_ring(pbf, 0, size, *poly, true);
paths.push_back(poly.release());
}
else if ((*lengths).size() > 0)
{
int j = 1;
for (int i = 0; i < (*lengths)[0]; ++i)
{
auto poly = std::make_shared<mapnik::geometry::geometry<double>>(mapnik::geometry::geometry_type::Polygon);
for (int k = 0; k < (*lengths)[j]; ++k)
{
read_linear_ring(pbf, (*lengths)[j + 1 + k], size, *poly, true);
}
paths.push_back(poly.release());
j += (*lengths)[j] + 1;
}
}
}
*/
template <typename T>
std::vector<int> read_lengths(T & pbf)
{
std::vector<int> coord_lengths;
auto size = pbf.varint();
uint8_t const* end = pbf.data + size;
while (pbf.data < end)
{
coord_lengths.push_back(pbf.varint());
}
return coord_lengths;
}
template <typename T>
mapnik::geometry::geometry<double> read_geometry(T & pbf)
{
mapnik::geometry::geometry<double> geom;
geometry_type_e type = Unknown;
boost::optional<std::vector<int>> coord_lengths;
while (pbf.next())
{
switch (pbf.tag)
{
case 1:
{
type = static_cast<geometry_type_e>(pbf.varint());
break;
}
case 2:
{
coord_lengths = std::move(read_lengths(pbf));
break;
}
case 3:
{
geom = std::move(read_coords(pbf, type, coord_lengths));
break;
}
case 4:
{
MAPNIK_LOG_ERROR(geobuf) << "fixme read geometry=?";
break;
}
case 11:
{
auto geom_id = pbf.string();
MAPNIK_LOG_ERROR(geobuf) << "geometry id's are not supported geom_id=" << geom_id ;
break;
}
case 12:
{
auto geom_id = pbf.template varint<std::int64_t>();
MAPNIK_LOG_ERROR(geobuf) << "geometry id's are not supported geom_id=" << geom_id ;
break;
}
case 13:
{
auto value = pbf.message();
read_value(value);
break;
}
case 14:
case 15:
{
pbf.skipBytes(pbf.varint());
break;
}
default:
break;
}
}
return geom;
}
};
}}
#endif // MAPNIK_UTIL_GEOBUF_HPP

View file

@ -0,0 +1,251 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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 "geobuf_datasource.hpp"
#include "geobuf_featureset.hpp"
#include "geobuf.hpp"
#include <fstream>
#include <algorithm>
#include <functional>
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <boost/algorithm/string.hpp>
#pragma GCC diagnostic pop
// mapnik
#include <mapnik/unicode.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/debug.hpp>
#include <mapnik/proj_transform.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/util/variant.hpp>
#include <mapnik/util/file_io.hpp>
#include <mapnik/make_unique.hpp>
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(geobuf_datasource)
struct attr_value_converter
{
mapnik::eAttributeType operator() (mapnik::value_integer) const
{
return mapnik::Integer;
}
mapnik::eAttributeType operator() (double) const
{
return mapnik::Double;
}
mapnik::eAttributeType operator() (float) const
{
return mapnik::Double;
}
mapnik::eAttributeType operator() (bool) const
{
return mapnik::Boolean;
}
mapnik::eAttributeType operator() (std::string const& ) const
{
return mapnik::String;
}
mapnik::eAttributeType operator() (mapnik::value_unicode_string const&) const
{
return mapnik::String;
}
mapnik::eAttributeType operator() (mapnik::value_null const& ) const
{
return mapnik::String;
}
};
geobuf_datasource::geobuf_datasource(parameters const& params)
: datasource(params),
type_(datasource::Vector),
desc_(geobuf_datasource::name(),
*params.get<std::string>("encoding","utf-8")),
filename_(),
extent_(),
features_(),
tree_(nullptr)
{
boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw mapnik::datasource_exception("Geobuf Plugin: missing <file> parameter");
boost::optional<std::string> base = params.get<std::string>("base");
if (base)
filename_ = *base + "/" + *file;
else
filename_ = *file;
mapnik::util::file in(filename_);
if (!in.open())
{
throw mapnik::datasource_exception("Geobuf Plugin: could not open: '" + filename_ + "'");
}
std::vector<std::uint8_t> geobuf;
geobuf.resize(in.size());
std::fread(geobuf.data(), in.size(), 1, in.get());
parse_geobuf(geobuf.data(), geobuf.size());
}
namespace {
template <typename T>
struct push_feature
{
using features_container = T;
push_feature(features_container & features)
: features_(features) {}
void operator() (mapnik::feature_ptr const& feature)
{
features_.push_back(feature);
}
features_container & features_;
};
}
void geobuf_datasource::parse_geobuf(std::uint8_t const* data, std::size_t size)
{
using push_feature_callback = push_feature<std::vector<mapnik::feature_ptr>>;
push_feature_callback callback(features_);
mapnik::util::geobuf<push_feature_callback> buf(data, size, callback);
buf.read();
using values_container = std::vector< std::pair<box_type, std::pair<std::size_t, std::size_t>>>;
values_container values;
values.reserve(features_.size());
std::size_t geometry_index = 0;
for (mapnik::feature_ptr const& f : features_)
{
mapnik::box2d<double> box = f->envelope();
if (box.valid())
{
if (geometry_index == 0)
{
extent_ = box;
for ( auto const& kv : *f)
{
desc_.add_descriptor(mapnik::attribute_descriptor(std::get<0>(kv),
mapnik::util::apply_visitor(attr_value_converter(),
std::get<1>(kv))));
}
}
else
{
extent_.expand_to_include(box);
}
}
values.emplace_back(box, std::make_pair(geometry_index,0));
++geometry_index;
}
// packing algorithm
tree_ = std::make_unique<spatial_index_type>(values);
}
geobuf_datasource::~geobuf_datasource() {}
const char * geobuf_datasource::name()
{
return "geobuf";
}
boost::optional<mapnik::datasource_geometry_t> geobuf_datasource::get_geometry_type() const
{
boost::optional<mapnik::datasource_geometry_t> result;
int multi_type = 0;
unsigned num_features = features_.size();
for (unsigned i = 0; i < num_features && i < 5; ++i)
{
result = mapnik::util::to_ds_type(features_[i]->get_geometry());
if (result)
{
int type = static_cast<int>(*result);
if (multi_type > 0 && multi_type != type)
{
result.reset(mapnik::datasource_geometry_t::Collection);
return result;
}
multi_type = type;
}
}
return result;
}
mapnik::datasource::datasource_t geobuf_datasource::type() const
{
return type_;
}
mapnik::box2d<double> geobuf_datasource::envelope() const
{
return extent_;
}
mapnik::layer_descriptor geobuf_datasource::get_descriptor() const
{
return desc_;
}
mapnik::featureset_ptr geobuf_datasource::features(mapnik::query const& q) const
{
// if the query box intersects our world extent then query for features
mapnik::box2d<double> const& box = q.get_bbox();
if (extent_.intersects(box))
{
geobuf_featureset::array_type index_array;
if (tree_)
{
tree_->query(boost::geometry::index::intersects(box),std::back_inserter(index_array));
return std::make_shared<geobuf_featureset>(features_, std::move(index_array));
}
}
return mapnik::featureset_ptr();
}
mapnik::featureset_ptr geobuf_datasource::features_at_point(mapnik::coord2d const& pt, double tol) const
{
mapnik::box2d<double> query_bbox(pt, pt);
query_bbox.pad(tol);
mapnik::query q(query_bbox);
std::vector<mapnik::attribute_descriptor> const& desc = desc_.get_descriptors();
for (auto const& item : desc)
{
q.add_property_name(item.get_name());
}
return features(q);
}

View file

@ -0,0 +1,103 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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
*
*****************************************************************************/
#ifndef GEOBUF_DATASOURCE_HPP
#define GEOBUF_DATASOURCE_HPP
// mapnik
#include <mapnik/datasource.hpp>
#include <mapnik/params.hpp>
#include <mapnik/query.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/coord.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/geometry_adapters.hpp>
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <boost/optional.hpp>
#include <boost/geometry.hpp>
#include <boost/version.hpp>
#include <boost/geometry/index/rtree.hpp>
#pragma GCC diagnostic pop
// stl
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <deque>
template <std::size_t Max, std::size_t Min>
struct geojson_linear : boost::geometry::index::linear<Max,Min> {};
namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree {
template <std::size_t Max, std::size_t Min>
struct options_type<geojson_linear<Max,Min> >
{
using type = options<geojson_linear<Max, Min>,
insert_default_tag,
choose_by_content_diff_tag,
split_default_tag,
linear_tag,
#if BOOST_VERSION >= 105700
node_variant_static_tag>;
#else
node_s_mem_static_tag>;
#endif
};
}}}}}
class geobuf_datasource : public mapnik::datasource
{
public:
using box_type = mapnik::box2d<double>;
using item_type = std::pair<box_type, std::pair<std::size_t, std::size_t>>;
using spatial_index_type = boost::geometry::index::rtree<item_type,geojson_linear<16,4> >;
// constructor
geobuf_datasource(mapnik::parameters const& params);
virtual ~geobuf_datasource ();
mapnik::datasource::datasource_t type() const;
static const char * name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt, double tol = 0) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
boost::optional<mapnik::datasource_geometry_t> get_geometry_type() const;
void parse_geobuf(std::uint8_t const* buffer, std::size_t size);
private:
mapnik::datasource::datasource_t type_;
mapnik::layer_descriptor desc_;
std::string filename_;
mapnik::box2d<double> extent_;
std::vector<mapnik::feature_ptr> features_;
std::unique_ptr<spatial_index_type> tree_;
};
#endif // GEOBUF_DATASOURCE_HPP

View file

@ -0,0 +1,56 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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
*
*****************************************************************************/
// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
// stl
#include <string>
#include <vector>
#include <deque>
#include "geobuf_featureset.hpp"
geobuf_featureset::geobuf_featureset(std::vector<mapnik::feature_ptr> const& features,array_type && index_array)
: features_(features),
index_array_(std::move(index_array)),
index_itr_(index_array_.begin()),
index_end_(index_array_.end()),
ctx_(std::make_shared<mapnik::context_type>()) {}
geobuf_featureset::~geobuf_featureset() {}
mapnik::feature_ptr geobuf_featureset::next()
{
if (index_itr_ != index_end_)
{
geobuf_datasource::item_type const& item = *index_itr_++;
std::size_t index = item.second.first;
//std::size_t size = item.second.second;
//std::cerr << file_offset << " (" << size << ") " << item.first << std::endl;
if ( index < features_.size())
{
return features_.at(index);
}
}
return mapnik::feature_ptr();
}

View file

@ -0,0 +1,50 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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
*
*****************************************************************************/
#ifndef GEOBUF_FEATURESET_HPP
#define GEOBUF_FEATURESET_HPP
#include <mapnik/feature.hpp>
#include "geobuf_datasource.hpp"
#include <vector>
#include <deque>
#include <cstdio>
class geobuf_featureset : public mapnik::Featureset
{
public:
typedef std::deque<geobuf_datasource::item_type> array_type;
geobuf_featureset(std::vector<mapnik::feature_ptr> const& features,
array_type && index_array);
virtual ~geobuf_featureset();
mapnik::feature_ptr next();
private:
std::vector<mapnik::feature_ptr> const& features_;
const array_type index_array_;
array_type::const_iterator index_itr_;
array_type::const_iterator index_end_;
mapnik::context_ptr ctx_;
};
#endif // GEOBUF_FEATURESET_HPP

View file

@ -0,0 +1,184 @@
#ifndef MBGL_UTIL_PBF
#define MBGL_UTIL_PBF
/*
* Some parts are from upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2008-2011 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*/
#include <string>
#include <cstring>
namespace mbgl {
struct pbf {
struct exception : std::exception { const char *what() const noexcept { return "pbf exception"; } };
struct unterminated_varint_exception : exception { const char *what() const noexcept { return "pbf unterminated varint exception"; } };
struct varint_too_long_exception : exception { const char *what() const noexcept { return "pbf varint too long exception"; } };
struct unknown_field_type_exception : exception { const char *what() const noexcept { return "pbf unknown field type exception"; } };
struct end_of_buffer_exception : exception { const char *what() const noexcept { return "pbf end of buffer exception"; } };
inline pbf(const unsigned char *data, size_t length);
inline pbf();
inline operator bool() const;
inline bool next();
inline bool next(uint32_t tag);
template <typename T = uint32_t> inline T varint();
template <typename T = uint32_t> inline T svarint();
template <typename T = uint32_t, int bytes = 4> inline T fixed();
inline float float32();
inline double float64();
inline std::string string();
inline bool boolean();
inline pbf message();
inline void skip();
inline void skipValue(uint32_t val);
inline void skipBytes(uint32_t bytes);
const uint8_t *data = nullptr;
const uint8_t *end = nullptr;
uint32_t value = 0;
uint32_t tag = 0;
};
pbf::pbf(const unsigned char *data_, size_t length)
: data(data_),
end(data_ + length),
value(0),
tag(0) {
}
pbf::pbf()
: data(nullptr),
end(nullptr),
value(0),
tag(0) {
}
pbf::operator bool() const {
return data < end;
}
bool pbf::next() {
if (data < end) {
value = static_cast<uint32_t>(varint());
tag = value >> 3;
return true;
}
return false;
}
bool pbf::next(uint32_t requested_tag) {
while (next()) {
if (tag == requested_tag) {
return true;
} else {
skip();
}
}
return false;
}
template <typename T>
T pbf::varint() {
uint8_t byte = 0x80;
T result = 0;
int bitpos;
for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
if (data >= end) {
throw unterminated_varint_exception();
}
result |= ((T)(byte = *data) & 0x7F) << bitpos;
data++;
}
if (bitpos == 70 && (byte & 0x80)) {
throw varint_too_long_exception();
}
return result;
}
template <typename T>
T pbf::svarint() {
T n = varint<T>();
return (n >> 1) ^ -(T)(n & 1);
}
template <typename T, int bytes>
T pbf::fixed() {
skipBytes(bytes);
T result;
memcpy(&result, data - bytes, bytes);
return result;
}
float pbf::float32() {
return fixed<float, 4>();
}
double pbf::float64() {
return fixed<double, 8>();
}
std::string pbf::string() {
uint32_t bytes = static_cast<uint32_t>(varint());
const char *string_data = reinterpret_cast<const char*>(data);
skipBytes(bytes);
return std::string(string_data, bytes);
}
bool pbf::boolean() {
skipBytes(1);
return *(bool *)(data - 1);
}
pbf pbf::message() {
uint32_t bytes = static_cast<uint32_t>(varint());
const uint8_t *pos = data;
skipBytes(bytes);
return pbf(pos, bytes);
}
void pbf::skip() {
skipValue(value);
}
void pbf::skipValue(uint32_t val) {
switch (val & 0x7) {
case 0: // varint
varint();
break;
case 1: // 64 bit
skipBytes(8);
break;
case 2: // string/message
skipBytes(static_cast<uint32_t>(varint()));
break;
case 5: // 32 bit
skipBytes(4);
break;
default:
throw unknown_field_type_exception();
}
}
void pbf::skipBytes(uint32_t bytes) {
if (data + bytes > end) {
throw end_of_buffer_exception();
}
data += bytes;
}
} // end namespace mbgl
#endif

View file

@ -0,0 +1,267 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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 <mapnik/datasource.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/geometry_type.hpp>
#include <mapnik/util/fs.hpp>
#include <cstdlib>
#include <boost/optional/optional_io.hpp>
namespace {
std::pair<mapnik::datasource_ptr,mapnik::feature_ptr> fetch_first_feature(std::string const& filename, bool cache_features)
{
mapnik::parameters params;
params["type"] = "geobuf";
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);
REQUIRE(features != nullptr);
auto feature = features->next();
REQUIRE(feature != nullptr);
return std::make_pair(ds,feature);
}
int create_disk_index(std::string const& filename, bool silent = true)
{
std::string cmd;
if (std::getenv("DYLD_LIBRARY_PATH") != nullptr)
{
cmd += std::string("export DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " && ";
}
cmd += "mapnik-index " + filename;
if (silent)
{
#ifndef _WINDOWS
cmd += " 2>/dev/null";
#else
cmd += " 2> nul";
#endif
}
return std::system(cmd.c_str());
}
}
TEST_CASE("geobuf") {
std::string geobuf_plugin("./plugins/input/geobuf.input");
if (mapnik::util::exists(geobuf_plugin))
{
SECTION("Geobuf I/O errors")
{
std::string filename = "does_not_exist.geobuf";
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"] = "geobuf";
params["file"] = filename;
params["cache_features"] = cache_features;
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params));
}
}
}
SECTION("geobuf point featurecollection")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/point-fc.geobuf", 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.0);
}
}
SECTION("geobuf point geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/point.geobuf", 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.0);
}
}
SECTION("geobuf lineString geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/linestring.geobuf", 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("geobuf polygon geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/polygon.geobuf", 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.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<double>(100,0,101,1));
}
}
SECTION("geobuf multipoint geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/multipoint.geobuf", 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("geobuf multilinestring geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/multilinestring.geobuf", 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("geobuf multipolygon geometry")
{
for (auto cache_features : {true, false})
{
auto result = fetch_first_feature("./test/data/geobuf/multipolygon.geobuf", 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].num_rings() == 1);
REQUIRE(multi_poly[1].num_rings() == 2);
REQUIRE(mapnik::geometry::envelope(multi_poly) == mapnik::box2d<double>(100,0,103,3));
}
}
SECTION("geobuf geometrycollection geometry")
{
std::string filename("./test/data/geobuf/geometrycollection.geobuf");
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));
}
}
}
}
}