| Home | Libraries | People | FAQ | More

##### svg_mapper

Helper class to create SVG maps.

###### Synopsis

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

###### 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
typename SvgCoordinateType | double | Coordinate type of SVG points. SVG is capable to use floating point coordinates. Therefore the default value is double

###### Constructor(s)

Function | Description | Parameters
--- | --- | ---
    svg_mapper(std::ostream & stream, SvgCoordinateType width, SvgCoordinateType height,
                calculation_type scale, 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 SvgCoordinateType: width: Width of the SVG map (in SVG pixels) SvgCoordinateType: height: Height of the SVG map (in SVG pixels) calculation_type: scale: Scale factor of the automatically determined bounding box. If the factor is less than 1.0, there will be a margin around the geometries. A factor of 0.95 is often a convenient margin. If the factor is more than 1.0, the SVG map is zoomed and not all geometries will be visible. std::string const &: width_height: Optional information to increase width and/or height
    svg_mapper(std::ostream & stream, SvgCoordinateType width, SvgCoordinateType 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 SvgCoordinateType: width: Width of the SVG map (in SVG pixels) SvgCoordinateType: height: Height of the SVG map (in SVG pixels) std::string const &: width_height: Optional information to increase width and/or height
    svg_mapper(svg_mapper const & )
|  | svg_mapper const &: :
    ~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, double size = -1.0)
| 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 double: 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,
              double offset_x = 0.0, double offset_y = 0.0, double lineheight = 10.0)
| 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 double: offset_x: Offset in SVG pixels, defaults to 0 double: offset_y: Offset in SVG pixels, defaults to 0 double: 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. Make sure to first call only the method `add` and then call only the methods `map` or `text`. Because `add` calculates the bounding box internally and that should be finished before calling mapping functions. This might require two loops.

    #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
        using point_type = boost::geometry::model::d2::point_xy<double>;

        // 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 exactly 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:

###### Example with arrows and groups

Shows the usage of svg_mapper with arrows, groups and a larger margin.

    #include <iostream>
    #include <fstream>

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

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

        // Declare linestrings and set their values
        boost::geometry::model::linestring<point_type> a, b, c;
        a.push_back({1, 0});
        a.push_back({3, 3});

        b.push_back({5, 0});
        b.push_back({3, 2});

        c.push_back({4, 5});
        c.push_back({3, 4});

        // Declare a stream and an SVG mapper
        // The factor of 0.95 zooms out to give a convenient margin
        std::ofstream svg("my_map.svg");
        boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400, 0.95);

        // Write a marker definition.
        svg << "<defs>";
        svg << "<marker id=\"arrowhead\" markerWidth=\"5\" markerHeight=\"3.5\""
               " refX=\"0\" refY=\"1.75\" orient=\"auto\">"
               " <polygon points=\"0 0, 5 1.75, 0 3.5\"/></marker>";
        svg << "</defs>";

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

        // Group the first two geometries
        svg << "<g>";
        mapper.map(a, "opacity:0.5;stroke-width:1;stroke:gray;marker-end:url(#arrowhead)");
        mapper.map(b, "opacity:0.5;stroke-width:3;stroke:gray;marker-end:url(#arrowhead)");
        svg << "</g>";

        mapper.map(c, "opacity:0.5;stroke-width:5;stroke:red;marker-end:url(#arrowhead)");

        return 0;
    }

Output:

###### See also

- The SVG manipulator