~ubuntu-branches/ubuntu/maverick/gpsdrive/maverick

« back to all changes in this revision

Viewing changes to src/gpsnasamap.c

  • Committer: Bazaar Package Importer
  • Author(s): Frank Kirschner
  • Date: 2004-05-25 11:44:03 UTC
  • Revision ID: james.westby@ubuntu.com-20040525114403-j3rsu57cavfax6z8
Tags: upstream-2.09
ImportĀ upstreamĀ versionĀ 2.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************
 
2
 
 
3
Copyright (c) 2001-2004 Fritz Ganter <ganter@ganter.at>
 
4
 
 
5
Website: www.gpsdrive.de
 
6
 
 
7
Disclaimer: Please do not use for navigation. 
 
8
 
 
9
    This program is free software; you can redistribute it and/or modify
 
10
    it under the terms of the GNU General Public License as published by
 
11
    the Free Software Foundation; either version 2 of the License, or
 
12
    (at your option) any later version.
 
13
 
 
14
    This program is distributed in the hope that it will be useful,
 
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
    GNU General Public License for more details.
 
18
 
 
19
    You should have received a copy of the GNU General Public License
 
20
    along with this program; if not, write to the Free Software
 
21
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
 
 
23
    *********************************************************************
 
24
 
 
25
$Log: gpsnasamap.c,v $
 
26
Revision 1.15  2004/02/08 17:16:25  ganter
 
27
replacing all strcat with g_strlcat to avoid buffer overflows
 
28
 
 
29
Revision 1.14  2004/02/08 16:35:10  ganter
 
30
replacing all sprintf with g_snprintf to avoid buffer overflows
 
31
 
 
32
Revision 1.13  2004/02/06 22:29:24  ganter
 
33
updated README and man page
 
34
 
 
35
Revision 1.12  2004/02/04 14:47:10  ganter
 
36
added GPGSA sentence for PDOP (Position Dilution Of Precision).
 
37
 
 
38
Revision 1.11  2004/02/03 07:11:21  ganter
 
39
working on problems if gpsdrive is not installed
 
40
 
 
41
Revision 1.10  2004/02/01 22:48:01  ganter
 
42
added output to gpsnasamap.c
 
43
 
 
44
Revision 1.9  2004/02/01 05:24:59  ganter
 
45
missing nasamaps should now really work!
 
46
upload again 2.08pre9!
 
47
 
 
48
Revision 1.8  2004/02/01 05:10:12  ganter
 
49
fixed bug if 1 nasamap is missing
 
50
 
 
51
Revision 1.7  2004/02/01 01:57:03  ganter
 
52
it seems that nasamaps now working fine
 
53
 
 
54
Revision 1.6  2004/01/31 14:48:03  ganter
 
55
pre8
 
56
 
 
57
Revision 1.5  2004/01/31 13:43:57  ganter
 
58
nasamaps are working better, but still bugs
 
59
 
 
60
Revision 1.4  2004/01/31 08:27:22  ganter
 
61
i hope the nasa maps work all over the world
 
62
I expect it works not in australia, will see after i get a little bit sleep
 
63
 
 
64
Revision 1.3  2004/01/31 06:24:21  ganter
 
65
nasa maps at lon=0 works now
 
66
 
 
67
Revision 1.2  2004/01/31 04:16:49  ganter
 
68
...
 
69
 
 
70
Revision 1.1  2004/01/31 04:11:01  ganter
 
71
oh, forgot to add to CVS
 
72
 
 
73
Revision 1.1  2004/01/30 22:20:44  ganter
 
74
convnasamap creates mapfiles from the big nasa map files
 
75
 
 
76
 
 
77
*/
 
78
 
 
79
#include <sys/types.h>
 
80
#include <sys/stat.h>
 
81
#include <fcntl.h>
 
82
#include <stdio.h>
 
83
#include <math.h>
 
84
#include <unistd.h>
 
85
#include <stdlib.h>
 
86
#include <string.h>
 
87
#include <gpsdrive.h>
 
88
 
 
89
/*  Defines for gettext I18n */
 
90
# include <libintl.h>
 
91
# define _(String) gettext(String)
 
92
# ifdef gettext_noop
 
93
#  define N_(String) gettext_noop(String)
 
94
# else
 
95
#  define N_(String) (String)
 
96
# endif
 
97
 
 
98
static char mybuffer[10000];
 
99
extern char homedir[500], mapdir[500];
 
100
extern GtkWidget *mainwindow;
 
101
static GtkWidget *nasawindow = NULL;
 
102
extern int debug;
 
103
static int fdin_w, fdin_e;
 
104
static char outfilename[100], inputfilename_e[255], inputfilename_w[255];
 
105
static int havenasamaps;
 
106
int
 
107
init_nasa_mapfile ()
 
