projects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /******************************************************************************
  2. * $Id: projects.h 2356 2013-06-25 01:02:23Z warmerdam $
  3. *
  4. * Project: PROJ.4
  5. * Purpose: Primary (private) include file for PROJ.4 library.
  6. * Author: Gerald Evenden
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2000, Frank Warmerdam
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. *****************************************************************************/
  29. /* General projections header file */
  30. #ifndef PROJECTS_H
  31. #define PROJECTS_H
  32. #ifdef _MSC_VER
  33. # ifndef _CRT_SECURE_NO_DEPRECATE
  34. # define _CRT_SECURE_NO_DEPRECATE
  35. # endif
  36. # ifndef _CRT_NONSTDC_NO_DEPRECATE
  37. # define _CRT_NONSTDC_NO_DEPRECATE
  38. # endif
  39. #endif
  40. /* standard inclusions */
  41. #include <math.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #ifdef __cplusplus
  46. #define C_NAMESPACE extern "C"
  47. #define C_NAMESPACE_VAR extern "C"
  48. extern "C" {
  49. #else
  50. #define C_NAMESPACE extern
  51. #define C_NAMESPACE_VAR
  52. #endif
  53. #ifndef NULL
  54. # define NULL 0
  55. #endif
  56. #ifndef FALSE
  57. # define FALSE 0
  58. #endif
  59. #ifndef TRUE
  60. # define TRUE 1
  61. #endif
  62. #ifndef MAX
  63. # define MIN(a,b) ((a<b) ? a : b)
  64. # define MAX(a,b) ((a>b) ? a : b)
  65. #endif
  66. #ifndef ABS
  67. # define ABS(x) ((x<0) ? (-1*(x)) : x)
  68. #endif
  69. /* maximum path/filename */
  70. #ifndef MAX_PATH_FILENAME
  71. #define MAX_PATH_FILENAME 1024
  72. #endif
  73. /* prototype hypot for systems where absent */
  74. #ifndef _WIN32
  75. extern double hypot(double, double);
  76. #endif
  77. #ifdef _WIN32_WCE
  78. # include <wce_stdlib.h>
  79. # include <wce_stdio.h>
  80. # define rewind wceex_rewind
  81. # define getenv wceex_getenv
  82. # define strdup _strdup
  83. # define hypot _hypot
  84. #endif
  85. /* some useful constants */
  86. #define HALFPI 1.5707963267948966
  87. #define FORTPI 0.78539816339744833
  88. #define PI 3.14159265358979323846
  89. #define TWOPI 6.2831853071795864769
  90. /* maximum tag id length for +init and default files */
  91. #ifndef ID_TAG_MAX
  92. #define ID_TAG_MAX 50
  93. #endif
  94. /* Use WIN32 as a standard windows 32 bit declaration */
  95. #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
  96. # define WIN32
  97. #endif
  98. #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
  99. # define WIN32
  100. #endif
  101. /* directory delimiter for DOS support */
  102. #ifdef WIN32
  103. #define DIR_CHAR '\\'
  104. #else
  105. #define DIR_CHAR '/'
  106. #endif
  107. struct projFileAPI_t;
  108. /* proj thread context */
  109. typedef struct {
  110. int last_errno;
  111. int debug_level;
  112. void (*logger)(void *, int, const char *);
  113. void *app_data;
  114. struct projFileAPI_t *fileapi;
  115. } projCtx_t;
  116. /* datum_type values */
  117. #define PJD_UNKNOWN 0
  118. #define PJD_3PARAM 1
  119. #define PJD_7PARAM 2
  120. #define PJD_GRIDSHIFT 3
  121. #define PJD_WGS84 4 /* WGS84 (or anything considered equivelent) */
  122. /* library errors */
  123. #define PJD_ERR_GEOCENTRIC -45
  124. #define PJD_ERR_AXIS -47
  125. #define PJD_ERR_GRID_AREA -48
  126. #define PJD_ERR_CATALOG -49
  127. #define USE_PROJUV
  128. typedef struct { double u, v; } projUV;
  129. typedef struct { double r, i; } COMPLEX;
  130. #ifndef PJ_LIB__
  131. #define XY projUV
  132. #define LP projUV
  133. #else
  134. typedef struct { double x, y; } XY;
  135. typedef struct { double lam, phi; } LP;
  136. #endif
  137. typedef union { double f; int i; char *s; } PVALUE;
  138. struct PJconsts;
  139. struct PJ_LIST {
  140. char *id; /* projection keyword */
  141. struct PJconsts *(*proj)(struct PJconsts*);/* projection entry point */
  142. char * const *descr; /* description text */
  143. };
  144. struct PJ_ELLPS {
  145. char *id; /* ellipse keyword name */
  146. char *major; /* a= value */
  147. char *ell; /* elliptical parameter */
  148. char *name; /* comments */
  149. };
  150. struct PJ_UNITS {
  151. char *id; /* units keyword */
  152. char *to_meter; /* multiply by value to get meters */
  153. char *name; /* comments */
  154. };
  155. struct PJ_DATUMS {
  156. char *id; /* datum keyword */
  157. char *defn; /* ie. "to_wgs84=..." */
  158. char *ellipse_id; /* ie from ellipse table */
  159. char *comments; /* EPSG code, etc */
  160. };
  161. struct PJ_PRIME_MERIDIANS {
  162. char *id; /* prime meridian keyword */
  163. char *defn; /* offset from greenwich in DMS format. */
  164. };
  165. typedef struct {
  166. double ll_long; /* lower left corner coordinates (radians) */
  167. double ll_lat;
  168. double ur_long; /* upper right corner coordinates (radians) */
  169. double ur_lat;
  170. } PJ_Region;
  171. struct DERIVS {
  172. double x_l, x_p; /* derivatives of x for lambda-phi */
  173. double y_l, y_p; /* derivatives of y for lambda-phi */
  174. };
  175. struct FACTORS {
  176. struct DERIVS der;
  177. double h, k; /* meridinal, parallel scales */
  178. double omega, thetap; /* angular distortion, theta prime */
  179. double conv; /* convergence */
  180. double s; /* areal scale factor */
  181. double a, b; /* max-min scale error */
  182. int code; /* info as to analytics, see following */
  183. };
  184. #define IS_ANAL_XL_YL 01 /* derivatives of lon analytic */
  185. #define IS_ANAL_XP_YP 02 /* derivatives of lat analytic */
  186. #define IS_ANAL_HK 04 /* h and k analytic */
  187. #define IS_ANAL_CONV 010 /* convergence analytic */
  188. /* parameter list struct */
  189. typedef struct ARG_list {
  190. struct ARG_list *next;
  191. char used;
  192. char param[1]; } paralist;
  193. /* base projection data structure */
  194. typedef struct PJconsts {
  195. projCtx_t *ctx;
  196. XY (*fwd)(LP, struct PJconsts *);
  197. LP (*inv)(XY, struct PJconsts *);
  198. void (*spc)(LP, struct PJconsts *, struct FACTORS *);
  199. void (*pfree)(struct PJconsts *);
  200. const char *descr;
  201. paralist *params; /* parameter list */
  202. int over; /* over-range flag */
  203. int geoc; /* geocentric latitude flag */
  204. int is_latlong; /* proj=latlong ... not really a projection at all */
  205. int is_geocent; /* proj=geocent ... not really a projection at all */
  206. double
  207. a, /* major axis or radius if es==0 */
  208. a_orig, /* major axis before any +proj related adjustment */
  209. es, /* e ^ 2 */
  210. es_orig, /* es before any +proj related adjustment */
  211. e, /* eccentricity */
  212. ra, /* 1/A */
  213. one_es, /* 1 - e^2 */
  214. rone_es, /* 1/one_es */
  215. lam0, phi0, /* central longitude, latitude */
  216. x0, y0, /* easting and northing */
  217. k0, /* general scaling factor */
  218. to_meter, fr_meter; /* cartesian scaling */
  219. int datum_type; /* PJD_UNKNOWN/3PARAM/7PARAM/GRIDSHIFT/WGS84 */
  220. double datum_params[7];
  221. struct _pj_gi **gridlist;
  222. int gridlist_count;
  223. int has_geoid_vgrids;
  224. struct _pj_gi **vgridlist_geoid;
  225. int vgridlist_geoid_count;
  226. double vto_meter, vfr_meter;
  227. double from_greenwich; /* prime meridian offset (in radians) */
  228. double long_wrap_center; /* 0.0 for -180 to 180, actually in radians*/
  229. int is_long_wrap_set;
  230. char axis[4];
  231. /* New Datum Shift Grid Catalogs */
  232. char *catalog_name;
  233. struct _PJ_GridCatalog *catalog;
  234. double datum_date;
  235. struct _pj_gi *last_before_grid;
  236. PJ_Region last_before_region;
  237. double last_before_date;
  238. struct _pj_gi *last_after_grid;
  239. PJ_Region last_after_region;
  240. double last_after_date;
  241. #ifdef PROJ_PARMS__
  242. PROJ_PARMS__
  243. #endif /* end of optional extensions */
  244. } PJ;
  245. /* public API */
  246. #include "proj_api.h"
  247. /* Generate pj_list external or make list from include file */
  248. #ifndef USE_PJ_LIST_H
  249. extern struct PJ_LIST pj_list[];
  250. #else
  251. #define PROJ_HEAD(id, name) \
  252. struct PJconsts *pj_##id(struct PJconsts*); extern char * const pj_s_##id;
  253. #include "pj_list.h"
  254. #undef PROJ_HEAD
  255. #define PROJ_HEAD(id, name) {#id, pj_##id, &pj_s_##id},
  256. struct PJ_LIST
  257. pj_list[] = {
  258. #include "pj_list.h"
  259. {0, 0, 0},
  260. };
  261. #undef PROJ_HEAD
  262. #endif
  263. #ifndef PJ_ELLPS__
  264. extern struct PJ_ELLPS pj_ellps[];
  265. #endif
  266. #ifndef PJ_UNITS__
  267. extern struct PJ_UNITS pj_units[];
  268. #endif
  269. #ifndef PJ_DATUMS__
  270. extern struct PJ_DATUMS pj_datums[];
  271. extern struct PJ_PRIME_MERIDIANS pj_prime_meridians[];
  272. #endif
  273. #ifdef PJ_LIB__
  274. /* repeatative projection code */
  275. #define PROJ_HEAD(id, name) static const char des_##id [] = name
  276. #define ENTRYA(name) \
  277. C_NAMESPACE_VAR const char * const pj_s_##name = des_##name; \
  278. C_NAMESPACE PJ *pj_##name(PJ *P) { if (!P) { \
  279. if( (P = (PJ*) pj_malloc(sizeof(PJ))) != NULL) { \
  280. memset( P, 0, sizeof(PJ) ); \
  281. P->pfree = freeup; P->fwd = 0; P->inv = 0; \
  282. P->spc = 0; P->descr = des_##name;
  283. #define ENTRYX } return P; } else {
  284. #define ENTRY0(name) ENTRYA(name) ENTRYX
  285. #define ENTRY1(name, a) ENTRYA(name) P->a = 0; ENTRYX
  286. #define ENTRY2(name, a, b) ENTRYA(name) P->a = 0; P->b = 0; ENTRYX
  287. #define ENDENTRY(p) } return (p); }
  288. #define E_ERROR(err) { pj_ctx_set_errno( P->ctx, err); freeup(P); return(0); }
  289. #define E_ERROR_0 { freeup(P); return(0); }
  290. #define F_ERROR { pj_ctx_set_errno( P->ctx, -20); return(xy); }
  291. #define I_ERROR { pj_ctx_set_errno( P->ctx, -20); return(lp); }
  292. #define FORWARD(name) static XY name(LP lp, PJ *P) { XY xy = {0.0,0.0}
  293. #define INVERSE(name) static LP name(XY xy, PJ *P) { LP lp = {0.0,0.0}
  294. #define FREEUP static void freeup(PJ *P) {
  295. #define SPECIAL(name) static void name(LP lp, PJ *P, struct FACTORS *fac)
  296. #endif
  297. #define MAX_TAB_ID 80
  298. typedef struct { float lam, phi; } FLP;
  299. typedef struct { int lam, phi; } ILP;
  300. struct CTABLE {
  301. char id[MAX_TAB_ID]; /* ascii info */
  302. LP ll; /* lower left corner coordinates */
  303. LP del; /* size of cells */
  304. ILP lim; /* limits of conversion matrix */
  305. FLP *cvs; /* conversion matrix */
  306. };
  307. typedef struct _pj_gi {
  308. char *gridname; /* identifying name of grid, eg "conus" or ntv2_0.gsb */
  309. char *filename; /* full path to filename */
  310. const char *format; /* format of this grid, ie "ctable", "ntv1",
  311. "ntv2" or "missing". */
  312. int grid_offset; /* offset in file, for delayed loading */
  313. struct CTABLE *ct;
  314. struct _pj_gi *next;
  315. struct _pj_gi *child;
  316. } PJ_GRIDINFO;
  317. typedef struct {
  318. PJ_Region region;
  319. int priority; /* higher used before lower */
  320. double date; /* year.fraction */
  321. char *definition; /* usually the gridname */
  322. PJ_GRIDINFO *gridinfo;
  323. int available; /* 0=unknown, 1=true, -1=false */
  324. } PJ_GridCatalogEntry;
  325. typedef struct _PJ_GridCatalog {
  326. char *catalog_name;
  327. PJ_Region region; /* maximum extent of catalog data */
  328. int entry_count;
  329. PJ_GridCatalogEntry *entries;
  330. struct _PJ_GridCatalog *next;
  331. } PJ_GridCatalog;
  332. /* procedure prototypes */
  333. double dmstor(const char *, char **);
  334. double dmstor_ctx(projCtx ctx, const char *, char **);
  335. void set_rtodms(int, int);
  336. char *rtodms(char *, double, int, int);
  337. double adjlon(double);
  338. double aacos(projCtx,double), aasin(projCtx,double), asqrt(double), aatan2(double, double);
  339. PVALUE pj_param(projCtx ctx, paralist *, const char *);
  340. paralist *pj_mkparam(char *);
  341. int pj_ell_set(projCtx ctx, paralist *, double *, double *);
  342. int pj_datum_set(projCtx,paralist *, PJ *);
  343. int pj_prime_meridian_set(paralist *, PJ *);
  344. int pj_angular_units_set(paralist *, PJ *);
  345. paralist *pj_clone_paralist( const paralist* );
  346. paralist*pj_search_initcache( const char *filekey );
  347. void pj_insert_initcache( const char *filekey, const paralist *list);
  348. double *pj_enfn(double);
  349. double pj_mlfn(double, double, double, double *);
  350. double pj_inv_mlfn(projCtx, double, double, double *);
  351. double pj_qsfn(double, double, double);
  352. double pj_tsfn(double, double, double);
  353. double pj_msfn(double, double, double);
  354. double pj_phi2(projCtx, double, double);
  355. double pj_qsfn_(double, PJ *);
  356. double *pj_authset(double);
  357. double pj_authlat(double, double *);
  358. COMPLEX pj_zpoly1(COMPLEX, COMPLEX *, int);
  359. COMPLEX pj_zpolyd1(COMPLEX, COMPLEX *, int, COMPLEX *);
  360. int pj_deriv(LP, double, PJ *, struct DERIVS *);
  361. int pj_factors(LP, PJ *, double, struct FACTORS *);
  362. struct PW_COEF {/* row coefficient structure */
  363. int m; /* number of c coefficients (=0 for none) */
  364. double *c; /* power coefficients */
  365. };
  366. /* Approximation structures and procedures */
  367. typedef struct { /* Chebyshev or Power series structure */
  368. projUV a, b; /* power series range for evaluation */
  369. /* or Chebyshev argument shift/scaling */
  370. struct PW_COEF *cu, *cv;
  371. int mu, mv; /* maximum cu and cv index (+1 for count) */
  372. int power; /* != 0 if power series, else Chebyshev */
  373. } Tseries;
  374. Tseries *mk_cheby(projUV, projUV, double, projUV *, projUV (*)(projUV), int, int, int);
  375. projUV bpseval(projUV, Tseries *);
  376. projUV bcheval(projUV, Tseries *);
  377. projUV biveval(projUV, Tseries *);
  378. void *vector1(int, int);
  379. void **vector2(int, int, int);
  380. void freev2(void **v, int nrows);
  381. int bchgen(projUV, projUV, int, int, projUV **, projUV(*)(projUV));
  382. int bch2bps(projUV, projUV, projUV **, int, int);
  383. /* nadcon related protos */
  384. LP nad_intr(LP, struct CTABLE *);
  385. LP nad_cvt(LP, int, struct CTABLE *);
  386. struct CTABLE *nad_init(projCtx ctx, char *);
  387. struct CTABLE *nad_ctable_init( projCtx ctx, PAFile fid );
  388. int nad_ctable_load( projCtx ctx, struct CTABLE *, PAFile fid );
  389. struct CTABLE *nad_ctable2_init( projCtx ctx, PAFile fid );
  390. int nad_ctable2_load( projCtx ctx, struct CTABLE *, PAFile fid );
  391. void nad_free(struct CTABLE *);
  392. /* higher level handling of datum grid shift files */
  393. int pj_apply_vgridshift( PJ *defn, const char *listname,
  394. PJ_GRIDINFO ***gridlist_p,
  395. int *gridlist_count_p,
  396. int inverse,
  397. long point_count, int point_offset,
  398. double *x, double *y, double *z );
  399. int pj_apply_gridshift_2( PJ *defn, int inverse,
  400. long point_count, int point_offset,
  401. double *x, double *y, double *z );
  402. int pj_apply_gridshift_3( projCtx ctx,
  403. PJ_GRIDINFO **gridlist, int gridlist_count,
  404. int inverse, long point_count, int point_offset,
  405. double *x, double *y, double *z );
  406. PJ_GRIDINFO **pj_gridlist_from_nadgrids( projCtx, const char *, int * );
  407. void pj_deallocate_grids();
  408. PJ_GRIDINFO *pj_gridinfo_init( projCtx, const char * );
  409. int pj_gridinfo_load( projCtx, PJ_GRIDINFO * );
  410. void pj_gridinfo_free( projCtx, PJ_GRIDINFO * );
  411. PJ_GridCatalog *pj_gc_findcatalog( projCtx, const char * );
  412. PJ_GridCatalog *pj_gc_readcatalog( projCtx, const char * );
  413. void pj_gc_unloadall( projCtx );
  414. int pj_gc_apply_gridshift( PJ *defn, int inverse,
  415. long point_count, int point_offset,
  416. double *x, double *y, double *z );
  417. int pj_gc_apply_gridshift( PJ *defn, int inverse,
  418. long point_count, int point_offset,
  419. double *x, double *y, double *z );
  420. PJ_GRIDINFO *pj_gc_findgrid( projCtx ctx,
  421. PJ_GridCatalog *catalog, int after,
  422. LP location, double date,
  423. PJ_Region *optional_region,
  424. double *grid_date );
  425. double pj_gc_parsedate( projCtx, const char * );
  426. void *proj_mdist_ini(double);
  427. double proj_mdist(double, double, double, const void *);
  428. double proj_inv_mdist(projCtx ctx, double, const void *);
  429. void *pj_gauss_ini(double, double, double *,double *);
  430. LP pj_gauss(projCtx, LP, const void *);
  431. LP pj_inv_gauss(projCtx, LP, const void *);
  432. extern char const pj_release[];
  433. struct PJ_ELLPS *pj_get_ellps_ref( void );
  434. struct PJ_DATUMS *pj_get_datums_ref( void );
  435. struct PJ_UNITS *pj_get_units_ref( void );
  436. struct PJ_LIST *pj_get_list_ref( void );
  437. struct PJ_PRIME_MERIDIANS *pj_get_prime_meridians_ref( void );
  438. #ifndef DISABLE_CVSID
  439. # if defined(__GNUC__) && __GNUC__ >= 4
  440. # define PJ_CVSID(string) static char pj_cvsid[] __attribute__((used)) = string;
  441. # else
  442. # define PJ_CVSID(string) static char pj_cvsid[] = string; \
  443. static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : pj_cvsid ); }
  444. # endif
  445. #else
  446. # define PJ_CVSID(string)
  447. #endif
  448. #ifdef __cplusplus
  449. }
  450. #endif
  451. #endif /* end of basic projections header */