summaryrefslogtreecommitdiff
path: root/boost/geometry/srs/projections/proj/krovak.hpp
blob: 09c24772ed2fe871d6cfc1c7a5a7edfaec3140cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef BOOST_GEOMETRY_PROJECTIONS_KROVAK_HPP
#define BOOST_GEOMETRY_PROJECTIONS_KROVAK_HPP

// Boost.Geometry - extensions-gis-projections (based on PROJ4)
// This file is automatically generated. DO NOT EDIT.

// Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.

// This file was modified by Oracle on 2017.
// Modifications copyright (c) 2017, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// This file is converted from PROJ4, http://trac.osgeo.org/proj
// PROJ4 is originally written by Gerald Evenden (then of the USGS)
// PROJ4 is maintained by Frank Warmerdam
// PROJ4 is converted to Boost.Geometry by Barend Gehrels

// Last updated version of proj: 4.9.1

// Original copyright notice:

// Purpose:  Implementation of the krovak (Krovak) projection.
// Definition: http://www.ihsenergy.com/epsg/guid7.html#1.4.3
// Author:   Thomas Flemming, tf@ttqv.com
// Copyright (c) 2001, Thomas Flemming, tf@ttqv.com

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#include <boost/geometry/srs/projections/impl/base_static.hpp>
#include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
#include <boost/geometry/srs/projections/impl/projects.hpp>
#include <boost/geometry/srs/projections/impl/factory_entry.hpp>

namespace boost { namespace geometry
{

namespace srs { namespace par4
{
    struct krovak {};

}} //namespace srs::par4

namespace projections
{
    #ifndef DOXYGEN_NO_DETAIL
    namespace detail { namespace krovak
    {
            template <typename T>
            struct par_krovak
            {
                T    C_x;
            };

            /**
               NOTES: According to EPSG the full Krovak projection method should have
                      the following parameters.  Within PROJ.4 the azimuth, and pseudo
                      standard parallel are hardcoded in the algorithm and can't be
                      altered from outside.  The others all have defaults to match the
                      common usage with Krovak projection.

              lat_0 = latitude of centre of the projection

              lon_0 = longitude of centre of the projection

              ** = azimuth (true) of the centre line passing through the centre of the projection

              ** = latitude of pseudo standard parallel

              k  = scale factor on the pseudo standard parallel

              x_0 = False Easting of the centre of the projection at the apex of the cone

              y_0 = False Northing of the centre of the projection at the apex of the cone

             **/

