projections_combined.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2017, 2018.
  5. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #if defined(_MSC_VER)
  11. #pragma warning( disable : 4305 ) // truncation double -> float
  12. #endif // defined(_MSC_VER)
  13. #define BOOST_GEOMETRY_SRS_ENABLE_STATIC_PROJECTION_HYBRID_INTERFACE
  14. #include <geometry_test_common.hpp>
  15. #include <boost/geometry/srs/projection.hpp>
  16. #include <boost/geometry/core/coordinate_type.hpp>
  17. #include <boost/geometry/algorithms/make.hpp>
  18. #include <boost/geometry.hpp>
  19. #include <boost/geometry/geometries/geometries.hpp>
  20. #include <boost/geometry/geometries/point_xy.hpp>
  21. #include "proj4.hpp"
  22. namespace srs = bg::srs;
  23. template <typename P>
  24. bool check_expected(P const& p1, P const& p2)
  25. {
  26. return bg::math::abs(bg::get<0>(p1) - bg::get<0>(p2)) <= 1.0
  27. && bg::math::abs(bg::get<1>(p1) - bg::get<1>(p2)) <= 1.0;
  28. }
  29. template <typename StaticParams, typename GeoPoint, typename CartPoint>
  30. void test_forward(std::string const& id, GeoPoint const& geo_point, CartPoint const& cart_point,
  31. StaticParams const& params, std::string const& proj4 = "")
  32. {
  33. try
  34. {
  35. srs::projection<StaticParams> prj = params;
  36. CartPoint xy;
  37. prj.forward(geo_point, xy);
  38. bool ok = check_expected(xy, cart_point);
  39. BOOST_CHECK_MESSAGE(ok,
  40. " id: " << id
  41. << " point: " << bg::wkt(xy)
  42. << " different than expected: " << bg::wkt(cart_point));
  43. if (! proj4.empty())
  44. {
  45. srs::projection<> prj2 = srs::proj4(proj4);
  46. CartPoint xy2;
  47. prj2.forward(geo_point, xy2);
  48. bool eq2 = bg::equals(xy, xy2);
  49. BOOST_CHECK_MESSAGE(eq2,
  50. " id: " << id << " result of static: "
  51. << bg::wkt(xy) << " different than dynamic: " << bg::wkt(xy2));
  52. #ifdef TEST_WITH_PROJ4
  53. pj_projection prj3(proj4);
  54. CartPoint xy3;
  55. prj3.forward(geo_point, xy3);
  56. bool eq3 = bg::equals(xy, xy3);
  57. BOOST_CHECK_MESSAGE(eq3,
  58. " id: " << id << " result: "
  59. << bg::wkt(xy) << " different than proj4: " << bg::wkt(xy3));
  60. #endif // TEST_WITH_PROJ4
  61. }
  62. }
  63. catch(bg::projection_exception const& e)
  64. {
  65. std::cout << "Exception in " << id << " : " << e.code() << std::endl;
  66. }
  67. catch(...)
  68. {
  69. std::cout << "Exception (unknown) in " << id << std::endl;
  70. }
  71. }
  72. template <typename StaticParams, typename GeoPoint, typename CartPoint>
  73. void test_forward(std::string const& id, GeoPoint const& geo_point, CartPoint const& cart_point,
  74. std::string const& proj4 = "")
  75. {
  76. test_forward(id, geo_point, cart_point, StaticParams(), proj4);
  77. }
  78. template <typename T>
  79. void test_all()
  80. {
  81. typedef bg::model::point<T, 2, bg::cs::geographic<bg::degree> > geo_point_type;
  82. typedef bg::model::point<T, 2, bg::cs::cartesian> cart;
  83. geo_point_type amsterdam = bg::make<geo_point_type>(4.8925, 52.3731);
  84. geo_point_type utrecht = bg::make<geo_point_type>(5.1213, 52.0907);
  85. geo_point_type anchorage = bg::make<geo_point_type>(-149.90, 61.22);
  86. geo_point_type juneau = bg::make<geo_point_type>(-134.42, 58.30);
  87. geo_point_type auckland = bg::make<geo_point_type>(174.74, -36.84);
  88. geo_point_type wellington = bg::make<geo_point_type>(177.78, -41.29);
  89. geo_point_type aspen = bg::make<geo_point_type>(-106.84, 39.19);
  90. geo_point_type denver = bg::make<geo_point_type>(-104.88, 39.76);
  91. using namespace srs::spar;
  92. // IGH (internally using moll/sinu)
  93. {
  94. typedef parameters<proj_igh, ellps_sphere, units_m> params_t;
  95. std::string sparams = "+proj=igh +ellps=sphere +units=m";
  96. test_forward<params_t>("igh-am", amsterdam, cart(1489299.1509520211, 5776413.4260336142), sparams);
  97. test_forward<params_t>("igh-ut", utrecht, cart(1498750.6627020084, 5747394.3313896423), sparams);
  98. test_forward<params_t>("igh-as", aspen, cart(-11708973.126426676, 4357727.1232166551), sparams);
  99. test_forward<params_t>("igh-de", denver, cart(-11536624.264589204, 4421108.2015589233), sparams);
  100. test_forward<params_t>("igh-au", auckland, cart(18658819.353676274, -4096419.1686476548), sparams);
  101. test_forward<params_t>("igh-we", wellington, cart(18733710.557981707, -4591140.1631184481), sparams);
  102. test_forward<params_t>("igh-an", anchorage, cart(-14275110.630537530, 6648284.9393376000), sparams);
  103. test_forward<params_t>("igh-ju", juneau, cart(-13421076.123140398, 6368936.3597440729), sparams);
  104. }
  105. // Using moll
  106. {
  107. typedef parameters<proj_ob_tran, ellps_wgs84, o_proj<proj_moll>, units_m, o_lat_p<>, o_lon_p<> > params_t;
  108. params_t params = params_t(proj_ob_tran(), ellps_wgs84(), o_proj<proj_moll>(), units_m(), o_lat_p<>(10), o_lon_p<>(90));
  109. std::string sparams = "+proj=ob_tran +ellps=WGS84 +o_proj=moll +units=m +o_lat_p=10 +o_lon_p=90 +o_lon_o=11.50";
  110. test_forward<params_t>("obt-m-am", amsterdam, cart(8688778.3518596273, -3348126.4518623645), params, sparams);
  111. test_forward<params_t>("obt-m-ut", utrecht, cart(8693109.5205437448, -3379708.1134765535), params, sparams);
  112. test_forward<params_t>("obt-m-as", aspen, cart(3691751.3259231807, 2371456.9674431868), params, sparams);
  113. test_forward<params_t>("obt-m-de", denver, cart(3764685.2104777521, 2185616.0182080171), params, sparams);
  114. }
  115. // Using sinu
  116. {
  117. // In Proj4 >= 5.0.0 ob_tran projection doesn't overwrite the ellipsoid's parameters to make sphere in underlying projection
  118. // So in order to use spherical sinu projection WGS84-compatible sphere has to be set manually.
  119. typedef parameters<proj_ob_tran, a<>, es<>, o_proj<proj_sinu>, units_m, o_lat_p<>, o_lon_p<> > params_t;
  120. params_t params = params_t(proj_ob_tran(), a<>(6378137), es<>(0), o_proj<proj_sinu>(), units_m(), o_lat_p<>(10), o_lon_p<>(90));
  121. std::string sparams = "+proj=ob_tran +a=6378137 +es=0 +o_proj=sinu +units=m +o_lat_p=10 +o_lon_p=90 +o_lon_o=11.50";
  122. test_forward<params_t>("obt-s-am", amsterdam, cart(9220221.4221933037, -3059652.3579233172), params, sparams);
  123. test_forward<params_t>("obt-s-ut", utrecht, cart(9216281.0977674071, -3089427.4415689218), params, sparams);
  124. test_forward<params_t>("obt-s-as", aspen, cart(4010672.3356677019, 2150730.9484995930), params, sparams);
  125. test_forward<params_t>("obt-s-de", denver, cart(4103945.8062708224, 1979964.9315176210), params, sparams);
  126. }
  127. }
  128. int test_main(int, char* [])
  129. {
  130. test_all<double>();
  131. return 0;
  132. }