summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-timezone/extract_map_data.c
blob: 8fd8e52a3e68a713e7545ab2a3e5d54ba16a7c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>

int main ()
{
  int fd;
  int num_vectors;
  int limits[4];
  double MAP_PIXEL_WIDTH = 1000;

  typedef struct
  {
    int id;
    int code;
    int limits[4];
    int num_points;
    int offset;
  } vector_info;

  vector_info *vector;
  int i;

  fd = open ("WorldTZ.mfd", O_RDONLY);

  if (fd == -1)
  {
    perror ("open");
    abort ();
  }

  read (fd, &num_vectors, 4);
  read (fd, limits, sizeof (limits));
  
  vector = calloc (num_vectors, sizeof (vector_info));
  
  read (fd, vector, sizeof (vector_info) * num_vectors);
  
#if 0
  for (i = 0; i < num_vectors; i++)
  {
    printf ("list%d %d %d %d %d %d %d %d %d\n",
      i,
      vector[i].id,
      vector[i].code,
      vector[i].limits[0],
      vector[i].limits[2],
      vector[i].limits[3],
      vector[i].limits[4],
      vector[i].num_points,
      vector[i].offset);                
  }
#endif

  for (i = 0; i < num_vectors; i++)
  {
    typedef struct
    {
      int lon;
      int lat;
    } point_type;
    
    int j;
    
    point_type *point = NULL;

    point = realloc (point, vector[i].num_points * sizeof (point_type));
    read (fd, point, vector[i].num_points * sizeof (point_type));

    /* Map the points onto an 800x400 pixel area */

    for (j = 0; j < vector[i].num_points; j++)
    {
      if (point[j].lon > -2000000)
      {
      point[j].lon = lrint (point[j].lon * MAP_PIXEL_WIDTH / 3600000);
      point[j].lat = lrint (point[j].lat * MAP_PIXEL_WIDTH / 3600000);
      }
    }

    /* Remove consecutive points that are now the same. */
    
    for (j = 0; j < vector[i].num_points - 1; j++)
    {
      if ((point[j].lon == point[j+1].lon) &&
          (point[j].lat == point[j+1].lat))
      {
        for (int k = j + 1; k < vector[i].num_points - 1; k++)
        {
          point[k].lat = point[k+1].lat;
          point[k].lon = point[k+1].lon;
        }
        
        vector[i].num_points--;
        j--;
      }
    }

    // Lop off any small corners one style at a time 
    //(so that aligning borders match) to make them diagonals.

    for (int lop = 0; lop < 8; lop++)
    {
      int dx1c[8] = {-1, 1,-1, 1, 0, 0, 0, 0,};
      int dx2c[8] = { 0, 0, 0, 0,-1, 1,-1, 1,};
      int dy1c[8] = { 0, 0, 0, 0,-1,-1, 1, 1,};
      int dy2c[8] = {-1,-1, 1, 1, 0, 0, 0, 0,};

      for (j = 0; j < vector[i].num_points - 2; j++)
      {
        int dx1,dx2,dy1,dy2;
        dx1 = point[j+0].lon - point[j+1].lon;
        dx2 = point[j+1].lon - point[j+2].lon;
        dy1 = point[j+0].lat - point[j+1].lat;
        dy2 = point[j+1].lat - point[j+2].lat;

        if ((dx1 == dx1c[lop]) && (dy1 == dy1c[lop]) &&
            (dx2 == dx2c[lop]) && (dy2 == dy2c[lop]))
        {
          for (int k = j + 1; k < vector[i].num_points - 1; k++)
          {
            point[k].lat = point[k+1].lat;
            point[k].lon = point[k+1].lon;
          }
        
          vector[i].num_points--;
          j--;
        }
      }
    } 

    /* Now weed out points where there are a number of points on the same line. */
    
    for (j = 0; j < vector[i].num_points - 2; j++)
    {
      int dx1,dx2,dy1,dy2;
      int same_dir = 0;
      dx1 = point[j+0].lon - point[j+1].lon;
      dx2 = point[j+1].lon - point[j+2].lon;
      dy1 = point[j+0].lat - point[j+1].lat;
      dy2 = point[j+1].lat - point[j+2].lat;

      if ((dx1 == 0) && (dx2 == 0) && (dy1 * dy2 >= 0))
        same_dir = 1;
      if ((dy1 == 0) && (dy2 == 0) && (dx1 * dx2 >= 0))
        same_dir = 1;

      if ((dx1 != 0) && (dx2 != 0))
      {
        if ((dy1 * dx2) == (dy2 * dx1) && ((dx1 * dx2) > 0))
        same_dir = 1;
      }
        
      if (same_dir)
      {
        for (int k = j + 1; k < vector[i].num_points - 1; k++)
        {
          point[k].lat = point[k+1].lat;
          point[k].lon = point[k+1].lon;
        }
        
        vector[i].num_points--;
        j--;
      }
    }
    
    for (j = 0; j < vector[i].num_points; j++)
    {
      if (point[j].lon == -2147483648)
      {
        printf ("%d,\n", vector[i].id);
      }
      else
      {
        printf ("%d,%d,%d,%.2lf,%.2lf\n",
          vector[i].id, vector[i].code, j, 
          point[j].lon * 360.0 / MAP_PIXEL_WIDTH, 
          point[j].lat * 360.0 / MAP_PIXEL_WIDTH);
      }
    }

    printf ("%d,\n", vector[i].id);
  }
}