transformation_interface.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2017, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <geometry_test_common.hpp>
  9. #include <boost/geometry.hpp>
  10. #include <boost/geometry/geometries/geometries.hpp>
  11. #include <boost/geometry/srs/transformation.hpp>
  12. #include "check_geometry.hpp"
  13. //#include <proj_api.h>
  14. template <typename T>
  15. void test_geometries()
  16. {
  17. using namespace boost::geometry;
  18. using namespace boost::geometry::model;
  19. using namespace boost::geometry::srs;
  20. typedef model::point<T, 2, cs::cartesian> point;
  21. typedef model::segment<point> segment;
  22. typedef model::linestring<point> linestring;
  23. typedef model::ring<point> ring;
  24. typedef model::polygon<point> polygon;
  25. typedef model::multi_point<point> mpoint;
  26. typedef model::multi_linestring<linestring> mlinestring;
  27. typedef model::multi_polygon<polygon> mpolygon;
  28. std::cout << std::setprecision(12);
  29. double d2r = math::d2r<T>();
  30. //double r2d = math::r2d<T>();
  31. std::string from = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
  32. std::string to = "+proj=longlat +ellps=airy +datum=OSGB36 +no_defs";
  33. {
  34. point pt(18.5 * d2r, 54.2 * d2r);
  35. point pt2(0, 0);
  36. segment seg(pt, pt);
  37. segment seg2;
  38. linestring ls; ls.push_back(pt);
  39. linestring ls2;
  40. ring rg; rg.push_back(pt);
  41. ring rg2;
  42. polygon poly; poly.outer() = rg;
  43. polygon poly2;
  44. mpoint mpt; mpt.push_back(pt);
  45. mpoint mpt2;
  46. mlinestring mls; mls.push_back(ls);
  47. mlinestring mls2;
  48. mpolygon mpoly; mpoly.push_back(poly);
  49. mpolygon mpoly2;
  50. transformation<> tr((proj4(from)), (proj4(to)));
  51. //transformation<> tr((epsg(4326)), (epsg(25832)));
  52. tr.forward(pt, pt2);
  53. tr.forward(seg, seg2);
  54. tr.forward(ls, ls2);
  55. tr.forward(rg, rg2);
  56. tr.forward(poly, poly2);
  57. tr.forward(mpt, mpt2);
  58. tr.forward(mls, mls2);
  59. tr.forward(mpoly, mpoly2);
  60. test::check_geometry(pt2, "POINT(0.322952937968 0.9459567165)", 0.001);
  61. test::check_geometry(seg2, "LINESTRING(0.322952937968 0.9459567165,0.322952937968 0.9459567165)", 0.001);
  62. test::check_geometry(ls2, "LINESTRING(0.322952937968 0.9459567165)", 0.001);
  63. test::check_geometry(rg2, "POLYGON((0.322952937968 0.9459567165))", 0.001);
  64. test::check_geometry(poly2, "POLYGON((0.322952937968 0.9459567165))", 0.001);
  65. test::check_geometry(mpt2, "MULTIPOINT((0.322952937968 0.9459567165))", 0.001);
  66. test::check_geometry(mls2, "MULTILINESTRING((0.322952937968 0.9459567165))", 0.001);
  67. test::check_geometry(mpoly2, "MULTIPOLYGON(((0.322952937968 0.9459567165)))", 0.001);
  68. tr.inverse(pt2, pt);
  69. tr.inverse(seg2, seg);
  70. tr.inverse(ls2, ls);
  71. tr.inverse(rg2, rg);
  72. tr.inverse(poly2, poly);
  73. tr.inverse(mpt2, mpt);
  74. tr.inverse(mls2, mls);
  75. tr.inverse(mpoly2, mpoly);
  76. test::check_geometry(pt, "POINT(0.322885911738 0.945968454552)", 0.001);
  77. test::check_geometry(seg, "LINESTRING(0.322885911738 0.945968454552,0.322885911738 0.945968454552)", 0.001);
  78. test::check_geometry(ls, "LINESTRING(0.322885911738 0.945968454552)", 0.001);
  79. test::check_geometry(rg, "POLYGON((0.322885911738 0.945968454552))", 0.001);
  80. test::check_geometry(poly, "POLYGON((0.322885911738 0.945968454552))", 0.001);
  81. test::check_geometry(mpt, "MULTIPOINT((0.322885911738 0.945968454552))", 0.001);
  82. test::check_geometry(mls, "MULTILINESTRING((0.322885911738 0.945968454552))", 0.001);
  83. test::check_geometry(mpoly, "MULTIPOLYGON(((0.322885911738 0.945968454552)))", 0.001);
  84. }
  85. /*{
  86. projPJ pj_from, pj_to;
  87. pj_from = pj_init_plus(from.c_str());
  88. pj_to = pj_init_plus(to.c_str());
  89. double x = get<0>(pt_xy);
  90. double y = get<1>(pt_xy);
  91. pj_transform(pj_from, pj_to, 1, 0, &x, &y, NULL );
  92. std::cout << x * r2d << " " << y * r2d << std::endl;
  93. }*/
  94. }
  95. template <typename P1, typename P2, typename Tr>
  96. inline void test_combination(Tr const& tr, P1 const& pt,
  97. std::string const& expected_fwd,
  98. P1 const& expected_inv)
  99. {
  100. using namespace boost::geometry;
  101. P2 pt2;
  102. tr.forward(pt, pt2);
  103. test::check_geometry(pt2, expected_fwd, 0.001);
  104. P1 pt1;
  105. tr.inverse(pt2, pt1);
  106. test::check_geometry(pt1, expected_inv, 0.001);
  107. }
  108. void test_combinations(std::string const& from, std::string const& to,
  109. std::string const& in_deg,
  110. std::string const& expected_deg,
  111. std::string const& expected_rad,
  112. std::string const& expected_inv_deg)
  113. {
  114. using namespace boost::geometry;
  115. using namespace boost::geometry::model;
  116. using namespace boost::geometry::srs;
  117. typedef model::point<double, 2, cs::cartesian> xy;
  118. typedef model::point<double, 2, cs::geographic<degree> > ll_d;
  119. typedef model::point<double, 2, cs::geographic<radian> > ll_r;
  120. //typedef model::point<double, 3, cs::cartesian> xyz;
  121. //typedef model::point<double, 3, cs::geographic<degree> > llz_d;
  122. //typedef model::point<double, 3, cs::geographic<radian> > llz_r;
  123. transformation<> tr((proj4(from)), (proj4(to)));
  124. ll_d d;
  125. bg::read_wkt(in_deg, d);
  126. ll_r r(bg::get_as_radian<0>(d), bg::get_as_radian<1>(d));
  127. xy c(bg::get<0>(r), bg::get<1>(r));
  128. ll_d inv_d;
  129. bg::read_wkt(expected_inv_deg, inv_d);
  130. ll_r inv_r(bg::get_as_radian<0>(inv_d), bg::get_as_radian<1>(inv_d));
  131. xy inv_c(bg::get<0>(inv_r), bg::get<1>(inv_r));
  132. test_combination<xy, xy>(tr, c, expected_rad, inv_c);
  133. test_combination<xy, ll_r>(tr, c, expected_rad, inv_c);
  134. test_combination<xy, ll_d>(tr, c, expected_deg, inv_c);
  135. test_combination<ll_r, xy>(tr, r, expected_rad, inv_r);
  136. test_combination<ll_r, ll_r>(tr, r, expected_rad, inv_r);
  137. test_combination<ll_r, ll_d>(tr, r, expected_deg, inv_r);
  138. test_combination<ll_d, xy>(tr, d, expected_rad, inv_d);
  139. test_combination<ll_d, ll_r>(tr, d, expected_rad, inv_d);
  140. test_combination<ll_d, ll_d>(tr, d, expected_deg, inv_d);
  141. }
  142. int test_main(int, char*[])
  143. {
  144. test_geometries<double>();
  145. test_geometries<float>();
  146. test_combinations("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
  147. "+proj=longlat +ellps=airy +datum=OSGB36 +no_defs",
  148. "POINT(18.5 54.2)",
  149. "POINT(18.5038403269 54.1993274575)",
  150. "POINT(0.322952937968 0.9459567165)",
  151. "POINT(18.5 54.2)");
  152. test_combinations("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
  153. "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +no_defs",
  154. "POINT(18.5 54.2)",
  155. "POINT(2059410.57968 7208125.2609)",
  156. "POINT(2059410.57968 7208125.2609)",
  157. "POINT(18.5 54.2)");
  158. test_combinations("+proj=longlat +ellps=clrk80 +units=m +no_defs",
  159. "+proj=tmerc +lat_0=0 +lon_0=-62 +k=0.9995000000000001 +x_0=400000 +y_0=0 +ellps=clrk80 +units=m +no_defs",
  160. "POINT(1 1)",
  161. "POINT(9413505.3284665551 237337.74515944949)",
  162. "POINT(9413505.3284665551 237337.74515944949)",
  163. // this result seems to be wrong, it's the same with projection
  164. "POINT(-2.4463131191981073 1.5066638962045082)");
  165. return 0;
  166. }