STR.pm

filter: Statementstmt 95.5% 108 / 113 Branchbran 87.5% 35 / 40 Conditioncond 82.6% 43 / 52 MC/DCmcdc 58.3% 14 / 24 Subroutinesub 93.3% 14 / 15 Podpod 80.0% 4 / 5 totaltotal 87.5% 218 / 249 CC 41 SCAR 20.4
CC
41
Cov
91%
query_partly_within_rect
12.4
query_completely_within_rect
11.1
query_point
8.0
CRAP 7.7
« STR/Node.pm ↑ Summary
1 package Tree::STR;
2
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use Tree::STR::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 ▶use POSIX qw /ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw /min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Ref::Util qw/is_blessed_ref is_arrayref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13
14 =head1 NAME
15
16 Tree::STR - A Sort-Tile-Recursive tree index
17
18 =head1 VERSION
19
20 Version 0.02
21
22 =cut
23
24 1 our $VERSION = '0.06';
25
26
27 =head1 SYNOPSIS
28
29 use Tree::STR;
30
31 my $data = [[0,1,2,2,'item 1'], [10,20,100,200,'item 2']];
32 my $tree = Tree::STR->new($data);
33 my $intersects_point = $tree->query_point(50,50); # ['item 2']
34 my $intersects_poly = $tree->query_partly_within_rect(20,20,200,200); # ['item 2']
35
36 # Or if you need the Tree:R interface you can pass an array ref after
37 # the bounding coords. It will be populated with the results.
38 my $intersects_poly = [];
39 $tree->query_partly_within_rect(20,20,200,200, $intersects_poly); # ['item 2']
40 ...
41
42 =head1 DESCRIPTION
43
44 Create a Sort-Tile-Recursive tree. This is a read-only R-Tree that
45 is more efficient to create than a standard R-Tree.
46
47 The input data need to be an array of arrays, where each internal
48 array contains the bounding box coordinates as (xmin, ymin, xmax, ymax)
49 followed by the item to be stored.
50
51 Alternately one can pass an array of objects that provide a bbox method
52 that returns an array of coordinates in the order (xmin, ymin, xmax, ymax).
53
54 =head1 EXPORT
55
56 None.
57
58 =head1 METHODS
59
60 =head2 new
61
62 =cut
63
64 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
65 3 my ($class, $data, $n) = @_;
66 3 ▶ my $self = bless { nrects => $n // 3}, $class;
Condition50%1/2
$n // 3
l!l
03
MC/DC0%0/1
$n // 3
$n
Truth table50%1/2
A
A= $n
Aresult
11
00
67 3 $self->{root} = $self->_load_data ($data);
68 3 return $self;
69 }
70
71 0 ▶sub bbox {
Subroutine bbox: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my ($self) = @_;
73 0 ▶ return if !$self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 0 0 0
74 0 $self->{root}->bbox;
75 }
76
77 3 ▶sub _load_data {
Subroutine _load_data: called CC=3 SCAR=11.3
78 3 my ($self, $data) = @_;
79 # we need to work on the bbox centres
80 3 ▶ my @bboxed = map {is_blessed_ref $_ && $_->can('bbox') ? [$_->bbox, $_] : $_} @$data;
Branch: 1 branch
BranchTrueFalseTotal
is_blessed_ref($_) && $_->can('bbox') ? : 0 84 84
Condition33%1/3
is_blessed_ref($_) and $_->can("bbox")
!ll&&!rl&&r
8400
MC/DC0%0/2
is_blessed_ref($_) and $_->can("bbox")
is_blessed_ref($_) $_->can("bbox")
Truth table33%1/3
A and B
A= is_blessed_ref($_)B= $_->can("bbox")
ABresult
0X0
100
111
81 3 my @centred = map {[($_->[0] + $_->[2] / 2), ($_->[1] + $_->[3] / 2), $_]} @bboxed;
82 3 my @bbox = $self->_get_bbox_from_centred_recs(\@centred);
83 3 my $children = $self->_load_data_inner(\@centred);
84 3 return Tree::STR::Node->new (
85 bbox => \@bbox,
86 children => $children,
87 );
88 }
89
90 42 ▶sub _load_data_inner {
Subroutine _load_data_inner: called CC=7 SCAR=19.5
91 42 my ($self, $data, $sort_axis) = @_;
92
93 42 ▶ $sort_axis //= 0;
Condition100%2/2
$sort_axis //= 0
l!l
393
MC/DC100%1/1
$sort_axis //= 0
$sort_axis
Truth table100%2/2
A
A= $sort_axis
Aresult
11
00
94 42 my @sorted = sort {$a->[$sort_axis] <=> $b->[$sort_axis]} @$data;
95
96 42 my $nrects = $self->{nrects};
97 42 my $nitems = @$data;
98 42 my $n_per_box = ceil ($nitems / $nrects);
99 42 my @ranges;
100 42 my $i = 0;
101 42 ▶ while ($i < $nitems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nitems) 123 42 165
102 123 push @ranges, [$i,$i+$n_per_box-1];
103 123 $i += $n_per_box;
104 }
105
106 # switch axis for inner calls
107 42 ▶ $sort_axis = $sort_axis ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$sort_axis ? : 30 12 42
108
109 42 my @nodes;
110 42 for my $range (@ranges) {
111 123 my @recs = @sorted[$range->[0]..min($#sorted, $range->[1])];
112
113 123 ▶ next if !@recs;
Branch: 1 branch
BranchTrueFalseTotal
unless @recs 0 123 123
114
115 123 my @bbox = $self->_get_bbox_from_centred_recs(\@recs);
116 123 ▶ if (@recs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@recs > 1) { } 39 84 123
117 39 push @nodes, Tree::STR::Node->new (
118 bbox => \@bbox,
119 children => $self->_load_data_inner(\@recs, $sort_axis),
120 );
121 }
122 else {
123 84 push @nodes, Tree::STR::Node->new (
124 bbox => \@bbox,
125 tip => $recs[0][2][-1], # nasty...
126 );
127 }
128 }
129 42 return \@nodes;
130 }
131
132 126 ▶sub _get_bbox_from_centred_recs {
Subroutine _get_bbox_from_centred_recs: called CC=3 SCAR=11.0
133 126 my ($self, $recs) = @_;
134 126 state $bbox_idx = 2;
135 126 my ($x1, $y1, $x2, $y2) = @{$recs->[0][$bbox_idx]}[0 .. 3];
136 126 ▶ return ($x1, $y1, $x2, $y2)
Branch: 1 branch
BranchTrueFalseTotal
if @$recs == 1 84 42 126
137 if @$recs == 1;
138 42 foreach my $rec (@$recs) {
139 324 my $bbox = $rec->[$bbox_idx];
140 324 $x1 = min($x1, $bbox->[0]);
141 324 $y1 = min($y1, $bbox->[1]);
142 324 $x2 = max($x2, $bbox->[2]);
143 324 $y2 = max($y2, $bbox->[3]);
144 }
145 42 return $x1, $y1, $x2, $y2;
146 }
147
148 =head2 query_point
149
150 =cut
151
152 5 ▶sub query_point {
Subroutine query_point: called CC=8 SCAR=20.8
Pod documented
153 5 my ($self, $x, $y, $aref) = @_;
154
155 5 my @tips;
156 5 my @children = ($self->{root});
157 CHILD:
158 5 ▶ while (my $child = shift @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = shift @children) 42 5 47
159 42 my $bbox = $child->bbox;
160 next CHILD
161 42 ▶ if $x < $bbox->[0] || $x > $bbox->[2] || $y < $bbox->[1] || $y > $bbox->[3];
Branch: 1 branch
BranchTrueFalseTotal
if $x < $bbox->[0] or $x > $bbox->[2] or $y < $bbox->[1] or $y > $bbox->[3] 25 17 42
Condition100%9/9
$x < $bbox->[0] or $x > $bbox->[2] or $y < $bbox->[1] or $y > $bbox->[3]
l!l&&r!l&&!r
20517
$x < $bbox->[0] or $x > $bbox->[2] or $y < $bbox->[1]
l!l&&r!l&&!r
14622
$x < $bbox->[0] or $x > $bbox->[2]
l!l&&r!l&&!r
8628
MC/DC100%4/4
$x < $bbox->[0] or $x > $bbox->[2] or $y < $bbox->[1] or $y > $bbox->[3]
$x < $bbox->[0] $x > $bbox->[2] $y < $bbox->[1] $y > $bbox->[3]
Truth table100%5/5
A or B or C or D
A= $x < $bbox->[0]B= $x > $bbox->[2]C= $y < $bbox->[1]D= $y > $bbox->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
162
163 17 ▶ if ($child->is_tip_node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_tip_node) { } 4 13 17
164 push @tips, $child->{tip}
165 4 }
166 else {
167 # add to search stack
168 13 push @children, @{$child->children};
169 }
170 }
171
172 5 ▶ return \@tips if !$aref;
Branch: 1 branch
BranchTrueFalseTotal
unless $aref 4 1 5
173
174 # Tree::R compat
175 1 push @$aref, @tips;
176 1 return;
177 }
178
179 =head2 query_partly_within_rect
180
181 =cut
182
183 # non-recursive algorithm
184 4 ▶sub query_partly_within_rect {
Subroutine query_partly_within_rect: called CC=12 SCAR=25.2
Pod documented
185 4 my ($self, $x1, $y1, $x2, $y2, $aref) = @_;
186
187 4 my @tips;
188 4 my @children = ($self->{root});
189 CHILD:
190 4 ▶ while (my $child = shift @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = shift @children) 53 4 57
191 53 my $bbox = $child->bbox;
192 # no overlap
193 next CHILD
194 53 ▶ if $x2 < $bbox->[0] || $x1 > $bbox->[2]
Branch: 1 branch
BranchTrueFalseTotal
if $x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3] 24 29 53
Condition100%9/9
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3]
l!l&&r!l&&!r
21329
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1]
l!l&&r!l&&!r
91232
$x2 < $bbox->[0] or $x1 > $bbox->[2]
l!l&&r!l&&!r
7244
MC/DC100%4/4
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3]
$x2 < $bbox->[0] $x1 > $bbox->[2] $y2 < $bbox->[1] $y1 > $bbox->[3]
Truth table100%5/5
A or B or C or D
A= $x2 < $bbox->[0]B= $x1 > $bbox->[2]C= $y2 < $bbox->[1]D= $y1 > $bbox->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
195 || $y2 < $bbox->[1] || $y1 > $bbox->[3];
196
197 29 ▶ if ($child->is_tip_node) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->is_tip_node) { } 12 17 29
elsif ($x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]) { } 0 17 17
198 push @tips, $child->{tip}
199 12 }
200 ▶ elsif ( $x1 < $bbox->[0] && $x2 > $bbox->[2]
Condition66%6/9
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]
!ll&&!rl&&r
1700
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1]
!ll&&!rl&&r
1610
$x1 < $bbox->[0] and $x2 > $bbox->[2]
!ll&&!rl&&r
1241
MC/DC0%0/4
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]
$x1 < $bbox->[0] $x2 > $bbox->[2] $y1 < $bbox->[1] $y2 > $bbox->[3]
Truth table60%3/5
A and B and C and D
A= $x1 < $bbox->[0]B= $x2 > $bbox->[2]C= $y1 < $bbox->[1]D= $y2 > $bbox->[3]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
201 && $y1 < $bbox->[1] && $y2 > $bbox->[3]) {
202 # bbox completely within search box, grab all tips
203 0 push @tips, @{$child->tips};
204 }
205 else {
206 # add to search stack
207 17 push @children, @{$child->children};
208 }
209 }
210
211 4 ▶ return \@tips if !$aref;
Branch: 1 branch
BranchTrueFalseTotal
unless $aref 3 1 4
212
213 # Tree::R compat
214 1 push @$aref, @tips;
215 1 return;
216 }
217
218 =head2 query_completely_within_rect
219
220 =cut
221
222 4 ▶sub query_completely_within_rect {
Subroutine query_completely_within_rect: called CC=11 SCAR=24.0
Pod documented
223 4 my ($self, $x1, $y1, $x2, $y2, $aref) = @_;
224
225 4 my @tips;
226 4 my @children = ($self->{root});
227
228 CHILD:
229 4 ▶ while (my $child = shift @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = shift @children) 110 4 114
230 110 my $bbox = $child->bbox;
231
232 # next if no overlap
233 next CHILD
234 110 ▶ if $x2 < $bbox->[0] || $x1 > $bbox->[2]
Branch: 1 branch
BranchTrueFalseTotal
if $x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3] 36 74 110
Condition77%7/9
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3]
l!l&&r!l&&!r
36074
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1]
l!l&&r!l&&!r
82874
$x2 < $bbox->[0] or $x1 > $bbox->[2]
l!l&&r!l&&!r
80102
MC/DC50%2/4
$x2 < $bbox->[0] or $x1 > $bbox->[2] or $y2 < $bbox->[1] or $y1 > $bbox->[3]
$x2 < $bbox->[0] $x1 > $bbox->[2] $y2 < $bbox->[1] $y1 > $bbox->[3]
Truth table60%3/5
A or B or C or D
A= $x2 < $bbox->[0]B= $x1 > $bbox->[2]C= $y2 < $bbox->[1]D= $y1 > $bbox->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
235 || $y2 < $bbox->[1] || $y1 > $bbox->[3];
236
237 # user box contains bbox, get all the subtending tips
238 74 ▶ if ( $x1 < $bbox->[0] && $x2 > $bbox->[2]
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]) { } 16 58 74
Condition88%8/9
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]
!ll&&!rl&&r
481016
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1]
!ll&&!rl&&r
321626
$x1 < $bbox->[0] and $x2 > $bbox->[2]
!ll&&!rl&&r
03242
MC/DC75%3/4
$x1 < $bbox->[0] and $x2 > $bbox->[2] and $y1 < $bbox->[1] and $y2 > $bbox->[3]
$x1 < $bbox->[0] $x2 > $bbox->[2] $y1 < $bbox->[1] $y2 > $bbox->[3]
Truth table80%4/5
A and B and C and D
A= $x1 < $bbox->[0]B= $x2 > $bbox->[2]C= $y1 < $bbox->[1]D= $y2 > $bbox->[3]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
239 && $y1 < $bbox->[1] && $y2 > $bbox->[3]) {
240 16 push @tips, @{$child->tips};
241 }
242 else {
243 # add to search stack
244 58 push @children, @{$child->children};
245 }
246
247 }
248
249 4 ▶ return \@tips if !$aref;
Branch: 1 branch
BranchTrueFalseTotal
unless $aref 3 1 4
250
251 # Tree::R compat
252 1 push @$aref, @tips;
253 1 return;
254 }
255
256 =head1 AUTHOR
257
258 Shawn Laffan <shawnlaffan@gmail.com>
259
260 =head1 BUGS
261
262 L<https://github.com/biogeospatial/Perl-Tree-STR/issues>
263
264
265 =head1 SEE ALSO
266
267 L<Tree::R>
268
269 L<Geo::Geos::Index::STRtree>
270
271 Leutenegger, Scott T.; Edgington, Jeffrey M.; Lopez, Mario A. (1997).
272 "STR: A Simple and Efficient Algorithm for R-Tree Packing".
273 L<https://ia600900.us.archive.org/27/items/nasa_techdoc_19970016975/19970016975.pdf>
274
275
276 =head1 LICENSE AND COPYRIGHT
277
278 This software is Copyright (c) 2026 by Shawn Laffan <shawnlaffan@gmail.com>.
279
280 This is free software, licensed under:
281
282 The Artistic License 2.0 (GPL Compatible)
283
284
285 =cut
286
287 1 1; # End of Tree::STR
« STR/Node.pm ↑ Summary