-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfcode.h
284 lines (260 loc) · 6.79 KB
/
sfcode.h
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
/* sfcode.h */
/*
* see bottom of the file for constructor
*/
static VALUE sf_add_field(int argc, VALUE *args, VALUE obj)
{
shapefile_t *self;
VALUE name, field_type, field_width, decimal_place;
int r, atype, width, wdeci;
rb_scan_args(argc, args, "31",
&name, &field_type, &field_width, &decimal_place);
SECURE(name);
StringValue(name);
atype = Ruby2FieldType(field_type);
width = NUM2INT(field_width);
wdeci = NIL_P(decimal_place) ? 0 : NUM2INT(decimal_place);
if (atype == FTDouble && argc == 3) {
rb_raise(rb_eArgError, "specify decimal places for Float attribute");
}
Data_Get_Struct(obj, shapefile_t, self);
r = shapefile_add_field(self, RSTRING(name)->ptr, atype, width, wdeci);
return obj;
}
static VALUE sf_close(VALUE obj)
{
shapefile_t *self;
Data_Get_Struct(obj, shapefile_t, self);
return INT2FIX(shapefile_close(self));
}
static VALUE sf_field_count(VALUE obj)
{
shapefile_t *self;
int r;
Data_Get_Struct(obj, shapefile_t, self);
r = shapefile_field_count(self);
return INT2FIX(r);
}
static int Ruby2FieldIndex(shapefile_t *sfile, VALUE field)
{
if (FIXNUM_P(field)) {
return FIX2INT(field);
} else {
SECURE(field);
return shapefile_field_index(sfile, StringValuePtr(field));
}
}
static VALUE sf_delete_shape(VALUE obj, VALUE ishape)
{
shapefile_t *self;
Data_Get_Struct(obj, shapefile_t, self);
shapefile_delete_shape(self, NUM2INT(ishape));
return Qnil;
}
static VALUE sf_field_index(VALUE obj, VALUE name)
{
shapefile_t *self;
int ifield;
Data_Get_Struct(obj, shapefile_t, self);
ifield = Ruby2FieldIndex(self, name);
return (ifield < 0) ? Qnil : INT2FIX(ifield);
}
static VALUE sf_field_name(VALUE obj, VALUE index)
{
shapefile_t *self;
const char *name;
Data_Get_Struct(obj, shapefile_t, self);
name = shapefile_field_name(self, NUM2INT(index));
return name ? rb_str_new2(name) : Qnil;
}
static VALUE sf_field_type(VALUE obj, VALUE field)
{
shapefile_t *self;
int ifield;
Data_Get_Struct(obj, shapefile_t, self);
ifield = Ruby2FieldIndex(self, field);
return FieldType2Ruby(shapefile_field_type(self, ifield));
}
static VALUE sf_field_width(VALUE obj, VALUE field)
{
shapefile_t *self;
int ifield;
Data_Get_Struct(obj, shapefile_t, self);
ifield = Ruby2FieldIndex(self, field);
return INT2FIX(shapefile_field_width(self, ifield));
}
static VALUE sf_field_decimals(VALUE obj, VALUE field)
{
shapefile_t *self;
int ifield;
Data_Get_Struct(obj, shapefile_t, self);
ifield = Ruby2FieldIndex(self, field);
return INT2FIX(shapefile_field_decimals(self, ifield));
}
static VALUE sf_fields(VALUE obj)
{
shapefile_t *self;
VALUE r;
int fc, i;
Data_Get_Struct(obj, shapefile_t, self);
fc = shapefile_field_count(self);
if (fc < 0)
return Qnil;
r = rb_ary_new2(fc);
for (i = 0; i < fc; i++) {
const char *fieldname;
fieldname = shapefile_field_name(self, i);
rb_ary_push(r, rb_str_new2(fieldname));
}
return r;
}
static VALUE sf_maxbound(VALUE obj)
{
shapefile_t *self;
double bound[4];
double z;
Data_Get_Struct(obj, shapefile_t, self);
shapefile_bound(self, NULL, bound);
z = bound[2];
bound[2] = bound[3];
bound[3] = z;
return DoubleUnpack(bound, 4);
}
static VALUE sf_minbound(VALUE obj)
{
shapefile_t *self;
double bound[4];
double z;
Data_Get_Struct(obj, shapefile_t, self);
shapefile_bound(self, bound, NULL);
z = bound[2];
bound[2] = bound[3];
bound[3] = z;
return DoubleUnpack(bound, 4);
}
static VALUE sf_read(int argc, VALUE *args, VALUE obj)
{
shapefile_t *self;
shape_t *s;
VALUE ishape, r;
int irec;
rb_scan_args(argc, args, "01", &ishape);
Data_Get_Struct(obj, shapefile_t, self);
irec = NIL_P(ishape) ? -1 : NUM2INT(ishape);
s = shapefile_read(self, irec);
if (s == NULL)
return Qnil;
r = Data_Wrap_Struct(cShape, 0, shape_close, s);
return r;
}
static VALUE sf_rewind(VALUE obj)
{
shapefile_t *self;
Data_Get_Struct(obj, shapefile_t, self);
self->irec = 0;
return obj;
}
static VALUE sf_each(VALUE obj)
{
VALUE row;
sf_rewind(obj);
while (1) {
row = sf_read(0, NULL, obj);
if (NIL_P(row))
break;
rb_yield(row);
}
return Qnil;
}
static VALUE sf_shape_type(VALUE obj)
{
shapefile_t *self;
Data_Get_Struct(obj, shapefile_t, self);
return ShapeType2Ruby(shapefile_shape_type(self));
}
static VALUE sf_size(VALUE obj)
{
shapefile_t *self;
int r;
Data_Get_Struct(obj, shapefile_t, self);
r = shapefile_size(self);
if (r < 0)
rb_raise(rb_eRuntimeError, "size %d", r);
return INT2FIX(r);
}
static VALUE sf_write(int argc, VALUE *args, VALUE obj)
{
shape_t *shape;
shapefile_t *self;
int r, irec;
if (argc < 1 || argc > 2) {
rb_raise(rb_eArgError, "usage: Shapefile#write shape, [irec = -1]");
}
Data_Get_Struct(obj, shapefile_t, self);
Data_Get_Struct(args[0], shape_t, shape);
irec = ((argc == 2) ? NUM2INT(args[1]) : -1);
r = shapefile_write(self, irec, shape);
return INT2FIX(r);
}
/*
* constructor
*/
static VALUE YieldIfPossible(VALUE proc, VALUE sf)
{
if (!NIL_P(proc)) {
rb_funcall(proc, rb_intern("call"), 1, sf);
sf_close(sf);
return Qnil;
} else {
return sf;
}
}
static VALUE sf_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE fnam, mode, sf, proc;
shapefile_t *sfile;
rb_scan_args(argc, argv, "11&", &fnam, &mode, &proc);
SECURE(fnam);
sfile = shapefile_open(StringValuePtr(fnam),
(NIL_P(mode) ? "rb" : StringValuePtr(mode))
);
sf = Data_Wrap_Struct(klass, 0, shapefile_close, sfile);
return YieldIfPossible(proc, sf);
}
static VALUE sf_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE fnam, shapetype, attrs, sf, proc;
shapefile_t *sfile;
int stype;
int i;
if (argc > 3 || argc < 2) {
rb_raise(rb_eArgError,
"usage: ShapeLib::Shapefile.new filename, shapetype, attrs = []");
return Qnil;
}
rb_scan_args(argc, argv, "21&", &fnam, &shapetype, &attrs, &proc);
SECURE(fnam);
if ((stype = Ruby2ShapeType(shapetype)) == -1) {
rb_raise(rb_eArgError, "shapetype must be Shape subclass");
}
if (argc > 2) {
if (T_ARRAY != TYPE(attrs)) {
rb_raise(rb_eArgError, "attrs must be Array");
}
}
sfile = shapefile_new(StringValuePtr(fnam), stype);
if (sfile == NULL) {
rb_raise(rb_eRuntimeError, "shapefile_new failed");
}
sf = Data_Wrap_Struct(klass, 0, shapefile_close, sfile);
if (argc > 2) {
for (i = 0; i < RARRAY(attrs)->len; i++) {
VALUE p = RARRAY(attrs)->ptr[i];
if (T_ARRAY != TYPE(p)) {
rb_raise(rb_eArgError, "non-array content in attrs");
}
sf_add_field(RARRAY(p)->len, RARRAY(p)->ptr, sf);
}
}
return YieldIfPossible(proc, sf);
}