]> err.no Git - mapper/blob - src/latlon.h
GpsBluez: cleanups
[mapper] / src / latlon.h
1 #ifndef _LATLON_H
2 #define _LATLON_H
3
4 #include <glib.h>
5 #include <math.h>
6
7 /* Int ranges for integerized lat/lon */
8 #define LATLON_MAX 2147483646
9 #define LATLON_MIN -2147483646
10 #define EARTH_RADIUS (3440.06479f)
11
12 #define MERCATOR_SPAN (-6.28318377773622f)
13 #define MERCATOR_TOP (3.14159188886811f)
14
15 #define latlon2unit(lat, lon, unitx, unity) { \
16         gdouble tmp; \
17         unitx=lrint((lon + 180.f) * (WORLD_SIZE_UNITS / 360.f)); \
18         tmp=sin(lat * (M_PIl / 180.f)); \
19         unity=lrint((WORLD_SIZE_UNITS / MERCATOR_SPAN) * (log((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP)); \
20 }
21
22 #define unit2latlon(unitx, unity, lat, lon) { \
23         (lon)=((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f; \
24         (lat)=(360.f * (atan(exp(((unity) * (MERCATOR_SPAN / WORLD_SIZE_UNITS)) + MERCATOR_TOP)))) * (1.f / M_PIl) - 90.f; \
25 }
26
27 typedef enum {
28         DDPDDDDD,
29         DD_MMPMMM,
30         DD_MM_SSPS,
31         DDPDDDDD_NSEW,
32         DD_MMPMMM_NSEW,
33         DD_MM_SSPS_NSEW,
34         DEG_FORMAT_ENUM_COUNT
35 } DegFormat;
36 gchar *DEG_FORMAT_TEXT[DEG_FORMAT_ENUM_COUNT];
37
38 /** This enum defines the possible units we can use. */
39 typedef enum {
40         UNITS_KM,
41         UNITS_MI,
42         UNITS_NM,
43         UNITS_ENUM_COUNT
44 } UnitType;
45 gchar *UNITS_TEXT[UNITS_ENUM_COUNT];
46
47 /* UNITS_CONVERTS, when multiplied, converts from NM. */
48 #define EARTH_RADIUS (3440.06479f)
49 gfloat UNITS_CONVERT[UNITS_ENUM_COUNT];
50
51 UnitType _units;
52
53 #define lat_format(F, A, B) deg_format((F),(A), (B), 'S', 'N')
54 #define lon_format(F, A, B) deg_format((F),(A), (B), 'W', 'E')
55
56 void latlon_init(void);
57
58 gint32 lon2mp_int(gdouble lon);
59 gint32 lat2mp_int(gdouble lat);
60
61 gdouble mp_int2lon(gint32 lon);
62 gdouble mp_int2lat(gint32 lat);
63
64 gdouble calculate_distance(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2);
65 gdouble calculate_ddistance(gint lat1, gint lon1, gint lat2, gint lon2);
66 gulong calculate_idistance(gint lat1, gint lon1, gint lat2, gint lon2);
67 gulong calculate_idistance_cmp(gint lat1, gint lon1, gint lat2, gint lon2);
68
69 gdouble calculate_course_rad(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2);
70 gdouble calculate_course(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2);
71
72 gfloat angle_diff(gfloat a, gfloat b);
73
74 gboolean distance_point_to_line(gdouble x, gdouble y, gdouble x1, gdouble y1, gdouble x2, gdouble y2, gdouble *d);
75
76 guint32 xy2tile(guint x, guint y);
77 guint32 lon2x(gdouble lon);
78 guint32 lat2y(gdouble lat);
79
80 void deg_format(DegFormat degformat, gdouble coor, gchar *scoor, gchar neg_char, gchar pos_char);
81
82 #endif