Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 335 lines (296 sloc) 10.981 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/*****************************************************************************
*
* 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_HPP
#define MAPNIK_OPENSCIENCEMAP_BACKEND_HPP

// protobuf io
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
//boost
#include <boost/algorithm/string/trim.hpp>
#include <boost/foreach.hpp>
#include "tags.hpp"
#include <vector>

namespace mapnik
{

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 tile_element
{
    eGeomType type;
    tags_type tags;
    path_type path;
    index_type index;
};

struct element_writer
{
    google::protobuf::io::StringOutputStream raw_output_;
    google::protobuf::io::CodedOutputStream coded_output_;

    explicit element_writer(std::string & buffer)
        : raw_output_(&buffer),
          coded_output_(&raw_output_)
    {}

    void write_varint32(uint32_t val)
    {
        coded_output_.WriteVarint32(val);
    }

    void write_tag(uint32_t val)
    {
        coded_output_.WriteTag(val);
    }

    void write_string(std::string const& str)
    {
        coded_output_.WriteString(str);
    }

    void write_raw(char const * data, uint32_t size)
    {
        coded_output_.WriteRaw(const_cast<char*>(data),size);
    }

    uint32_t bytes_written() const
    {
        return coded_output_.ByteCount();
    }
};

const static double SCALE = 16.0;
const static uint32_t TAGS_MAX = 627;

struct opensciencemap_backend
{
private:
    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_;
    google::protobuf::io::StringOutputStream raw_output_;
    google::protobuf::io::CodedOutputStream coded_output_;
public:
    explicit opensciencemap_backend(std::string & output)
        : raw_output_(&output),
          coded_output_(&raw_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_index_from_kv(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)
                {
                    // zigzag encoding
                    uint32_t xe = (dx << 1) ^ (dx >> 31);
                    uint32_t ye = (dy << 1) ^ (dy >> 31);
                    element_->path.push_back(coord_type(xe,ye));
                }

                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()
    {
        // tile tags
        // num tags
        coded_output_.WriteTag(0x08);
        coded_output_.WriteVarint32(tags_.size());
        coded_output_.WriteTag(0x12);

        std::string buffer;
        element_writer tag_writer(buffer);

        BOOST_FOREACH(tags::tag_type tag, tags_)
        {
            tag_writer.write_varint32(tag);
        }
        std::size_t bytes = tag_writer.bytes_written();

        coded_output_.WriteVarint32(bytes);
        coded_output_.WriteRaw(buffer.data(), bytes);

        BOOST_FOREACH(std::string const& val, custom_values_)
        {
            coded_output_.WriteTag(0x1a);
            coded_output_.WriteVarint32(val.size());
            coded_output_.WriteRaw(val.data(),val.size());
        }

        // output tile elements
        BOOST_FOREACH(tile_element const& elem, tile_elements_)
        {
            if (elem.tags.size() > 0 && !elem.path.empty())
            {
                if (elem.type == Polygon)
                    coded_output_.WriteTag(0x62);
                else if (elem.type == LineString)
                    coded_output_.WriteTag(0x5a);
                else if (elem.type == Point)
                    coded_output_.WriteTag(0x6a);

                // ELEMENT
                std::size_t element_bytes=0;
                std::string element_buffer;

                {
                    // ELEMENT TAGS
                    std::string tags_buffer;
                    element_writer writer(tags_buffer);
                    writer.write_tag(0x5a);
                    std::string element_tags;
                    uint32_t tags_bytes = output_short_array(element_tags,elem.tags);
                    writer.write_varint32(tags_bytes);
                    writer.write_raw(element_tags.data(), tags_bytes);
                    element_bytes += writer.bytes_written();
                    element_buffer+=tags_buffer.substr(0,writer.bytes_written());
                }

                if (elem.index.size() > 1)
                {
                    //TAG_ELEM_NUM_INDICES
                    std::string indices_buffer;
                    element_writer writer(indices_buffer);
                    writer.write_tag(0x08);
                    writer.write_varint32(elem.index.size());
                    element_bytes += writer.bytes_written();
                    element_buffer+=indices_buffer.substr(0,writer.bytes_written());
                }

                if (elem.type != Point)
                {
                    // TAG_ELEM_INDEX
                    std::string index_buffer;
                    element_writer writer(index_buffer);
                    writer.write_tag(0x62);
                    std::string array_buffer;
                    uint32_t array_bytes = output_short_array(array_buffer,elem.index);
                    writer.write_varint32(array_bytes);
                    writer.write_raw(array_buffer.data(),array_bytes);
                    element_bytes += writer.bytes_written();
                    element_buffer+=index_buffer.substr(0, writer.bytes_written());
                }

                // COORDS
                {
                    std::string coord_buffer;
                    element_writer writer(coord_buffer);
                    writer.write_tag(0x6a);
                    std::string element_paths;
                    uint32_t paths_bytes = output_element_paths(element_paths,elem.path);
                    writer.write_varint32(paths_bytes);
                    writer.write_raw(element_paths.data(),paths_bytes);
                    element_bytes += writer.bytes_written();
                    element_buffer+=coord_buffer.substr(0,writer.bytes_written());
                }
                // write element
                coded_output_.WriteVarint32(element_bytes);
                coded_output_.WriteRaw(element_buffer.data(), element_bytes);
            }
        }
        return coded_output_.ByteCount();
    }

    template <typename Array>
    uint32_t output_short_array(std::string & buffer, Array const& ar)
    {
        element_writer writer(buffer);
        BOOST_FOREACH(typename Array::value_type elem, ar)
        {
            writer.write_varint32(elem);
        }
        return writer.bytes_written();
    }

    template <typename Path>
    uint32_t output_element_paths(std::string & buffer, Path const& path)
    {
        element_writer writer(buffer);
        BOOST_FOREACH(coord_type c, path)
        {
            writer.write_varint32(c.first);
            writer.write_varint32(c.second);
        }
        return writer.bytes_written();
    }

};
}

#endif //MAPNIK_OPENSCIENCEMAP_BACKEND_HPP
Something went wrong with that request. Please try again.