OpenCPN Partial API docs
Loading...
Searching...
No Matches
georef.h
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: OpenCPN Georef utility
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2010 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 ***************************************************************************
25
26 ***************************************************************************
27 * Parts of this file were adapted from source code found in *
28 * John F. Waers (jfwaers@csn.net) public domain program MacGPS45 *
29 ***************************************************************************
30
31 */
32
33#ifndef _GEOREF_H__
34#define _GEOREF_H__
35
36#include <stdio.h>
37#include <string.h>
38#include <ctype.h>
39
40//------------------------
41struct DATUM {
42 const char *name;
43 short ellipsoid;
44 double dx;
45 double dy;
46 double dz;
47};
48
49struct ELLIPSOID {
50 const char *name; // name of ellipsoid
51 double a; // semi-major axis, meters
52 double invf; // 1/f
53};
54
69struct GeoRef {
70 int status;
72 int count;
73 int order;
74 double *tx;
75 double *ty;
76 double *lon;
77 double *lat;
78 double *pwx;
80 double *pwy;
82 double *wpx;
83 double *wpy;
84 int txmax;
85 int tymax;
86 int txmin;
87 int tymin;
88 double lonmax;
89 double lonmin;
90 double latmax;
91 double latmin;
92};
93
94#ifndef PI
95#define PI 3.1415926535897931160E0 /* pi */
96#endif
97#define DEGREE (PI / 180.0)
98#define RADIAN (180.0 / PI)
99
100#define DATUM_INDEX_WGS84 101
101#define DATUM_INDEX_UNKNOWN -1
102
103static const double WGS84_semimajor_axis_meters =
104 6378137.0; // WGS84 semimajor axis
105static const double mercator_k0 = 0.9996;
106static const double WGSinvf = 298.257223563; /* WGS84 1/f */
107
108void datumParams(short datum, double *a, double *es);
109
110// Make these conversion routines useable by C or C++
111
112#ifdef __cplusplus
123extern "C" void toDMS(double a, char *bufp, int bufplen);
134extern "C" void toDMM(double a, char *bufp, int bufplen);
135
149extern "C" void todmm(int flag, double a, char *bufp, int bufplen);
150
167extern "C" void toTM(float lat, float lon, float lat0, float lon0, double *x,
168 double *y);
169
186extern "C" void fromTM(double x, double y, double lat0, double lon0,
187 double *lat, double *lon);
188
203extern "C" void toSM(double lat, double lon, double lat0, double lon0,
204 double *x, double *y);
205
216extern "C" double toSMcache_y30(double lat0);
217
233extern "C" void toSMcache(double lat, double lon, double y30, double lon0,
234 double *x, double *y);
250extern "C" void fromSM(double x, double y, double lat0, double lon0,
251 double *lat, double *lon);
252
269extern "C" void fromSMR(double x, double y, double lat0, double lon0,
270 double axis_meters, double *lat, double *lon);
271
287extern "C" void toSM_ECC(double lat, double lon, double lat0, double lon0,
288 double *x, double *y);
289
305extern "C" void fromSM_ECC(double x, double y, double lat0, double lon0,
306 double *lat, double *lon);
307
323extern "C" void toPOLY(double lat, double lon, double lat0, double lon0,
324 double *x, double *y);
325
340extern "C" void fromPOLY(double x, double y, double lat0, double lon0,
341 double *lat, double *lon);
342
355extern "C" void cache_phi0(double lat0, double *sin_phi0, double *cos_phi0);
356
373extern "C" void toORTHO(double lat, double lon, double sin_phi0,
374 double cos_phi0, double lon0, double *x, double *y);
375
390extern "C" void fromORTHO(double x, double y, double lat0, double lon0,
391 double *lat, double *lon);
392
403extern "C" double toPOLARcache_e(double lat0);
404
420extern "C" void toPOLAR(double lat, double lon, double e, double lat0,
421 double lon0, double *x, double *y);
422
436extern "C" void fromPOLAR(double x, double y, double lat0, double lon0,
437 double *lat, double *lon);
438
457extern "C" void toSTEREO(double lat, double lon, double sin_phi0,
458 double cos_phi0, double lon0, double *x, double *y);
475extern "C" void fromSTEREO(double x, double y, double lat0, double lon0,
476 double *lat, double *lon);
477
497extern "C" void toGNO(double lat, double lon, double sin_phi0, double cos_phi0,
498 double lon0, double *x, double *y);
516extern "C" void fromGNO(double x, double y, double lat0, double lon0,
517 double *lat, double *lon);
518
534extern "C" void toEQUIRECT(double lat, double lon, double lat0, double lon0,
535 double *x, double *y);
536
551extern "C" void fromEQUIRECT(double x, double y, double lat0, double lon0,
552 double *lat, double *lon);
553
570extern "C" void ll_gc_ll(double lat, double lon, double crs, double dist,
571 double *dlat, double *dlon);
587extern "C" void ll_gc_ll_reverse(double lat1, double lon1, double lat2,
588 double lon2, double *bearing, double *dist);
589
607extern "C" void PositionBearingDistanceMercator(double lat, double lon,
608 double brg, double dist,
609 double *dlat, double *dlon);
623extern "C" double DistGreatCircle(double slat, double slon, double dlat,
624 double dlon);
639extern "C" double DistLoxodrome(double slat, double slon, double dlat,
640 double dlon);
641
653extern "C" int GetDatumIndex(const char *str);
654
670extern "C" void MolodenskyTransform(double lat, double lon, double *to_lat,
671 double *to_lon, int from_datum_index,
672 int to_datum_index);
673
691extern "C" void DistanceBearingMercator(double lat1, double lon1, double lat0,
692 double lon0, double *brg, double *dist);
693
694extern "C" int Georef_Calculate_Coefficients(struct GeoRef *cp, int nlin_lon);
695extern "C" int Georef_Calculate_Coefficients_Proj(struct GeoRef *cp);
713extern "C" double lat_gc_crosses_meridian(double lat1, double lon1, double lat2,
714 double lon2, double lon);
732extern "C" double lat_rl_crosses_meridian(double lat1, double lon1, double lat2,
733 double lon2, double lon);
734
735#else
736void toDMS(double a, char *bufp, int bufplen);
737void toDMM(double a, char *bufp, int bufplen);
738int GetDatumIndex(const char *str);
739void MolodenskyTransform(double lat, double lon, double *to_lat, double *to_lon,
740 int from_datum_index, int to_datum_index);
741double lat_gc_crosses_meridian(double lat1, double lon1, double lat2,
742 double lon2, double lon);
743double lat_rl_crosses_meridian(double lat1, double lon1, double lat2,
744 double lon2, double lon);
745
746#endif
747
748//--------------------
749
750/*
751 * lmfit
752 *
753 * Solves or minimizes the sum of squares of m nonlinear
754 * functions of n variables.
755 *
756 * From public domain Fortran version
757 * of Argonne National Laboratories MINPACK
758 * argonne national laboratory. minpack project. march 1980.
759 * burton s. garbow, kenneth e. hillstrom, jorge j. more
760 * C translation by Steve Moshier
761 * Joachim Wuttke converted the source into C++ compatible ANSI style
762 * and provided a simplified interface
763 */
764
765// parameters for calling the high-level interface lmfit
766// ( lmfit.c provides lm_initialize_control which sets default values ):
767typedef struct {
768 double ftol; // relative error desired in the sum of squares.
769 double xtol; // relative error between last two approximations.
770 double gtol; // orthogonality desired between fvec and its derivs.
771 double epsilon; // step used to calculate the jacobian.
772 double stepbound; // initial bound to steps in the outer loop.
773 double fnorm; // norm of the residue vector fvec.
774 int maxcall; // maximum number of iterations.
775 int nfev; // actual number of iterations.
776 int info; // status of minimization.
778
779// the subroutine that calculates fvec:
780typedef void(lm_evaluate_ftype)(double *par, int m_dat, double *fvec,
781 void *data, int *info);
782// default implementation therof, provided by lm_eval.c:
783void lm_evaluate_default(double *par, int m_dat, double *fvec, void *data,
784 int *info);
785
786// the subroutine that informs about fit progress:
787typedef void(lm_print_ftype)(int n_par, double *par, int m_dat, double *fvec,
788 void *data, int iflag, int iter, int nfev);
789// default implementation therof, provided by lm_eval.c:
790void lm_print_default(int n_par, double *par, int m_dat, double *fvec,
791 void *data, int iflag, int iter, int nfev);
792
793// compact high-level interface:
794void lm_initialize_control(lm_control_type *control);
795void lm_minimize(int m_dat, int n_par, double *par, lm_evaluate_ftype *evaluate,
796 lm_print_ftype *printout, void *data,
797 lm_control_type *control);
798double lm_enorm(int, double *);
799
800// low-level interface for full control:
801void lm_lmdif(int m, int n, double *x, double *fvec, double ftol, double xtol,
802 double gtol, int maxfev, double epsfcn, double *diag, int mode,
803 double factor, int *info, int *nfev, double *fjac, int *ipvt,
804 double *qtf, double *wa1, double *wa2, double *wa3, double *wa4,
805 lm_evaluate_ftype *evaluate, lm_print_ftype *printout,
806 void *data);
807
808#ifndef _LMDIF
809extern const char *lm_infmsg[];
810extern const char *lm_shortmsg[];
811#endif
812
813// This is an opaque (to lmfit) structure set up before the call to lmfit()
814typedef struct {
815 double *user_tx;
816 double *user_ty;
817 double *user_y;
818 double (*user_func)(double user_tx_point, double user_ty_point, int n_par,
819 double *par);
820 int print_flag;
821 int n_par;
823
824#endif // guard
Definition georef.h:41
Structure containing georeferencing information for transforming between geographic and projected/pix...
Definition georef.h:69
double lonmin
Minimum longitude in reference data.
Definition georef.h:89
int order
Polynomial order for the transformation (1, 2, or 3)
Definition georef.h:73
int count
Number of reference points used.
Definition georef.h:72
double lonmax
Maximum longitude in reference data.
Definition georef.h:88
int txmin
Minimum x value in target space.
Definition georef.h:86
double * pwx
Polynomial coefficients for pixel-to-world longitude transformation.
Definition georef.h:78
double * tx
Array of x-coordinates in target (typically pixel) space.
Definition georef.h:74
double * wpx
Polynomial coefficients for world-to-pixel x transformation.
Definition georef.h:82
double * lat
Array of latitudes corresponding to reference points.
Definition georef.h:77
double * lon
Array of longitudes corresponding to reference points.
Definition georef.h:76
int status
Status of the georeferencing (0 = valid, other values indicate errors)
Definition georef.h:70
int tymin
Minimum y value in target space.
Definition georef.h:87
double * ty
Array of y-coordinates in target (typically pixel) space.
Definition georef.h:75
int tymax
Maximum y value in target space.
Definition georef.h:85
double latmin
Minimum latitude in reference data.
Definition georef.h:91
int txmax
Maximum x value in target space.
Definition georef.h:84
double latmax
Maximum latitude in reference data.
Definition georef.h:90
double * wpy
Polynomial coefficients for world-to-pixel y transformation.
Definition georef.h:83
double * pwy
Polynomial coefficients for pixel-to-world latitude transformation.
Definition georef.h:80