            // template class, using CRTP to implement forward/inverse
            template <typename CalculationType, typename Parameters>
            struct base_krovak_ellipsoid : public base_t_fi<base_krovak_ellipsoid<CalculationType, Parameters>,
                     CalculationType, Parameters>
            {

                typedef CalculationType geographic_type;
                typedef CalculationType cartesian_type;

                par_krovak<CalculationType> m_proj_parm;

                inline base_krovak_ellipsoid(const Parameters& par)
                    : base_t_fi<base_krovak_ellipsoid<CalculationType, Parameters>,
                     CalculationType, Parameters>(*this, par) {}

                // FORWARD(e_forward)  ellipsoid
                // Project coordinates from geographic (lon, lat) to cartesian (x, y)
                inline void fwd(geographic_type& lp_lon, geographic_type& lp_lat, cartesian_type& xy_x, cartesian_type& xy_y) const
                {
                /* calculate xy from lat/lon */

                /* Constants, identical to inverse transform function */
                    CalculationType s45, s90, e2, e, alfa, uq, u0, g, k, k1, n0, ro0, ad, a, s0, n;
                    CalculationType gfi, u, fi0, deltav, s, d, eps, ro;


                    s45 = 0.785398163397448;    /* 45 DEG */
                    s90 = 2 * s45;
                    fi0 = this->m_par.phi0;    /* Latitude of projection centre 49 DEG 30' */

                   /* Ellipsoid is used as Parameter in for.c and inv.c, therefore a must
                      be set to 1 here.
                      Ellipsoid Bessel 1841 a = 6377397.155m 1/f = 299.1528128,
                      e2=0.006674372230614;
                   */
                    a =  1; /* 6377397.155; */
                    /* e2 = this->m_par.es;*/       /* 0.006674372230614; */
                    e2 = 0.006674372230614;
                    e = sqrt(e2);

                    alfa = sqrt(1. + (e2 * pow(cos(fi0), 4)) / (1. - e2));

                    uq = 1.04216856380474;      /* DU(2, 59, 42, 42.69689) */
                    u0 = asin(sin(fi0) / alfa);
                    g = pow(   (1. + e * sin(fi0)) / (1. - e * sin(fi0)) , alfa * e / 2.  );

                    k = tan( u0 / 2. + s45) / pow  (tan(fi0 / 2. + s45) , alfa) * g;

                    k1 = this->m_par.k0;
                    n0 = a * sqrt(1. - e2) / (1. - e2 * pow(sin(fi0), 2));
                    s0 = 1.37008346281555;       /* Latitude of pseudo standard parallel 78 DEG 30'00" N */
                    n = sin(s0);
                    ro0 = k1 * n0 / tan(s0);
                    ad = s90 - uq;

                /* Transformation */

                    gfi =pow ( ((1. + e * sin(lp_lat)) /
                               (1. - e * sin(lp_lat))) , (alfa * e / 2.));

                    u= 2. * (atan(k * pow( tan(lp_lat / 2. + s45), alfa) / gfi)-s45);

                    deltav = - lp_lon * alfa;

                    s = asin(cos(ad) * sin(u) + sin(ad) * cos(u) * cos(deltav));
                    d = asin(cos(u) * sin(deltav) / cos(s));
                    eps = n * d;
                    ro = ro0 * pow(tan(s0 / 2. + s45) , n) / pow(tan(s / 2. + s45) , n)   ;

                   /* x and y are reverted! */
                    xy_y = ro * cos(eps) / a;
                    xy_x = ro * sin(eps) / a;

                        if( !pj_param(this->m_par.params, "tczech").i )
                      {
                        xy_y *= -1.0;
                        xy_x *= -1.0;
                      }
                }

                // INVERSE(e_inverse)  ellipsoid
                // Project coordinates from cartesian (x, y) to geographic (lon, lat)
                inline void inv(cartesian_type& xy_x, cartesian_type& xy_y, geographic_type& lp_lon, geographic_type& lp_lat) const
                {
                    /* calculate lat/lon from xy */

                /* Constants, identisch wie in der Umkehrfunktion */
                    CalculationType s45, s90, fi0, e2, e, alfa, uq, u0, g, k, k1, n0, ro0, ad, a, s0, n;
                    CalculationType u, deltav, s, d, eps, ro, fi1, xy0;
                    int ok;

                    s45 = 0.785398163397448;    /* 45 DEG */
                    s90 = 2 * s45;
                    fi0 = this->m_par.phi0;    /* Latitude of projection centre 49 DEG 30' */


                   /* Ellipsoid is used as Parameter in for.c and inv.c, therefore a must
                      be set to 1 here.
                      Ellipsoid Bessel 1841 a = 6377397.155m 1/f = 299.1528128,
                      e2=0.006674372230614;
                   */
                    a = 1; /* 6377397.155; */
                    /* e2 = this->m_par.es; */      /* 0.006674372230614; */
                    e2 = 0.006674372230614;
                    e = sqrt(e2);

                    alfa = sqrt(1. + (e2 * pow(cos(fi0), 4)) / (1. - e2));
                    uq = 1.04216856380474;      /* DU(2, 59, 42, 42.69689) */
                    u0 = asin(sin(fi0) / alfa);
                    g = pow(   (1. + e * sin(fi0)) / (1. - e * sin(fi0)) , alfa * e / 2.  );

                    k = tan( u0 / 2. + s45) / pow  (tan(fi0 / 2. + s45) , alfa) * g;

                    k1 = this->m_par.k0;
                    n0 = a * sqrt(1. - e2) / (1. - e2 * pow(sin(fi0), 2));
                    s0 = 1.37008346281555;       /* Latitude of pseudo standard parallel 78 DEG 30'00" N */
                    n = sin(s0);
                    ro0 = k1 * n0 / tan(s0);
                    ad = s90 - uq;


                /* Transformation */
                   /* revert y, x*/
                    xy0=xy_x;
                    xy_x=xy_y;
                    xy_y=xy0;

                        if( !pj_param(this->m_par.params, "tczech").i )
                      {
                        xy_x *= -1.0;
                        xy_y *= -1.0;
                      }

                    ro = sqrt(xy_x * xy_x + xy_y * xy_y);
                    eps = atan2(xy_y, xy_x);
                    d = eps / sin(s0);
                    s = 2. * (atan(  pow(ro0 / ro, 1. / n) * tan(s0 / 2. + s45)) - s45);

                    u = asin(cos(ad) * sin(s) - sin(ad) * cos(s) * cos(d));
                    deltav = asin(cos(s) * sin(d) / cos(u));

                    lp_lon = this->m_par.lam0 - deltav / alfa;

                /* ITERATION FOR lp_lat */
                   fi1 = u;

                   ok = 0;
                   do
                   {
                       lp_lat = 2. * ( atan( pow( k, -1. / alfa)  *
                                            pow( tan(u / 2. + s45) , 1. / alfa)  *
                                            pow( (1. + e * sin(fi1)) / (1. - e * sin(fi1)) , e / 2.)
                                           )  - s45);

                      if (fabs(fi1 - lp_lat) < 0.000000000000001) ok=1;
                      fi1 = lp_lat;

                   }
                   while (ok==0);

                   lp_lon -= this->m_par.lam0;
                }

                static inline std::string get_name()
                {
                    return "krovak_ellipsoid";
                }

            };

