shapelib 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +59 -0
- data/Rakefile +47 -0
- data/doc/Interface.html +558 -0
- data/doc/Interface.rd +498 -0
- data/doc/style.css +136 -0
- data/ext/shapelib_ext/depend +49 -0
- data/ext/shapelib_ext/extconf.rb +19 -0
- data/ext/shapelib_ext/main.c +102 -0
- data/ext/shapelib_ext/sfcode.h +284 -0
- data/ext/shapelib_ext/sflist.h +21 -0
- data/ext/shapelib_ext/shpplus.c +588 -0
- data/ext/shapelib_ext/shpplus.h +86 -0
- data/ext/shapelib_ext/spcode.h +704 -0
- data/ext/shapelib_ext/splist.h +71 -0
- data/ext/shapelib_ext/spwkt.h +219 -0
- data/ext/shapelib_ext/valconv.h +313 -0
- data/lib/shapelib.rb +2 -0
- data/lib/shapelib/shape.rb +14 -0
- data/lib/shapelib/version.rb +3 -0
- data/test/libtest.rb +80 -0
- data/test/zsample1.rb +168 -0
- data/test/ztest1.rb +253 -0
- metadata +103 -0
data/doc/style.css
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
/*
|
2
|
+
* style.css: CSS style definition file
|
3
|
+
* taken from srwiki
|
4
|
+
* originally for `Rubyavailable' <URL:http://www.jin.gr.jp/~nahi/Ruby/*>
|
5
|
+
*/
|
6
|
+
|
7
|
+
body {
|
8
|
+
/* background: #EEEEEE;
|
9
|
+
*/
|
10
|
+
background: white;
|
11
|
+
color: black;
|
12
|
+
margin-left: 2%;
|
13
|
+
margin-right: 2%;
|
14
|
+
/* font-size: 10.5pt; */
|
15
|
+
font-family: verdana, arial, helvetica, Sans-Serif;
|
16
|
+
}
|
17
|
+
|
18
|
+
h1 {
|
19
|
+
font-family: "times roman", verdana, sans-serif;
|
20
|
+
/* text-align: right; */
|
21
|
+
/*
|
22
|
+
color: #FFFFFF;
|
23
|
+
background: #33AA99;
|
24
|
+
*/
|
25
|
+
color: #5040f0;
|
26
|
+
background: #f0f0f0;
|
27
|
+
margin-top: 1%;
|
28
|
+
margin-right: 10%;
|
29
|
+
border-top: #dddddd 4px solid;
|
30
|
+
border-left: #e0e0e0 4px solid;
|
31
|
+
border-right: #e0e0e0 4px solid;
|
32
|
+
border-bottom: #eeeeee 4px solid;
|
33
|
+
}
|
34
|
+
|
35
|
+
h2, h3, h4, h5, h6 {
|
36
|
+
font-family: verdana, arial, helvetica, Sans-Serif;
|
37
|
+
}
|
38
|
+
|
39
|
+
h2 {
|
40
|
+
color: #FFFFFF;
|
41
|
+
background: #5040f0;
|
42
|
+
margin-right: 30%;
|
43
|
+
border-bottom: #9080f0 3px solid;
|
44
|
+
}
|
45
|
+
|
46
|
+
h3 {
|
47
|
+
color: #5040f0;
|
48
|
+
border-left: #9080f0 0.5em solid;
|
49
|
+
border-bottom: #5040f0 4px solid;
|
50
|
+
margin-right: 50%;
|
51
|
+
}
|
52
|
+
|
53
|
+
div.header p.status { text-align: right; }
|
54
|
+
div.header p.last-modified { text-align: left; }
|
55
|
+
|
56
|
+
/*
|
57
|
+
li p {
|
58
|
+
margin-top: -1ex;
|
59
|
+
}
|
60
|
+
*/
|
61
|
+
|
62
|
+
dt {
|
63
|
+
font-weight: bold;
|
64
|
+
margin-top: 2ex;
|
65
|
+
margin-left: 1em;
|
66
|
+
}
|
67
|
+
|
68
|
+
td {
|
69
|
+
color: black;
|
70
|
+
}
|
71
|
+
|
72
|
+
/*
|
73
|
+
a {
|
74
|
+
color: #0000f0;
|
75
|
+
}
|
76
|
+
*/
|
77
|
+
|
78
|
+
a:hover {
|
79
|
+
color: red;
|
80
|
+
}
|
81
|
+
|
82
|
+
address {
|
83
|
+
color: gray;
|
84
|
+
background: #EEEEEE;
|
85
|
+
text-align: right;
|
86
|
+
font-family: Times, serif;
|
87
|
+
font-style: normal;
|
88
|
+
font-variant: normal;
|
89
|
+
font-weight: normal;
|
90
|
+
}
|
91
|
+
|
92
|
+
var {
|
93
|
+
color: #408000;
|
94
|
+
}
|
95
|
+
|
96
|
+
em {
|
97
|
+
color: #004080;
|
98
|
+
font-family: arial, helvetica, Sans-Serif, gothic;
|
99
|
+
font-style: normal;
|
100
|
+
font-variant: normal;
|
101
|
+
font-weight: bold;
|
102
|
+
}
|
103
|
+
|
104
|
+
pre {
|
105
|
+
border-right: #646464 1px solid;
|
106
|
+
padding-right: 0.5em;
|
107
|
+
border-top: #646464 1px solid;
|
108
|
+
padding-top: 0.5em;
|
109
|
+
border-left: #646464 1px solid;
|
110
|
+
padding-left: 0.5em;
|
111
|
+
border-bottom: #646464 1px solid;
|
112
|
+
padding-bottom: 0.5em;
|
113
|
+
margin-left: 1em;
|
114
|
+
margin-right: 2em;
|
115
|
+
white-space: pre;
|
116
|
+
background-color: #e6e6e6;
|
117
|
+
color: black;
|
118
|
+
}
|
119
|
+
|
120
|
+
span.download {
|
121
|
+
font-weight: normal;
|
122
|
+
color: blue;
|
123
|
+
background: #EEEEEE;
|
124
|
+
}
|
125
|
+
|
126
|
+
.label-not-found {
|
127
|
+
color: #FF4400;
|
128
|
+
}
|
129
|
+
|
130
|
+
.path {
|
131
|
+
font-family: Verdana, Arial, Helvetica, sans-serif;
|
132
|
+
}
|
133
|
+
|
134
|
+
.navi {
|
135
|
+
text-align: right;
|
136
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
MYNAME = ruby-shapelib
|
2
|
+
|
3
|
+
main.o:: sflist.h splist.h shpplus.h valconv.h sfcode.h spcode.h \
|
4
|
+
spwkt.h
|
5
|
+
shpplus.o:: shpplus.h
|
6
|
+
|
7
|
+
RD2 = rd2 -r rd/rd2html-lib --with-css=style.css
|
8
|
+
|
9
|
+
doc: Interface.html
|
10
|
+
Interface.html: Interface.rd
|
11
|
+
$(RD2) --html-title="$(MYNAME) API" $< > $@
|
12
|
+
|
13
|
+
#rpm:
|
14
|
+
# $(MAKE) -f build.mk rpm
|
15
|
+
|
16
|
+
clean2: clean
|
17
|
+
-rm -f test*
|
18
|
+
|
19
|
+
tags: main.c shpplus.c sfcode.h sflist.h shpplus.h spcode.h \
|
20
|
+
splist.h valconv.h spwkt.h
|
21
|
+
ctags *.h *.c
|
22
|
+
|
23
|
+
test: shapelib.so
|
24
|
+
$(RUBY) $(RUBYOPT) ztest1.rb
|
25
|
+
|
26
|
+
distcrean: clean
|
27
|
+
-rm -f mkmf.log Makefile
|
28
|
+
|
29
|
+
TARDIR=..
|
30
|
+
|
31
|
+
tar:
|
32
|
+
$(MAKE) tar.b VERSION=`awk '/Version:/ {print $$2}' $(MYNAME).spec`
|
33
|
+
|
34
|
+
tar.b:
|
35
|
+
cvs export -r HEAD -d $(MYNAME)-$(VERSION) $(MYNAME)
|
36
|
+
tar -czf $(TARDIR)/$(MYNAME)-$(VERSION).tar.gz $(MYNAME)-$(VERSION)/*
|
37
|
+
rm -rf $(MYNAME)-$(VERSION)
|
38
|
+
|
39
|
+
rpm:
|
40
|
+
$(MAKE) rpm.b VERSION=`awk '/Version:/ {print $$2}' $(MYNAME).spec`
|
41
|
+
|
42
|
+
rpm.b:
|
43
|
+
test -d $(RPM)/../rpm/SPECS
|
44
|
+
test ! -d $(MYNAME)-$(VERSION) || rm -rf $(MYNAME)-$(VERSION)
|
45
|
+
cvs export -r HEAD -d $(MYNAME)-$(VERSION) $(MYNAME)
|
46
|
+
tar -czf $(RPM)/SOURCES/$(MYNAME).tar.gz $(MYNAME)-$(VERSION)/*
|
47
|
+
rm -rf $(MYNAME)-$(VERSION)
|
48
|
+
cp $(MYNAME).spec $(RPM)/SPECS
|
49
|
+
rpmbuild -ba $(RPM)/SPECS/$(MYNAME).spec
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
HEADER_DIRS = [
|
5
|
+
'/opt/local/include',
|
6
|
+
'/usr/local/include',
|
7
|
+
'/usr/include',
|
8
|
+
]
|
9
|
+
|
10
|
+
LIB_DIRS = [
|
11
|
+
'/opt/local/lib',
|
12
|
+
'/usr/local/lib',
|
13
|
+
'/usr/lib',
|
14
|
+
]
|
15
|
+
|
16
|
+
dir_config('shapelib', HEADER_DIRS, LIB_DIRS)
|
17
|
+
have_header('shapefil.h') or have_header('libshp/shapefil.h') or raise 'shapefil.h not found'
|
18
|
+
have_library('shp') or raise 'libshp not found'
|
19
|
+
create_makefile('shapelib_ext')
|
@@ -0,0 +1,102 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#if defined(HAVE_SHAPEFIL_H)
|
3
|
+
#include <shapefil.h>
|
4
|
+
#endif
|
5
|
+
#if defined(HAVE_LIBSHP_SHAPEFIL_H)
|
6
|
+
#include <libshp/shapefil.h>
|
7
|
+
#endif
|
8
|
+
#include "shpplus.h"
|
9
|
+
|
10
|
+
typedef struct {
|
11
|
+
const char *mname;
|
12
|
+
VALUE (*func)();
|
13
|
+
int argc;
|
14
|
+
} method_table_entry;
|
15
|
+
|
16
|
+
typedef struct {
|
17
|
+
const char *name;
|
18
|
+
VALUE val;
|
19
|
+
} const_table_entry;
|
20
|
+
|
21
|
+
static VALUE mShapeLib;
|
22
|
+
static VALUE cShapeFile;
|
23
|
+
static VALUE cShape;
|
24
|
+
static VALUE cPoint;
|
25
|
+
static VALUE cPointM;
|
26
|
+
static VALUE cPointZ;
|
27
|
+
static VALUE cArc;
|
28
|
+
static VALUE cArcM;
|
29
|
+
static VALUE cArcZ;
|
30
|
+
static VALUE cPolygon;
|
31
|
+
static VALUE cPolygonM;
|
32
|
+
static VALUE cPolygonZ;
|
33
|
+
static VALUE cMultiPoint;
|
34
|
+
static VALUE cMultiPointM;
|
35
|
+
static VALUE cMultiPointZ;
|
36
|
+
static VALUE cMultiPatch;
|
37
|
+
|
38
|
+
/* code begins here */
|
39
|
+
|
40
|
+
#include "valconv.h"
|
41
|
+
#include "sfcode.h"
|
42
|
+
#include "sflist.h"
|
43
|
+
#include "spcode.h"
|
44
|
+
#include "spwkt.h"
|
45
|
+
#include "splist.h"
|
46
|
+
|
47
|
+
static void register_consts(VALUE klass, const_table_entry *table)
|
48
|
+
{
|
49
|
+
const_table_entry *p;
|
50
|
+
for (p = table; p->name; p++) {
|
51
|
+
rb_define_const(klass, p->name, p->val);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
static void register_methods(VALUE klass, method_table_entry *table)
|
56
|
+
{
|
57
|
+
method_table_entry *p;
|
58
|
+
for (p = table; p->mname; p++) {
|
59
|
+
rb_define_method(klass, p->mname, p->func, p->argc);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
void Init_shapelib_ext()
|
64
|
+
{
|
65
|
+
mShapeLib = rb_define_module("Shapelib");
|
66
|
+
cShapeFile = rb_define_class_under(mShapeLib, "ShapeFile", rb_cObject);
|
67
|
+
cShape = rb_define_class_under(mShapeLib, "Shape", rb_cObject);
|
68
|
+
cPoint = rb_define_class_under(mShapeLib, "Point", cShape);
|
69
|
+
cPointM = rb_define_class_under(mShapeLib, "PointM", cPoint);
|
70
|
+
cPointZ = rb_define_class_under(mShapeLib, "PointZ", cPointM);
|
71
|
+
cArc = rb_define_class_under(mShapeLib, "Arc", cShape);
|
72
|
+
cArcM = rb_define_class_under(mShapeLib, "ArcM", cArc);
|
73
|
+
cArcZ = rb_define_class_under(mShapeLib, "ArcZ", cArcM);
|
74
|
+
cPolygon = rb_define_class_under(mShapeLib, "Polygon", cShape);
|
75
|
+
cPolygonM = rb_define_class_under(mShapeLib, "PolygonM", cPolygon);
|
76
|
+
cPolygonZ = rb_define_class_under(mShapeLib, "PolygonZ", cPolygonM);
|
77
|
+
cMultiPoint = rb_define_class_under(mShapeLib, "MultiPoint", cShape);
|
78
|
+
cMultiPointM = rb_define_class_under(mShapeLib, "MultiPointM", cMultiPoint);
|
79
|
+
cMultiPointZ = rb_define_class_under(mShapeLib, "MultiPointZ", cMultiPointM);
|
80
|
+
cMultiPatch = rb_define_class_under(mShapeLib, "MultiPatch", cShape);
|
81
|
+
rb_define_module_function(mShapeLib, "new_point", sl_m_new_point, -1);
|
82
|
+
rb_define_singleton_method(cShapeFile, "new", sf_s_new, -1);
|
83
|
+
rb_define_singleton_method(cShapeFile, "open", sf_s_open, -1);
|
84
|
+
rb_define_singleton_method(cShape, "new", sp_s_new, 1);
|
85
|
+
rb_define_singleton_method(cPoint, "new", pt_s_new, -1);
|
86
|
+
rb_define_singleton_method(cPointM, "new", ptm_s_new, -1);
|
87
|
+
rb_define_singleton_method(cPointZ, "new", ptz_s_new, -1);
|
88
|
+
rb_define_singleton_method(cArc, "new", arc_s_new, -1);
|
89
|
+
rb_define_singleton_method(cArcM, "new", arcm_s_new, -1);
|
90
|
+
rb_define_singleton_method(cArcZ, "new", arcz_s_new, -1);
|
91
|
+
rb_define_singleton_method(cPolygon, "new", pl_s_new, -1);
|
92
|
+
rb_define_singleton_method(cPolygonM, "new", plm_s_new, -1);
|
93
|
+
rb_define_singleton_method(cPolygonZ, "new", plz_s_new, -1);
|
94
|
+
rb_define_singleton_method(cMultiPoint, "new", mp_s_new, -1);
|
95
|
+
rb_define_singleton_method(cMultiPointM, "new", mpm_s_new, -1);
|
96
|
+
rb_define_singleton_method(cMultiPointZ, "new", mpz_s_new, -1);
|
97
|
+
rb_define_singleton_method(cMultiPatch, "new", mpatch_s_new, -1);
|
98
|
+
register_consts(mShapeLib, sl_consts);
|
99
|
+
register_methods(cShapeFile, sf_methods);
|
100
|
+
register_methods(cShape, sp_methods);
|
101
|
+
register_methods(cPoint, pt_methods);
|
102
|
+
}
|
@@ -0,0 +1,284 @@
|
|
1
|
+
/* sfcode.h */
|
2
|
+
/*
|
3
|
+
* see bottom of the file for constructor
|
4
|
+
*/
|
5
|
+
|
6
|
+
static VALUE sf_add_field(int argc, VALUE *args, VALUE obj)
|
7
|
+
{
|
8
|
+
shapefile_t *self;
|
9
|
+
VALUE name, field_type, field_width, decimal_place;
|
10
|
+
int r, atype, width, wdeci;
|
11
|
+
|
12
|
+
rb_scan_args(argc, args, "31",
|
13
|
+
&name, &field_type, &field_width, &decimal_place);
|
14
|
+
SECURE(name);
|
15
|
+
StringValue(name);
|
16
|
+
atype = Ruby2FieldType(field_type);
|
17
|
+
width = NUM2INT(field_width);
|
18
|
+
wdeci = NIL_P(decimal_place) ? 0 : NUM2INT(decimal_place);
|
19
|
+
if (atype == FTDouble && argc == 3) {
|
20
|
+
rb_raise(rb_eArgError, "specify decimal places for Float attribute");
|
21
|
+
}
|
22
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
23
|
+
r = shapefile_add_field(self, RSTRING_PTR(name), atype, width, wdeci);
|
24
|
+
return obj;
|
25
|
+
}
|
26
|
+
|
27
|
+
static VALUE sf_close(VALUE obj)
|
28
|
+
{
|
29
|
+
shapefile_t *self;
|
30
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
31
|
+
return INT2FIX(shapefile_close(self));
|
32
|
+
}
|
33
|
+
|
34
|
+
static VALUE sf_field_count(VALUE obj)
|
35
|
+
{
|
36
|
+
shapefile_t *self;
|
37
|
+
int r;
|
38
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
39
|
+
r = shapefile_field_count(self);
|
40
|
+
return INT2FIX(r);
|
41
|
+
}
|
42
|
+
|
43
|
+
static int Ruby2FieldIndex(shapefile_t *sfile, VALUE field)
|
44
|
+
{
|
45
|
+
if (FIXNUM_P(field)) {
|
46
|
+
return FIX2INT(field);
|
47
|
+
} else {
|
48
|
+
SECURE(field);
|
49
|
+
return shapefile_field_index(sfile, StringValuePtr(field));
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
static VALUE sf_delete_shape(VALUE obj, VALUE ishape)
|
54
|
+
{
|
55
|
+
shapefile_t *self;
|
56
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
57
|
+
shapefile_delete_shape(self, NUM2INT(ishape));
|
58
|
+
return Qnil;
|
59
|
+
}
|
60
|
+
|
61
|
+
static VALUE sf_field_index(VALUE obj, VALUE name)
|
62
|
+
{
|
63
|
+
shapefile_t *self;
|
64
|
+
int ifield;
|
65
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
66
|
+
ifield = Ruby2FieldIndex(self, name);
|
67
|
+
return (ifield < 0) ? Qnil : INT2FIX(ifield);
|
68
|
+
}
|
69
|
+
|
70
|
+
static VALUE sf_field_name(VALUE obj, VALUE index)
|
71
|
+
{
|
72
|
+
shapefile_t *self;
|
73
|
+
const char *name;
|
74
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
75
|
+
name = shapefile_field_name(self, NUM2INT(index));
|
76
|
+
return name ? rb_str_new2(name) : Qnil;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE sf_field_type(VALUE obj, VALUE field)
|
80
|
+
{
|
81
|
+
shapefile_t *self;
|
82
|
+
int ifield;
|
83
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
84
|
+
ifield = Ruby2FieldIndex(self, field);
|
85
|
+
return FieldType2Ruby(shapefile_field_type(self, ifield));
|
86
|
+
}
|
87
|
+
|
88
|
+
static VALUE sf_field_width(VALUE obj, VALUE field)
|
89
|
+
{
|
90
|
+
shapefile_t *self;
|
91
|
+
int ifield;
|
92
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
93
|
+
ifield = Ruby2FieldIndex(self, field);
|
94
|
+
return INT2FIX(shapefile_field_width(self, ifield));
|
95
|
+
}
|
96
|
+
|
97
|
+
static VALUE sf_field_decimals(VALUE obj, VALUE field)
|
98
|
+
{
|
99
|
+
shapefile_t *self;
|
100
|
+
int ifield;
|
101
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
102
|
+
ifield = Ruby2FieldIndex(self, field);
|
103
|
+
return INT2FIX(shapefile_field_decimals(self, ifield));
|
104
|
+
}
|
105
|
+
|
106
|
+
static VALUE sf_fields(VALUE obj)
|
107
|
+
{
|
108
|
+
shapefile_t *self;
|
109
|
+
VALUE r;
|
110
|
+
int fc, i;
|
111
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
112
|
+
fc = shapefile_field_count(self);
|
113
|
+
if (fc < 0)
|
114
|
+
return Qnil;
|
115
|
+
r = rb_ary_new2(fc);
|
116
|
+
for (i = 0; i < fc; i++) {
|
117
|
+
const char *fieldname;
|
118
|
+
fieldname = shapefile_field_name(self, i);
|
119
|
+
rb_ary_push(r, rb_str_new2(fieldname));
|
120
|
+
}
|
121
|
+
return r;
|
122
|
+
}
|
123
|
+
|
124
|
+
static VALUE sf_maxbound(VALUE obj)
|
125
|
+
{
|
126
|
+
shapefile_t *self;
|
127
|
+
double bound[4];
|
128
|
+
double z;
|
129
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
130
|
+
shapefile_bound(self, NULL, bound);
|
131
|
+
z = bound[2];
|
132
|
+
bound[2] = bound[3];
|
133
|
+
bound[3] = z;
|
134
|
+
return DoubleUnpack(bound, 4);
|
135
|
+
}
|
136
|
+
|
137
|
+
static VALUE sf_minbound(VALUE obj)
|
138
|
+
{
|
139
|
+
shapefile_t *self;
|
140
|
+
double bound[4];
|
141
|
+
double z;
|
142
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
143
|
+
shapefile_bound(self, bound, NULL);
|
144
|
+
z = bound[2];
|
145
|
+
bound[2] = bound[3];
|
146
|
+
bound[3] = z;
|
147
|
+
return DoubleUnpack(bound, 4);
|
148
|
+
}
|
149
|
+
|
150
|
+
static VALUE sf_read(int argc, VALUE *args, VALUE obj)
|
151
|
+
{
|
152
|
+
shapefile_t *self;
|
153
|
+
shape_t *s;
|
154
|
+
VALUE ishape, r;
|
155
|
+
int irec;
|
156
|
+
rb_scan_args(argc, args, "01", &ishape);
|
157
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
158
|
+
irec = NIL_P(ishape) ? -1 : NUM2INT(ishape);
|
159
|
+
s = shapefile_read(self, irec);
|
160
|
+
if (s == NULL)
|
161
|
+
return Qnil;
|
162
|
+
r = Data_Wrap_Struct(cShape, 0, shape_close, s);
|
163
|
+
return r;
|
164
|
+
}
|
165
|
+
|
166
|
+
static VALUE sf_rewind(VALUE obj)
|
167
|
+
{
|
168
|
+
shapefile_t *self;
|
169
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
170
|
+
self->irec = 0;
|
171
|
+
return obj;
|
172
|
+
}
|
173
|
+
|
174
|
+
static VALUE sf_each(VALUE obj)
|
175
|
+
{
|
176
|
+
VALUE row;
|
177
|
+
sf_rewind(obj);
|
178
|
+
while (1) {
|
179
|
+
row = sf_read(0, NULL, obj);
|
180
|
+
if (NIL_P(row))
|
181
|
+
break;
|
182
|
+
rb_yield(row);
|
183
|
+
}
|
184
|
+
return Qnil;
|
185
|
+
}
|
186
|
+
|
187
|
+
static VALUE sf_shape_type(VALUE obj)
|
188
|
+
{
|
189
|
+
shapefile_t *self;
|
190
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
191
|
+
return ShapeType2Ruby(shapefile_shape_type(self));
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE sf_size(VALUE obj)
|
195
|
+
{
|
196
|
+
shapefile_t *self;
|
197
|
+
int r;
|
198
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
199
|
+
r = shapefile_size(self);
|
200
|
+
if (r < 0)
|
201
|
+
rb_raise(rb_eRuntimeError, "size %d", r);
|
202
|
+
return INT2FIX(r);
|
203
|
+
}
|
204
|
+
|
205
|
+
static VALUE sf_write(int argc, VALUE *args, VALUE obj)
|
206
|
+
{
|
207
|
+
shape_t *shape;
|
208
|
+
shapefile_t *self;
|
209
|
+
int r, irec;
|
210
|
+
if (argc < 1 || argc > 2) {
|
211
|
+
rb_raise(rb_eArgError, "usage: Shapefile#write shape, [irec = -1]");
|
212
|
+
}
|
213
|
+
Data_Get_Struct(obj, shapefile_t, self);
|
214
|
+
Data_Get_Struct(args[0], shape_t, shape);
|
215
|
+
irec = ((argc == 2) ? NUM2INT(args[1]) : -1);
|
216
|
+
r = shapefile_write(self, irec, shape);
|
217
|
+
return INT2FIX(r);
|
218
|
+
}
|
219
|
+
|
220
|
+
/*
|
221
|
+
* constructor
|
222
|
+
*/
|
223
|
+
|
224
|
+
static VALUE YieldIfPossible(VALUE proc, VALUE sf)
|
225
|
+
{
|
226
|
+
if (!NIL_P(proc)) {
|
227
|
+
rb_funcall(proc, rb_intern("call"), 1, sf);
|
228
|
+
sf_close(sf);
|
229
|
+
return Qnil;
|
230
|
+
} else {
|
231
|
+
return sf;
|
232
|
+
}
|
233
|
+
}
|
234
|
+
|
235
|
+
static VALUE sf_s_open(int argc, VALUE *argv, VALUE klass)
|
236
|
+
{
|
237
|
+
VALUE fnam, mode, sf, proc;
|
238
|
+
shapefile_t *sfile;
|
239
|
+
rb_scan_args(argc, argv, "11&", &fnam, &mode, &proc);
|
240
|
+
SECURE(fnam);
|
241
|
+
sfile = shapefile_open(StringValuePtr(fnam),
|
242
|
+
(NIL_P(mode) ? "rb" : StringValuePtr(mode))
|
243
|
+
);
|
244
|
+
sf = Data_Wrap_Struct(klass, 0, shapefile_close, sfile);
|
245
|
+
return YieldIfPossible(proc, sf);
|
246
|
+
}
|
247
|
+
|
248
|
+
static VALUE sf_s_new(int argc, VALUE *argv, VALUE klass)
|
249
|
+
{
|
250
|
+
VALUE fnam, shapetype, attrs, sf, proc;
|
251
|
+
shapefile_t *sfile;
|
252
|
+
int stype;
|
253
|
+
int i;
|
254
|
+
if (argc > 3 || argc < 2) {
|
255
|
+
rb_raise(rb_eArgError,
|
256
|
+
"usage: ShapeLib::Shapefile.new filename, shapetype, attrs = []");
|
257
|
+
return Qnil;
|
258
|
+
}
|
259
|
+
rb_scan_args(argc, argv, "21&", &fnam, &shapetype, &attrs, &proc);
|
260
|
+
SECURE(fnam);
|
261
|
+
if ((stype = Ruby2ShapeType(shapetype)) == -1) {
|
262
|
+
rb_raise(rb_eArgError, "shapetype must be Shape subclass");
|
263
|
+
}
|
264
|
+
if (argc > 2) {
|
265
|
+
if (T_ARRAY != TYPE(attrs)) {
|
266
|
+
rb_raise(rb_eArgError, "attrs must be Array");
|
267
|
+
}
|
268
|
+
}
|
269
|
+
sfile = shapefile_new(StringValuePtr(fnam), stype);
|
270
|
+
if (sfile == NULL) {
|
271
|
+
rb_raise(rb_eRuntimeError, "shapefile_new failed");
|
272
|
+
}
|
273
|
+
sf = Data_Wrap_Struct(klass, 0, shapefile_close, sfile);
|
274
|
+
if (argc > 2) {
|
275
|
+
for (i = 0; i < RARRAY_LEN(attrs); i++) {
|
276
|
+
VALUE p = RARRAY_PTR(attrs)[i];
|
277
|
+
if (T_ARRAY != TYPE(p)) {
|
278
|
+
rb_raise(rb_eArgError, "non-array content in attrs");
|
279
|
+
}
|
280
|
+
sf_add_field(RARRAY_LEN(p), RARRAY_PTR(p), sf);
|
281
|
+
}
|
282
|
+
}
|
283
|
+
return YieldIfPossible(proc, sf);
|
284
|
+
}
|