write.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.
  5. // Modifications copyright (c) 2016, 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/mpl/assert.hpp>
  18. #include <boost/range.hpp>
  19. #include <boost/variant/apply_visitor.hpp>
  20. #include <boost/variant/static_visitor.hpp>
  21. #include <boost/variant/variant_fwd.hpp>
  22. #include <boost/geometry/algorithms/detail/interior_iterator.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/geometries/concepts/check.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. #ifndef DOXYGEN_NO_DETAIL
  30. namespace detail { namespace svg
  31. {
  32. template <typename Point>
  33. struct svg_point
  34. {
  35. template <typename Char, typename Traits>
  36. static inline void apply(std::basic_ostream<Char, Traits>& os,
  37. Point const& p, std::string const& style, double size)
  38. {
  39. os << "<circle cx=\"" << geometry::get<0>(p)
  40. << "\" cy=\"" << geometry::get<1>(p)
  41. << "\" r=\"" << (size < 0 ? 5 : size)
  42. << "\" style=\"" << style << "\"/>";
  43. }
  44. };
  45. template <typename Box>
  46. struct svg_box
  47. {
  48. template <typename Char, typename Traits>
  49. static inline void apply(std::basic_ostream<Char, Traits>& os,
  50. Box const& box, std::string const& style, double)
  51. {
  52. // Prevent invisible boxes, making them >=1, using "max"
  53. BOOST_USING_STD_MAX();
  54. typedef typename coordinate_type<Box>::type ct;
  55. ct x = geometry::get<geometry::min_corner, 0>(box);
  56. ct y = geometry::get<geometry::min_corner, 1>(box);
  57. ct width = max BOOST_PREVENT_MACRO_SUBSTITUTION (ct(1),
  58. geometry::get<geometry::max_corner, 0>(box) - x);
  59. ct height = max BOOST_PREVENT_MACRO_SUBSTITUTION (ct(1),
  60. geometry::get<geometry::max_corner, 1>(box) - y);
  61. os << "<rect x=\"" << x << "\" y=\"" << y
  62. << "\" width=\"" << width << "\" height=\"" << height
  63. << "\" style=\"" << style << "\"/>";
  64. }
  65. };
  66. template <typename Segment>
  67. struct svg_segment
  68. {
  69. template <typename Char, typename Traits>
  70. static inline void apply(std::basic_ostream<Char, Traits>& os,
  71. Segment const& segment, std::string const& style, double)
  72. {
  73. typedef typename coordinate_type<Segment>::type ct;
  74. ct x1 = geometry::get<0, 0>(segment);
  75. ct y1 = geometry::get<0, 1>(segment);
  76. ct x2 = geometry::get<1, 0>(segment);
  77. ct y2 = geometry::get<1, 1>(segment);
  78. os << "<line x1=\"" << x1 << "\" y1=\"" << y1
  79. << "\" x2=\"" << x2 << "\" y2=\"" << y2
  80. << "\" style=\"" << style << "\"/>";
  81. }
  82. };
  83. /*!
  84. \brief Stream ranges as SVG
  85. \note policy is used to select type (polyline/polygon)
  86. */
  87. template <typename Range, typename Policy>
  88. struct svg_range
  89. {
  90. template <typename Char, typename Traits>
  91. static inline void apply(std::basic_ostream<Char, Traits>& os,
  92. Range const& range, std::string const& style, double)
  93. {
  94. typedef typename boost::range_iterator<Range const>::type iterator;
  95. bool first = true;
  96. os << "<" << Policy::prefix() << " points=\"";
  97. for (iterator it = boost::begin(range);
  98. it != boost::end(range);
  99. ++it, first = false)
  100. {
  101. os << (first ? "" : " " )
  102. << geometry::get<0>(*it)
  103. << ","
  104. << geometry::get<1>(*it);
  105. }
  106. os << "\" style=\"" << style << Policy::style() << "\"/>";
  107. }
  108. };
  109. template <typename Polygon>
  110. struct svg_poly
  111. {
  112. template <typename Char, typename Traits>
  113. static inline void apply(std::basic_ostream<Char, Traits>& os,
  114. Polygon const& polygon, std::string const& style, double)
  115. {
  116. typedef typename geometry::ring_type<Polygon>::type ring_type;
  117. typedef typename boost::range_iterator<ring_type const>::type iterator_type;
  118. bool first = true;
  119. os << "<g fill-rule=\"evenodd\"><path d=\"";
  120. ring_type const& ring = geometry::exterior_ring(polygon);
  121. for (iterator_type it = boost::begin(ring);
  122. it != boost::end(ring);
  123. ++it, first = false)
  124. {
  125. os << (first ? "M" : " L") << " "
  126. << geometry::get<0>(*it)
  127. << ","
  128. << geometry::get<1>(*it);
  129. }
  130. // Inner rings:
  131. {
  132. typename interior_return_type<Polygon const>::type
  133. rings = interior_rings(polygon);
  134. for (typename detail::interior_iterator<Polygon const>::type
  135. rit = boost::begin(rings); rit != boost::end(rings); ++rit)
  136. {
  137. first = true;
  138. for (typename detail::interior_ring_iterator<Polygon const>::type
  139. it = boost::begin(*rit); it != boost::end(*rit);
  140. ++it, first = false)
  141. {
  142. os << (first ? "M" : " L") << " "
  143. << geometry::get<0>(*it)
  144. << ","
  145. << geometry::get<1>(*it);
  146. }
  147. }
  148. }
  149. os << " z \" style=\"" << style << "\"/></g>";
  150. }
  151. };
  152. struct prefix_linestring
  153. {
  154. static inline const char* prefix() { return "polyline"; }
  155. static inline const char* style() { return ";fill:none"; }
  156. };
  157. struct prefix_ring
  158. {
  159. static inline const char* prefix() { return "polygon"; }
  160. static inline const char* style() { return ""; }
  161. };
  162. template <typename MultiGeometry, typename Policy>
  163. struct svg_multi
  164. {
  165. template <typename Char, typename Traits>
  166. static inline void apply(std::basic_ostream<Char, Traits>& os,
  167. MultiGeometry const& multi, std::string const& style, double size)
  168. {
  169. for (typename boost::range_iterator<MultiGeometry const>::type
  170. it = boost::begin(multi);
  171. it != boost::end(multi);
  172. ++it)
  173. {
  174. Policy::apply(os, *it, style, size);
  175. }
  176. }
  177. };
  178. }} // namespace detail::svg
  179. #endif // DOXYGEN_NO_DETAIL
  180. #ifndef DOXYGEN_NO_DISPATCH
  181. namespace dispatch
  182. {
  183. /*!
  184. \brief Dispatching base struct for SVG streaming, specialized below per geometry type
  185. \details Specializations should implement a static method "stream" to stream a geometry
  186. The static method should have the signature:
  187. template <typename Char, typename Traits>
  188. static inline void apply(std::basic_ostream<Char, Traits>& os, G const& geometry)
  189. */
  190. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  191. struct svg
  192. {
  193. BOOST_MPL_ASSERT_MSG
  194. (
  195. false, NOT_OR_NOT_YET_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE
  196. , (Geometry)
  197. );
  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