PostGIS.pm

filter: Statementstmt 100% 44 / 44 Branchbran 94.4% 17 / 18 Conditioncond 55.0% 11 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 81.9% 86 / 105 CC 17 SCAR 16.8
CC
17
Cov
88%
register_column
7.2
__ANON__
6.8
__ANON__
6.1
CRAP 5.4
« PostGIS/Geometry.pm ↑ Summary PostGIS/Storage.pm »
1 package DBIO::PostgreSQL::PostGIS;
2 # ABSTRACT: PostGIS spatial extension support for DBIO::PostgreSQL
3 4 our $VERSION = '0.900001';
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use DBIO::PostgreSQL::PostGIS::Geometry ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::PostgreSQL::PostGIS::ResultSet ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 __PACKAGE__->load_components(qw( InflateColumn ));
13  
14 1 ▶sub connection {
Subroutine connection: called CC=1 SCAR=0.0
Pod undocumented
15 1 my ($self, @info) = @_;
16 1 $self->register_storage_layer('DBIO::PostgreSQL::PostGIS::Storage');
17 1 return $self->next::method(@info);
18 }
19  
20  
21 9 ▶sub register_column {
Subroutine register_column: called CC=7 SCAR=19.8
Pod documented
22 9 my ($self, $column, $info, @rest) = @_;
23 9 $self->next::method($column, $info, @rest);
24  
25 9 ▶ my $data_type = lc($info->{data_type} // '');
Condition50%1/2
$info->{"data_type"} // ""
l!l
90
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
26 my $is_spatial =
27 exists $info->{inflate_geometry}
28 ? $info->{inflate_geometry}
29 9 ▶ : ($data_type eq 'geometry' || $data_type eq 'geography');
Branch: 1 branch
BranchTrueFalseTotal
exists $info->{'inflate_geometry'} ? : 1 8 9
Condition100%3/3
$data_type eq "geometry" or $data_type eq "geography"
l!l&&r!l&&!r
215
MC/DC100%2/2
$data_type eq "geometry" or $data_type eq "geography"
$data_type eq "geometry" $data_type eq "geography"
Truth table100%3/3
A or B
A= $data_type eq "geometry"B= $data_type eq "geography"
ABresult
1X1
011
000
30 9 ▶ return unless $is_spatial;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_spatial 6 3 9
31  
32 # Promote the resultset class once, leaving custom user subclasses alone.
33 3 my $rs_class = $self->resultset_class;
34 3 ▶ if ($rs_class && !$rs_class->isa('DBIO::PostgreSQL::PostGIS::ResultSet')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rs_class and not $rs_class->isa("DBIO::PostgreSQL::PostGIS::ResultSet")) 2 1 3
Condition66%2/3
$rs_class and not $rs_class->isa("DBIO::PostgreSQL::PostGIS::ResultSet")
!ll&&!rl&&r
012
MC/DC50%1/2
$rs_class and not $rs_class->isa("DBIO::PostgreSQL::PostGIS::ResultSet")
$rs_class not $rs_class->isa("DBIO::PostgreSQL::PostGIS::ResultSet")
Truth table66%2/3
A and not(B)
A= $rs_classB= not $rs_class->isa("DBIO::PostgreSQL::PostGIS::ResultSet")
ABresult
0X0
100
111
35 2 $self->resultset_class('DBIO::PostgreSQL::PostGIS::ResultSet');
36 }
37  
38 3 my $default_srid = $info->{srid};
39  
40 $self->inflate_column(
41 $column => {
42 4 ▶ inflate => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
43 4 my ($value, $obj) = @_;
44 4 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 3 4
45 3 ▶ return $value if ref($value) && $value->isa('DBIO::PostgreSQL::PostGIS::Geometry');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry") 0 3 3
Condition33%1/3
ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
!ll&&!rl&&r
300
MC/DC0%0/2
ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ref $value $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
Truth table33%1/3
A and B
A= ref $valueB= $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ABresult
0X0
100
111
46 # PostGIS hands EWKB-hex back by default, EWKT when ST_AsEWKT is used.
47 3 ▶ if ($value =~ /\A[0-9A-Fa-f]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A[0-9A-Fa-f]+\z/) 1 2 3
48 1 return DBIO::PostgreSQL::PostGIS::Geometry->from_ewkb_hex($value);
49 }
50 2 ▶ if ($value =~ /\ASRID=\d+;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\ASRID=\d+;/) 1 1 2
51 1 return DBIO::PostgreSQL::PostGIS::Geometry->from_ewkt($value);
52 }
53 1 return DBIO::PostgreSQL::PostGIS::Geometry->from_wkt(
54 $value, srid => $default_srid,
55 );
56 },
57 3 ▶ deflate => sub {
Subroutine __ANON__: called CC=6 SCAR=19.1
58 3 my ($value, $obj) = @_;
59 3 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 2 3
60 2 ▶ if (ref($value) && $value->isa('DBIO::PostgreSQL::PostGIS::Geometry')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")) 1 1 2
Condition66%2/3
ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
!ll&&!rl&&r
101
MC/DC50%1/2
ref $value and $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ref $value $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
Truth table66%2/3
A and B
A= ref $valueB= $value->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ABresult
0X0
100
111
61 1 ▶ return $value->ewkt // $value->wkt // $value->ewkb_hex;
Condition33%2/6
($value->ewkt // $value->wkt) // $value->ewkb_hex
l!l&&r!l&&!r
100
$value->ewkt // $value->wkt
l!l&&r!l&&!r
100
MC/DC0%0/3
($value->ewkt // $value->wkt) // $value->ewkb_hex
$value->ewkt $value->wkt $value->ewkb_hex
Truth table25%1/4
A // B // C
A= $value->ewktB= $value->wktC= $value->ewkb_hex
ABCresult
1XX1
01X1
0011
0000
62 }
63 # Already an EWKT/WKT/EWKB-hex string — pass through.
64 1 return $value;
65 },
66 },
67 3 );
68 }
69  
70  
71 4 1;
72  
73 __END__
74  
75 =pod
76  
77 =encoding UTF-8
78  
79 =head1 NAME
80  
81 DBIO::PostgreSQL::PostGIS - PostGIS spatial extension support for DBIO::PostgreSQL
82  
83 =head1 VERSION
84  
85 version 0.900001
86  
87 =head1 SYNOPSIS
88  
89 package MyApp::Schema::Result::Place;
90 use base 'DBIO::Core';
91 __PACKAGE__->load_components('PostgreSQL::PostGIS');
92 __PACKAGE__->table('place');
93 __PACKAGE__->add_columns(
94 id => { data_type => 'integer', is_auto_increment => 1 },
95 name => { data_type => 'text' },
96 geom => {
97 data_type => 'geometry',
98 geometry_type => 'POINT',
99 srid => 4326,
100 },
101 );
102  
103 # Inflated reads
104 my $place = $schema->resultset('Place')->find(1);
105 $place->geom->isa('DBIO::PostgreSQL::PostGIS::Geometry');
106 $place->geom->x; # longitude
107 $place->geom->y; # latitude
108  
109 # Deflated writes — pass a Geometry object, the helper builds EWKT
110 $place->geom(
111 DBIO::PostgreSQL::PostGIS::Geometry->point(13.4, 52.5, srid => 4326),
112 );
113 $place->update;
114  
115 # Spatial query — raw SQL via -bool literal
116 my $rs = $schema->resultset('Place')->search({
117 -bool => \['ST_DWithin(geom, ST_MakePoint(?, ?)::geography, ?)',
118 $lon, $lat, $meters],
119 });
120  
121 =head1 DESCRIPTION
122  
123 L<DBIO::PostgreSQL::PostGIS> is a result-class component that adds
124 PostGIS spatial type handling to L<DBIO> result classes:
125  
126 =over 4
127  
128 =item *
129  
130 Columns with C<< data_type => 'geometry' >> or C<< 'geography' >> are
131 automatically inflated to L<DBIO::PostgreSQL::PostGIS::Geometry>
132 objects on read and deflated back to EWKT on write.
133  
134 =item *
135  
136 Spatial-query helpers are mixed into the resultset class via
137 L<DBIO::PostgreSQL::PostGIS::ResultSet>.
138  
139 =back
140  
141 The schema-level storage extensions (C<ensure_postgis>, C<postgis_version>,
142 the PostGIS-aware deploy class) live on the
143 L<DBIO::PostgreSQL::PostGIS::Storage> B<layer>. Loading this component
144 registers that layer (via L<DBIO::Schema/register_storage_layer>), so it is
145 composed over the resolved driver storage (see L<DBIO::Storage::Composed>).
146 Loading the component is all you do:
147  
148 package MyApp::Schema;
149 use DBIO 'Schema';
150 __PACKAGE__->load_components('PostgreSQL', 'PostgreSQL::PostGIS');
151  
152 To pull in the storage layer without the result-class geometry inflation
153 (unusual), register it directly instead of loading the component:
154  
155 __PACKAGE__->register_storage_layer('DBIO::PostgreSQL::PostGIS::Storage');
156  
157 =head1 METHODS
158  
159 =head2 register_column
160  
161 Hooks into the column registration chain. When a column declares
162 C<< data_type => 'geometry' >> or C<< 'geography' >>, registers
163 inflate/deflate handlers that round-trip
164 L<DBIO::PostgreSQL::PostGIS::Geometry> objects through EWKT.
165  
166 Override the auto-detection by setting C<< inflate_geometry => 0 >>
167 explicitly on the column.
168  
169 Also promotes the result source's C<resultset_class> to
170 L<DBIO::PostgreSQL::PostGIS::ResultSet> the first time a spatial column is
171 registered, giving the resultset spatial-query helpers (C<within_distance>,
172 C<nearest_to>, etc.). If you need custom resultset methods alongside the
173 PostGIS helpers, subclass L<DBIO::PostgreSQL::PostGIS::ResultSet> and set
174 C<resultset_class> to your subclass — the promotion guard skips any class
175 that already inherits from C<DBIO::PostgreSQL::PostGIS::ResultSet>.
176  
177 =seealso
178  
179 =over 4
180  
181 =item * L<DBIO::PostgreSQL> - Base PostgreSQL driver component
182  
183 =item * L<DBIO::PostgreSQL::PostGIS::Geometry> - Lightweight geometry value object
184  
185 =item * L<DBIO::PostgreSQL::PostGIS::Storage> - Storage layer with spatial methods
186  
187 =item * L<DBIO::PostgreSQL::PostGIS::ResultSet> - Spatial query helpers
188  
189 =back
190  
191 =head1 AUTHOR
192  
193 DBIO Authors
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 Copyright (C) 2026 DBIO Authors
198  
199 This is free software; you can redistribute it and/or modify it under
200 the same terms as the Perl 5 programming language system itself.
201  
202 =cut
« PostGIS/Geometry.pm ↑ Summary PostGIS/Storage.pm »