1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
#include "gpx2navit_txt.h"
|
21 |
#include "projects.h"
|
22 |
#include "geodesic.h"
|
23 |
|
24 |
double getDistanceCore(char *p1, char *l1, char *p2, char *l2);
|
25 |
void checkEllpsUnit(char *unit);
|
26 |
double checkLengthUnit(char *unit);
|
27 |
int checkTimeUnit(char *unit);
|
28 |
double getTimeInterval(char *_t, char *t);
|
29 |
double getSpeed(double length, double ti, double to_meter, int to_sec);
|
30 |
double getDistance(double _x, double _y, double x, double y);
|
31 |
// todo void closeShpFiles(shphandles * shps);
|
32 |
// todo void closeDbfFiles(dbfhandles * dbfs);
|
33 |
void *myMallocRep(size_t size, const char *fileName, int line);
|
34 |
|
35 |
void checkEllpsUnit(char *unit)
|
36 |
{
|
37 |
/*
|
38 |
* checks ellipse unit can be used by proj4
|
39 |
*/
|
40 |
int isOK = 0;
|
41 |
struct PJ_ELLPS *el; /* project.h of proj4 */
|
42 |
for (el = pj_ellps; el->id; ++el) {
|
43 |
if (!strcmp(el->id, unit)) {
|
44 |
isOK = 1;
|
45 |
}
|
46 |
}
|
47 |
if (!isOK) {
|
48 |
fputs
|
49 |
("The ellipse argument is not correct or supported by libproj\n",
|
50 |
stderr);
|
51 |
fputs("You can choose the argument from a list below.\n\n",
|
52 |
stderr);
|
53 |
for (el = pj_ellps; el->id; el++) {
|
54 |
printf("%10s\t%s\n", el->id, el->name);
|
55 |
}
|
56 |
exit(ERR_ELLPSUNIT);
|
57 |
}
|
58 |
}
|
59 |
|
60 |
double checkLengthUnit(char *unit)
|
61 |
{
|
62 |
/*
|
63 |
* checks length unit can be used by proj4
|
64 |
* then returns unit value to meter
|
65 |
*/
|
66 |
int isOK = 0;
|
67 |
double to_meter = 0;
|
68 |
struct PJ_UNITS *ut; /* project.h of proj4 */
|
69 |
for (ut = pj_units; ut->id; ut++) {
|
70 |
if (!strcmp(ut->id, unit)) {
|
71 |
isOK = 1;
|
72 |
to_meter = atof(ut->to_meter);
|
73 |
}
|
74 |
}
|
75 |
if (!isOK) {
|
76 |
fputs
|
77 |
("The length unit argument is not correct or supported by libproj.\n",
|
78 |
stderr);
|
79 |
fputs("You can choose the argument from a list below.\n\n",
|
80 |
stderr);
|
81 |
for (ut = pj_units; ut->id; ut++) {
|
82 |
printf("%s\t%s\n", ut->id, ut->name);
|
83 |
}
|
84 |
exit(ERR_LENGTHUNIT);
|
85 |
}
|
86 |
return to_meter;
|
87 |
}
|
88 |
|
89 |
int checkTimeUnit(char *unit)
|
90 |
{
|
91 |
char *u[8] = { "sec", "s", "min", "m", "hour", "h", "day", "d" };
|
92 |
int p[8] = { 1, 1, 60, 60, 3600, 3600, 86400, 86400 };
|
93 |
int i, to_sec = 0;
|
94 |
for (i = 0; i < 8; i++) {
|
95 |
if (!strcmp(u[i], unit)) {
|
96 |
to_sec = p[i];
|
97 |
}
|
98 |
}
|
99 |
if (!to_sec) {
|
100 |
fputs("The time unit argument is not correct.\n", stderr);
|
101 |
fputs("You can choose the argument from sec, min, hour or day.\n",
|
102 |
stderr);
|
103 |
exit(ERR_TIMEUNIT);
|
104 |
}
|
105 |
return to_sec;
|
106 |
}
|
107 |
|
108 |
double getTimeInterval(char *_t, char *t)
|
109 |
{
|
110 |
/*
|
111 |
* Returns a time interval between _t and t.
|
112 |
* The arguments should be "YYYY-MM-DDThh:mm:ssZ" (xml schema
|
113 |
* datetime format without time zone) format.
|
114 |
*/
|
115 |
double ti;
|
116 |
struct tm _tt;
|
117 |
struct tm tt;
|
118 |
time_t _tmt, tmt;
|
119 |
memset(&_tt, 0, sizeof(_tt));
|
120 |
memset(&tt, 0, sizeof(tt));
|
121 |
sscanf(_t, "%d-%d-%dT%d:%d:%dZ", &_tt.tm_year, &_tt.tm_mon,
|
122 |
&_tt.tm_mday, &_tt.tm_hour, &_tt.tm_min, &_tt.tm_sec);
|
123 |
_tt.tm_year -= 1900;
|
124 |
_tt.tm_mon -= 1;
|
125 |
sscanf(t, "%d-%d-%dT%d:%d:%d", &tt.tm_year, &tt.tm_mon, &tt.tm_mday,
|
126 |
&tt.tm_hour, &tt.tm_min, &tt.tm_sec);
|
127 |
tt.tm_year -= 1900;
|
128 |
tt.tm_mon -= 1;
|
129 |
_tmt = mktime(&_tt);
|
130 |
tmt = mktime(&tt);
|
131 |
ti = difftime(tmt, _tmt);
|
132 |
return ti;
|
133 |
}
|
134 |
|
135 |
double getSpeed(double length, double ti, double to_meter, int to_sec)
|
136 |
{
|
137 |
/*
|
138 |
* Culculates speed from length and time.
|
139 |
*/
|
140 |
double speed;
|
141 |
if (!length || !ti)
|
142 |
speed = 0;
|
143 |
else
|
144 |
speed = (length / to_meter) / (ti / to_sec);
|
145 |
return speed;
|
146 |
}
|
147 |
|
148 |
double getDistanceCore(char *p1, char *l1, char *p2, char *l2)
|
149 |
{
|
150 |
/*
|
151 |
* Culculates a geodesic length between two points
|
152 |
* using geod_*.c
|
153 |
*/
|
154 |
phi1 = dmstor(p1, &p1);
|
155 |
lam1 = dmstor(l1, &l1);
|
156 |
phi2 = dmstor(p2, &p2);
|
157 |
lam2 = dmstor(l2, &l2);
|
158 |
geod_inv();
|
159 |
return geod_S;
|
160 |
}
|
161 |
|
162 |
double getDistance(double _x, double _y, double x, double y)
|
163 |
{
|
164 |
/*
|
165 |
* Culculates a geodesic length between two points
|
166 |
*/
|
167 |
double length;
|
168 |
char p1[17], l1[17], p2[17], l2[17];
|
169 |
sprintf(p1, "%f", _x);
|
170 |
sprintf(l1, "%f", _y);
|
171 |
sprintf(p2, "%f", x);
|
172 |
sprintf(l2, "%f", y);
|
173 |
length = getDistanceCore(p1, l1, p2, l2);
|
174 |
return length;
|
175 |
}
|
176 |
|
177 |
//todo void closeShpFiles(shphandles * shps)
|
178 |
//{
|
179 |
/*
|
180 |
* Closes all SHP files if they opened
|
181 |
*/
|
182 |
// if (shps->wpt)
|
183 |
// SHPClose(shps->wpt);
|
184 |
// if (shps->trk)
|
185 |
// SHPClose(shps->trk);
|
186 |
// if (shps->trk_edg)
|
187 |
// SHPClose(shps->trk_edg);
|
188 |
// if (shps->trk_pnt)
|
189 |
// SHPClose(shps->trk_pnt);
|
190 |
// if (shps->rte)
|
191 |
// SHPClose(shps->rte);
|
192 |
// if (shps->rte_edg)
|
193 |
// SHPClose(shps->rte_edg);
|
194 |
// if (shps->rte_pnt)
|
195 |
// SHPClose(shps->rte_pnt);
|
196 |
//}
|
197 |
|
198 |
//todo void closeDbfFiles(dbfhandles * dbfs)
|
199 |
//{
|
200 |
/*
|
201 |
* Closes all DBF files if they opened
|
202 |
*/
|
203 |
// if (dbfs->wpt)
|
204 |
// DBFClose(dbfs->wpt);
|
205 |
// if (dbfs->trk)
|
206 |
// DBFClose(dbfs->trk);
|
207 |
// if (dbfs->trk_edg)
|
208 |
// DBFClose(dbfs->trk_edg);
|
209 |
// if (dbfs->trk_pnt)
|
210 |
// DBFClose(dbfs->trk_pnt);
|
211 |
// if (dbfs->rte)
|
212 |
// DBFClose(dbfs->rte);
|
213 |
// if (dbfs->rte_edg)
|
214 |
// DBFClose(dbfs->rte_edg);
|
215 |
// if (dbfs->rte_pnt)
|
216 |
// DBFClose(dbfs->rte_pnt);
|
217 |
//}
|
218 |
|