PostGIS/Geometry.pm

filter: Statementstmt 96.0% 169 / 176 Branchbran 89.3% 109 / 122 Conditioncond 76.7% 43 / 56 MC/DCmcdc 63.6% 21 / 33 Subroutinesub 97.1% 34 / 35 Podpod 100% 24 / 24 totaltotal 89.6% 400 / 446 CC 82 SCAR 19.2
CC
82
Cov
91%
_point_ordinate
16.3
from_geojson
13.4
_dimension_flags
10.0
CRAP 6.8
« PostGIS/Codec/WKT/Builder.pm ↑ Summary PostGIS/ResultSet.pm »
1 package DBIO::PostgreSQL::PostGIS::Geometry;
2 # ABSTRACT: Lightweight PostGIS geometry/geography value object
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = '0.900007';
8  
9 10 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use DBIO::PostgreSQL::PostGIS::Codec::WKT::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 189 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
16 189 my ($class, %args) = @_;
17 my $self = {
18 srid => $args{srid},
19 wkt => $args{wkt},
20 ewkb_hex => $args{ewkb_hex},
21 geometry_type => defined $args{geometry_type} ? uc $args{geometry_type} : undef,
22 coordinates => $args{coordinates},
23 has_z => $args{has_z},
24 has_m => $args{has_m},
25 is_empty => $args{is_empty},
26 189 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'geometry_type'} ? : 142 47 189
27 189 return bless $self, $class;
28 }
29  
30  
31 41 ▶sub from_wkt {
Subroutine from_wkt: called CC=4 SCAR=14.2
Pod documented
32 41 my ($class, $wkt, %args) = @_;
33 41 ▶ DBIO::Exception->throw("from_wkt requires a WKT string") unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 1 40 41
34 40 my $parsed = DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser->parse($wkt);
35 return $class->new(
36 wkt => $wkt,
37 srid => $args{srid},
38 geometry_type => $parsed ? $parsed->{type} : undef,
39 coordinates => $parsed ? $parsed->{coords} : undef,
40 40 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$parsed ? : 40 0 40
$parsed ? : 40 0 40
41 }
42  
43  
44 28 ▶sub from_ewkt {
Subroutine from_ewkt: called CC=3 SCAR=11.0
Pod documented
45 28 my ($class, $ewkt) = @_;
46 28 ▶ DBIO::Exception->throw("from_ewkt requires an EWKT string") unless defined $ewkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ewkt 1 27 28
47 27 ▶ if ($ewkt =~ /\A\s*srid=(\d+)\s*;\s*(.+)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ewkt =~ /\A\s*srid=(\d+)\s*;\s*(.+)\z/is) 25 2 27
48 25 return $class->new(srid => $1, wkt => $2);
49 }
50 2 return $class->new(wkt => $ewkt);
51 }
52  
53  
54 72 ▶sub from_ewkb_hex {
Subroutine from_ewkb_hex: called CC=5 SCAR=16.1
Pod documented
55 72 my ($class, $hex) = @_;
56 72 ▶ DBIO::Exception->throw("from_ewkb_hex requires an EWKB hex string") unless defined $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hex 1 71 72
57 71 my $decoder = 'DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder';
58 71 my $decoded = eval { $decoder->decode_hex($hex) };
59 71 ▶ unless ($decoded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decoded) 11 60 71
60 # No value: no object.
61 11 ▶ return undef unless length $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless length $hex 1 10 11
62 # Refused by the decoder. It is a value all the same and must not
63 # read as none; its header, if it has one, says what it claims to be.
64 10 ▶ $decoded = eval { $decoder->decode_header($hex) } // {};
Condition100%2/2
eval { do { $decoder->decode_header($hex) } } // {}
l!l
55
MC/DC100%1/1
eval { do { $decoder->decode_header($hex) } } // {}
eval { do { $decoder->decode_header($hex) } }
Truth table100%2/2
A
A= eval { do { $decoder->decode_header($hex) } }
Aresult
11
00
65 }
66 return $class->new(
67 ewkb_hex => $hex,
68 srid => $decoded->{srid},
69 geometry_type => $decoded->{type},
70 coordinates => $decoded->{coords},
71 has_z => $decoded->{has_z},
72 has_m => $decoded->{has_m},
73 is_empty => $decoded->{is_empty},
74 70 );
75 }
76  
77  
78 9 ▶sub point {
Subroutine point: called CC=4 SCAR=13.9
Pod documented
79 9 my $class = shift;
80 9 my @coords;
81 9 ▶ push @coords, shift while @_ && !ref $_[0] && $_[0] =~ /\A-?[0-9.eE+\-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
@_ and not ref $_[0] and $_[0] =~ /\A-?[0-9.eE+\-]+\z/ and do { push @coords, shift() } 18 9 27
Condition83%5/6
@_ and not ref $_[0] and $_[0] =~ /\A-?[0-9.eE+\-]+\z/
!ll&&!rl&&r
2718
@_ and not ref $_[0]
!ll&&!rl&&r
2025
MC/DC66%2/3
@_ and not ref $_[0] and $_[0] =~ /\A-?[0-9.eE+\-]+\z/
@_ not ref $_[0] $_[0] =~ /\A-?[0-9.eE+\-]+\z/
Truth table75%3/4
A and not(B) and C
A= @_B= not ref $_[0]C= $_[0] =~ /\A-?[0-9.eE+\-]+\z/
ABCresult
0XX0
10X0
1100
1111
82 9 my %args = @_;
83 9 my $wkt = 'POINT(' . join(' ', @coords) . ')';
84 9 return $class->new(wkt => $wkt, srid => $args{srid}, geometry_type => 'POINT');
85 }
86  
87  
88 3 ▶sub from_lat_lon {
Subroutine from_lat_lon: called CC=1 SCAR=0.0
Pod documented
89 3 my ($class, $lat, $lon) = @_;
90 3 return $class->point($lon, $lat, srid => 4326);
91 }
92  
93  
94 5 ▶sub linestring {
Subroutine linestring: called CC=1 SCAR=0.0
Pod documented
95 5 my ($class, $coords, %args) = @_;
96 5 my $wkt = 'LINESTRING(' . join(',', map { join ' ', @$_ } @$coords) . ')';
97 5 return $class->new(wkt => $wkt, srid => $args{srid}, geometry_type => 'LINESTRING');
98 }
99  
100  
101 5 ▶sub polygon {
Subroutine polygon: called CC=1 SCAR=0.0
Pod documented
102 5 my ($class, $rings, %args) = @_;
103 my $wkt = 'POLYGON('
104 . join(',', map {
105 5 '(' . join(',', map { join ' ', @$_ } @$_) . ')'
106 } @$rings)
107 . ')';
108 5 return $class->new(wkt => $wkt, srid => $args{srid}, geometry_type => 'POLYGON');
109 }
110  
111  
112 2 ▶sub bbox_polygon {
Subroutine bbox_polygon: called CC=1 SCAR=0.0
Pod documented
113 2 my ($class, $xmin, $ymin, $xmax, $ymax, %args) = @_;
114 2 return $class->polygon(
115 [[ [$xmin,$ymin], [$xmax,$ymin], [$xmax,$ymax], [$xmin,$ymax], [$xmin,$ymin] ]],
116 %args,
117 );
118 }
119  
120  
121 10 ▶sub from_geojson {
Subroutine from_geojson: called CC=12 SCAR=26.0
Pod documented
122 10 my ($class, $gj, %args) = @_;
123 10 ▶ my $srid = exists $args{srid} ? $args{srid} : 4326;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'srid'} ? : 0 10 10
124 my $type = $gj->{type}
125 10 ▶ or DBIO::Exception->throw("from_geojson: missing 'type'");
Branch: 1 branch
BranchTrueFalseTotal
my $type = $gj->{"type"} or "DBIO::Exception"->throw("from_geojson: missing 'type'") 9 1 10
126 9 my $coords = $gj->{coordinates};
127 9 my $wkt;
128 9 ▶ if ($type eq 'Point') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'Point') { } 4 5 9
elsif ($type eq 'LineString' or $type eq 'MultiPoint') { } 3 2 5
elsif ($type eq 'Polygon' or $type eq 'MultiLineString') { } 1 1 2
elsif ($type eq 'MultiPolygon') { } 0 1 1
129 4 $wkt = 'POINT(' . join(' ', @$coords) . ')';
130 }
131 ▶ elsif ($type eq 'LineString' or $type eq 'MultiPoint') {
Condition100%3/3
$type eq "LineString" or $type eq "MultiPoint"
l!l&&r!l&&!r
122
MC/DC100%2/2
$type eq "LineString" or $type eq "MultiPoint"
$type eq "LineString" $type eq "MultiPoint"
Truth table100%3/3
A or B
A= $type eq "LineString"B= $type eq "MultiPoint"
ABresult
1X1
011
000
132 $wkt = ($type eq 'LineString' ? 'LINESTRING' : 'MULTIPOINT')
133 3 ▶ . '(' . join(',', map { join ' ', @$_ } @$coords) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'LineString' ? : 1 2 3
134 }
135 ▶ elsif ($type eq 'Polygon' or $type eq 'MultiLineString') {
Condition66%2/3
$type eq "Polygon" or $type eq "MultiLineString"
l!l&&r!l&&!r
101
MC/DC50%1/2
$type eq "Polygon" or $type eq "MultiLineString"
$type eq "Polygon" $type eq "MultiLineString"
Truth table66%2/3
A or B
A= $type eq "Polygon"B= $type eq "MultiLineString"
ABresult
1X1
011
000
136 $wkt = ($type eq 'Polygon' ? 'POLYGON' : 'MULTILINESTRING') . '('
137 1 ▶ . join(',', map { '(' . join(',', map { join ' ', @$_ } @$_) . ')' } @$coords)
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'Polygon' ? : 1 0 1
138 . ')';
139 }
140 elsif ($type eq 'MultiPolygon') {
141 $wkt = 'MULTIPOLYGON('
142 . join(',',
143 0 map { '('
144 0 . join(',', map { '(' . join(',', map { join ' ', @$_ } @$_) . ')' } @$_)
145 . ')'
146 } @$coords)
147 . ')';
148 }
149 else {
150 1 DBIO::Exception->throw("from_geojson: unsupported type '$type'");
151 }
152 8 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
$wkt =~ /\A([A-Z]+)/ ? : 8 0 8
153 wkt => $wkt,
154 srid => $srid,
155 geometry_type => uc($wkt =~ /\A([A-Z]+)/ ? $1 : ''),
156 coordinates => $coords,
157 );
158 }
159  
160  
161 36 ▶sub srid { $_[0]{srid} }
Subroutine srid: called CC=1 SCAR=0.0
Pod documented
162 253 ▶sub wkt {
Subroutine wkt: called CC=2 SCAR=6.9
Pod documented
163 253 my $self = shift;
164 253 ▶ return $self->{wkt} if defined $self->{wkt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"wkt"} 94 159 253
165 159 return $self->to_wkt;
166 }
167 77 ▶sub ewkb_hex { $_[0]{ewkb_hex} }
Subroutine ewkb_hex: called CC=1 SCAR=0.0
Pod documented
168  
169 400 ▶sub geometry_type {
Subroutine geometry_type: called CC=4 SCAR=13.9
Pod documented
170 400 my $self = shift;
171 400 ▶ return $self->{geometry_type} if defined $self->{geometry_type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"geometry_type"} 347 53 400
172 53 ▶ if (defined $self->{wkt} && $self->{wkt} =~ /\A\s*([A-Z]+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"wkt"} and $self->{"wkt"} =~ /\A\s*([A-Z]+)/i) 15 38 53
Condition66%2/3
defined $self->{"wkt"} and $self->{"wkt"} =~ /\A\s*([A-Z]+)/i
!ll&&!rl&&r
38015
MC/DC50%1/2
defined $self->{"wkt"} and $self->{"wkt"} =~ /\A\s*([A-Z]+)/i
defined $self->{"wkt"} $self->{"wkt"} =~ /\A\s*([A-Z]+)/i
Truth table66%2/3
A and B
A= defined $self->{"wkt"}B= $self->{"wkt"} =~ /\A\s*([A-Z]+)/i
ABresult
0X0
100
111
173 # ST_AsEWKT glues the M of a measured geometry to the type: POINTM(...).
174 # No geometry type ends in Z or M, so the marker is told by that.
175 15 (my $type = uc $1) =~ s/(?:ZM|Z|M)\z//;
176 15 return $self->{geometry_type} = $type;
177 }
178 38 return undef;
179 }
180  
181  
182 56 ▶sub ewkt {
Subroutine ewkt: called CC=3 SCAR=11.0
Pod documented
183 56 my $self = shift;
184 56 my $wkt = $self->wkt;
185 56 ▶ return undef unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 13 43 56
186 43 ▶ return defined $self->{srid} ? "SRID=$self->{srid};$wkt" : $wkt;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'srid'} ? : 32 11 43
187 }
188  
189  
190 199 ▶sub to_wkt {
Subroutine to_wkt: called CC=4 SCAR=13.9
Pod documented
191 199 my ($self) = @_;
192 199 ▶ return $self->{wkt} if defined $self->{wkt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"wkt"} 2 197 199
193 # The stored coordinates, not ->coordinates: without them that method
194 # asks for the WKT, which is this one.
195 197 my $coords = $self->{coordinates};
196 197 ▶ return undef unless defined $coords;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coords 94 103 197
197 # Coordinates are not a WKT without the type they are coordinates of;
198 # the builder would write '()'.
199 103 ▶ return undef unless defined $self->geometry_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->geometry_type 10 93 103
200 93 my ($has_z, $has_m) = $self->_dimension_flags($coords);
201 return DBIO::PostgreSQL::PostGIS::Codec::WKT::Builder->build({
202 type => $self->geometry_type,
203 coords => $coords,
204 has_z => $has_z,
205 has_m => $has_m,
206 is_empty => $self->{is_empty},
207 93 });
208 }
209  
210 # Why this object cannot be written to the database or bound as a helper
211 # argument, as the message of the exception for it (karr k23); undef when it
212 # can. A hex, a WKT or coordinates under a type are a writable form.
213 26 ▶sub _unwritable_reason {
Subroutine _unwritable_reason: called CC=5 SCAR=16.1
214 26 my ($self) = @_;
215 26 my $hex = $self->ewkb_hex;
216 26 ▶ return undef if defined $hex && length $hex;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hex and length $hex 7 19 26
Condition100%3/3
defined $hex and length $hex
!ll&&!rl&&r
1727
MC/DC100%2/2
defined $hex and length $hex
defined $hex length $hex
Truth table100%3/3
A and B
A= defined $hexB= length $hex
ABresult
0X0
100
111
217 19 ▶ return undef if defined $self->wkt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->wkt 11 8 19
218 return 'Geometry has coordinates but no geometry_type, so no WKT can be built from them'
219 8 ▶ if defined $self->{coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"coordinates"} 4 4 8
220 4 return 'Geometry has no ewkb_hex, wkt or coordinates, so there is nothing to write';
221 }
222  
223 # Z/M of a geometry that has no WKT to say it: the flags given to the
224 # constructor, else the number of ordinates of one point. Never the shape
225 # of the coordinate list -- its third element is a Z only for a POINT; for
226 # a line it is the third point, for a polygon the third ring.
227 93 ▶sub _dimension_flags {
Subroutine _dimension_flags: called CC=10 SCAR=23.1
228 93 my ($self, $coords) = @_;
229 93 ▶ if (defined $self->{has_z} or defined $self->{has_m}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"has_z"} or defined $self->{"has_m"}) 85 8 93
Condition100%3/3
defined $self->{"has_z"} or defined $self->{"has_m"}
l!l&&r!l&&!r
8418
MC/DC100%2/2
defined $self->{"has_z"} or defined $self->{"has_m"}
defined $self->{"has_z"} defined $self->{"has_m"}
Truth table100%3/3
A or B
A= defined $self->{"has_z"}B= defined $self->{"has_m"}
ABresult
1X1
011
000
230 85 ▶ return ($self->{has_z} ? 1 : 0, $self->{has_m} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$self->{'has_z'} ? : 25 60 85
$self->{'has_m'} ? : 23 62 85
231 }
232 8 my $point = $coords;
233 8 ▶ $point = $point->[0] while ref $point eq 'ARRAY' and ref $point->[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $point eq "ARRAY" and ref $point->[0] and do { $point = $point->[0] } 4 8 12
Condition66%2/3
ref $point eq "ARRAY" and ref $point->[0]
!ll&&!rl&&r
084
MC/DC50%1/2
ref $point eq "ARRAY" and ref $point->[0]
ref $point eq "ARRAY" ref $point->[0]
Truth table66%2/3
A and B
A= ref $point eq "ARRAY"B= ref $point->[0]
ABresult
0X0
100
111
234 8 ▶ my $ordinates = ref $point eq 'ARRAY' ? scalar @$point : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $point eq 'ARRAY' ? : 8 0 8
235 8 ▶ return ($ordinates >= 3 ? 1 : 0, $ordinates >= 4 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$ordinates >= 3 ? : 3 5 8
$ordinates >= 4 ? : 1 7 8
236 }
237  
238  
239 178 ▶sub coordinates {
Subroutine coordinates: called CC=4 SCAR=13.9
Pod documented
240 178 my $self = shift;
241 178 ▶ return $self->{coordinates} if defined $self->{coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"coordinates"} 112 66 178
242 66 my $wkt = $self->wkt;
243 66 ▶ return undef unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 42 24 66
244 24 my $parsed = DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser->parse($wkt);
245 24 ▶ return $self->{coordinates} = $parsed ? $parsed->{coords} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$parsed ? : 24 0 24
246 }
247  
248  
249 89 ▶sub _point_ordinate {
Subroutine _point_ordinate: called CC=14 SCAR=27.9
250 89 my ($self, $i) = @_;
251 89 my $type = $self->geometry_type;
252 89 ▶ return undef unless defined $type && $type eq 'POINT';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $type eq "POINT" 54 35 89
Condition100%3/3
defined $type and $type eq "POINT"
!ll&&!rl&&r
183635
MC/DC100%2/2
defined $type and $type eq "POINT"
defined $type $type eq "POINT"
Truth table100%3/3
A and B
A= defined $typeB= $type eq "POINT"
ABresult
0X0
100
111
253 35 my $c = $self->coordinates;
254 35 ▶ return undef unless ref $c eq 'ARRAY' && !ref $c->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "ARRAY" and not ref $c->[0] 6 29 35
Condition66%2/3
ref $c eq "ARRAY" and not ref $c->[0]
!ll&&!rl&&r
6029
MC/DC50%1/2
ref $c eq "ARRAY" and not ref $c->[0]
ref $c eq "ARRAY" not ref $c->[0]
Truth table66%2/3
A and not(B)
A= ref $c eq "ARRAY"B= not ref $c->[0]
ABresult
0X0
100
111
255 29 ▶ return $c->[$i] if $i < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 2 21 8 29
256 8 ▶ return undef unless @$c > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$c > 2 4 4 8
257 # A third ordinate of a measured point is M, not Z.
258 4 ▶ if (@$c == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$c == 3) 4 0 4
259 4 ▶ return undef if $self->{has_m} && !$self->{has_z};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has_m"} and not $self->{"has_z"} 0 4 4
Condition33%1/3
$self->{"has_m"} and not $self->{"has_z"}
!ll&&!rl&&r
400
MC/DC0%0/2
$self->{"has_m"} and not $self->{"has_z"}
$self->{"has_m"} not $self->{"has_z"}
Truth table33%1/3
A and not(B)
A= $self->{"has_m"}B= not $self->{"has_z"}
ABresult
0X0
100
111
260 4 my $wkt = $self->{wkt};
261 return undef if !defined $self->{has_z} && !defined $self->{has_m}
262 4 ▶ && defined $wkt && $wkt =~ /\A\s*POINT\s*M\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"has_z"} || defined $self->{"has_m"} and defined $wkt and $wkt =~ /\A\s*POINT\s*M\b/i 1 3 4
Condition44%4/9
not defined $self->{"has_z"} || defined $self->{"has_m"} and defined $wkt and $wkt =~ /\A\s*POINT\s*M\b/i
!ll&&!rl&&r
031
not defined $self->{"has_z"} || defined $self->{"has_m"} and defined $wkt
!ll&&!rl&&r
004
defined $self->{'has_z'} || defined $self->{'has_m'}
l!l&&r!l&&!r
004
MC/DC25%1/4
not defined $self->{"has_z"} || defined $self->{"has_m"} and defined $wkt and $wkt =~ /\A\s*POINT\s*M\b/i
defined $self->{'has_z'} defined $self->{'has_m'} defined $wkt $wkt =~ /\A\s*POINT\s*M\b/i
Truth table40%2/5
not(A || B) and C and D
A= defined $self->{'has_z'}B= defined $self->{'has_m'}C= defined $wktD= $wkt =~ /\A\s*POINT\s*M\b/i
ABCDresult
1XXX0
01XX0
000X0
00100
00111
263 }
264 3 return $c->[2];
265 }
266  
267 36 ▶sub x { $_[0]->_point_ordinate(0) }
Subroutine x: called CC=1 SCAR=0.0
Pod documented
268 27 ▶sub y { $_[0]->_point_ordinate(1) }
Subroutine y: called CC=1 SCAR=0.0
Pod documented
269 26 ▶sub z { $_[0]->_point_ordinate(2) }
Subroutine z: called CC=1 SCAR=0.0
Pod documented
270  
271  
272 44 ▶sub is_empty {
Subroutine is_empty: called CC=6 SCAR=17.9
Pod documented
273 44 my $self = shift;
274 44 ▶ return $self->{is_empty} ? 1 : 0 if defined $self->{is_empty};
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->{"is_empty"} 15 29 44
$self->{'is_empty'} ? : 11 4 15
275 29 my $wkt = $self->wkt;
276 # A number in the WKT is a real ordinate: EMPTY as one part of several
277 # (MULTIPOINT(EMPTY,(1 2))) does not make the whole empty.
278 29 ▶ return defined $wkt && $wkt =~ /EMPTY\b/i && $wkt !~ /[0-9]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $wkt && $wkt =~ /EMPTY\b/i && !($wkt =~ /[0-9]/) ? : 10 19 29
Condition100%6/6
defined $wkt and $wkt =~ /EMPTY\b/i and not $wkt =~ /[0-9]/
!ll&&!rl&&r
17210
defined $wkt and $wkt =~ /EMPTY\b/i
!ll&&!rl&&r
14312
MC/DC100%3/3
defined $wkt and $wkt =~ /EMPTY\b/i and not $wkt =~ /[0-9]/
defined $wkt $wkt =~ /EMPTY\b/i not $wkt =~ /[0-9]/
Truth table100%4/4
A and B and not(C)
A= defined $wktB= $wkt =~ /EMPTY\b/iC= not $wkt =~ /[0-9]/
ABCresult
0XX0
10X0
1100
1111
279 }
280  
281  
282 30 ▶sub bbox {
Subroutine bbox: called CC=3 SCAR=11.0
Pod documented
283 30 my $self = shift;
284 30 my $coords = $self->coordinates;
285 30 ▶ return undef unless defined $coords;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coords 12 18 30
286 18 my (@xs, @ys);
287 18 _walk_xy($coords, \@xs, \@ys);
288 18 ▶ return undef unless @xs;
Branch: 1 branch
BranchTrueFalseTotal
unless @xs 9 9 18
289 9 my @sx = sort { $a <=> $b } @xs;
290 9 my @sy = sort { $a <=> $b } @ys;
291 9 return [ $sx[0], $sy[0], $sx[-1], $sy[-1] ];
292 }
293  
294 60 ▶sub _walk_xy {
Subroutine _walk_xy: called CC=6 SCAR=17.9
295 60 my ($node, $xs, $ys) = @_;
296 # A member of a collection: its type does not matter for the extent.
297 60 ▶ $node = $node->{coords} if ref $node eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" 7 53 60
298 60 ▶ return unless ref $node eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "ARRAY" 0 60 60
299 60 ▶ if (@$node && !ref $node->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$node and not ref $node->[0]) 33 27 60
Condition100%3/3
@$node and not ref $node->[0]
!ll&&!rl&&r
101733
MC/DC100%2/2
@$node and not ref $node->[0]
@$node not ref $node->[0]
Truth table100%3/3
A and not(B)
A= @$nodeB= not ref $node->[0]
ABresult
0X0
100
111
300 33 push @$xs, $node->[0];
301 33 push @$ys, $node->[1];
302 33 return;
303 }
304 27 _walk_xy($_, $xs, $ys) for @$node;
305 }
306  
307  
308 60 ▶sub to_geojson {
Subroutine to_geojson: called CC=6 SCAR=18.0
Pod documented
309 60 my $self = shift;
310 60 ▶ my $type = $self->geometry_type or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->geometry_type or return undef 54 6 60
311 54 my $coords = $self->coordinates;
312 54 ▶ if ($type eq 'GEOMETRYCOLLECTION') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "GEOMETRYCOLLECTION") 7 47 54
313 # A collection from WKT has no coordinates (the parser does not read
314 # it); it is known to be empty, and then it has no members to give.
315 7 ▶ return { type => 'GeometryCollection', geometries => [] }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $coords and $self->is_empty 0 7 7
Condition66%2/3
not defined $coords and $self->is_empty
!ll&&!rl&&r
520
MC/DC0%0/2
not defined $coords and $self->is_empty
not defined $coords $self->is_empty
Truth table66%2/3
not(A) and B
A= not defined $coordsB= $self->is_empty
ABresult
0X0
100
111
316 if !defined $coords && $self->is_empty;
317 }
318 54 ▶ return undef unless defined $coords;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coords 6 48 54
319 48 return _geojson($type, $coords);
320 }
321  
322 # GeoJSON of one geometry, or a member of a collection: undef for a type
323 # GeoJSON has no name for.
324 54 ▶sub _geojson {
Subroutine _geojson: called CC=3 SCAR=11.0
325 54 my ($type, $coords) = @_;
326 54 my %map = (
327 POINT => 'Point', LINESTRING => 'LineString', POLYGON => 'Polygon',
328 MULTIPOINT => 'MultiPoint', MULTILINESTRING => 'MultiLineString',
329 MULTIPOLYGON => 'MultiPolygon',
330 );
331 # The type of a collection member comes from the codec, lower-case.
332 54 ▶ if (uc $type eq 'GEOMETRYCOLLECTION') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $type eq "GEOMETRYCOLLECTION") 5 49 54
333 5 my @geometries = map { _geojson($_->{type}, $_->{coords}) } @$coords;
334 5 return { type => 'GeometryCollection', geometries => \@geometries };
335 }
336 49 ▶ my $gj_type = $map{uc $type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $gj_type = $map{uc $type} or return undef 49 0 49
337 49 return { type => $gj_type, coordinates => $coords };
338 }
339  
340  
341 0 ▶sub to_ogr {
Subroutine to_ogr: not called CC=1 SCAR=6.9
Pod documented
342 0 my $self = shift;
343 0 require Geo::OGR;
344 0 return Geo::OGR::Geometry->new(WKT => $self->wkt);
345 }
346  
347 10 1;
348  
349 __END__
« PostGIS/Codec/WKT/Builder.pm ↑ Summary PostGIS/ResultSet.pm »