forked from rgeo/rgeo
/
main.c
303 lines (239 loc) · 8.04 KB
/
main.c
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
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
229
230
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
266
267
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
301
302
303
/*
-----------------------------------------------------------------------------
Main initializer for Proj4 wrapper
-----------------------------------------------------------------------------
Copyright 2010 Daniel Azuma
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder, nor the names of any other
contributors to this software, may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------
*/
#ifdef HAVE_PROJ_API_H
#ifdef HAVE_PJ_INIT_PLUS
#define RGEO_PROJ4_SUPPORTED
#endif
#endif
#ifdef __cplusplus
#define RGEO_BEGIN_C extern "C" {
#define RGEO_END_C }
#else
#define RGEO_BEGIN_C
#define RGEO_END_C
#endif
#ifdef RGEO_PROJ4_SUPPORTED
#include <ruby.h>
#include <proj_api.h>
#endif
RGEO_BEGIN_C
#ifdef RGEO_PROJ4_SUPPORTED
typedef struct {
projPJ pj;
VALUE original_str;
char uses_radians;
} RGeo_Proj4Data;
#define RGEO_PROJ4_DATA_PTR(obj) ((RGeo_Proj4Data*)DATA_PTR(obj))
// Destroy function for proj data.
static void destroy_proj4_func(RGeo_Proj4Data* data)
{
if (data->pj) {
pj_free(data->pj);
}
free(data);
}
static void mark_proj4_func(RGeo_Proj4Data* data)
{
if (!NIL_P(data->original_str)) {
rb_gc_mark(data->original_str);
}
}
static VALUE alloc_proj4(VALUE klass)
{
VALUE result = Qnil;
RGeo_Proj4Data* data = ALLOC(RGeo_Proj4Data);
if (data) {
data->pj = NULL;
data->original_str = Qnil;
data->uses_radians = 0;
result = Data_Wrap_Struct(klass, mark_proj4_func, destroy_proj4_func, data);
}
return result;
}
static VALUE method_proj4_initialize_copy(VALUE self, VALUE orig)
{
// Clear out any existing value
RGeo_Proj4Data* self_data = RGEO_PROJ4_DATA_PTR(self);
projPJ pj = self_data->pj;
if (pj) {
pj_free(pj);
self_data->pj = NULL;
self_data->original_str = Qnil;
}
// Copy value from orig
RGeo_Proj4Data* orig_data = RGEO_PROJ4_DATA_PTR(orig);
if (!NIL_P(orig_data->original_str)) {
self_data->pj = pj_init_plus(RSTRING_PTR(orig_data->original_str));
}
else {
char* str = pj_get_def(orig_data->pj, 0);
self_data->pj = pj_init_plus(str);
pj_dalloc(str);
}
self_data->original_str = orig_data->original_str;
self_data->uses_radians = orig_data->uses_radians;
return self;
}
static VALUE method_proj4_set_value(VALUE self, VALUE str, VALUE uses_radians)
{
Check_Type(str, T_STRING);
// Clear out any existing value
RGeo_Proj4Data* self_data = RGEO_PROJ4_DATA_PTR(self);
projPJ pj = self_data->pj;
if (pj) {
pj_free(pj);
self_data->pj = NULL;
self_data->original_str = Qnil;
}
// Set new data
self_data->pj = pj_init_plus(RSTRING_PTR(str));
self_data->original_str = str;
self_data->uses_radians = RTEST(uses_radians) ? 1 : 0;
return self;
}
static VALUE method_proj4_get_geographic(VALUE self)
{
VALUE result = Qnil;
RGeo_Proj4Data* new_data = ALLOC(RGeo_Proj4Data);
if (new_data) {
RGeo_Proj4Data* self_data = RGEO_PROJ4_DATA_PTR(self);
new_data->pj = pj_latlong_from_proj(self_data->pj);
new_data->original_str = Qnil;
new_data->uses_radians = self_data->uses_radians;
result = Data_Wrap_Struct(CLASS_OF(self), mark_proj4_func, destroy_proj4_func, new_data);
}
return result;
}
static VALUE method_proj4_original_str(VALUE self)
{
return RGEO_PROJ4_DATA_PTR(self)->original_str;
}
static VALUE method_proj4_uses_radians(VALUE self)
{
return RGEO_PROJ4_DATA_PTR(self)->uses_radians ? Qtrue : Qfalse;
}
static VALUE method_proj4_canonical_str(VALUE self)
{
VALUE result = Qnil;
projPJ pj = RGEO_PROJ4_DATA_PTR(self)->pj;
if (pj) {
char* str = pj_get_def(pj, 0);
if (str) {
result = rb_str_new2(str);
pj_dalloc(str);
}
}
return result;
}
static VALUE method_proj4_is_geographic(VALUE self)
{
VALUE result = Qnil;
projPJ pj = RGEO_PROJ4_DATA_PTR(self)->pj;
if (pj) {
result = pj_is_latlong(pj) ? Qtrue : Qfalse;
}
return result;
}
static VALUE method_proj4_is_geocentric(VALUE self)
{
VALUE result = Qnil;
projPJ pj = RGEO_PROJ4_DATA_PTR(self)->pj;
if (pj) {
result = pj_is_geocent(pj) ? Qtrue : Qfalse;
}
return result;
}
static VALUE method_proj4_is_valid(VALUE self)
{
return RGEO_PROJ4_DATA_PTR(self)->pj ? Qtrue : Qfalse;
}
static VALUE cmethod_proj4_transform(VALUE method, VALUE from, VALUE to, VALUE x, VALUE y, VALUE z)
{
VALUE result = Qnil;
projPJ from_pj = RGEO_PROJ4_DATA_PTR(from)->pj;
projPJ to_pj = RGEO_PROJ4_DATA_PTR(to)->pj;
if (from_pj && to_pj) {
double xval = rb_num2dbl(x);
double yval = rb_num2dbl(y);
double zval = 0.0;
if (!NIL_P(z)) {
zval = rb_num2dbl(z);
}
int err = pj_transform(from_pj, to_pj, 1, 1, &xval, &yval, NIL_P(z) ? NULL : &zval);
if (!err && xval != HUGE_VAL && yval != HUGE_VAL && (NIL_P(z) || zval != HUGE_VAL)) {
result = rb_ary_new2(NIL_P(z) ? 2 : 3);
rb_ary_push(result, rb_float_new(xval));
rb_ary_push(result, rb_float_new(yval));
if (!NIL_P(z)) {
rb_ary_push(result, rb_float_new(zval));
}
}
}
return result;
}
static VALUE cmethod_proj4_create(VALUE klass, VALUE str, VALUE uses_radians)
{
VALUE result = Qnil;
Check_Type(str, T_STRING);
RGeo_Proj4Data* data = ALLOC(RGeo_Proj4Data);
if (data) {
data->pj = pj_init_plus(RSTRING_PTR(str));
data->original_str = str;
data->uses_radians = RTEST(uses_radians) ? 1 : 0;
result = Data_Wrap_Struct(klass, mark_proj4_func, destroy_proj4_func, data);
}
return result;
}
static void rgeo_init_proj4()
{
VALUE rgeo_module = rb_define_module("RGeo");
VALUE coordsys_module = rb_define_module_under(rgeo_module, "CoordSys");
VALUE proj4_class = rb_define_class_under(coordsys_module, "Proj4", rb_cObject);
rb_define_alloc_func(proj4_class, alloc_proj4);
rb_define_module_function(proj4_class, "_create", cmethod_proj4_create, 2);
rb_define_method(proj4_class, "initialize_copy", method_proj4_initialize_copy, 1);
rb_define_method(proj4_class, "_set_value", method_proj4_set_value, 2);
rb_define_method(proj4_class, "_original_str", method_proj4_original_str, 0);
rb_define_method(proj4_class, "_canonical_str", method_proj4_canonical_str, 0);
rb_define_method(proj4_class, "_valid?", method_proj4_is_valid, 0);
rb_define_method(proj4_class, "_geographic?", method_proj4_is_geographic, 0);
rb_define_method(proj4_class, "_geocentric?", method_proj4_is_geocentric, 0);
rb_define_method(proj4_class, "_radians?", method_proj4_uses_radians, 0);
rb_define_method(proj4_class, "_get_geographic", method_proj4_get_geographic, 0);
rb_define_module_function(proj4_class, "_transform_coords", cmethod_proj4_transform, 5);
}
#endif
void Init_proj4_c_impl()
{
#ifdef RGEO_PROJ4_SUPPORTED
rgeo_init_proj4();
#endif
}
RGEO_END_C