/
proj.cpp
325 lines (266 loc) · 9.85 KB
/
proj.cpp
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
#include <iostream>
#include <fstream>
//#include "../../proj/trunk/src/proj_api.h"
#include "../../proj/trunk/src/projects.h"
//#include "../../proj/trunk/src/emess.h"
#include "../../proj/trunk/src/proj_config.h"
using namespace std;
/************************************************************************/
/* pj_geodetic_to_geocentric() */
/************************************************************************/
int pj_geodetic_to_geocentric2( double a, double es,
double &x, double &y, double &z )
{
return -1;
}
int pj_transform1( PJ *srcdefn, PJ *dstdefn, double &x, double & y, double & z )
{
long i;
int need_datum_shift;
pj_errno = 0;
/* -------------------------------------------------------------------- */
/* Transform geocentric source coordinates to lat/long. */
/* -------------------------------------------------------------------- */
if( srcdefn->is_geocent )
{
if( z == 0 )
{
pj_errno = PJD_ERR_GEOCENTRIC;
return PJD_ERR_GEOCENTRIC;
}
if( srcdefn->to_meter != 1.0 )
{
if( x != HUGE_VAL )
{
x *= srcdefn->to_meter;
y *= srcdefn->to_meter;
}
}
// if( pj_geocentric_to_geodetic( srcdefn->a_orig, srcdefn->es_orig, point_count, point_offset, x, y, z ) != 0)
// return pj_errno;
}
/* -------------------------------------------------------------------- */
/* Transform source points to lat/long, if they aren't */
/* already. */
/* -------------------------------------------------------------------- */
else if( !srcdefn->is_latlong )
{
if( srcdefn->inv == NULL )
{
pj_errno = -17; /* this isn't correct, we need a no inverse err */
if( getenv( "PROJ_DEBUG" ) != NULL )
{
fprintf( stderr,
"pj_transform(): source projection not invertable\n" );
}
return pj_errno;
}
//for( i = 0; i < point_count; i++ )
{
XY projected_loc;
LP geodetic_loc;
projected_loc.u = 0.0;
projected_loc.v = 0.0;
projected_loc.u = x;
projected_loc.v = y;
if( projected_loc.u == HUGE_VAL )
return -1;
// continue;
geodetic_loc = pj_inv( projected_loc, srcdefn );
if( pj_errno != 0 )
{
if( (pj_errno != 33 /*EDOM*/ && pj_errno != 34 /*ERANGE*/ )
&& (pj_errno > 0 || pj_errno < -44
//|| transient_error[-pj_errno] == 0
) )
return pj_errno;
else
{
geodetic_loc.u = HUGE_VAL;
geodetic_loc.v = HUGE_VAL;
}
}
x = geodetic_loc.u;
y = geodetic_loc.v;
}
}
/* -------------------------------------------------------------------- */
/* But if they are already lat long, adjust for the prime */
/* meridian if there is one in effect. */
/* -------------------------------------------------------------------- */
if( srcdefn->from_greenwich != 0.0 )
{
// for( i = 0; i < point_count; i++ )
{
if( x != HUGE_VAL )
x += srcdefn->from_greenwich;
}
}
/* -------------------------------------------------------------------- */
/* Convert datums if needed, and possible. */
/* -------------------------------------------------------------------- */
// if( pj_datum_transform( srcdefn, dstdefn, point_count, point_offset,
// x, y, z ) != 0 )
// return pj_errno;
/* -------------------------------------------------------------------- */
/* But if they are staying lat long, adjust for the prime */
/* meridian if there is one in effect. */
/* -------------------------------------------------------------------- */
if( dstdefn->from_greenwich != 0.0 )
{
// for( i = 0; i < point_count; i++ )
{
if( x != HUGE_VAL )
x -= dstdefn->from_greenwich;
}
}
/* -------------------------------------------------------------------- */
/* Transform destination latlong to geocentric if required. */
/* -------------------------------------------------------------------- */
if( dstdefn->is_geocent )
{
if( z == 0 )
{
pj_errno = PJD_ERR_GEOCENTRIC;
return PJD_ERR_GEOCENTRIC;
}
// pj_geodetic_to_geocentric( dstdefn->a_orig, dstdefn->es_orig,
// point_count, point_offset, x, y, z );
if( dstdefn->fr_meter != 1.0 )
{
// for( i = 0; i < point_count; i++ )
{
if( x != HUGE_VAL )
{
x *= dstdefn->fr_meter;
y *= dstdefn->fr_meter;
}
}
}
}
/* -------------------------------------------------------------------- */
/* Transform destination points to projection coordinates, if */
/* desired. */
/* -------------------------------------------------------------------- */
else if( !dstdefn->is_latlong )
{
// for( i = 0; i < point_count; i++ )
{
XY projected_loc;
LP geodetic_loc;
geodetic_loc.u = x;
geodetic_loc.v = y;
if( geodetic_loc.u == HUGE_VAL )
return -1;
// continue;
projected_loc = pj_fwd( geodetic_loc, dstdefn );
if( pj_errno != 0 )
{
if( (pj_errno != 33 /*EDOM*/ && pj_errno != 34 /*ERANGE*/ )
&& (pj_errno > 0 || pj_errno < -44 //|| //point_count == 1
//|| transient_error[-pj_errno] == 0
) )
return pj_errno;
else
{
projected_loc.u = HUGE_VAL;
projected_loc.v = HUGE_VAL;
}
}
x = projected_loc.u;
y = projected_loc.v;
}
}
/* -------------------------------------------------------------------- */
/* If a wrapping center other than 0 is provided, rewrap around */
/* the suggested center (for latlong coordinate systems only). */
/* -------------------------------------------------------------------- */
else if( dstdefn->is_latlong && dstdefn->long_wrap_center != 0 )
{
// for( i = 0; i < point_count; i++ )
{
if( x == HUGE_VAL )
return -1;
// continue;
while( x < dstdefn->long_wrap_center - HALFPI )
x += PI;
while( x > dstdefn->long_wrap_center + HALFPI )
x -= PI;
}
}
return 0;
}
void convertPoint(double x, double y, double & rx, double & ry)
{
projPJ fromProj, toProj;
//+proj=utm +south +ellps=intl +zone=24K +units=m
// #s2cs +proj=utm +south +ellps=intl +zone=24K +units=m +proj=tmerc
if (!(fromProj = pj_init_plus("+proj=utm +south +ellps=intl +zone=24K +units=m -f \"%.7f\" ")) )
exit(1);
// if (!(pj_merc = pj_init_plus("+proj=merc +ellps=clrk66 +lat_ts=33")) )
// exit(1);
// if (!(pj_latlong = pj_init_plus("+proj=latlong +ellps=clrk66")) )
// exit(1);
if (!(toProj = pj_latlong_from_proj( fromProj )))
exit(1);
int islatlon= pj_is_latlong( fromProj );
int isgeocent=pj_is_geocent( fromProj );
char * name = pj_get_def( fromProj , 0);
std::cerr << "FROm NAME:"<< name << "|" << islatlon << "|" << isgeocent << std::endl;
islatlon= pj_is_latlong( toProj );
isgeocent=pj_is_geocent( toProj );
name = pj_get_def( toProj , 0);
std::cerr << "TO NAME:"<< name << "|" << islatlon << "|" << isgeocent << std::endl;
// while (scanf("%lf %lf", &x, &y) == 2) {
// rx = x * DEG_TO_RAD;
// ry = y * DEG_TO_RAD;
// int p = pj_transform(pj_latlong, pj_merc, 1, 1, &rx, &ry, NULL );
// cerr <<
//int pj_transform( projPJ srcdefn, projPJ dstdefn, long point_count, int point_offset, double *x, double *y, double *z );
//printf("%.2f\t%.2f\n", x, y);
// }
// exit(0);
// double ax[1], ay[1], az[1];
std::cerr.precision(10);
std::cerr.width(10);
std::cerr << x << "|" << y << std::endl;
// const double D2R =DEG_TO_RAD;
double ax = x;// * DEG_TO_RAD;
double ay = y;// * DEG_TO_RAD ;
double az = 0;
/** end of "caution" section. */
std::cerr.precision(10);
std::cerr.width(10);
std::cerr << ax << "|" << ay << "|" << std::endl;
int ret=pj_transform1(fromProj, toProj, ax, ay, az);
std::cerr.precision(10);
std::cerr.width(10);
std::cerr << ax << "|" << ay << "|" << ret << "|" << std::endl;
ax *= RAD_TO_DEG;
ay *= RAD_TO_DEG;
std::cerr << ax << "|" << ay << "|" << ret << "|" << std::endl;
//printf("%.4f\t%.4f -> %.4f\t%.4f\n", *lat, *lon, y[0], x[0]);
rx = ay;
ry = ax;
pj_free(fromProj);
pj_free(toProj);
}
int main ()
{
//cs2cs +proj=utm +south +ellps=intl +zone=24K +units=m -f %.7f
// std::fstream cs2cs( "cs2cs +proj=utm +south +ellps=intl +zone=24K +units=m -f %.7f |");
while(std::cin)
{
double a;
double b;
double lat;
double lon;
double h=0;
std::cin >> a;
std::cin >> b;
convertPoint(a, b, lat, lon) ;
std::cout << lat << ":" << lon << std::endl;
lat *= DEG_TO_RAD;
lon *= DEG_TO_RAD;
}
}