svg_mapper.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. typename geometry::tag<Multi>::type
  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. typename tag_cast
  155. <
  156. typename tag<Geometry>::type,
  157. multi_tag
  158. >::type,
  159. typename std::remove_const<Geometry>::type,
  160. SvgPoint
  161. >::apply(stream, style, size, geometry, strategy);
  162. }
  163. };
  164. template <typename SvgPoint, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  165. struct devarianted_svg_map<SvgPoint, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  166. {
  167. template <typename TransformStrategy>
  168. struct visitor: static_visitor<void>
  169. {
  170. std::ostream& m_os;
  171. std::string const& m_style;
  172. double m_size;
  173. TransformStrategy const& m_strategy;
  174. visitor(std::ostream& os,
  175. std::string const& style,
  176. double size,
  177. TransformStrategy const& strategy)
  178. : m_os(os)
  179. , m_style(style)
  180. , m_size(size)
  181. , m_strategy(strategy)
  182. {}
  183. template <typename Geometry>
  184. inline void operator()(Geometry const& geometry) const
  185. {
  186. devarianted_svg_map<SvgPoint, Geometry>::apply(m_os, m_style, m_size, geometry, m_strategy);
  187. }
  188. };
  189. template <typename TransformStrategy>
  190. static inline void apply(std::ostream& stream,
  191. std::string const& style,
  192. double size,
  193. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  194. TransformStrategy const& strategy)
  195. {
  196. boost::apply_visitor(visitor<TransformStrategy>(stream, style, size, strategy), geometry);
  197. }
  198. };
  199. } // namespace dispatch
  200. #endif
  201. template <typename SvgPoint, typename Geometry, typename TransformStrategy>
  202. inline void svg_map(std::ostream& stream,
  203. std::string const& style, double size,
  204. Geometry const& geometry, TransformStrategy const& strategy)
  205. {
  206. dispatch::devarianted_svg_map<SvgPoint, Geometry>::apply(stream,
  207. style, size, geometry, strategy);
  208. }
  209. /*!
  210. \brief Helper class to create SVG maps
  211. \tparam Point Point type, for input geometries.
  212. \tparam SameScale Boolean flag indicating if horizontal and vertical scale should
  213. be the same. The default value is true
  214. \tparam SvgCoordinateType Coordinate type of SVG points. SVG is capable to
  215. use floating point coordinates. Therefore the default value is double
  216. \ingroup svg
  217. \qbk{[include reference/io/svg.qbk]}
  218. */
  219. template
  220. <
  221. typename Point,
  222. bool SameScale = true,
  223. typename SvgCoordinateType = double
  224. >
  225. class svg_mapper : boost::noncopyable
  226. {
  227. typedef model::point<SvgCoordinateType, 2, cs::cartesian> svg_point_type;
  228. typedef typename geometry::select_most_precise
  229. <
  230. typename coordinate_type<Point>::type,
  231. double
  232. >::type calculation_type;
  233. typedef strategy::transform::map_transformer
  234. <
  235. calculation_type,
  236. geometry::dimension<Point>::type::value,
  237. geometry::dimension<Point>::type::value,
  238. true,
  239. SameScale
  240. > transformer_type;
  241. model::box<Point> m_bounding_box;
  242. std::unique_ptr<transformer_type> m_matrix;
  243. std::ostream& m_stream;
  244. SvgCoordinateType const m_width;
  245. SvgCoordinateType const m_height;
  246. calculation_type const m_scale{1.0};
  247. void scale_bounding_box()
  248. {
  249. if (m_scale != 1.0 && m_scale > 0)
  250. {
  251. // Zoom out (scale < 1) or zoom in (scale > 1).
  252. // The default is 0.95, giving a small margin around geometries.
  253. auto& b = m_bounding_box;
  254. auto const w = geometry::get<1, 0>(b) - geometry::get<0, 0>(b);
  255. auto const h = geometry::get<1, 1>(b) - geometry::get<0, 1>(b);
  256. auto const& m = (std::max)(w, h) * (1.0 - m_scale);
  257. geometry::set<0, 0>(b, geometry::get<0, 0>(b) - m);
  258. geometry::set<0, 1>(b, geometry::get<0, 1>(b) - m);
  259. geometry::set<1, 0>(b, geometry::get<1, 0>(b) + m);
  260. geometry::set<1, 1>(b, geometry::get<1, 1>(b) + m);
  261. }
  262. }
  263. void init_matrix()
  264. {
  265. if (! m_matrix)
  266. {
  267. scale_bounding_box();
  268. m_matrix.reset(new transformer_type(m_bounding_box,
  269. m_width, m_height));
  270. }
  271. }
  272. void write_header(std::string const& width_height)
  273. {
  274. m_stream << "<?xml version=\"1.0\" standalone=\"no\"?>"
  275. << std::endl
  276. << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\""
  277. << std::endl
  278. << "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">"
  279. << std::endl
  280. << "<svg " << width_height << " version=\"1.1\""
  281. << std::endl
  282. << "xmlns=\"http://www.w3.org/2000/svg\""
  283. << std::endl
  284. << "xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  285. << ">"
  286. << std::endl;
  287. }
  288. public :
  289. /*!
  290. \brief Constructor, initializing the SVG map. Opens and initializes the SVG.
  291. Should be called explicitly.
  292. \param stream Output stream, should be a stream already open
  293. \param width Width of the SVG map (in SVG pixels)
  294. \param height Height of the SVG map (in SVG pixels)
  295. \param scale Scale factor of the automatically determined bounding box.
  296. If the factor is less than 1.0, there will be a margin around the
  297. geometries. A factor of 0.95 is often a convenient margin. If the
  298. factor is more than 1.0, the SVG map is zoomed and not all
  299. geometries will be visible.
  300. \param width_height Optional information to increase width and/or height
  301. */
  302. svg_mapper(std::ostream& stream
  303. , SvgCoordinateType width
  304. , SvgCoordinateType height
  305. , calculation_type scale
  306. , std::string const& width_height = "width=\"100%\" height=\"100%\"")
  307. : m_stream(stream)
  308. , m_width(width)
  309. , m_height(height)
  310. , m_scale(scale)
  311. {
  312. assign_inverse(m_bounding_box);
  313. write_header(width_height);
  314. }
  315. /*!
  316. \brief Constructor, initializing the SVG map. Opens and initializes the SVG.
  317. Should be called explicitly.
  318. \param stream Output stream, should be a stream already open
  319. \param width Width of the SVG map (in SVG pixels)
  320. \param height Height of the SVG map (in SVG pixels)
  321. \param width_height Optional information to increase width and/or height
  322. */
  323. svg_mapper(std::ostream& stream
  324. , SvgCoordinateType width
  325. , SvgCoordinateType height
  326. , std::string const& width_height = "width=\"100%\" height=\"100%\"")
  327. : m_stream(stream)
  328. , m_width(width)
  329. , m_height(height)
  330. {
  331. assign_inverse(m_bounding_box);
  332. write_header(width_height);
  333. }
  334. /*!
  335. \brief Destructor, called automatically. Closes the SVG by streaming <\/svg>
  336. */
  337. virtual ~svg_mapper()
  338. {
  339. m_stream << "</svg>" << std::endl;
  340. }
  341. /*!
  342. \brief Adds a geometry to the transformation matrix. After doing this,
  343. the specified geometry can be mapped fully into the SVG map
  344. \tparam Geometry \tparam_geometry
  345. \param geometry \param_geometry
  346. */
  347. template <typename Geometry>
  348. void add(Geometry const& geometry)
  349. {
  350. if (! geometry::is_empty(geometry))
  351. {
  352. expand(m_bounding_box,
  353. return_envelope
  354. <
  355. model::box<Point>
  356. >(geometry));
  357. }
  358. }
  359. /*!
  360. \brief Maps a geometry into the SVG map using the specified style
  361. \tparam Geometry \tparam_geometry
  362. \param geometry \param_geometry
  363. \param style String containing verbatim SVG style information
  364. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  365. specify linewidth in the SVG style information
  366. */
  367. template <typename Geometry>
  368. void map(Geometry const& geometry, std::string const& style,
  369. double size = -1.0)
  370. {
  371. init_matrix();
  372. svg_map<svg_point_type>(m_stream, style, size, geometry, *m_matrix);
  373. }
  374. /*!
  375. \brief Adds a text to the SVG map
  376. \tparam TextPoint \tparam_point
  377. \param point Location of the text (in map units)
  378. \param s The text itself
  379. \param style String containing verbatim SVG style information, of the text
  380. \param offset_x Offset in SVG pixels, defaults to 0
  381. \param offset_y Offset in SVG pixels, defaults to 0
  382. \param lineheight Line height in SVG pixels, in case the text contains \n
  383. */
  384. template <typename TextPoint>
  385. void text(TextPoint const& point, std::string const& s,
  386. std::string const& style,
  387. double offset_x = 0.0, double offset_y = 0.0,
  388. double lineheight = 10.0)
  389. {
  390. init_matrix();
  391. svg_point_type map_point;
  392. transform(point, map_point, *m_matrix);
  393. m_stream
  394. << "<text style=\"" << style << "\""
  395. << " x=\"" << get<0>(map_point) + offset_x << "\""
  396. << " y=\"" << get<1>(map_point) + offset_y << "\""
  397. << ">";
  398. if (s.find('\n') == std::string::npos)
  399. {
  400. m_stream << s;
  401. }
  402. else
  403. {
  404. // Multi-line modus
  405. std::vector<std::string> split;
  406. boost::split(split, s, boost::is_any_of("\n"));
  407. for (auto const& item : split)
  408. {
  409. m_stream
  410. << "<tspan x=\"" << get<0>(map_point) + offset_x
  411. << "\""
  412. << " y=\"" << get<1>(map_point) + offset_y
  413. << "\""
  414. << ">" << item << "</tspan>";
  415. offset_y += lineheight;
  416. }
  417. }
  418. m_stream << "</text>" << std::endl;
  419. }
  420. };
  421. }} // namespace boost::geometry
  422. #endif // BOOST_GEOMETRY_IO_SVG_MAPPER_HPP