write.hpp 11 KB

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