108
{
 
109
 
 
110
  havenasamaps = FALSE;
 
111
 
 
112
  if (mapdir[strlen (mapdir) - 1] != '/')
 
113
    g_strlcat (mapdir, "/", sizeof(mapdir));
 
114
 
 
115
 
 
116
  g_snprintf (outfilename, sizeof (outfilename), "%stop_NASA_IMAGE.ppm",
 
117
              mapdir);
 
118
 
 
119
  g_snprintf (inputfilename_e, sizeof (inputfilename_e),
 
120
              "%snasamaps/top_nasamap_east.raw", homedir);
 
121
 
 
122
  g_snprintf (inputfilename_w, sizeof (inputfilename_w),
 
123
              "%snasamaps/top_nasamap_west.raw", homedir);
 
124
 
 
125
  fdin_e = open (inputfilename_e, O_RDONLY);
 
126
  if (fdin_e >= 0)
 
127
    havenasamaps = TRUE;
 
128
  fdin_w = open (inputfilename_w, O_RDONLY);
 
129
  if (fdin_w >= 0)
 
130
    havenasamaps = TRUE;
 
131
 
 
132
  return 0;
 
133
}
 
134
 
 
135
void
 
136
cleanup_nasa_mapfile ()
 
137
{
 
138
  if (fdin_w >= 0)
 
139
    close (fdin_w);
 
140
  if (fdin_e >= 0)
 
141
    close (fdin_e);
 
142
}
 
143
 
 
144
int
 
145
create_nasa_mapfile (double lat, double lon, int test, char *fn)
 
