PostGIS/ResultSet.pm

filter: Statementstmt 31.0% 18 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 33.3% 4 / 12 Podpod 100% 5 / 5 totaltotal 27.0% 27 / 100 CC 10 SCAR 30.4
CC
10
Cov
23%
_geom_arg
56.0
_spatial_search
6.0
nearest_to
6.0
CRAP 20.9
↑ Summary PostGIS/Codec/WKT/Parser.pm »
1 package DBIO::PostgreSQL::PostGIS::ResultSet;
2 # ABSTRACT: Spatial query helpers for PostGIS-aware result classes
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use base 'DBIO::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Render the user-supplied geometry argument into ('SQL fragment', @binds)
11 # suitable for splicing into a -bool/-and ScalarRef literal.
12 0 ▶sub _geom_arg {
Subroutine _geom_arg: not called CC=7 SCAR=40.3
13 0 my ($self, $geom) = @_;
14 0 ▶ if (ref($geom) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $geom eq "SCALAR") 0 0 0
15 0 return ($$geom);
16 }
17 0 ▶ if (ref($geom) eq 'REF' && ref($$geom) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $geom eq "REF" and ref $$geom eq "ARRAY") 0 0 0
Condition0%0/3
ref $geom eq "REF" and ref $$geom eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $geom eq "REF" and ref $$geom eq "ARRAY"
ref $geom eq "REF" ref $$geom eq "ARRAY"
Truth table0%0/3
A and B
A= ref $geom eq "REF"B= ref $$geom eq "ARRAY"
ABresult
0X0
100
111
18 0 my ($sql, @binds) = @{$$geom};
19 0 return ($sql, @binds);
20 }
21 0 ▶ if (ref($geom) && $geom->isa('DBIO::PostgreSQL::PostGIS::Geometry')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $geom and $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")) 0 0 0
Condition0%0/3
ref $geom and $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $geom and $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ref $geom $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
Truth table0%0/3
A and B
A= ref $geomB= $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ABresult
0X0
100
111
22 0 my $srid = $geom->srid;
23 0 my $wkt = $geom->wkt;
24 0 ▶ if (defined $srid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $srid) 0 0 0
25 0 return ('ST_GeomFromText(?, ?)', $wkt, $srid);
26 }
27 0 return ('ST_GeomFromText(?)', $wkt);
28 }
29 # Plain EWKT string
30 0 return ('ST_GeomFromEWKT(?)', $geom);
31 }
32  
33 0 ▶sub _spatial_search {
Subroutine _spatial_search: not called CC=2 SCAR=17.9
34 0 my ($self, $sql_func, $col, $geom, @extra) = @_;
35 0 my ($geom_sql, @binds) = $self->_geom_arg($geom);
36 0 my $me = $self->current_source_alias;
37 0 ▶ return $self->search({
Branch: 1 branch
BranchTrueFalseTotal
@extra ? : 0 0 0
38 -bool => \[ "$sql_func(${me}.${col}, $geom_sql"
39 . (@extra ? ', ' . join(', ', ('?') x scalar @extra) : '')
40 . ')',
41 @binds, @extra ],
42 });
43 }
44  
45 5 my @_SIMPLE_PREDICATES = (
46 [ intersects => 'ST_Intersects' ],
47 [ contains => 'ST_Contains' ],
48 [ within => 'ST_Within' ],
49 [ touches => 'ST_Touches' ],
50 [ crosses => 'ST_Crosses' ],
51 [ overlaps => 'ST_Overlaps' ],
52 );
53  
54  
55 5 for my $pred (@_SIMPLE_PREDICATES) {
56 30 my ($name, $func) = @$pred;
57 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 30 ▶ *{__PACKAGE__ . "::$name"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=2.6
59 0 my ($self, $col, $geom) = @_;
60 0 return $self->_spatial_search($func, $col, $geom);
61 30 };
62 }
63  
64  
65 0 ▶sub within_distance {
Subroutine within_distance: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($self, $col, $geom, $dist) = @_;
67 0 return $self->_spatial_search('ST_DWithin', $col, $geom, $dist);
68 }
69  
70  
71 0 ▶sub bbox_intersects {
Subroutine bbox_intersects: not called CC=1 SCAR=6.9
Pod documented
72 0 my ($self, $col, $geom) = @_;
73 0 my ($geom_sql, @binds) = $self->_geom_arg($geom);
74 0 my $me = $self->current_source_alias;
75 0 return $self->search({
76 -bool => \[ "${me}.${col} && $geom_sql", @binds ],
77 });
78 }
79  
80  
81 0 ▶sub nearest_to {
Subroutine nearest_to: not called CC=2 SCAR=17.9
Pod documented
82 0 my ($self, $col, $geom, $limit) = @_;
83 0 my ($geom_sql, @binds) = $self->_geom_arg($geom);
84 0 my $me = $self->current_source_alias;
85 0 ▶ my $rs = $self->search(undef, {
Branch: 1 branch
BranchTrueFalseTotal
$limit ? : 0 0 0
86 order_by => \[ "${me}.${col} <-> $geom_sql", @binds ],
87 ($limit ? (rows => $limit) : ()),
88 });
89 0 return $rs;
90 }
91  
92  
93 0 ▶sub order_by_distance {
Subroutine order_by_distance: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($self, $col, $geom) = @_;
95 0 my ($geom_sql, @binds) = $self->_geom_arg($geom);
96 0 my $me = $self->current_source_alias;
97 0 return $self->search(undef, {
98 order_by => \[ "ST_Distance(${me}.${col}, $geom_sql)", @binds ],
99 });
100 }
101  
102  
103 0 ▶sub with_distance {
Subroutine with_distance: not called CC=2 SCAR=17.9
Pod documented
104 0 my ($self, $col, $geom, $alias) = @_;
105 0 ▶ $alias //= 'distance';
Condition0%0/2
$alias //= "distance"
l!l
00
MC/DC0%0/1
$alias //= "distance"
$alias
Truth table0%0/2
A
A= $alias
Aresult
11
00
106 0 my ($geom_sql, @binds) = $self->_geom_arg($geom);
107 0 my $me = $self->current_source_alias;
108 0 return $self->search(undef, {
109 '+select' => [ \[ "ST_Distance(${me}.${col}, $geom_sql)", @binds ] ],
110 '+as' => [ $alias ],
111 });
112 }
113  
114 5 1;
115  
116 __END__
117  
118 =pod
119  
120 =encoding UTF-8
121  
122 =head1 NAME
123  
124 DBIO::PostgreSQL::PostGIS::ResultSet - Spatial query helpers for PostGIS-aware result classes
125  
126 =head1 VERSION
127  
128 version 0.900001
129  
130 =head1 SYNOPSIS
131  
132 # Auto-set as resultset_class when load_components('PostgreSQL::PostGIS')
133 # registers the first geometry/geography column.
134  
135 # Within distance (geography metres, geometry units of the SRID)
136 my $rs = $schema->resultset('Place')->within_distance(
137 geom => $point, 1000,
138 );
139  
140 # Bounding-box / index-friendly intersect
141 my $rs = $schema->resultset('Place')->bbox_intersects(geom => $bbox);
142  
143 # Exact spatial predicates
144 $places->intersects(geom => $polygon);
145 $places->contains (geom => $point);
146 $places->within (geom => $polygon);
147  
148 # KNN order — uses the <-> operator on a GIST index
149 my $nearest = $schema->resultset('Place')->nearest_to(geom => $point, 10);
150  
151 =head1 DESCRIPTION
152  
153 Mixin of common spatial-query shortcuts. Every helper returns a chainable
154 resultset, so they compose with regular C<< ->search >> calls.
155  
156 The geometry argument can be either a
157 L<DBIO::PostgreSQL::PostGIS::Geometry> instance, an EWKT string, or a
158 raw scalarref C<\['ST_...']> for fully custom SQL.
159  
160 =head2 Generated spatial predicate methods
161  
162 The following methods are generated from C<@_SIMPLE_PREDICATES>:
163 C<intersects>, C<contains>, C<within>, C<touches>, C<crosses>, C<overlaps>.
164  
165 Each takes C<($column, $geometry)> and returns a filtered resultset using the
166 corresponding PostGIS function.
167  
168 =head1 METHODS
169  
170 =head2 within_distance
171  
172 $rs->within_distance($column, $geometry, $distance);
173  
174 Filter to rows where C<ST_DWithin($column, $geometry, $distance)> is true.
175 Distance is in metres for C<geography>, in SRID units for C<geometry>.
176  
177 =head2 bbox_intersects
178  
179 $rs->bbox_intersects($column, $geometry);
180  
181 Bounding-box overlap using the C<&&> operator. This is the cheapest
182 spatial predicate and the only one that uses a GIST index without
183 extra hints.
184  
185 =head2 nearest_to
186  
187 $rs->nearest_to($column, $geometry);
188 $rs->nearest_to($column, $geometry, $limit);
189  
190 Order rows by distance from C<$geometry> using the C<< <-> >> KNN
191 operator, optionally limited.
192  
193 =head2 order_by_distance
194  
195 $rs->order_by_distance($column, $geometry);
196  
197 Like L</nearest_to> but uses C<ST_Distance> (exact, not the KNN
198 operator). Use this when you need the actual distance values; use
199 L</nearest_to> when you just want the closest N rows.
200  
201 =head2 with_distance
202  
203 my $rs = $places->with_distance(geom => $point);
204 while (my $row = $rs->next) {
205 print $row->name, ' is ', $row->get_column('distance'), ' away';
206 }
207  
208 Selects an extra C<distance> column computed by C<ST_Distance>.
209  
210 =head1 AUTHOR
211  
212 DBIO Authors
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 Copyright (C) 2026 DBIO Authors
217  
218 This is free software; you can redistribute it and/or modify it under
219 the same terms as the Perl 5 programming language system itself.
220  
221 =cut
↑ Summary PostGIS/Codec/WKT/Parser.pm »