Permalink
Browse files

+ high level protobuf implementation

  • Loading branch information...
1 parent f3bb9fd commit 8bdeb36831a3f001f48a5c6876937f32de5ee5c6 @artemp committed Nov 29, 2012
Showing with 261 additions and 11 deletions.
  1. +3 −2 Jamroot
  2. +6 −4 binding.gyp
  3. +4 −3 src/main.cpp
  4. +242 −0 src/opensciencemap_backend_pbf.hpp
  5. +3 −0 src/vector_renderer.cpp
  6. +3 −2 src/vector_server.cpp
View
@@ -11,19 +11,20 @@ lib system : : <name>boost_system <search>$(BOOST_DIR)/lib ;
lib regex : : <name>boost_regex <search>$(BOOST_DIR)/lib ;
lib sqlite3 : : <name>sqlite3 <search>$(MAPNIK_DIR)/lib ;
lib icuuc : : <name>icuuc <search>$(MAPNIK_DEPS_DIR)/lib ;
-lib protobuf-lite : : <name>protobuf-lite <search>$(MAPNIK_DEPS_DIR)/lib ;
+lib protobuf : : <name>protobuf <search>$(MAPNIK_DEPS_DIR)/lib ;
exe vector-tile-server
:
./src/main.cpp
./src/vector_renderer.cpp
./src/tags.cpp
+ ./src/TileData.pb.cc
.//mapnik
.//icuuc
.//program_options
.//regex
.//system
- .//protobuf-lite
+ .//protobuf
:
<include>$(BOOST_DIR)/include
<include>$(MAPNIK_DIR)/include
View
@@ -5,7 +5,8 @@
'sources': [
'src/vector_server.cpp',
'src/tags.cpp',
- 'src/vector_renderer.cpp'
+ 'src/vector_renderer.cpp',
+ 'src/TileData.pb.cc'
],
'dependencies': [
],
@@ -17,7 +18,7 @@
['OS=="mac"', {
'libraries':[
'-lmapnik',
- '-lprotobuf-lite',
+ '-lprotobuf',
'-undefined dynamic_lookup'
],
'xcode_settings': {
@@ -33,7 +34,8 @@
'sources': [
'./src/main.cpp',
'./src/vector_renderer.cpp',
- './src/tags.cpp'
+ './src/tags.cpp',
+ 'src/TileData.pb.cc'
],
'include_dirs': [
'node_modules/mapnik/src',
@@ -43,7 +45,7 @@
['OS=="mac"', {
'libraries':[
'-lmapnik',
- '-lprotobuf-lite',
+ '-lprotobuf',
'-undefined dynamic_lookup'
],
'xcode_settings': {
View
@@ -29,6 +29,7 @@
#include <mapnik/font_engine_freetype.hpp>
#include "vector_renderer.hpp"
#include "opensciencemap_backend.hpp"
+#include "opensciencemap_backend_pbf.hpp"
#include <iostream>
#include <fstream>
@@ -57,7 +58,7 @@ int main(int argc, char** argv)
//m.zoom_to_box(mapnik::box2d<double>(-141233.598318,6756305.73374,-140782.086463,6756728.00478));
//mapnik::box2d<double> bbox(-141233.598318,6755505.73374,-140782.086463,6756728.00478);
- //mapnik::box2d<double> bbox(-141233.598318 + 1000,6755505.73374 + 1000,-140782.086463+1000,6756728.00478+1000);
+//mapnik::box2d<double> bbox(-141233.598318 + 1000,6755505.73374 + 1000,-140782.086463+1000,6756728.00478+1000);
mapnik::box2d<double> bbox(-141867.12449728712,6760702.277767269,-141255.62827100573,6761313.773993552);
m.zoom_to_box(mapnik::box2d<double>(bbox));
@@ -70,8 +71,8 @@ int main(int argc, char** argv)
}
std::string output;
- mapnik::opensciencemap_backend backend(output);
- mapnik::vector_renderer<mapnik::opensciencemap_backend> ren(m, backend);
+ mapnik::opensciencemap_backend_pbf backend(output);
+ mapnik::vector_renderer<mapnik::opensciencemap_backend_pbf> ren(m, backend);
ren.apply();
std::cerr << "TILE TAGS SIZE=" << backend.tags().size() << std::endl;
@@ -0,0 +1,242 @@
+/*****************************************************************************
+ *
+ * This file is part of Mapnik (c++ mapping toolkit)
+ *
+ * Copyright (C) 2012 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_OPENSCIENCEMAP_BACKEND_PBF_HPP
+#define MAPNIK_OPENSCIENCEMAP_BACKEND_PBF_HPP
+
+//boost
+#include <boost/algorithm/string/trim.hpp>
+#include <boost/foreach.hpp>
+
+////
+#include "tags.hpp"
+#include "TileData.pb.h"
+// stl
+#include <vector>
+
+namespace mapnik
+{
+
+using org::oscim::database::oscimap::Data;
+using org::oscim::database::oscimap::Data_Element;
+
+typedef std::pair<uint32_t,uint32_t> coord_type;
+typedef std::vector<coord_type> path_type;
+typedef std::vector<uint32_t> tags_type;
+typedef std::vector<uint32_t> index_type;
+
+struct opensciencemap_backend_pbf
+{
+ struct tile_element
+ {
+ eGeomType type;
+ tags_type tags;
+ path_type path;
+ index_type index;
+ };
+
+private:
+ const double SCALE;
+ const uint32_t TAGS_MAX;
+ std::vector<tags::tag_type> tags_;
+ std::vector<std::string> custom_values_;
+ boost::ptr_vector<tile_element> tile_elements_;
+ std::auto_ptr<tile_element> element_;
+ std::string & output_;
+public:
+ explicit opensciencemap_backend_pbf(std::string & output)
+ : SCALE(16.0),
+ TAGS_MAX(627),
+ output_(output)
+ {}
+
+ boost::ptr_vector<tile_element> const& tile_elements() const
+ {
+ return tile_elements_;
+ }
+ std::vector<tags::tag_type> const& tags() const
+ {
+ return tags_;
+ }
+
+ template <typename T>
+ void start_tile_element( T & feature, eGeomType type)
+ {
+ /////////////////////////////////////////////////////
+ element_.reset(new tile_element);
+ element_->type = type; // geometry type POLY/LINE/POINT
+
+ feature_kv_iterator itr = feature.begin();
+ feature_kv_iterator end = feature.end();
+ for ( ;itr!=end; ++itr)
+ {
+ std::string const& name = boost::get<0>(*itr);
+ mapnik::value const& val = boost::get<1>(*itr);
+ if (!val.is_null())
+ {
+ boost::optional<tags::tag_type> tag = tags::tag_value_from_name(name,val.to_string());
+ if ( tag )
+ {
+ if (*tag < TAGS_MAX)
+ {
+ element_->tags.push_back(*tag);
+ }
+ else
+ {
+ boost::optional<tags::tag_type> tag_key = tags::tag_from_name(name);
+ if (tag_key)
+ {
+ tags_.push_back(*tag_key);
+ custom_values_.push_back(val.to_string());
+ element_->tags.push_back((1023 + tags_.size()));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ void stop_tile_element()
+ {
+ tile_elements_.push_back(element_);
+ }
+
+ template <typename T>
+ void add_path(T & path)
+ {
+ vertex2d vtx(vertex2d::no_init);
+ path.rewind(0);
+ unsigned count = 0;
+ int32_t x=0,y=0;
+
+ if (element_.get())
+ {
+ uint32_t start = element_->path.size();
+ while ((vtx.cmd = path.vertex(&vtx.x, &vtx.y)) != SEG_END)
+ {
+ int32_t cur_x = static_cast<int32_t>(vtx.x * SCALE);
+ int32_t cur_y = static_cast<int32_t>(vtx.y * SCALE);
+ int32_t dx = cur_x - x;
+ int32_t dy = cur_y - y;
+ if (count > 0 && vtx.cmd == SEG_LINETO &&
+ std::fabs(dx) < 1.0 &&
+ std::fabs(dy) < 1.0)
+ {
+ continue;
+ }
+
+ if (vtx.cmd == SEG_MOVETO && element_->path.size() > start)
+ {
+ uint32_t size = element_->path.size() - start;
+ element_->index.push_back(size);
+ start = element_->path.size();
+ }
+
+ if (vtx.cmd == SEG_LINETO || vtx.cmd == SEG_MOVETO)
+ {
+ element_->path.push_back(coord_type(dx,dy));
+ }
+
+ x = cur_x;
+ y = cur_y;
+ ++count;
+ }
+ if (element_->path.size() > start)
+ {
+ uint32_t size = element_->path.size() - start;
+ element_->index.push_back(size);
+ }
+ }
+ }
+
+ uint32_t output_vector_tile()
+ {
+ Data data;
+ data.set_num_tags(tags_.size());
+
+ BOOST_FOREACH(tags::tag_type tag, tags_)
+ {
+ data.add_keys(tag);
+ }
+
+ BOOST_FOREACH(std::string const& val, custom_values_)
+ {
+ data.add_values(val);
+ }
+
+ // output tile elements
+ BOOST_FOREACH(tile_element const& elem, tile_elements_)
+ {
+ if (elem.tags.size() > 0 && !elem.path.empty())
+ {
+ Data_Element * element = 0;
+ if (elem.type == Polygon)
+ element = data.add_polygons();
+ else if (elem.type == LineString)
+ element = data.add_lines();
+ else if (elem.type == Point)
+ element = data.add_points();
+
+ if (element)
+ {
+ // optional uint32 num_indices = 1 [default = 1];
+ if (elem.index.size() > 1)
+ {
+ element->set_num_indices(elem.index.size());
+ }
+
+ // repeated uint32 tags = 11 [packed = true];
+ BOOST_FOREACH( tags_type::value_type tag , elem.tags)
+ {
+ element->add_tags(tag);
+ }
+
+ if (elem.type != Point)
+ {
+ // repeated uint32 indices = 12 [packed = true];
+ BOOST_FOREACH(index_type::value_type index, elem.index)
+ {
+ element->add_indices(index);
+ }
+ }
+ // repeated sint32 coordinates = 13 [packed = true];
+ BOOST_FOREACH(coord_type c, elem.path)
+ {
+ element->add_coordinates(c.first);
+ element->add_coordinates(c.second);
+ }
+ }
+ }
+ }
+ if (data.SerializeToString(&output_))
+ {
+ return data.ByteSize();
+ }
+ else
+ {
+ return 0;
+ }
+ }
+};
+}
+
+#endif //MAPNIK_OPENSCIENCEMAP_BACKEND_PBF_HPP
@@ -24,9 +24,12 @@
#include <mapnik/feature_style_processor_impl.hpp>
#include "vector_renderer_impl.hpp"
#include "opensciencemap_backend.hpp"
+#include "opensciencemap_backend_pbf.hpp"
namespace mapnik
{
template class feature_style_processor<vector_renderer<opensciencemap_backend> >;
template class vector_renderer<opensciencemap_backend>;
+template class feature_style_processor<vector_renderer<opensciencemap_backend_pbf> >;
+template class vector_renderer<opensciencemap_backend_pbf>;
}
@@ -27,6 +27,7 @@
#include <string>
#include "vector_renderer.hpp"
#include "opensciencemap_backend.hpp"
+#include "opensciencemap_backend_pbf.hpp"
using namespace v8;
@@ -88,8 +89,8 @@ void async_render(uv_work_t* req)
render_state * state = static_cast<render_state*>(req->data);
Map * map_ptr = state->m;
std::string output;
- mapnik::opensciencemap_backend backend(output);
- mapnik::vector_renderer<mapnik::opensciencemap_backend> ren(*map_ptr->get(),backend);
+ mapnik::opensciencemap_backend_pbf backend(output);
+ mapnik::vector_renderer<mapnik::opensciencemap_backend_pbf> ren(*map_ptr->get(),backend);
ren.apply();
uint32_t bytes = backend.output_vector_tile();
state->output = output.substr(0,bytes);

0 comments on commit 8bdeb36

Please sign in to comment.