            // Krovak
            template <typename Parameters, typename T>
            inline void setup_krovak(Parameters& par, par_krovak<T>& proj_parm)
            {
                T ts;
                /* read some Parameters,
                 * here Latitude Truescale */

                ts = pj_param(par.params, "rlat_ts").f;
                proj_parm.C_x = ts;

                /* we want Bessel as fixed ellipsoid */
                par.a = 6377397.155;
                par.e = sqrt(par.es = 0.006674372230614);

                    /* if latitude of projection center is not set, use 49d30'N */
                if (!pj_param(par.params, "tlat_0").i)
                        par.phi0 = 0.863937979737193;

                    /* if center long is not set use 42d30'E of Ferro - 17d40' for Ferro */
                    /* that will correspond to using longitudes relative to greenwich    */
                    /* as input and output, instead of lat/long relative to Ferro */
                if (!pj_param(par.params, "tlon_0").i)
                        par.lam0 = 0.7417649320975901 - 0.308341501185665;

                    /* if scale not set default to 0.9999 */
                if (!pj_param(par.params, "tk").i)
                        par.k0 = 0.9999;

                /* always the same */
            }

    }} // namespace detail::krovak
    #endif // doxygen

    /*!
        \brief Krovak projection
        \ingroup projections
        \tparam Geographic latlong point type
        \tparam Cartesian xy point type
        \tparam Parameters parameter type
        \par Projection characteristics
         - Pseudocylindrical
         - Ellipsoid
        \par Projection parameters
         - lat_ts: Latitude of true scale (degrees)
         - lat_0: Latitude of origin
         - lon_0: Central meridian
         - k: Scale factor on the pseudo standard parallel
        \par Example
        \image html ex_krovak.gif
    */
    template <typename CalculationType, typename Parameters>
    struct krovak_ellipsoid : public detail::krovak::base_krovak_ellipsoid<CalculationType, Parameters>
    {
        inline krovak_ellipsoid(const Parameters& par) : detail::krovak::base_krovak_ellipsoid<CalculationType, Parameters>(par)
        {
            detail::krovak::setup_krovak(this->m_par, this->m_proj_parm);
        }
    };

    #ifndef DOXYGEN_NO_DETAIL
    namespace detail
    {

        // Static projection
        BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::krovak, krovak_ellipsoid, krovak_ellipsoid)

        // Factory entry(s)
        template <typename CalculationType, typename Parameters>
        class krovak_entry : public detail::factory_entry<CalculationType, Parameters>
        {
            public :
                virtual base_v<CalculationType, Parameters>* create_new(const Parameters& par) const
                {
                    return new base_v_fi<krovak_ellipsoid<CalculationType, Parameters>, CalculationType, Parameters>(par);
                }
        };

        template <typename CalculationType, typename Parameters>
        inline void krovak_init(detail::base_factory<CalculationType, Parameters>& factory)
        {
            factory.add_to_factory("krovak", new krovak_entry<CalculationType, Parameters>);
        }

    } // namespace detail
    #endif // doxygen

} // namespace projections

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_PROJECTIONS_KROVAK_HPP