blib/lib/DBIx/Class/GeomColumns.pm

filter: Statementstmt 27.1% 22 / 81 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 50.0% 6 / 12 Podpod 83.3% 5 / 6 totaltotal 18.8% 33 / 175 CC 29 SCAR 42.1
CC
29
Cov
16%
get_column
132.0
get_columns
72.0
store_column
56.0
CRAP 67.0
↑ Summary
1 package DBIx::Class::GeomColumns;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Geo::Converter::WKT2KML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use version; our $VERSION = qv('0.0.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 __PACKAGE__->mk_classdata( '_geom_columns' );
11 1 __PACKAGE__->mk_classdata( '_kml_columns' );
12  
13 =head1 NAME
14  
15 DBIx::Class::GeomColumns - Filter of geometry columns to access with WKT
16  
17 =head1 SYNOPSIS
18  
19 package POI;
20 __PACKAGE__->load_components(qw/GeomColumns Core/);
21 __PACKAGE__->utf8_columns('wgs84_col',{'tokyo_col' => 4301});
22 __PACKAGE__->kml_columns('kml_col');
23
24 # Then belows return the result of 'AsText(wgs84_col)'
25 $poi->wgs84_col;
26  
27 # You can also create or update 'GeomFromText($data,$srid)';
28 # below example is insert 'GeomFromText('POINT(135 35)',4301)'
29 $poi->tokyo_col('POINT(135 35)');
30 $poi->update;
31  
32 # Access by KML geometry fragment
33 $poi->kml_col;
34 $poi->kml_col('<LineString><coordinates>135,35 136,36</coordinates></LineString>');
35 $poi->update;
36  
37 =head1 DESCRIPTION
38  
39 This module allows you to access geometry columns by WKT or KML format.
40  
41 =head1 METHODS
42  
43 =head2 geom_columns
44  
45 =cut
46  
47 0 ▶sub geom_columns { shift->set_geom_columns( 'geom', @_ ) }
Subroutine geom_columns: not called CC=1 SCAR=6.9
Pod documented
48  
49 =head2 kml_columns
50  
51 =cut
52  
53 0 ▶sub kml_columns { shift->set_geom_columns( 'kml', @_ ) }
Subroutine kml_columns: not called CC=1 SCAR=6.9
Pod documented
54  
55 =head1 INTERNAL METHODS
56  
57 =head2 set_geom_column
58  
59 =cut
60  
61 0 ▶sub set_geom_columns {
Subroutine set_geom_columns: not called CC=6 SCAR=37.4
Pod undocumented
62 0 my $self = shift;
63 0 my $type = shift;
64 0 my $property = "_${type}_columns";
65  
66 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
67 0 my %args;
68 0 foreach my $elm (@_) {
69 0 ▶ my $ref = ref($elm) ? $elm : { $elm => 4326 };
Branch: 1 branch
BranchTrueFalseTotal
ref $elm ? : 0 0 0
70 0 foreach my $col ( keys %$ref ) {
71 0 ▶ $self->throw_exception("column $col doesn't exist")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($col) 0 0 0
72 unless $self->has_column($col);
73 }
74 0 %args = ( %args, %$ref );
75 }
76 0 my @keys = keys %args;
77  
78 $self->resultset_attributes(
79 {
80 0 '+select' => [ map { { 'AsText' => "me.$_" } } @keys ],
81 '+as' => \@keys,
82 }
83 );
84  
85 0 return $self->$property({ map { $_ => $args{$_} } @keys });
86 } else {
87 0 return $self->$property;
88 }
89 }
90  
91 =head2 get_column
92  
93 =cut
94  
95 0 ▶sub get_column {
Subroutine get_column: not called CC=11 SCAR=48.8
Pod documented
96 0 my ( $self, $column ) = @_;
97 0 my $value = $self->next::method($column);
98  
99 0 ▶ my $kcols = $self->_kml_columns || {};
Condition0%0/2
$self->_kml_columns || {}
l!l
00
MC/DC0%0/1
$self->_kml_columns || {}
$self->_kml_columns
Truth table0%0/2
A
A= $self->_kml_columns
Aresult
11
00
100 0 ▶ my $cols = { %{ $self->_geom_columns || {} }, %{ $kcols } };
Condition0%0/2
$self->_geom_columns || {}
l!l
00
MC/DC0%0/1
$self->_geom_columns || {}
$self->_geom_columns
Truth table0%0/2
A
A= $self->_geom_columns
Aresult
11
00
101  
102 0 ▶ if ( $cols and defined $value and ref($value) and ref($value) eq 'SCALAR' and $cols->{$column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols and defined $value and ref $value and ref $value eq "SCALAR" and $cols->{$column}) 0 0 0
Condition0%0/12
$cols and defined $value and ref $value and ref $value eq "SCALAR" and $cols->{$column}
!ll&&!rl&&r
000
$cols and defined $value and ref $value and ref $value eq "SCALAR"
!ll&&!rl&&r
000
$cols and defined $value and ref $value
!ll&&!rl&&r
000
$cols and defined $value
!ll&&!rl&&r
000
MC/DC0%0/5
$cols and defined $value and ref $value and ref $value eq "SCALAR" and $cols->{$column}
$cols defined $value ref $value ref $value eq "SCALAR" $cols->{$column}
Truth table0%0/6
A and B and C and D and E
A= $colsB= defined $valueC= ref $valueD= ref $value eq "SCALAR"E= $cols->{$column}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
103 0 $value = ${$value} . "";
104 0 $value =~ s/GeomFromText\('(.+)',\d+\)/$1/;
105 }
106  
107 0 ▶ if ( $kcols and defined $value and $kcols->{$column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kcols and defined $value and $kcols->{$column}) 0 0 0
Condition0%0/6
$kcols and defined $value and $kcols->{$column}
!ll&&!rl&&r
000
$kcols and defined $value
!ll&&!rl&&r
000
MC/DC0%0/3
$kcols and defined $value and $kcols->{$column}
$kcols defined $value $kcols->{$column}
Truth table0%0/4
A and B and C
A= $kcolsB= defined $valueC= $kcols->{$column}
ABCresult
0XX0
10X0
1100
1111
108 0 $value = wkt2kml($value);
109 }
110  
111 0 $value;
112 }
113  
114 =head2 get_columns
115  
116 =cut
117  
118 0 ▶sub get_columns {
Subroutine get_columns: not called CC=8 SCAR=42.8
Pod documented
119 0 my $self = shift;
120 0 my %data = $self->next::method(@_);
121  
122 0 ▶ my $kcols = $self->_kml_columns || {};
Condition0%0/2
$self->_kml_columns || {}
l!l
00
MC/DC0%0/1
$self->_kml_columns || {}
$self->_kml_columns
Truth table0%0/2
A
A= $self->_kml_columns
Aresult
11
00
123 0 ▶ my $cols = { %{ $self->_geom_columns || {} }, %{ $kcols } };
Condition0%0/2
$self->_geom_columns || {}
l!l
00
MC/DC0%0/1
$self->_geom_columns || {}
$self->_geom_columns
Truth table0%0/2
A
A= $self->_geom_columns
Aresult
11
00
124  
125 0 ▶ unless ( (caller(1))[3] eq 'DBIx::Class::Row::insert' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller 1)[3] eq "DBIx::Class::Row::insert") 0 0 0
126 0 foreach my $col (grep { defined $data{$_} } keys %{ $cols }) {
127 0 my $value = $data{$col};
128  
129 0 ▶ if ( ref($value) and ref($value) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq "SCALAR") 0 0 0
Condition0%0/3
ref $value and ref $value eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and ref $value eq "SCALAR"
ref $value ref $value eq "SCALAR"
Truth table0%0/3
A and B
A= ref $valueB= ref $value eq "SCALAR"
ABresult
0X0
100
111
130 0 $value = ${$value};
131 0 $value =~ s/GeomFromText\('(.+)',\d+\)/$1/;
132 }
133  
134 0 ▶ $value = wkt2kml($value) if ( $kcols->{$col} );
Branch: 1 branch
BranchTrueFalseTotal
if $kcols->{$col} 0 0 0
135  
136 0 $data{$col} = $value;
137 }
138 }
139  
140 0 %data;
141 }
142  
143 =head2 store_column
144  
145 =cut
146  
147 0 ▶sub store_column {
Subroutine store_column: not called CC=7 SCAR=40.3
Pod documented
148 0 my ( $self, $column, $value ) = @_;
149  
150 0 ▶ my $kcols = $self->_kml_columns || {};
Condition0%0/2
$self->_kml_columns || {}
l!l
00
MC/DC0%0/1
$self->_kml_columns || {}
$self->_kml_columns
Truth table0%0/2
A
A= $self->_kml_columns
Aresult
11
00
151 0 ▶ my $cols = { %{ $self->_geom_columns || {} }, %{ $kcols } };
Condition0%0/2
$self->_geom_columns || {}
l!l
00
MC/DC0%0/1
$self->_geom_columns || {}
$self->_geom_columns
Truth table0%0/2
A
A= $self->_geom_columns
Aresult
11
00
152  
153 0 ▶ if ( $cols and defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols and defined $value) 0 0 0
Condition0%0/3
$cols and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
$cols and defined $value
$cols defined $value
Truth table0%0/3
A and B
A= $colsB= defined $value
ABresult
0X0
100
111
154 0 ▶ $value = kml2wkt($value) if ( $kcols->{$column} );
Branch: 1 branch
BranchTrueFalseTotal
if $kcols->{$column} 0 0 0
155  
156 0 ▶ if ( my $srid = $cols->{$column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $srid = $cols->{$column}) 0 0 0
157 0 $value = \"GeomFromText('$value',$srid)";
158 }
159 }
160  
161 0 $self->next::method( $column, $value );
162 }
163  
164 =head1 DEPENDENCIES
165  
166 L<Geo::Converter::WKT2KML>.
167  
168 L<DBIx::Class>.
169  
170 =head1 AUTHOR
171  
172 OHTSUKA Ko-hei <nene@kokogiko.net>
173  
174 =head1 COPYRIGHT
175  
176 This program is free software; you can redistribute
177 it and/or modify it under the same terms as Perl itself.
178  
179 The full text of the license can be found in the
180 LICENSE file included with this module.
181  
182 =cut
183  
184 1 1;
185  
↑ Summary