146
{
 
147
/*     lat,lon= koordinates */
 
148
/* test= test if maps are present */
 
149
/* fn = filename of the generated file */
 
150
  int fdout, uc = 0;
 
151
  int scale, e, xsize_e, xsize_w;
 
152
  int xstart, ystart, y, x_w, x_e;
 
153
  double mylon;
 
154
  GtkWidget *myprogress, *text, *vbox;
 
155
  char textbuf[40];
 
156
 
 
157
  if (!havenasamaps)
 
158
    return -1;
 
159
 
 
160
  scale = 2614061;
 
161
  mylon = lon;
 
162
 
 
163
  g_strlcpy (fn, "nofile.sorry", 255);
 
164
 
 
165
/* return if no map found */
 
166
  if (lon > 0)
 
167
    {
 
168
      xstart = (int) (21600.0 * (lon / 180.0));
 
169
      if (((xstart < 1280) || (xstart > 20320)) && (fdin_w < 0))
 
170
        return -1;
 
171
      if (fdin_e < 0)
 
172
        return -1;
 
173
    }
 
174
  else
 
175
    {
 
176
      lon = 180.0 + lon;
 
177
      xstart = (int) (21600.0 * (lon / 180.0));
 
178
      if (((xstart < 1280) || (xstart > 20320)) && (fdin_e < 0))
 
179
        return -1;
 
180
      if (fdin_w < 0)
 
181
        return -1;
 
182
    }
 
183
 
 
184
  if (!test)
 
185
    {
 
186
      fdout = open (outfilename, O_RDWR | O_TRUNC | O_CREAT, 0644);
 
187
      if (fdout < 0)
 
188
        {
 
189
          fprintf (stderr, _("could not create output map file %s!\n"),
 
190
                   outfilename);
 
191
          return -1;
 
192
        }
 
193
 
 
194
      nasawindow = gtk_window_new (GTK_WINDOW_POPUP);
 
195
      vbox = gtk_vbox_new (FALSE, 6);
 
196
      gtk_container_add (GTK_CONTAINER (nasawindow), vbox);
 
197
/*       gtk_window_set_transient_for (GTK_WINDOW (nasawindow), */
 
198
/*                                  GTK_WINDOW (mainwindow)); */
 
199
      gtk_window_set_position (GTK_WINDOW (nasawindow), GTK_WIN_POS_CENTER);
 
200
 
 
201
/*   g_signal_connect (window, "destroy", */
 
202
/*                  G_CALLBACK (gtk_widget_destroyed), &window); */
 
203
 
 
204
      gtk_window_set_title (GTK_WINDOW (nasawindow), _("Creating map..."));
 
205
      gtk_container_set_border_width (GTK_CONTAINER (nasawindow), 20);
 
206
 
 
207
      myprogress = gtk_progress_bar_new ();
 
208
      gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (myprogress), 0.0);
 
209
      gtk_box_pack_start (GTK_BOX (vbox), myprogress, TRUE, TRUE, 2);
 
210
      text =
 
211
        gtk_label_new (_
 
212
                       ("Creating a temporary map from NASA satellite images"));
 
213
      gtk_box_pack_start (GTK_BOX (vbox), text, TRUE, TRUE, 2);
 
214
 
 
215
/*       gtk_widget_show_all (nasawindow); */
 
216
      gtk_widget_show_all (nasawindow);
 
217
      if (debug)
 
218
        fprintf (stdout,
 
219
                 _("converting map for latitude: %f and longitude: %f ...\n"),
 
220
                 lat, lon);
 
221
 
 
222
/*       if (lon < 0.0) */
 
223
/*      lon = 180.0 + lon; */
 
224
      g_strlcpy (fn, "top_NASA_IMAGE.ppm", 255);
 
225
 
 
226
 
 
227
      g_snprintf (mybuffer, sizeof (mybuffer),
 
228
                  "P6\n# CREATOR: GpsDrive\n1280 1024\n255\n");
 
229
 
 
230
      e = write (fdout, mybuffer, strlen (mybuffer));
 
231
      uc = e;
 
232
      lon = mylon;
 
233
      xstart = (int) (21600.0 * (lon / 180.0));
 
234
 
 
235
      ystart = 3 * 21600 * (int) (10800 - 10800.0 * (lat / 90.0));
 
236
 
 
237
/*    fprintf (stdout, "xstart: %d, ystart: %d\n", xstart, ystart);  */
 
238
      xstart -= 640;
 
239
      ystart = ystart - 512 * 21600 * 3;
 
240
 
 
241
      x_w = x_e = -1;
 
242
      xsize_w = xsize_e = 1280;
 
243
 
 
244
      if (xstart < 0)
 
245
        {
 
246
          x_w = 21600 + xstart;
 
247
          x_e = 1280 - x_w;
 
248
 
 
249
          if (x_e < -20320)
 
250
            x_e = -1;
 
251
          else if (x_e < 0)
 
252
            x_e = 0;
 
253
 
 
254
          if (x_w < -20320)
 
255
            x_w = -1;
 
256
          else if (x_w < 0)
 
257
            x_w = 0;
 
258
          xsize_w = 21600 - x_w;
 
259
          xsize_e = 1280 - xsize_w;
 
260
        }
 
261
      else if (xstart > 20320)
 
262
        {
 
263
          x_w = (xstart + 1280) - 21600;
 
264
          x_e = 1280 - x_w;
 
265
          if (x_e < 20320)
 
266
            x_e = -1;
 
267
          if (x_e < 0)
 
268
            x_e = 0;
 
269
          xsize_w = x_w;
 
270
          xsize_e = 1280 - xsize_w;
 
271
        }
 
272
      else
 
273
        {
 
274
          if (mylon >= 0.0)
 
275
            x_e = xstart;
 
276
          else
 
277
            x_w = xstart;
 
278
        }
 
279
      if (xsize_w > 1280)
 
280
        xsize_w = 1280;
 
281
      if (xsize_e > 1280)
 
282
        xsize_e = 1280;
 
283
      x_w *= 3;
 
284
      x_e *= 3;
 
285
 
 
286
      for (y = 0; y < 1024; y++)
 
287
        {
 
288
          if ((y % 32) == 0)
 
289
            {
 
290
              gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (myprogress),
 
291
                                             y / 1024.0);
 
292
              g_snprintf (textbuf, sizeof (textbuf), "%d%%",
 
293
                          (int) (100.0 * y / 1024));
 
294
              gtk_progress_bar_set_text (GTK_PROGRESS_BAR (myprogress),
 
295
                                         textbuf);
 
296
              while (gtk_events_pending ())
 
297
                gtk_main_iteration ();
 
298
 
 
299
            }
 
300
 
 
301
          if (x_w != -3)
 
302
            {
 
303
              e = lseek (fdin_w, x_w + ystart + y * 21600 * 3, SEEK_SET);
 
304
              e = read (fdin_w, mybuffer, xsize_w * 3);
 
305
              e = write (fdout, mybuffer, xsize_w * 3);
 
306
              uc += e;
 
307
            }
 
308
          if (x_e != -3)
 
309
            {
 
310
              e = lseek (fdin_e, x_e + ystart + y * 21600 * 3, SEEK_SET);
 
311
              e = read (fdin_e, mybuffer, xsize_e * 3);
 
312
              e = write (fdout, mybuffer, xsize_e * 3);
 
313
              uc += e;
 
314
            }
 
315
        }
 
316
/*       fprintf (stderr, "wrote %d bytes (%.1f MB) to mapfile\n", uc, */
 
317
/*             uc / (1024.0 * 1024.0)); */
 
318
 
 
319
      gtk_widget_destroy (GTK_WIDGET (nasawindow));
 
320
      close (fdout);
 
321
      g_strlcpy (mybuffer, g_basename (outfilename), sizeof (mybuffer));
 
322
      fprintf (stdout,
 
323
               _
 
324
               ("\nYou can permanently add this map file with following line in your\nmap_koord.txt (rename the file!):\n"));
 
325
      fprintf (stdout, "\n%s %f %f %d\n", mybuffer, lat, lon, scale);
 
326
 
 
327
    }                           /* End of if !test */
 
328
 
 
329
 
 
330
  return scale;
 
331
}