librtree 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/rtree/lib/csv.c +75 -0
- data/ext/rtree/lib/private/csv.h +1 -0
- data/ext/rtree/lib/rtree-base.c +15 -2
- data/ext/rtree/lib/rtree.h +2 -0
- data/ext/rtree/rtree.c +34 -5
- data/lib/rtree.rb +43 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d23f82b41d0757dbca1d30fc447bd2573db6d1584727c3244c84bbae0bc6b83
|
4
|
+
data.tar.gz: 36b7cda50a05140949537890a4ab99ebc555f08d7f7789cd7d4d0bd14c7b4c27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ad5636536266f5ac027bd9f7ee59b359f4a3b00c07107057c7b83d378122be21343ebb54db36fe0e7683c926e674a7c8e4f3a6013b4a38c3019078851995c38
|
7
|
+
data.tar.gz: 22292e552cd05823802107094df3ff12c0a0a04129e26dd0fe8a9dd1c1b966f47b1b1cf4a1ac046f0965710965235c5e3a195c4de87b5612aa968e042a3c38fb
|
data/CHANGELOG.md
CHANGED
data/ext/rtree/lib/csv.c
CHANGED
@@ -81,3 +81,78 @@ rtree_t* csv_rtree_read(FILE *stream, size_t dims, state_flags_t flags)
|
|
81
81
|
|
82
82
|
return NULL;
|
83
83
|
}
|
84
|
+
|
85
|
+
static int write_node(const state_t*, const node_t*, FILE *);
|
86
|
+
|
87
|
+
static int write_internal_branch(const state_t *state,
|
88
|
+
const branch_t *branch,
|
89
|
+
void *arg)
|
90
|
+
{
|
91
|
+
return write_node(state, branch_get_child(branch), arg);
|
92
|
+
}
|
93
|
+
|
94
|
+
#if SIZEOF_RTREE_COORD_T == 4
|
95
|
+
#define PRECISION 6
|
96
|
+
#elif SIZEOF_RTREE_COORD_T == 8
|
97
|
+
#define PRECISION 14
|
98
|
+
#else
|
99
|
+
#error "strange size for rtree_coord_t"
|
100
|
+
#endif
|
101
|
+
|
102
|
+
static int write_leaf_branch(const state_t *state,
|
103
|
+
const branch_t *branch,
|
104
|
+
void *arg)
|
105
|
+
{
|
106
|
+
FILE *stream = arg;
|
107
|
+
rtree_id_t id = branch_get_id(branch);
|
108
|
+
|
109
|
+
fprintf(stream, "%li", (long int)id);
|
110
|
+
|
111
|
+
const rtree_coord_t *rect = branch_get_rect(branch);
|
112
|
+
size_t n = state_dims(state);
|
113
|
+
|
114
|
+
for (size_t i = 0 ; i < 2 * n ; i++)
|
115
|
+
fprintf(stream, ",%.*e", PRECISION, (double)rect[i]);
|
116
|
+
|
117
|
+
fprintf(stream, "\r\n");
|
118
|
+
|
119
|
+
return RTREE_OK;
|
120
|
+
}
|
121
|
+
|
122
|
+
#undef PRECISION
|
123
|
+
|
124
|
+
static int write_internal_node(const state_t *state,
|
125
|
+
const node_t *node,
|
126
|
+
FILE *stream)
|
127
|
+
{
|
128
|
+
return node_branch_each(state, node, write_internal_branch, stream);
|
129
|
+
}
|
130
|
+
|
131
|
+
static int write_leaf_node(const state_t *state,
|
132
|
+
const node_t *node,
|
133
|
+
FILE *stream)
|
134
|
+
{
|
135
|
+
return node_branch_each(state, node, write_leaf_branch, stream);
|
136
|
+
}
|
137
|
+
|
138
|
+
static int write_node(const state_t *state,
|
139
|
+
const node_t *node,
|
140
|
+
FILE *stream)
|
141
|
+
{
|
142
|
+
int err;
|
143
|
+
|
144
|
+
if (node_level(node) > 0)
|
145
|
+
err = write_internal_node(state, node, stream);
|
146
|
+
else
|
147
|
+
err = write_leaf_node(state, node, stream);
|
148
|
+
|
149
|
+
return err;
|
150
|
+
}
|
151
|
+
|
152
|
+
int csv_rtree_write(const rtree_t *rtree, FILE *stream)
|
153
|
+
{
|
154
|
+
if ((rtree == NULL) || (stream == NULL))
|
155
|
+
return RTREE_ERR_INVAL;
|
156
|
+
|
157
|
+
return write_node(rtree->state, rtree->root, stream);
|
158
|
+
}
|
data/ext/rtree/lib/private/csv.h
CHANGED
data/ext/rtree/lib/rtree-base.c
CHANGED
@@ -104,9 +104,9 @@ void rtree_destroy(rtree_t *rtree)
|
|
104
104
|
free(rtree);
|
105
105
|
}
|
106
106
|
|
107
|
-
|
107
|
+
int rtree_csv_write(const rtree_t *rtree, FILE *stream)
|
108
108
|
{
|
109
|
-
return
|
109
|
+
return csv_rtree_write(rtree, stream);
|
110
110
|
}
|
111
111
|
|
112
112
|
rtree_t* rtree_csv_read(FILE *stream, size_t dim, state_flags_t flags)
|
@@ -182,6 +182,11 @@ const char* rtree_strerror(int err)
|
|
182
182
|
return strerror_rtree(err);
|
183
183
|
}
|
184
184
|
|
185
|
+
rtree_height_t rtree_height(const rtree_t *rtree)
|
186
|
+
{
|
187
|
+
return node_height(rtree->state, rtree->root);
|
188
|
+
}
|
189
|
+
|
185
190
|
size_t rtree_bytes(const rtree_t *rtree)
|
186
191
|
{
|
187
192
|
if (rtree == NULL)
|
@@ -255,3 +260,11 @@ bool rtree_empty(const rtree_t *rtree)
|
|
255
260
|
(rtree == NULL) ||
|
256
261
|
(! node_nonempty(rtree->state, rtree->root));
|
257
262
|
}
|
263
|
+
|
264
|
+
int rtree_envelope(const rtree_t *rtree, rtree_coord_t *rect)
|
265
|
+
{
|
266
|
+
if (rtree_empty(rtree))
|
267
|
+
return 1;
|
268
|
+
else
|
269
|
+
return node_envelope(rtree->state, rtree->root, rect);
|
270
|
+
}
|
data/ext/rtree/lib/rtree.h
CHANGED
@@ -34,6 +34,7 @@ int rtree_search(const rtree_t*, const rtree_coord_t*, rtree_search_t*, void*);
|
|
34
34
|
int rtree_add_rect(rtree_t*, rtree_id_t, rtree_coord_t*);
|
35
35
|
int rtree_update(rtree_t*, rtree_update_t*, void*);
|
36
36
|
bool rtree_identical(const rtree_t*, const rtree_t*);
|
37
|
+
int rtree_csv_write(const rtree_t*, FILE*);
|
37
38
|
rtree_t* rtree_csv_read(FILE*, size_t, state_flags_t);
|
38
39
|
int rtree_json_write(const rtree_t*, FILE*);
|
39
40
|
rtree_t* rtree_json_read(FILE*);
|
@@ -50,6 +51,7 @@ size_t rtree_branch_size(const rtree_t*);
|
|
50
51
|
size_t rtree_branching_factor(const rtree_t*);
|
51
52
|
double rtree_unit_sphere_volume(const rtree_t*);
|
52
53
|
bool rtree_empty(const rtree_t*);
|
54
|
+
int rtree_envelope(const rtree_t*, rtree_coord_t*);
|
53
55
|
|
54
56
|
#ifdef __cplusplus
|
55
57
|
}
|
data/ext/rtree/rtree.c
CHANGED
@@ -281,14 +281,19 @@ static VALUE serialise(VALUE self, VALUE io_obj, serialise_t *f)
|
|
281
281
|
return self;
|
282
282
|
}
|
283
283
|
|
284
|
-
static VALUE
|
284
|
+
static VALUE rt_bsrt_write(VALUE self, VALUE io_obj)
|
285
285
|
{
|
286
|
-
return serialise(self, io_obj,
|
286
|
+
return serialise(self, io_obj, rtree_bsrt_write);
|
287
287
|
}
|
288
288
|
|
289
|
-
static VALUE
|
289
|
+
static VALUE rt_csv_write(VALUE self, VALUE io_obj)
|
290
290
|
{
|
291
|
-
return serialise(self, io_obj,
|
291
|
+
return serialise(self, io_obj, rtree_csv_write);
|
292
|
+
}
|
293
|
+
|
294
|
+
static VALUE rt_json_write(VALUE self, VALUE io_obj)
|
295
|
+
{
|
296
|
+
return serialise(self, io_obj, rtree_json_write);
|
292
297
|
}
|
293
298
|
|
294
299
|
static VALUE rt_identical(VALUE self, VALUE other)
|
@@ -373,6 +378,28 @@ static VALUE rt_size(VALUE self)
|
|
373
378
|
return INT2NUM(rtree_bytes(rtree));
|
374
379
|
}
|
375
380
|
|
381
|
+
static VALUE rt_envelope(VALUE self)
|
382
|
+
{
|
383
|
+
rtree_t *rtree;
|
384
|
+
TypedData_Get_Struct(self, rtree_t, &rtree_type, rtree);
|
385
|
+
|
386
|
+
if (rtree_empty(rtree))
|
387
|
+
return Qnil;
|
388
|
+
|
389
|
+
size_t n = 2 * rtree_dims(rtree);
|
390
|
+
double u[n];
|
391
|
+
|
392
|
+
if (rtree_envelope(rtree, u) != 0)
|
393
|
+
rb_sys_fail(__func__);
|
394
|
+
|
395
|
+
VALUE v = rb_ary_new_capa(n);
|
396
|
+
|
397
|
+
for (size_t i = 0 ; i < n ; i++)
|
398
|
+
rb_ary_store(v, i, DBL2NUM(u[i]));
|
399
|
+
|
400
|
+
return v;
|
401
|
+
}
|
402
|
+
|
376
403
|
static VALUE rt_version(VALUE self)
|
377
404
|
{
|
378
405
|
return rb_str_new_cstr(rtree_package_version);
|
@@ -518,11 +545,13 @@ void Init_rtree(void)
|
|
518
545
|
rb_define_method(cRTreeBase, "height", rt_height, 0);
|
519
546
|
rb_define_method(cRTreeBase, "add_rect", rt_add_rect, 2);
|
520
547
|
rb_define_method(cRTreeBase, "search", rt_search, 1);
|
521
|
-
rb_define_method(cRTreeBase, "json_write", rt_json_write, 1);
|
522
548
|
rb_define_method(cRTreeBase, "bsrt_write", rt_bsrt_write, 1);
|
549
|
+
rb_define_method(cRTreeBase, "csv_write", rt_csv_write, 1);
|
550
|
+
rb_define_method(cRTreeBase, "json_write", rt_json_write, 1);
|
523
551
|
rb_define_method(cRTreeBase, "eq?", rt_identical, 1);
|
524
552
|
rb_define_method(cRTreeBase, "dim", rt_dim, 0);
|
525
553
|
rb_define_method(cRTreeBase, "size", rt_size, 0);
|
554
|
+
rb_define_method(cRTreeBase, "envelope", rt_envelope, 0);
|
526
555
|
rb_define_method(cRTreeBase, "page_size", rt_page_size, 0);
|
527
556
|
rb_define_method(cRTreeBase, "node_size", rt_node_size, 0);
|
528
557
|
rb_define_method(cRTreeBase, "rect_size", rt_rect_size, 0);
|
data/lib/rtree.rb
CHANGED
@@ -386,36 +386,40 @@ class RTree < RTreeBase
|
|
386
386
|
super
|
387
387
|
end
|
388
388
|
|
389
|
-
# Serialise to
|
389
|
+
# Serialise to BSRT (binary serialised R-tree)
|
390
390
|
# @param io_arg [String|IO] a path or writable stream
|
391
391
|
# @return [self]
|
392
|
-
# @see .
|
392
|
+
# @see .bsrt_read
|
393
393
|
# @example Write to file
|
394
|
-
# rtree.
|
394
|
+
# rtree.bsrt_write('rtree.bsrt')
|
395
395
|
#
|
396
|
-
def
|
397
|
-
RTree::IOUtil.io_with_mode(io_arg, '
|
396
|
+
def bsrt_write(io_arg)
|
397
|
+
RTree::IOUtil.io_with_mode(io_arg, 'wb') { |io| super(io) }
|
398
398
|
self
|
399
399
|
end
|
400
400
|
|
401
|
-
# Serialise to
|
401
|
+
# Serialise to CSV
|
402
402
|
# @param io_arg [String|IO] a path or writable stream
|
403
403
|
# @return [self]
|
404
|
-
# @see .
|
404
|
+
# @see .csv_read
|
405
405
|
# @example Write to file
|
406
|
-
# rtree.
|
406
|
+
# rtree.csv_write('rtree.csv')
|
407
407
|
#
|
408
|
-
def
|
408
|
+
def csv_write(io_arg)
|
409
409
|
RTree::IOUtil.io_with_mode(io_arg, 'wb') { |io| super(io) }
|
410
410
|
self
|
411
411
|
end
|
412
412
|
|
413
|
-
# Serialise to JSON
|
414
|
-
# @
|
415
|
-
# @
|
413
|
+
# Serialise to JSON
|
414
|
+
# @param io_arg [String|IO] a path or writable stream
|
415
|
+
# @return [self]
|
416
|
+
# @see .json_read
|
417
|
+
# @example Write to file
|
418
|
+
# rtree.json_write('rtree.json')
|
416
419
|
#
|
417
|
-
def
|
418
|
-
|
420
|
+
def json_write(io_arg)
|
421
|
+
RTree::IOUtil.io_with_mode(io_arg, 'w') { |io| super(io) }
|
422
|
+
self
|
419
423
|
end
|
420
424
|
|
421
425
|
# Serialise to BSRT string
|
@@ -426,6 +430,22 @@ class RTree < RTreeBase
|
|
426
430
|
serialise(Encoding::BINARY) { |io| bsrt_write(io) }
|
427
431
|
end
|
428
432
|
|
433
|
+
# Serialise to CSV string
|
434
|
+
# @return [String] the CSV
|
435
|
+
# @see .from_csv
|
436
|
+
#
|
437
|
+
def to_csv
|
438
|
+
serialise(Encoding::BINARY) { |io| csv_write(io) }
|
439
|
+
end
|
440
|
+
|
441
|
+
# Serialise to JSON string
|
442
|
+
# @return [String] the UTF-8 encoded JSON
|
443
|
+
# @see .from_json
|
444
|
+
#
|
445
|
+
def to_json
|
446
|
+
serialise(Encoding::UTF_8) { |io| json_write(io) }
|
447
|
+
end
|
448
|
+
|
429
449
|
# The RTree structure in hash form
|
430
450
|
# @return [Hash]
|
431
451
|
#
|
@@ -462,6 +482,15 @@ class RTree < RTreeBase
|
|
462
482
|
super
|
463
483
|
end
|
464
484
|
|
485
|
+
# @return [Array[float]] the bounding rectangle
|
486
|
+
# @note Ruturns the bounding rectangle (in the same format
|
487
|
+
# as the rectangle input) for all rectangles in the tree.
|
488
|
+
# In the case that the input is empty, returns nil.
|
489
|
+
#
|
490
|
+
def envelope
|
491
|
+
super
|
492
|
+
end
|
493
|
+
|
465
494
|
# @return [Integer] the bytes in a page of memory
|
466
495
|
#
|
467
496
|
def page_size
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: librtree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J.J. Green
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|