wkt.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2014.
  7. // Modifications copyright (c) 2014 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #include <sstream>
  15. #include <string>
  16. #include <boost/algorithm/string.hpp>
  17. #include <geometry_test_common.hpp>
  18. #include <boost/geometry/geometries/geometries.hpp>
  19. #include <boost/geometry/algorithms/area.hpp>
  20. #include <boost/geometry/algorithms/length.hpp>
  21. #include <boost/geometry/algorithms/num_points.hpp>
  22. #include <boost/geometry/algorithms/perimeter.hpp>
  23. #include <boost/geometry/strategies/strategies.hpp>
  24. #include <boost/geometry/core/point_type.hpp>
  25. #include <boost/geometry/core/topological_dimension.hpp>
  26. #include <boost/geometry/io/wkt/read.hpp>
  27. #include <boost/geometry/io/wkt/write.hpp>
  28. #include <boost/variant/variant.hpp>
  29. template <typename G>
  30. void check_wkt(G const& geometry, std::string const& expected)
  31. {
  32. std::ostringstream out;
  33. out << bg::wkt(geometry);
  34. BOOST_CHECK_EQUAL(boost::to_upper_copy(out.str()),
  35. boost::to_upper_copy(expected));
  36. }
  37. template <typename G>
  38. void test_wkt(std::string const& wkt, std::string const& expected,
  39. std::size_t n, double len = 0, double ar = 0, double peri = 0)
  40. {
  41. G geometry;
  42. bg::read_wkt(wkt, geometry);
  43. /*
  44. std::cout << "n=" << bg::num_points(geometry)
  45. << " dim=" << bg::topological_dimension<G>::value
  46. << " length=" << bg::length(geometry)
  47. << " area=" << bg::area(geometry)
  48. << " perimeter=" << bg::perimeter(geometry)
  49. << std::endl << "\t\tgeometry=" << dsv(geometry)
  50. << std::endl;
  51. */
  52. BOOST_CHECK_EQUAL(bg::num_points(geometry), n);
  53. if (n > 0)
  54. {
  55. BOOST_CHECK_CLOSE(double(bg::length(geometry)), len, 0.0001);
  56. BOOST_CHECK_CLOSE(double(bg::area(geometry)), ar, 0.0001);
  57. BOOST_CHECK_CLOSE(double(bg::perimeter(geometry)), peri, 0.0001);
  58. }
  59. check_wkt(geometry, expected);
  60. check_wkt(boost::variant<G>(geometry), expected);
  61. }
  62. template <typename G>
  63. void test_wkt(std::string const& wkt,
  64. std::size_t n, double len = 0, double ar = 0, double peri = 0)
  65. {
  66. test_wkt<G>(wkt, wkt, n, len, ar, peri);
  67. }
  68. template <typename G>
  69. void test_relaxed_wkt(std::string const& wkt, std::string const& expected)
  70. {
  71. std::string e;
  72. G geometry;
  73. bg::read_wkt(wkt, geometry);
  74. std::ostringstream out;
  75. out << bg::wkt(geometry);
  76. BOOST_CHECK_EQUAL(boost::to_upper_copy(out.str()), boost::to_upper_copy(expected));
  77. }
  78. template <typename G>
  79. void test_wrong_wkt(std::string const& wkt, std::string const& start)
  80. {
  81. std::string e("no exception");
  82. G geometry;
  83. try
  84. {
  85. bg::read_wkt(wkt, geometry);
  86. }
  87. catch(bg::read_wkt_exception const& ex)
  88. {
  89. e = ex.what();
  90. boost::to_lower(e);
  91. }
  92. catch(...)
  93. {
  94. e = "other exception";
  95. }
  96. bool check = true;
  97. #if defined(HAVE_TTMATH)
  98. // For ttmath we skip bad lexical casts
  99. typedef typename bg::coordinate_type<G>::type ct;
  100. if (boost::is_same<ct, ttmath_big>::type::value
  101. && boost::starts_with(start, "bad lexical cast"))
  102. {
  103. check = false;
  104. }
  105. #endif
  106. if (check)
  107. {
  108. BOOST_CHECK_MESSAGE(boost::starts_with(e, start), " Expected:"
  109. << start << " Got:" << e << " with WKT: " << wkt);
  110. }
  111. }
  112. template <typename G>
  113. void test_wkt_output_iterator(std::string const& wkt)
  114. {
  115. G geometry;
  116. bg::read_wkt<G>(wkt, std::back_inserter(geometry));
  117. }
  118. #ifndef GEOMETRY_TEST_MULTI
  119. template <typename T>
  120. void test_order_closure()
  121. {
  122. using namespace boost::geometry;
  123. typedef bg::model::point<T, 2, bg::cs::cartesian> Pt;
  124. typedef bg::model::polygon<Pt, true, true> PCWC;
  125. typedef bg::model::polygon<Pt, true, false> PCWO;
  126. typedef bg::model::polygon<Pt, false, true> PCCWC;
  127. typedef bg::model::polygon<Pt, false, false> PCCWO;
  128. {
  129. std::string wkt_cwc = "POLYGON((0 0,0 2,2 2,2 0,0 0))";
  130. std::string wkt_cwo = "POLYGON((0 0,0 2,2 2,2 0))";
  131. std::string wkt_ccwc = "POLYGON((0 0,2 0,2 2,0 2,0 0))";
  132. std::string wkt_ccwo = "POLYGON((0 0,2 0,2 2,0 2))";
  133. test_wkt<PCWC>(wkt_cwc, 5, 0, 4, 8);
  134. test_wkt<PCWO>(wkt_cwc, 4, 0, 4, 8);
  135. test_wkt<PCWO>(wkt_cwo, wkt_cwc, 4, 0, 4, 8);
  136. test_wkt<PCCWC>(wkt_ccwc, 5, 0, 4, 8);
  137. test_wkt<PCCWO>(wkt_ccwc, 4, 0, 4, 8);
  138. test_wkt<PCCWO>(wkt_ccwo, wkt_ccwc, 4, 0, 4, 8);
  139. }
  140. {
  141. std::string wkt_cwc = "POLYGON((0 0,0 3,3 3,3 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
  142. std::string wkt_cwo = "POLYGON((0 0,0 3,3 3,3 0),(1 1,2 1,2 2,1 2))";
  143. std::string wkt_ccwc = "POLYGON((0 0,3 0,3 3,0 3,0 0),(1 1,1 2,2 2,2 1,1 1))";
  144. std::string wkt_ccwo = "POLYGON((0 0,3 0,3 3,0 3),(1 1,1 2,2 2,2 1,1 1))";
  145. test_wkt<PCWC>(wkt_cwc, 10, 0, 8, 16);
  146. test_wkt<PCWO>(wkt_cwc, 8, 0, 8, 16);
  147. test_wkt<PCWO>(wkt_cwo, wkt_cwc, 8, 0, 8, 16);
  148. test_wkt<PCCWC>(wkt_ccwc, 10, 0, 8, 16);
  149. test_wkt<PCCWO>(wkt_ccwc, 8, 0, 8, 16);
  150. test_wkt<PCCWO>(wkt_ccwo, wkt_ccwc, 8, 0, 8, 16);
  151. }
  152. }
  153. template <typename T>
  154. void test_all()
  155. {
  156. using namespace boost::geometry;
  157. typedef bg::model::point<T, 2, bg::cs::cartesian> P;
  158. test_wkt<P>("POINT(1 2)", 1);
  159. test_wkt<bg::model::linestring<P> >("LINESTRING(1 1,2 2,3 3)", 3, 2 * sqrt(2.0));
  160. test_wkt<bg::model::polygon<P> >("POLYGON((0 0,0 4,4 4,4 0,0 0)"
  161. ",(1 1,1 2,2 2,2 1,1 1),(1 1,1 2,2 2,2 1,1 1))", 15, 0, 18, 24);
  162. // Non OGC: a box defined by a polygon
  163. //test_wkt<box<P> >("POLYGON((0 0,0 1,1 1,1 0,0 0))", 4, 0, 1, 4);
  164. test_wkt<bg::model::ring<P> >("POLYGON((0 0,0 1,1 1,1 0,0 0))", 5, 0, 1, 4);
  165. // We accept empty sequences as well (much better than EMPTY)...
  166. // ...or even POINT() (see below)
  167. test_wkt<bg::model::linestring<P> >("LINESTRING()", 0, 0);
  168. test_wkt<bg::model::polygon<P> >("POLYGON(())", 0);
  169. // ... or even with empty holes
  170. test_wkt<bg::model::polygon<P> >("POLYGON((),(),())", 0);
  171. // which all make no valid geometries, but they can exist.
  172. // These WKT's are incomplete or abnormal but they are considered OK
  173. test_relaxed_wkt<P>("POINT(1)", "POINT(1 0)");
  174. test_relaxed_wkt<P>("POINT()", "POINT(0 0)");
  175. test_relaxed_wkt<bg::model::linestring<P> >("LINESTRING(1,2,3)",
  176. "LINESTRING(1 0,2 0,3 0)");
  177. test_relaxed_wkt<P>("POINT ( 1 2) ", "POINT(1 2)");
  178. test_relaxed_wkt<P>("POINT M ( 1 2)", "POINT(1 2)");
  179. test_relaxed_wkt<bg::model::box<P> >("BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))");
  180. test_relaxed_wkt<bg::model::linestring<P> >("LINESTRING EMPTY", "LINESTRING()");
  181. test_relaxed_wkt<bg::model::polygon<P> >("POLYGON( ( ) , ( ) , ( ) )",
  182. "POLYGON((),(),())");
  183. // Wrong WKT's
  184. test_wrong_wkt<P>("POINT(1 2", "expected ')'");
  185. test_wrong_wkt<P>("POINT 1 2)", "expected '('");
  186. test_wrong_wkt<P>("POINT(1 2,)", "expected ')'");
  187. test_wrong_wkt<P>("POINT(1 2)foo", "too many tokens at 'foo'");
  188. test_wrong_wkt<P>("POINT(1 2 3)", "expected ')'");
  189. test_wrong_wkt<P>("POINT(a 2 3)", "bad lexical cast");
  190. test_wrong_wkt<P>("POINT 2 3", "expected '('");
  191. test_wrong_wkt<P>("POINT Z (1 2 3)", "z only allowed");
  192. test_wrong_wkt<P>("PIONT (1 2)", "should start with 'point'");
  193. test_wrong_wkt<bg::model::linestring<P> >("LINESTRING())", "too many tokens");
  194. test_wrong_wkt<bg::model::polygon<P> >("POLYGON((1 1,1 4,4 4,4 1,1 1)"
  195. ",((2 2,2 3,3 3,3 2,2 2))", "bad lexical cast");
  196. test_wrong_wkt<bg::model::box<P> >("BOX(1 1,2 2,3 3)", "box should have 2");
  197. test_wrong_wkt<bg::model::box<P> >("BOX(1 1,2 2) )", "too many tokens");
  198. if ( BOOST_GEOMETRY_CONDITION(boost::is_floating_point<T>::type::value
  199. || ! boost::is_fundamental<T>::type::value ) )
  200. {
  201. test_wkt<P>("POINT(1.1 2.1)", 1);
  202. }
  203. // Deprecated:
  204. // test_wkt_output_iterator<bg::model::linestring<P> >("LINESTRING(1 1,2 2,3 3)");
  205. // test_wkt_output_iterator<bg::model::ring<P> >("POLYGON((1 1,2 2,3 3))");
  206. test_order_closure<T>();
  207. }
  208. #endif
  209. int test_main(int, char* [])
  210. {
  211. test_all<double>();
  212. test_all<int>();
  213. #if defined(HAVE_TTMATH)
  214. test_all<ttmath_big>();
  215. #endif
  216. return 0;
  217. }
  218. /*
  219. Results can be checked in PostGIS by query below,
  220. or by MySQL (but replace length by glength and remove the perimeter)
  221. Note:
  222. - PostGIS gives "3" for a numpoints of a multi-linestring of 6 points in total (!)
  223. --> "npoints" should be taken for all geometries
  224. - SQL Server 2008 gives "6"
  225. select geometry::STGeomFromText('MULTILINESTRING((1 1,2 2,3 3),(4 4,5 5,6 6))',0).STNumPoints()
  226. - MySQL gives "NULL"
  227. select 1 as code,'np p' as header,npoints(geomfromtext('POINT(1 2)')) as contents
  228. union select 2,'length point', length(geomfromtext('POINT(1 2)'))
  229. union select 3,'peri point', perimeter(geomfromtext('POINT(1 2)'))
  230. union select 4,'area point',area(geomfromtext('POINT(1 2)'))
  231. union select 5,'# ls',npoints(geomfromtext('LINESTRING(1 1,2 2,3 3)'))
  232. union select 6,'length ls',length(geomfromtext('LINESTRING(1 1,2 2,3 3)'))
  233. union select 7,'peri ls',perimeter(geomfromtext('LINESTRING(1 1,2 2,3 3)'))
  234. union select 8,'aera ls',area(geomfromtext('LINESTRING(1 1,2 2,3 3)'))
  235. union select 9,'# poly',npoints(geomfromtext('POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1),(1 1,1 2,2 2,2 1,1 1))'))
  236. union select 10,'length poly',length(geomfromtext('POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1),(1 1,1 2,2 2,2 1,1 1))'))
  237. union select 11,'peri poly',perimeter(geomfromtext('POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1),(1 1,1 2,2 2,2 1,1 1))'))
  238. union select 12,'area poly',area(geomfromtext('POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1),(1 1,1 2,2 2,2 1,1 1))'))
  239. */