projection_interface_p4.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2017-2018, 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/projection.hpp>
  12. #include "check_geometry.hpp"
  13. int test_main(int, char*[])
  14. {
  15. using namespace boost::geometry;
  16. using namespace boost::geometry::model;
  17. using namespace boost::geometry::srs;
  18. typedef point<double, 2, cs::geographic<degree> > point_ll;
  19. typedef point<double, 2, cs::cartesian> point_xy;
  20. {
  21. point_ll pt_ll(1, 1);
  22. point_ll pt_ll2(0, 0);
  23. point_xy pt_xy(0, 0);
  24. projection<> prj = proj4("+proj=tmerc +ellps=WGS84 +units=m");
  25. prj.forward(pt_ll, pt_xy);
  26. test::check_geometry(pt_xy, "POINT(111308.33561309829 110591.34223734379)", 0.001);
  27. prj.inverse(pt_xy, pt_ll2);
  28. test::check_geometry(pt_ll2, "POINT(1 1)", 0.001);
  29. }
  30. // run-time errors
  31. {
  32. point_ll pt_ll(1, 1);
  33. point_xy pt_xy(0, 0);
  34. BOOST_CHECK_THROW(projection<> prj1((proj4(""))), bg::projection_exception);
  35. BOOST_CHECK_THROW(projection<> prj1((proj4("+proj=abcd"))), bg::projection_exception);
  36. projection<> prj3 = proj4("+proj=bacon +a=6400000");
  37. BOOST_CHECK_THROW(prj3.inverse(pt_xy, pt_ll), bg::projection_exception);
  38. }
  39. {
  40. segment<point_ll> seg_ll;
  41. segment<point_xy> seg_xy;
  42. linestring<point_ll> ls_ll;
  43. linestring<point_xy> ls_xy;
  44. ring<point_ll> ring_ll;
  45. ring<point_xy> ring_xy;
  46. polygon<point_ll> poly_ll;
  47. polygon<point_xy> poly_xy;
  48. multi_point<point_ll> mpt_ll;
  49. multi_point<point_xy> mpt_xy;
  50. multi_linestring<linestring<point_ll> > mls_ll;
  51. multi_linestring<linestring<point_xy> > mls_xy;
  52. multi_polygon<polygon<point_ll> > mpoly_ll;
  53. multi_polygon<polygon<point_xy> > mpoly_xy;
  54. bg::read_wkt("LINESTRING(0 0, 1 1)", seg_ll);
  55. bg::read_wkt("LINESTRING(0 0, 1 1, 2 2)", ls_ll);
  56. bg::read_wkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", ring_ll);
  57. bg::read_wkt("POLYGON((0 0, 0 3, 3 3, 3 0, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))", poly_ll);
  58. bg::read_wkt("MULTIPOINT(0 0, 1 1, 2 2)", mpt_ll);
  59. bg::read_wkt("MULTILINESTRING((0 0, 1 1),(2 2, 3 3))", mls_ll);
  60. bg::read_wkt("MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1)),((3 3,3 4,4 4,4 3,3 3)))", mpoly_ll);
  61. projection<> prj = proj4("+proj=tmerc +ellps=WGS84 +units=m");
  62. prj.forward(seg_ll, seg_xy);
  63. prj.forward(ls_ll, ls_xy);
  64. prj.forward(ring_ll, ring_xy);
  65. prj.forward(poly_ll, poly_xy);
  66. prj.forward(mpt_ll, mpt_xy);
  67. prj.forward(mls_ll, mls_xy);
  68. prj.forward(mpoly_ll, mpoly_xy);
  69. test::check_geometry(seg_xy, "LINESTRING(0 0,111308 110591)", 0.001);
  70. test::check_geometry(ls_xy, "LINESTRING(0 0,111308 110591,222550 221285)", 0.001);
  71. test::check_geometry(ring_xy, "POLYGON((0 0,0 110574,111308 110591,111325 0,0 0))", 0.001);
  72. test::check_geometry(poly_xy, "POLYGON((0 0,0 331726,333657 332183,334112 0,0 0),(111308 110591,222651 110642,222550 221285,111258 221183,111308 110591))", 0.001);
  73. test::check_geometry(mpt_xy, "MULTIPOINT((0 0),(111308 110591),(222550 221285))", 0.001);
  74. test::check_geometry(mls_xy, "MULTILINESTRING((0 0,111308 110591),(222550 221285,333657 332183))", 0.001);
  75. test::check_geometry(mpoly_xy, "MULTIPOLYGON(((0 0,0 331726,333657 332183,334112 0,0 0),(111308 110591,222651 110642,222550 221285,111258 221183,111308 110591)),((333657 332183,333302 442913,444561 443388,445034 332540,333657 332183)))", 0.001);
  76. bg::clear(seg_ll);
  77. bg::clear(ls_ll);
  78. bg::clear(ring_ll);
  79. bg::clear(poly_ll);
  80. bg::clear(mpt_ll);
  81. bg::clear(mls_ll);
  82. bg::clear(mpoly_ll);
  83. prj.inverse(seg_xy, seg_ll);
  84. prj.inverse(ls_xy, ls_ll);
  85. prj.inverse(ring_xy, ring_ll);
  86. prj.inverse(poly_xy, poly_ll);
  87. prj.inverse(mpt_xy, mpt_ll);
  88. prj.inverse(mls_xy, mls_ll);
  89. prj.inverse(mpoly_xy, mpoly_ll);
  90. test::check_geometry(seg_ll, "LINESTRING(0 0, 1 1)", 0.001);
  91. test::check_geometry(ls_ll, "LINESTRING(0 0, 1 1, 2 2)", 0.001);
  92. test::check_geometry(ring_ll, "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", 0.001);
  93. test::check_geometry(poly_ll, "POLYGON((0 0, 0 3, 3 3, 3 0, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))", 0.001);
  94. test::check_geometry(mpt_ll, "MULTIPOINT(0 0, 1 1, 2 2)", 0.001);
  95. test::check_geometry(mls_ll, "MULTILINESTRING((0 0, 1 1),(2 2, 3 3))", 0.001);
  96. test::check_geometry(mpoly_ll, "MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1)),((3 3,3 4,4 4,4 3,3 3)))", 0.001);
  97. }
  98. return 0;
  99. }