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