| Home | Libraries | People | FAQ | More

##### svg_mapper

Helper class to create SVG maps.

###### Synopsis

    template<typename Point, bool SameScale>
    class svg_mapper
          : noncopyable
    {
      // ...
    };

###### Template parameter(s)

Parameter | Default | Description
--- | --- | ---
typename Point |  | Point type, for input geometries.
bool SameScale | true | Boolean flag indicating if horizontal and vertical scale should be the same. The default value is true

###### Constructor(s)

Function | Description | Parameters
--- | --- | ---

    svg_mapper(std::ostream & stream, int width, int height,
                std::string const & width_height = "width=\"100%\" height=\"100%\"")

Constructor, initializing the SVG map. Opens and initializes the SVG. Should be called explicitly.

std::ostream &: stream: Output stream, should be a stream already open

int: width: Width of the SVG map (in SVG pixels)

int: height: Height of the SVG map (in SVG pixels)

std::string const &: width_height: Optional information to increase width and/or height

    ~svg_mapper()

Destructor, called automatically. Closes the SVG by streaming </svg>

###### Member Function(s)

Function | Description | Parameters | Returns
--- | --- | --- | ---

    template<typename Geometry>
    void add(Geometry const & geometry)

Adds a geometry to the transformation matrix. After doing this, the specified geometry can be mapped fully into the SVG map.

Geometry const &: geometry: A model of the specified concept

    template<typename Geometry>
    void map(Geometry const & geometry, std::string const & style, int size = -1)

Maps a geometry into the SVG map using the specified style.

Geometry const &: geometry: A model of the specified concept

std::string const &: style: String containing verbatim SVG style information

int: size: Optional size (used for SVG points) in SVG pixels. For linestrings, specify linewidth in the SVG style information

    template<typename TextPoint>
    void text(TextPoint const & point, std::string const & s, std::string const & style,
              int offset_x = 0, int offset_y = 0, int lineheight = 10)

Adds a text to the SVG map.

TextPoint const &: point: Location of the text (in map units)

std::string const &: s: The text itself

std::string const &: style: String containing verbatim SVG style information, of the text

int: offset_x: Offset in SVG pixels, defaults to 0

int: offset_y: Offset in SVG pixels, defaults to 0

int: lineheight: Line height in SVG pixels, in case the text contains

###### Header

Either

`#include <boost/geometry.hpp>`

Or

`#include <boost/geometry/io/svg/svg_mapper.hpp>`

###### Example

Shows the usage of svg_mapper

    #include <iostream>
    #include <fstream>

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>

    int main()
    {
        // Specify the basic type
        typedef boost::geometry::model::d2::point_xy<double> point_type;

        // Declare some geometries and set their values
        point_type a;
        boost::geometry::assign_values(a, 3, 6);

        boost::geometry::model::polygon<point_type> b;
        boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", b);

        boost::geometry::model::linestring<point_type> c;
        c.push_back(point_type(3, 4));
        c.push_back(point_type(4, 5));

        // Declare a stream and an SVG mapper
        std::ofstream svg("my_map.svg");
        boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);

        // Add geometries such that all these geometries fit on the map
        mapper.add(a);
        mapper.add(b);
        mapper.add(c);

        // Draw the geometries on the SVG map, using a specific SVG style
        mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2", 5);
        mapper.map(b, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:2");
        mapper.map(c, "opacity:0.4;fill:none;stroke:rgb(212,0,0);stroke-width:5");

        // Destructor of map will be called - adding </svg>
        // Destructor of stream will be called, closing the file

        return 0;
    }

Output:

###### See also

- The SVG manipulator