svg_mapper.hpp 15 KB

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