Class: Wgs84

Inherits:
Object
  • Object
show all
Defined in:
ext/geodesic_wgs84/geodesic_wgs84.c

Instance Method Summary collapse

Constructor Details

#initializeObject



18
19
20
21
22
23
24
25
26
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 18

static VALUE
wgs84_init(VALUE klass)
{
  double a = 6378137, f = 1/298.257223563; /* WGS84 */

  geod_init(&g, a, f);

  return klass;
}

Instance Method Details

#as_bigdec(arg) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 132

static VALUE
wgs84_as_bigdec(VALUE klass, VALUE arg)
{
  double val;
  char buf[64], *ptr;

  val = wgs84_get_value(arg);
  memset(buf, 0, sizeof(buf));
  sprintf(buf, "%07.0lf", round(val * 1000000.0));
  ptr = buf + (strlen(buf) - 6);
  memmove(ptr + 1, ptr, 6);
  *ptr = '.';

  return rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, rb_str_new2(buf));
}

#as_deg(arg) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 67

static VALUE
wgs84_as_deg(VALUE klass, VALUE arg)
{
  double val;

  val = wgs84_get_value(arg);

  return rb_float_new(val);
}

#as_dms(arg) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 78

static VALUE
wgs84_as_dms(VALUE klass, VALUE arg)
{
  double val;
  char buf[64], *ptr;

  val = wgs84_get_value(arg);
  memset(buf, 0, sizeof(buf));
  sprintf(buf, "%d ", (int) trunc(val));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%d ", (int) fmod(trunc(fabs(val) * 60.0), 60.0));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%.1lf", fmod(fabs(val) * 3600.0, 60.0));

  return rb_str_new2(buf);
}

#as_dms_a(arg) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 96

static VALUE
wgs84_as_dms_a(VALUE klass, VALUE arg)
{
  double val;
  char buf[64], *ptr;

  val = wgs84_get_value(arg);
  memset(buf, 0, sizeof(buf));
  sprintf(buf, "%2d ", (int) trunc(val));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%2d ", (int) fmod(trunc(fabs(val) * 60.0), 60.0));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%4.1lf", fmod(fabs(val) * 3600.0, 60.0));

  return rb_str_new2(buf);
}

#as_dms_z(arg) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 114

static VALUE
wgs84_as_dms_z(VALUE klass, VALUE arg)
{
  double val;
  char buf[64], *ptr;

  val = wgs84_get_value(arg);
  memset(buf, 0, sizeof(buf));
  sprintf(buf, "%02d ", (int) trunc(val));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%02d ", (int) fmod(trunc(fabs(val) * 60.0), 60.0));
  ptr = buf + strlen(buf);
  sprintf(ptr, "%04.1lf", fmod(fabs(val) * 3600.0, 60.0));

  return rb_str_new2(buf);
}

#average(array, target) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 231

static VALUE
wgs84_average(VALUE klass, VALUE array, VALUE target)
{
  VALUE tmp, arr_val;
  double dst_lat, dst_lon, arr_lat, arr_lon, azi1, azi2, s12;
  double arr_min = 0.0, arr_max = 0.0, arr_sum = 0.0;
  int cnt, arr_azi = 0;

  tmp = rb_check_array_type(target);
  if (NIL_P(tmp))
    rb_raise(rb_eArgError, "invalid (target) argument");
  dst_lat = wgs84_get_value(rb_ary_entry(tmp, 0));
  dst_lon = wgs84_get_value(rb_ary_entry(tmp, 1));

  arr_val = rb_check_convert_type(array, T_ARRAY, "Array", "to_a");
  for (cnt = 0; cnt < RARRAY_LEN(arr_val); cnt++) {
    tmp = rb_check_array_type(rb_ary_entry(arr_val, cnt));
    if (NIL_P(tmp))
      rb_raise(rb_eArgError, "invalid (array) argument");
    arr_lat = wgs84_get_value(rb_ary_entry(tmp, 0));
    arr_lon = wgs84_get_value(rb_ary_entry(tmp, 1));
    geod_inverse(&g, arr_lat, arr_lon, dst_lat, dst_lon, &s12, &azi1, &azi2);
    s12 = round(s12);
    if (arr_min == 0.0 || s12 < arr_min)
      arr_min = s12;
    if (arr_max == 0.0 || s12 > arr_max)
      arr_max = s12;
    arr_sum += s12;
    arr_azi += (int) azi1;
  }
  s12  = round(round(arr_sum / (double) cnt) / 1000.0);
  azi1 = round(round((arr_min + arr_max) / 2.0) / 1000.0);

  return rb_ary_new3(3L, INT2NUM((int) s12), INT2NUM((int) azi1), INT2NUM(arr_azi / cnt));
}

#center(array, center) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 268

