write.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2016-2020.
  5. // Modifications copyright (c) 2016-2020, Oracle and/or its affiliates.
  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_WRITE_HPP
  13. #define BOOST_GEOMETRY_IO_SVG_WRITE_HPP
  14. #include <ostream>
  15. #include <string>
  16. #include <boost/config.hpp>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/value_type.hpp>
  20. #include <boost/variant/apply_visitor.hpp>
  21. #include <boost/variant/static_visitor.hpp>
  22. #include <boost/variant/variant_fwd.hpp>
  23. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  24. #include <boost/geometry/core/exterior_ring.hpp>
  25. #include <boost/geometry/core/interior_rings.hpp>
  26. #include <boost/geometry/core/ring_type.hpp>
  27. #include <boost/geometry/core/static_assert.hpp>
  28. #include <boost/geometry/geometries/concepts/check.hpp>
  29. namespace boost { namespace geometry
  30. {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail { namespace svg
  33. {
  34. template <typename Point>
  35. struct svg_point
  36. {
  37. template <typename Char, typename Traits>
  38. static inline void apply(std::basic_ostream<Char, Traits>& os,
  39. Point const& p, std::string const& style, double size)
  40. {
  41. os << "<circle cx=\"" << geometry::get<0>(p)
  42. << "\" cy=\"" << geometry::get<1>(p)
  43. << "\" r=\"" << (size < 0 ? 5 : size)
  44. << "\" style=\"" << style << "\"/>";
  45. }
  46. };
  47. template <typename Box>
  48. struct svg_box
  49. {
  50. template <typename Char, typename Traits>
  51. static inline void apply(std::basic_ostream<Char, Traits>& os,
  52. Box const& box, std::string const& style, double)
  53. {
  54. // Prevent invisible boxes, making them >=1, using "max"
  55. BOOST_USING_STD_MAX();
  56. typedef typename coordinate_type<Box>::type ct;
  57. ct x = geometry::get<geometry::min_corner, 0>(box);
  58. ct y = geometry::get<geometry::min_corner, 1>(box);
  59. ct width = max BOOST_PREVENT_MACRO_SUBSTITUTION (ct(1),
  60. geometry::get<geometry::max_corner, 0>(box) - x);
  61. ct height = max BOOST_PREVENT_MACRO_SUBSTITUTION (ct(1),
  62. geometry::get<geometry::max_corner, 1>(box) - y);
  63. os << "<rect x=\"" << x << "\" y=\"" << y
  64. << "\" width=\"" << width << "\" height=\"" << height
  65. << "\" style=\"" << style << "\"/>";
  66. }
  67. };
  68. template <typename Segment>
  69. struct svg_segment
  70. {
  71. template <typename Char, typename Traits>
  72. static inline void apply(std::basic_ostream<Char, Traits>& os,
  73. Segment const& segment, std::string const& style, double)
  74. {
  75. typedef typename coordinate_type<Segment>::type ct;
  76. ct x1 = geometry::get<0, 0>(segment);
  77. ct y1 = geometry::get<0, 1>(segment);
  78. ct x2 = geometry::get<1, 0>(segment);
  79. ct y2 = geometry::get<1, 1>(segment);
  80. os << "<line x1=\"" << x1 << "\" y1=\"" << y1
  81. << "\" x2=\"" << x2 << "\" y2=\"" << y2
  82. << "\" style=\"" << style << "\"/>";
  83. }
  84. };
  85. /*!
  86. \brief Stream ranges as SVG
  87. \note policy is used to select type (polyline/polygon)
  88. */
  89. template <typename Range, typename Policy>
  90. struct svg_range
  91. {
  92. template <typename Char, typename Traits>
  93. static inline void apply(std::basic_ostream<Char, Traits>& os,
  94. Range const& range, std::string const& style, double)
  95. {
  96. typedef typename boost::range_iterator<Range const>::type iterator;
  97. bool first = true;
  98. os << "<" << Policy::prefix() << " points=\"";
  99. for (iterator it = boost::begin(range);
  100. it != boost::end(range);
  101. ++it, first = false)
  102. {
  103. os << (first ? "" : " " )
  104. << geometry::get<0>(*it)
  105. << ","
  106. << geometry::get<1>(*it);
  107. }
  108. os << "\" style=\"" << style << Policy::style() << "\"/>";
  109. }
  110. };
  111. template <typename Polygon>
  112. struct svg_poly
  113. {
  114. template <typename Char, typename Traits>
  115. static inline void apply(std::basic_ostream<Char, Traits>& os,
  116. Polygon const& polygon, std::string const& style, double)
  117. {
  118. typedef typename geometry::ring_type<Polygon>::type ring_type;
  119. typedef typename boost::range_iterator<ring_type const>::type iterator_type;
  120. bool first = true;
  121. os << "<g fill-rule=\"evenodd\"><path d=\"";
  122. ring_type const& ring = geometry::exterior_ring(polygon);
  123. for (iterator_type it = boost::begin(ring);
  124. it != boost::end(ring);
  125. ++it, first = false)
  126. {
  127. os << (first ? "M" : " L") << " "
  128. << geometry::get<0>(*it)
  129. << ","
  130. << geometry::get<1>(*it);
  131. }
  132. // Inner rings:
  133. {
  134. typename interior_return_type<Polygon const>::type
  135. rings = interior_rings(polygon);
  136. for (typename detail::interior_iterator<Polygon const>::type
  137. rit = boost::begin(rings); rit != boost::end(rings); ++rit)
  138. {
  139. first = true;
  140. for (typename detail::interior_ring_iterator<Polygon const>::type
  141. it = boost::begin(*rit); it != boost::end(*rit);
  142. ++it, first = false)
  143. {
  144. os << (first ? "M" : " L") << " "
  145. << geometry::get<0>(*it)
  146. << ","
  147. << geometry::get<1>(*it);
  148. }
  149. }
  150. }
  151. os << " z \" style=\"" << style << "\"/></g>";
  152. }
  153. };
  154. struct prefix_linestring
  155. {
  156. static inline const char* prefix() { return "polyline"; }
  157. static inline const char* style() { return ";fill:none"; }
  158. };
  159. struct prefix_ring
  160. {
  161. static inline const char* prefix() { return "polygon"; }
  162. static inline const char* style() { return ""; }
  163. };
  164. template <typename MultiGeometry, typename Policy>
  165. struct svg_multi
  166. {
  167. template <typename Char, typename Traits>
  168. static inline void apply(std::basic_ostream<Char, Traits>& os,
  169. MultiGeometry const& multi, std::string const& style, double size)
  170. {
  171. for (typename boost::range_iterator<MultiGeometry const>::type
  172. it = boost::begin(multi);
  173. it != boost::end(multi);
  174. ++it)
  175. {
  176. Policy::apply(os, *it, style, size);
  177. }
  178. }
  179. };
  180. }} // namespace detail::svg
  181. #endif // DOXYGEN_NO_DETAIL
  182. #ifndef DOXYGEN_NO_DISPATCH
  183. namespace dispatch
  184. {
  185. /*!
  186. \brief Dispatching base struct for SVG streaming, specialized below per geometry type
  187. \details Specializations should implement a static method "stream" to stream a geometry
  188. The static method should have the signature:
  189. template <typename Char, typename Traits>
  190. static inline void apply(std::basic_ostream<Char, Traits>& os, G const& geometry)
  191. */
  192. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  193. struct svg
  194. {
  195. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  196. "Not or not yet implemented for this Geometry type.",
  197. Geometry, Tag);
  198. };
  199. template <typename Point>
  200. struct svg<Point, point_tag> : detail::svg::svg_point<Point> {};
  201. template <typename Segment>
  202. struct svg<Segment, segment_tag> : detail::svg::svg_segment<Segment> {};
  203. template <typename Box>
  204. struct svg<Box, box_tag> : detail::svg::svg_box<Box> {};
  205. template <typename Linestring>
  206. struct svg<Linestring, linestring_tag>
  207. : detail::svg::svg_range<Linestring, detail::svg::prefix_linestring> {};
  208. template <typename Ring>
  209. struct svg<Ring, ring_tag>
  210. : detail::svg::svg_range<Ring, detail::svg::prefix_ring> {};
  211. template <typename Polygon>
  212. struct svg<Polygon, polygon_tag>
  213. : detail::svg::svg_poly<Polygon> {};
  214. template <typename MultiPoint>
  215. struct svg<MultiPoint, multi_point_tag>
  216. : detail::svg::svg_multi
  217. <
  218. MultiPoint,
  219. detail::svg::svg_point
  220. <
  221. typename boost::range_value<MultiPoint>::type
  222. >
  223. >
  224. {};
  225. template <typename MultiLinestring>
  226. struct svg<MultiLinestring, multi_linestring_tag>
  227. : detail::svg::svg_multi
  228. <
  229. MultiLinestring,
  230. detail::svg::svg_range
  231. <
  232. typename boost::range_value<MultiLinestring>::type,
  233. detail::svg::prefix_linestring
  234. >
  235. >
  236. {};
  237. template <typename MultiPolygon>
  238. struct svg<MultiPolygon, multi_polygon_tag>
  239. : detail::svg::svg_multi
  240. <
  241. MultiPolygon,
  242. detail::svg::svg_poly
  243. <
  244. typename boost::range_value<MultiPolygon>::type
  245. >
  246. >
  247. {};
  248. template <typename Geometry>
  249. struct devarianted_svg
  250. {
  251. template <typename OutputStream>
  252. static inline void apply(OutputStream& os,
  253. Geometry const& geometry,
  254. std::string const& style,
  255. double size)
  256. {
  257. svg<Geometry>::apply(os, geometry, style, size);
  258. }
  259. };
  260. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  261. struct devarianted_svg<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  262. {
  263. template <typename OutputStream>
  264. struct visitor: static_visitor<void>
  265. {
  266. OutputStream& m_os;
  267. std::string const& m_style;
  268. double m_size;
  269. visitor(OutputStream& os, std::string const& style, double size)
  270. : m_os(os)
  271. , m_style(style)
  272. , m_size(size)
  273. {}
  274. template <typename Geometry>
  275. inline void operator()(Geometry const& geometry) const
  276. {
  277. devarianted_svg<Geometry>::apply(m_os, geometry, m_style, m_size);
  278. }
  279. };
  280. template <typename OutputStream>
  281. static inline void apply(
  282. OutputStream& os,
  283. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  284. std::string const& style,
  285. double size
  286. )
  287. {
  288. boost::apply_visitor(visitor<OutputStream>(os, style, size), geometry);
  289. }
  290. };
  291. } // namespace dispatch
  292. #endif // DOXYGEN_NO_DISPATCH
  293. /*!
  294. \brief Generic geometry template manipulator class, takes corresponding output class from traits class
  295. \ingroup svg
  296. \details Stream manipulator, streams geometry classes as SVG (Scalable Vector Graphics)
  297. */
  298. template <typename Geometry>
  299. class svg_manipulator
  300. {
  301. public:
  302. inline svg_manipulator(Geometry const& g, std::string const& style, double size)
  303. : m_geometry(g)
  304. , m_style(style)
  305. , m_size(size)
  306. {}
  307. template <typename Char, typename Traits>
  308. inline friend std::basic_ostream<Char, Traits>& operator<<(
  309. std::basic_ostream<Char, Traits>& os, svg_manipulator const& m)
  310. {
  311. dispatch::devarianted_svg<Geometry>::apply(os,
  312. m.m_geometry,
  313. m.m_style,
  314. m.m_size);
  315. os.flush();
  316. return os;
  317. }
  318. private:
  319. Geometry const& m_geometry;
  320. std::string const& m_style;
  321. double m_size;
  322. };
  323. /*!
  324. \brief Manipulator to stream geometries as SVG
  325. \tparam Geometry \tparam_geometry
  326. \param geometry \param_geometry
  327. \param style String containing verbatim SVG style information
  328. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  329. specify linewidth in the SVG style information
  330. \ingroup svg
  331. */
  332. template <typename Geometry>
  333. inline svg_manipulator<Geometry> svg(Geometry const& geometry,
  334. std::string const& style, double size = -1.0)
  335. {
  336. concepts::check<Geometry const>();
  337. return svg_manipulator<Geometry>(geometry, style, size);
  338. }
  339. }} // namespace boost::geometry
  340. #endif // BOOST_GEOMETRY_IO_SVG_WRITE_HPP