svg_mapper.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2015-2021.
  4. // Modifications copyright (c) 2015-2020, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_IO_SVG_MAPPER_HPP
  13. #define BOOST_GEOMETRY_IO_SVG_MAPPER_HPP
  14. #include <cstdio>
  15. #include <type_traits>
  16. #include <vector>
  17. #include <boost/algorithm/string/classification.hpp>
  18. #include <boost/algorithm/string/split.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/noncopyable.hpp>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <boost/geometry/core/static_assert.hpp>
  23. #include <boost/geometry/core/tags.hpp>
  24. #include <boost/geometry/core/tag_cast.hpp>
  25. #include <boost/geometry/algorithms/envelope.hpp>
  26. #include <boost/geometry/algorithms/expand.hpp>
  27. #include <boost/geometry/algorithms/is_empty.hpp>
  28. #include <boost/geometry/algorithms/transform.hpp>
  29. #include <boost/geometry/strategies/transform/map_transformer.hpp>
  30. #include <boost/geometry/views/segment_view.hpp>
  31. #include <boost/geometry/io/svg/write.hpp>
  32. // Helper geometries (all points are transformed to svg-points)
  33. #include <boost/geometry/geometries/geometries.hpp>
  34. namespace boost { namespace geometry
  35. {
  36. #ifndef DOXYGEN_NO_DISPATCH
  37. namespace dispatch
  38. {
  39. template <typename GeometryTag, typename Geometry, typename SvgPoint>
  40. struct svg_map
  41. {
  42. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  43. "Not or not yet implemented for this Geometry type.",
  44. GeometryTag, Geometry);
  45. };
  46. template <typename Point, typename SvgPoint>
  47. struct svg_map<point_tag, Point, SvgPoint>
  48. {
  49. template <typename TransformStrategy>
  50. static inline void apply(std::ostream& stream,
  51. std::string const& style, double size,
  52. Point const& point, TransformStrategy const& strategy)
  53. {
  54. SvgPoint ipoint;
  55. geometry::transform(point, ipoint, strategy);
  56. stream << geometry::svg(ipoint, style, size) << std::endl;
  57. }
  58. };
  59. template <typename BoxSeg1, typename BoxSeg2, typename SvgPoint>
  60. struct svg_map_box_seg
  61. {
  62. template <typename TransformStrategy>
  63. static inline void apply(std::ostream& stream,
  64. std::string const& style, double size,
  65. BoxSeg1 const& box_seg, TransformStrategy const& strategy)
  66. {
  67. BoxSeg2 ibox_seg;
  68. // Fix bug in gcc compiler warning for possible uninitialization
  69. #if defined(BOOST_GCC)
  70. geometry::assign_zero(ibox_seg);
  71. #endif
  72. geometry::transform(box_seg, ibox_seg, strategy);
  73. stream << geometry::svg(ibox_seg, style, size) << std::endl;
  74. }
  75. };
  76. template <typename Box, typename SvgPoint>
  77. struct svg_map<box_tag, Box, SvgPoint>
  78. : svg_map_box_seg<Box, model::box<SvgPoint>, SvgPoint>
  79. {};
  80. template <typename Segment, typename SvgPoint>
  81. struct svg_map<segment_tag, Segment, SvgPoint>
  82. : svg_map_box_seg<Segment, model::segment<SvgPoint>, SvgPoint>
  83. {};
  84. template <typename Range1, typename Range2, typename SvgPoint>
  85. struct svg_map_range
  86. {
  87. template <typename TransformStrategy>
  88. static inline void apply(std::ostream& stream,
  89. std::string const& style, double size,
  90. Range1 const& range, TransformStrategy const& strategy)
  91. {
  92. Range2 irange;
  93. geometry::transform(range, irange, strategy);
  94. stream << geometry::svg(irange, style, size) << std::endl;
  95. }
  96. };
  97. template <typename Ring, typename SvgPoint>
  98. struct svg_map<ring_tag, Ring, SvgPoint>
  99. : svg_map_range<Ring, model::ring<SvgPoint>, SvgPoint>
  100. {};
  101. template <typename Linestring, typename SvgPoint>
  102. struct svg_map<linestring_tag, Linestring, SvgPoint>
  103. : svg_map_range<Linestring, model::linestring<SvgPoint>, SvgPoint>
  104. {};
  105. template <typename Polygon, typename SvgPoint>
  106. struct svg_map<polygon_tag, Polygon, SvgPoint>
  107. {
  108. template <typename TransformStrategy>
  109. static inline void apply(std::ostream& stream,
  110. std::string const& style, double size,
  111. Polygon const& polygon, TransformStrategy const& strategy)
  112. {
  113. model::polygon<SvgPoint> ipoly;
  114. geometry::transform(polygon, ipoly, strategy);
  115. stream << geometry::svg(ipoly, style, size) << std::endl;
  116. }
  117. };
  118. template <typename Multi, typename SvgPoint>
  119. struct svg_map<multi_tag, Multi, SvgPoint>
  120. {
  121. typedef typename single_tag_of
  122. <
  123. typename geometry::tag<Multi>::type
  124. >::type stag;
  125. template <typename TransformStrategy>
  126. static inline void apply(std::ostream& stream,
  127. std::string const& style, double size,
  128. Multi const& multi, TransformStrategy const& strategy)
  129. {
  130. for (typename boost::range_iterator<Multi const>::type it
  131. = boost::begin(multi);
  132. it != boost::end(multi);
  133. ++it)
  134. {
  135. svg_map
  136. <
  137. stag,
  138. typename boost::range_value<Multi>::type,
  139. SvgPoint
  140. >::apply(stream, style, size, *it, strategy);
  141. }
  142. }
  143. };
  144. template <typename SvgPoint, typename Geometry>
  145. struct devarianted_svg_map
  146. {
  147. template <typename TransformStrategy>
  148. static inline void apply(std::ostream& stream,
  149. std::string const& style,
  150. double size,
  151. Geometry const& geometry,
  152. TransformStrategy const& strategy)
  153. {
  154. svg_map
  155. <
  156. typename tag_cast
  157. <
  158. typename tag<Geometry>::type,
  159. multi_tag
  160. >::type,
  161. typename std::remove_const<Geometry>::type,
  162. SvgPoint
  163. >::apply(stream, style, size, geometry, strategy);
  164. }
  165. };
  166. template <typename SvgPoint, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  167. struct devarianted_svg_map<SvgPoint, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  168. {
  169. template <typename TransformStrategy>
  170. struct visitor: static_visitor<void>
  171. {
  172. std::ostream& m_os;
  173. std::string const& m_style;
  174. double m_size;
  175. TransformStrategy const& m_strategy;
  176. visitor(std::ostream& os,
  177. std::string const& style,
  178. double size,
  179. TransformStrategy const& strategy)
  180. : m_os(os)
  181. , m_style(style)
  182. , m_size(size)
  183. , m_strategy(strategy)
  184. {}
  185. template <typename Geometry>
  186. inline void operator()(Geometry const& geometry) const
  187. {
  188. devarianted_svg_map<SvgPoint, Geometry>::apply(m_os, m_style, m_size, geometry, m_strategy);
  189. }
  190. };
  191. template <typename TransformStrategy>
  192. static inline void apply(std::ostream& stream,
  193. std::string const& style,
  194. double size,
  195. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  196. TransformStrategy const& strategy)
  197. {
  198. boost::apply_visitor(visitor<TransformStrategy>(stream, style, size, strategy), geometry);
  199. }
  200. };
  201. } // namespace dispatch
  202. #endif
  203. template <typename SvgPoint, typename Geometry, typename TransformStrategy>
  204. inline void svg_map(std::ostream& stream,
  205. std::string const& style, double size,
  206. Geometry const& geometry, TransformStrategy const& strategy)
  207. {
  208. dispatch::devarianted_svg_map<SvgPoint, Geometry>::apply(stream,
  209. style, size, geometry, strategy);
  210. }
  211. /*!
  212. \brief Helper class to create SVG maps
  213. \tparam Point Point type, for input geometries.
  214. \tparam SameScale Boolean flag indicating if horizontal and vertical scale should
  215. be the same. The default value is true
  216. \tparam SvgCoordinateType Coordinate type of SVG points. SVG is capable to
  217. use floating point coordinates. Therefore the default value is double
  218. \ingroup svg
  219. \qbk{[include reference/io/svg.qbk]}
  220. */
  221. template
  222. <
  223. typename Point,
  224. bool SameScale = true,
  225. typename SvgCoordinateType = double
  226. >
  227. class svg_mapper : boost::noncopyable
  228. {
  229. typedef model::point<SvgCoordinateType, 2, cs::cartesian> svg_point_type;
  230. typedef typename geometry::select_most_precise
  231. <
  232. typename coordinate_type<Point>::type,
  233. double
  234. >::type calculation_type;
  235. typedef strategy::transform::map_transformer
  236. <
  237. calculation_type,
  238. geometry::dimension<Point>::type::value,
  239. geometry::dimension<Point>::type::value,
  240. true,
  241. SameScale
  242. > transformer_type;
  243. model::box<Point> m_bounding_box;
  244. boost::scoped_ptr<transformer_type> m_matrix;
  245. std::ostream& m_stream;
  246. SvgCoordinateType const m_width;
  247. SvgCoordinateType const m_height;
  248. calculation_type const m_scale{1.0};
  249. void scale_bounding_box()
  250. {
  251. if (m_scale != 1.0 && m_scale > 0)
  252. {
  253. // Zoom out (scale < 1) or zoom in (scale > 1).
  254. // The default is 0.95, giving a small margin around geometries.
  255. auto& b = m_bounding_box;
  256. auto const w = geometry::get<1, 0>(b) - geometry::get<0, 0>(b);
  257. auto const h = geometry::get<1, 1>(b) - geometry::get<0, 1>(b);
  258. auto const& m = (std::max)(w, h) * (1.0 - m_scale);
  259. geometry::set<0, 0>(b, geometry::get<0, 0>(b) - m);
  260. geometry::set<0, 1>(b, geometry::get<0, 1>(b) - m);
  261. geometry::set<1, 0>(b, geometry::get<1, 0>(b) + m);
  262. geometry::set<1, 1>(b, geometry::get<1, 1>(b) + m);
  263. }
  264. }
  265. void init_matrix()
  266. {
  267. if (! m_matrix)
  268. {
  269. scale_bounding_box();
  270. m_matrix.reset(new transformer_type(m_bounding_box,
  271. m_width, m_height));
  272. }
  273. }
  274. void write_header(std::string const& width_height)
  275. {
  276. m_stream << "<?xml version=\"1.0\" standalone=\"no\"?>"
  277. << std::endl
  278. << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\""
  279. << std::endl
  280. << "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">"
  281. << std::endl
  282. << "<svg " << width_height << " version=\"1.1\""
  283. << std::endl
  284. << "xmlns=\"http://www.w3.org/2000/svg\""
  285. << std::endl
  286. << "xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  287. << ">"
  288. << std::endl;
  289. }
  290. public :
  291. /*!
  292. \brief Constructor, initializing the SVG map. Opens and initializes the SVG.
  293. Should be called explicitly.
  294. \param stream Output stream, should be a stream already open
  295. \param width Width of the SVG map (in SVG pixels)
  296. \param height Height of the SVG map (in SVG pixels)
  297. \param scale Scale factor of the automatically determined bounding box.
  298. If the factor is less than 1.0, there will be a margin around the
  299. geometries. A factor of 0.95 is often a convenient margin. If the
  300. factor is more than 1.0, the SVG map is zoomed and not all
  301. geometries will be visible.
  302. \param width_height Optional information to increase width and/or height
  303. */
  304. svg_mapper(std::ostream& stream
  305. , SvgCoordinateType width
  306. , SvgCoordinateType height
  307. , calculation_type scale
  308. , std::string const& width_height = "width=\"100%\" height=\"100%\"")
  309. : m_stream(stream)
  310. , m_width(width)
  311. , m_height(height)
  312. , m_scale(scale)
  313. {
  314. assign_inverse(m_bounding_box);
  315. write_header(width_height);
  316. }
  317. /*!
  318. \brief Constructor, initializing the SVG map. Opens and initializes the SVG.
  319. Should be called explicitly.
  320. \param stream Output stream, should be a stream already open
  321. \param width Width of the SVG map (in SVG pixels)
  322. \param height Height of the SVG map (in SVG pixels)
  323. \param width_height Optional information to increase width and/or height
  324. */
  325. svg_mapper(std::ostream& stream
  326. , SvgCoordinateType width
  327. , SvgCoordinateType height
  328. , std::string const& width_height = "width=\"100%\" height=\"100%\"")
  329. : m_stream(stream)
  330. , m_width(width)
  331. , m_height(height)
  332. {
  333. assign_inverse(m_bounding_box);
  334. write_header(width_height);
  335. }
  336. /*!
  337. \brief Destructor, called automatically. Closes the SVG by streaming <\/svg>
  338. */
  339. virtual ~svg_mapper()
  340. {
  341. m_stream << "</svg>" << std::endl;
  342. }
  343. /*!
  344. \brief Adds a geometry to the transformation matrix. After doing this,
  345. the specified geometry can be mapped fully into the SVG map
  346. \tparam Geometry \tparam_geometry
  347. \param geometry \param_geometry
  348. */
  349. template <typename Geometry>
  350. void add(Geometry const& geometry)
  351. {
  352. if (! geometry::is_empty(geometry))
  353. {
  354. expand(m_bounding_box,
  355. return_envelope
  356. <
  357. model::box<Point>
  358. >(geometry));
  359. }
  360. }
  361. /*!
  362. \brief Maps a geometry into the SVG map using the specified style
  363. \tparam Geometry \tparam_geometry
  364. \param geometry \param_geometry
  365. \param style String containing verbatim SVG style information
  366. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  367. specify linewidth in the SVG style information
  368. */
  369. template <typename Geometry>
  370. void map(Geometry const& geometry, std::string const& style,
  371. double size = -1.0)
  372. {
  373. init_matrix();
  374. svg_map<svg_point_type>(m_stream, style, size, geometry, *m_matrix);
  375. }
  376. /*!
  377. \brief Adds a text to the SVG map
  378. \tparam TextPoint \tparam_point
  379. \param point Location of the text (in map units)
  380. \param s The text itself
  381. \param style String containing verbatim SVG style information, of the text
  382. \param offset_x Offset in SVG pixels, defaults to 0
  383. \param offset_y Offset in SVG pixels, defaults to 0
  384. \param lineheight Line height in SVG pixels, in case the text contains \n
  385. */
  386. template <typename TextPoint>
  387. void text(TextPoint const& point, std::string const& s,
  388. std::string const& style,
  389. double offset_x = 0.0, double offset_y = 0.0,
  390. double lineheight = 10.0)
  391. {
  392. init_matrix();
  393. svg_point_type map_point;
  394. transform(point, map_point, *m_matrix);
  395. m_stream
  396. << "<text style=\"" << style << "\""
  397. << " x=\"" << get<0>(map_point) + offset_x << "\""
  398. << " y=\"" << get<1>(map_point) + offset_y << "\""
  399. << ">";
  400. if (s.find("\n") == std::string::npos)
  401. {
  402. m_stream << s;
  403. }
  404. else
  405. {
  406. // Multi-line modus
  407. std::vector<std::string> splitted;
  408. boost::split(splitted, s, boost::is_any_of("\n"));
  409. for (std::vector<std::string>::const_iterator it
  410. = splitted.begin();
  411. it != splitted.end();
  412. ++it, offset_y += lineheight)
  413. {
  414. m_stream
  415. << "<tspan x=\"" << get<0>(map_point) + offset_x
  416. << "\""
  417. << " y=\"" << get<1>(map_point) + offset_y
  418. << "\""
  419. << ">" << *it << "</tspan>";
  420. }
  421. }
  422. m_stream << "</text>" << std::endl;
  423. }
  424. };
  425. }} // namespace boost::geometry
  426. #endif // BOOST_GEOMETRY_IO_SVG_MAPPER_HPP