static VALUE
wgs84_center(VALUE klass, VALUE array, VALUE center)
{
  VALUE tmp, arr_val;
  double min_lat, min_lon, max_lat, max_lon, tmp_lat, tmp_lon;
  int cnt;

  tmp = rb_check_array_type(center);
  if (NIL_P(tmp))
    rb_raise(rb_eArgError, "invalid (default) argument");
  min_lat = max_lat = wgs84_get_value(rb_ary_entry(tmp, 0));
  min_lon = max_lon = wgs84_get_value(rb_ary_entry(tmp, 1));

  arr_val = rb_check_convert_type(array, T_ARRAY, "Array", "to_a");
  for (cnt = 0; cnt < RARRAY_LEN(arr_val); cnt++) {
    tmp = rb_check_array_type(rb_ary_entry(arr_val, cnt));
    if (NIL_P(tmp))
      rb_raise(rb_eArgError, "invalid (array) argument");
    tmp_lat = wgs84_get_value(rb_ary_entry(tmp, 0));
    tmp_lon = wgs84_get_value(rb_ary_entry(tmp, 1));
    if (tmp_lat < min_lat)
      min_lat = tmp_lat;
    if (tmp_lon < min_lon)
      min_lon = tmp_lon;
    if (tmp_lat > max_lat)
      max_lat = tmp_lat;
    if (tmp_lon > max_lon)
      max_lon = tmp_lon;
  }
  min_lat = (min_lat + max_lat) / 2.0;
  min_lon = (min_lon + max_lon) / 2.0;
  return rb_ary_new3(2L, rb_float_new(min_lat), rb_float_new(min_lon));
}

#distance(*args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 179

static VALUE
wgs84_distance(int argc, VALUE *argv, VALUE klass)
{
  int i;
  double dbl[4], azi1, azi2, s12;
  VALUE tmp1, tmp2;

  if (argc == 4) {
    for (i = 0; i < 4; i++)
      dbl[i] = wgs84_get_value(argv[i]);
    geod_inverse(&g, dbl[0], dbl[1], dbl[2], dbl[3], &s12, &azi1, &azi2);
    return rb_ary_new3(2L, INT2NUM((int) round(s12)), INT2NUM((int) azi1));
  }

  if (argc == 1 && TYPE(*argv) == T_ARRAY && RARRAY_LEN(*argv) == 4) {
    for (i = 0; i < 4; i++)
      dbl[i] = wgs84_get_value(rb_ary_entry(*argv, i));
    geod_inverse(&g, dbl[0], dbl[1], dbl[2], dbl[3], &s12, &azi1, &azi2);
    return rb_ary_new3(2L, INT2NUM((int) round(s12)), INT2NUM((int) azi1));
  }

  if (argc == 2) {
    tmp1 = rb_check_array_type(argv[0]);
    tmp2 = rb_check_array_type(argv[1]);
    if (!NIL_P(tmp1) && !NIL_P(tmp2)) {
      dbl[0] = wgs84_get_value(rb_ary_entry(tmp1, 0));
      dbl[1] = wgs84_get_value(rb_ary_entry(tmp1, 1));
      dbl[2] = wgs84_get_value(rb_ary_entry(tmp2, 0));
      dbl[3] = wgs84_get_value(rb_ary_entry(tmp2, 1));
      geod_inverse(&g, dbl[0], dbl[1], dbl[2], dbl[3], &s12, &azi1, &azi2);
      return rb_ary_new3(2L, INT2NUM((int) round(s12)), INT2NUM((int) azi1));
    }
  }

  if (argc == 1 && TYPE(*argv) == T_ARRAY && RARRAY_LEN(*argv) == 2) {
    tmp1 = rb_check_array_type(rb_ary_entry(*argv, 0));
    tmp2 = rb_check_array_type(rb_ary_entry(*argv, 1));
    if (!NIL_P(tmp1) && !NIL_P(tmp2)) {
      dbl[0] = wgs84_get_value(rb_ary_entry(tmp1, 0));
      dbl[1] = wgs84_get_value(rb_ary_entry(tmp1, 1));
      dbl[2] = wgs84_get_value(rb_ary_entry(tmp2, 0));
      dbl[3] = wgs84_get_value(rb_ary_entry(tmp2, 1));
      geod_inverse(&g, dbl[0], dbl[1], dbl[2], dbl[3], &s12, &azi1, &azi2);
      return rb_ary_new3(2L, INT2NUM((int) round(s12)), INT2NUM((int) azi1));
    }
  }

  rb_raise(rb_eArgError, "wrong number of arguments");
  return Qnil;
}

#lat_lon(*args) ⇒ Object



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
# File 'ext/geodesic_wgs84/geodesic_wgs84.c', line 149

static VALUE
wgs84_lat_lon(int argc, VALUE *argv, VALUE klass)
{
  double lat, lon;
  VALUE tmp;

  if (argc == 2) {
    lat = wgs84_get_value(argv[0]);
    lon = wgs84_get_value(argv[1]);
    return rb_ary_new3(2L, rb_float_new(lat), rb_float_new(lon));
  }

  if (argc != 1) {
    rb_raise(rb_eArgError, "wrong number of arguments");
    return Qnil;
  }

  if (TYPE(*argv) == T_ARRAY && RARRAY_LEN(*argv) == 2) {
    lat = wgs84_get_value(rb_ary_entry(*argv, 0));
    lon = wgs84_get_value(rb_ary_entry(*argv, 1));
    return rb_ary_new3(2L, rb_float_new(lat), rb_float_new(lon));
  }

  tmp = rb_funcall(*argv, rb_intern("to_lat_lon"), 0);
  lat = wgs84_get_value(rb_ary_entry(tmp, 0));
  lon = wgs84_get_value(rb_ary_entry(tmp, 1));
  return rb_ary_new3(2L, rb_float_new(lat), rb_float_new(lon));
}