write.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. typedef typename coordinate_type<Box>::type ct;
  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. typedef typename coordinate_type<Segment>::type ct;
  75. ct x1 = geometry::get<0, 0>(segment);
  76. ct y1 = geometry::get<0, 1>(segment);
  77. ct x2 = geometry::get<1, 0>(segment);
  78. ct 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. typedef typename geometry::ring_type<Polygon>::type ring_type;
  115. bool first = true;
  116. os << "<g fill-rule=\"evenodd\"><path d=\"";
  117. ring_type const& ring = geometry::exterior_ring(polygon);
  118. for (auto it = boost::begin(ring); it != boost::end(ring); ++it, first = false)
  119. {
  120. os << (first ? "M" : " L") << " "
  121. << geometry::get<0>(*it)
  122. << ","
  123. << geometry::get<1>(*it);
  124. }
  125. // Inner rings:
  126. {
  127. auto const& rings = interior_rings(polygon);
  128. for (auto rit = boost::begin(rings); rit != boost::end(rings); ++rit)
  129. {
  130. first = true;
  131. for (auto it = boost::begin(*rit); it != boost::end(*rit); ++it, first = false)
  132. {
  133. os << (first ? "M" : " L") << " "
  134. << geometry::get<0>(*it)
  135. << ","
  136. << geometry::get<1>(*it);
  137. }
  138. }
  139. }
  140. os << " z \" style=\"" << style << "\"/></g>";
  141. }
  142. };
  143. struct prefix_linestring
  144. {
  145. static inline const char* prefix() { return "polyline"; }
  146. static inline const char* style() { return ";fill:none"; }
  147. };
  148. struct prefix_ring
  149. {
  150. static inline const char* prefix() { return "polygon"; }
  151. static inline const char* style() { return ""; }
  152. };
  153. template <typename MultiGeometry, typename Policy>
  154. struct svg_multi
  155. {
  156. template <typename Char, typename Traits>
  157. static inline void apply(std::basic_ostream<Char, Traits>& os,
  158. MultiGeometry const& multi, std::string const& style, double size)
  159. {
  160. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  161. {
  162. Policy::apply(os, *it, style, size);
  163. }
  164. }
  165. };
  166. }} // namespace detail::svg
  167. #endif // DOXYGEN_NO_DETAIL
  168. #ifndef DOXYGEN_NO_DISPATCH
  169. namespace dispatch
  170. {
  171. /*!
  172. \brief Dispatching base struct for SVG streaming, specialized below per geometry type
  173. \details Specializations should implement a static method "stream" to stream a geometry
  174. The static method should have the signature:
  175. template <typename Char, typename Traits>
  176. static inline void apply(std::basic_ostream<Char, Traits>& os, G const& geometry)
  177. */
  178. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  179. struct svg
  180. {
  181. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  182. "Not or not yet implemented for this Geometry type.",
  183. Geometry, Tag);
  184. };
  185. template <typename Point>
  186. struct svg<Point, point_tag> : detail::svg::svg_point<Point> {};
  187. template <typename Segment>
  188. struct svg<Segment, segment_tag> : detail::svg::svg_segment<Segment> {};
  189. template <typename Box>
  190. struct svg<Box, box_tag> : detail::svg::svg_box<Box> {};
  191. template <typename Linestring>
  192. struct svg<Linestring, linestring_tag>
  193. : detail::svg::svg_range<Linestring, detail::svg::prefix_linestring> {};
  194. template <typename Ring>
  195. struct svg<Ring, ring_tag>
  196. : detail::svg::svg_range<Ring, detail::svg::prefix_ring> {};
  197. template <typename Polygon>
  198. struct svg<Polygon, polygon_tag>
  199. : detail::svg::svg_poly<Polygon> {};
  200. template <typename MultiPoint>
  201. struct svg<MultiPoint, multi_point_tag>
  202. : detail::svg::svg_multi
  203. <
  204. MultiPoint,
  205. detail::svg::svg_point
  206. <
  207. typename boost::range_value<MultiPoint>::type
  208. >
  209. >
  210. {};
  211. template <typename MultiLinestring>
  212. struct svg<MultiLinestring, multi_linestring_tag>
  213. : detail::svg::svg_multi
  214. <
  215. MultiLinestring,
  216. detail::svg::svg_range
  217. <
  218. typename boost::range_value<MultiLinestring>::type,
  219. detail::svg::prefix_linestring
  220. >
  221. >
  222. {};
  223. template <typename MultiPolygon>
  224. struct svg<MultiPolygon, multi_polygon_tag>
  225. : detail::svg::svg_multi
  226. <
  227. MultiPolygon,
  228. detail::svg::svg_poly
  229. <
  230. typename boost::range_value<MultiPolygon>::type
  231. >
  232. >
  233. {};
  234. template <typename Geometry>
  235. struct devarianted_svg
  236. {
  237. template <typename OutputStream>
  238. static inline void apply(OutputStream& os,
  239. Geometry const& geometry,
  240. std::string const& style,
  241. double size)
  242. {
  243. svg<Geometry>::apply(os, geometry, style, size);
  244. }
  245. };
  246. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  247. struct devarianted_svg<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  248. {
  249. template <typename OutputStream>
  250. struct visitor: static_visitor<void>
  251. {
  252. OutputStream& m_os;
  253. std::string const& m_style;
  254. double m_size;
  255. visitor(OutputStream& os, std::string const& style, double size)
  256. : m_os(os)
  257. , m_style(style)
  258. , m_size(size)
  259. {}
  260. template <typename Geometry>
  261. inline void operator()(Geometry const& geometry) const
  262. {
  263. devarianted_svg<Geometry>::apply(m_os, geometry, m_style, m_size);
  264. }
  265. };
  266. template <typename OutputStream>
  267. static inline void apply(
  268. OutputStream& os,
  269. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  270. std::string const& style,
  271. double size
  272. )
  273. {
  274. boost::apply_visitor(visitor<OutputStream>(os, style, size), geometry);
  275. }
  276. };
  277. } // namespace dispatch
  278. #endif // DOXYGEN_NO_DISPATCH
  279. /*!
  280. \brief Generic geometry template manipulator class, takes corresponding output class from traits class
  281. \ingroup svg
  282. \details Stream manipulator, streams geometry classes as SVG (Scalable Vector Graphics)
  283. */
  284. template <typename Geometry>
  285. class svg_manipulator
  286. {
  287. public:
  288. inline svg_manipulator(Geometry const& g, std::string const& style, double size)
  289. : m_geometry(g)
  290. , m_style(style)
  291. , m_size(size)
  292. {}
  293. template <typename Char, typename Traits>
  294. inline friend std::basic_ostream<Char, Traits>& operator<<(
  295. std::basic_ostream<Char, Traits>& os, svg_manipulator const& m)
  296. {
  297. dispatch::devarianted_svg<Geometry>::apply(os,
  298. m.m_geometry,
  299. m.m_style,
  300. m.m_size);
  301. os.flush();
  302. return os;
  303. }
  304. private:
  305. Geometry const& m_geometry;
  306. std::string const& m_style;
  307. double m_size;
  308. };
  309. /*!
  310. \brief Manipulator to stream geometries as SVG
  311. \tparam Geometry \tparam_geometry
  312. \param geometry \param_geometry
  313. \param style String containing verbatim SVG style information
  314. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  315. specify linewidth in the SVG style information
  316. \ingroup svg
  317. */
  318. template <typename Geometry>
  319. inline svg_manipulator<Geometry> svg(Geometry const& geometry,
  320. std::string const& style, double size = -1.0)
  321. {
  322. concepts::check<Geometry const>();
  323. return svg_manipulator<Geometry>(geometry, style, size);
  324. }
  325. }} // namespace boost::geometry
  326. #endif // BOOST_GEOMETRY_IO_SVG_WRITE_HPP