Primitive.pm

filter: Statementstmt 33.3% 14 / 42 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 44.4% 4 / 9 Podpod 100% 4 / 4 totaltotal 29.7% 22 / 74 CC 9 SCAR 24.1
CC
9
Cov
24%
new
20.0
role
20.0
member_of
6.0
CRAP 11.2
↑ Summary Primitive/Relation.pm »
1 # Encoding and name #_{
2  
3 =encoding utf8
4 =head1 NAME
5  
6 Geo::OSM::Primitive - Abstract base classes for the three Open Street Map primitives: L<node|Geo::OSM::Primitive::Node>, L<way|Geo::OSM::Primitive::Way> and L<relation|Geo::OSM::Primitive::Relation>.
7  
8 =cut
9 package Geo::OSM::Primitive;
10 #_}
11 #_{ use …
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 #_}
19 1 our $VERSION = 0.01;
20 #_{ Synopsis
21  
22 =head1 SYNOPSIS
23  
24 …
25  
26 =cut
27 #_}
28 #_{ Overview
29  
30 =head1 OVERVIEW
31  
32 The idea is to encapsulte methods that use OpenStreetMap data (that is possibly stored in L<Geo::OSM::DBI>.
33  
34 =cut
35  
36 #_}
37 #_{ Methods
38  
39 =head1 METHODS
40 =cut
41  
42 0 ▶sub new { #_{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
43 #_{ POD
44  
45 =head2 new
46  
47 new($osm_id, $primitive_type);
48  
49 =cut
50  
51 #_}
52  
53 0 my $class = shift;
54 0 my $id = shift;
55 0 my $type = shift;
56  
57 0 my $self = {};
58 0 bless $self, $class;
59  
60  
61 0 ▶ croak "Wrong class $class" unless $self->isa('Geo::OSM::Primitive');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Geo::OSM::Primitive") 0 0 0
62 0 ▶ croak "id $id is not an integer" if ref($id) or int($id) != $id;
Branch: 1 branch
BranchTrueFalseTotal
if ref $id or int $id != $id 0 0 0
Condition0%0/3
ref $id or int $id != $id
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $id or int $id != $id
ref $id int $id != $id
Truth table0%0/3
A or B
A= ref $idB= int $id != $id
ABresult
1X1
011
000
63  
64 0 $self->{id} = $id;
65 0 $self->{primitive_type} = $type;
66  
67 0 return $self;
68  
69 } #_}
70 0 ▶sub primitive_type { #_{
Subroutine primitive_type: not called CC=1 SCAR=6.9
Pod documented
71 #_{ POD
72  
73 =head2 primitive_type
74  
75 my $type = $osm_primitive->primitive_type();
76  
77 if ($type eq 'way') {
78 …
79 }
80 elsif ($type eq 'nod') {
81 …
82 }
83 elsif ($type eq 'rel') {
84 …
85 }
86  
87 Returns the type of the primitive: C<'nod'> (I<sic!>) if the primitive is a L<<node|Geo::OSM::Primitive::Node>>,
88 C<'way'> if the primitive is a L<<way|Geo::OSM::Primitive::Way>> or C<'rel'> if the primitive is
89 a L<<relation|Geo::OSM::Primitive::Relation>>.
90  
91 =cut
92  
93 #_}
94  
95 0 my $self=shift;
96  
97 0 return $self->{primitive_type};
98  
99 } #_}
100 0 ▶sub member_of { #_{
Subroutine member_of: not called CC=2 SCAR=17.9
Pod documented
101 #_{ POD
102  
103 =head2 member_of
104  
105 if ($osm_primitive->member_of($rel)) { …
106 }
107  
108 Tests whether the primitive is a member of the L<relation|Geo::OSM::Primitive::Relation> C<$rel>.
109 (B<TODO>: currently only checks the cache. If the membership is not found in the cache, it is not
110 further checked for its existence. That should of course be fixed once).
111  
112 =cut
113  
114 #_}
115  
116 0 my $self = shift;
117 0 my $rel = shift;
118  
119 0 ▶ croak unless $rel->isa('Geo::OSM::Primitive::Relation');
Branch: 1 branch
BranchTrueFalseTotal
unless $rel->isa("Geo::OSM::Primitive::Relation") 0 0 0
120  
121 0 return $self->{cache}->{member_of}->{$rel->{id}};
122 } #_}
123 0 ▶sub role { #_{
Subroutine role: not called CC=4 SCAR=30.0
Pod documented
124  
125 #_{ POD
126  
127 =head2 role
128  
129 my $role = $osm_primitive->role($osm_relation);
130  
131 Returns the role of C<$osm_relation> in L<<relation|Geo::OSM::Primitive::Relation>> C<<$osm_relation>>.
132 (B<TODO>: currently only works if the role had been set with L</_set_cache_role>. Of course, the role
133 should also be found if it is not in the cache.).
134  
135  
136 =cut
137  
138 #_}
139  
140  
141 0 my $self = shift;
142 0 my $rel = shift;
143  
144 0 ▶ croak unless $rel->isa('Geo::OSM::Primitive::Relation');
Branch: 1 branch
BranchTrueFalseTotal
unless $rel->isa("Geo::OSM::Primitive::Relation") 0 0 0
145  
146 0 ▶ return undef unless $self->member_of($rel);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->member_of($rel) 0 0 0
147  
148 0 ▶ if (exists $self->{cache}->{member_of}->{$rel->{id}}->{rol}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cache"}{"member_of"}{$rel->{"id"}}{"rol"}) 0 0 0
149 0 return $self->{cache}->{member_of}->{$rel->{id}}->{rol};
150 }
151  
152 0 return undef;
153  
154  
155 } #_}
156 0 ▶sub _set_cache_role { #_{
Subroutine _set_cache_role: not called CC=2 SCAR=17.9
157 #_{ POD
158  
159 =head2 _set_cache_role
160  
161 my $role = 'outer';
162 $osm_primitive->_set_cache_role($osm_relation, $role);
163  
164 This method assumes that the primitive on which it is called is a member of C<< $osm_relation >> (which must
165 be a L<Geo::OSM::Primitive::Relation>) and that the role (which is a string) is C<< $role >>.
166  
167 This method is internal and should not be called from a user of C<Geo::OSM::DBI>.
168  
169 =cut
170  
171 #_}
172  
173 0 my $self = shift;
174 0 my $rel = shift;
175 0 my $rol = shift;
176  
177 0 ▶ croak "Relation expected unless " unless $self->isa('Geo::OSM::Primitive');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Geo::OSM::Primitive") 0 0 0
178  
179 0 $self->{cache}->{member_of}->{$rel->{id}}->{rol}=$rol;
180  
181 } #_}
182  
183 #_}
184 #_{ POD: Author
185  
186 =head1 AUTHOR
187  
188 René Nyffenegger
189  
190 =cut
191  
192 #_}
193 #_{ POD: See also
194  
195 =head1 SEE ALSO
196  
197 L<Geo::OSM::Render> is a base class to render osm data. L<Geo::OSM::Render::SVG> is a derivation of that base class to
198 render SVG files.
199  
200 L<Geo::OSM::DBI> can be used to store Open Street Map data in a database. (It should be database independant (hence DBI), yet currently, it probably only works
201 with SQLite.
202  
203 =cut
204  
205 #_}
206 #_{ POD: Copyright and license
207  
208 =head1 COPYRIGHT AND LICENSE
209 Copyright © 2017 René Nyffenegger, Switzerland. All rights reserved.
210  
211 This program is free software; you can redistribute it and/or modify it
212 under the terms of the the Artistic License (2.0). You may obtain a
213 copy of the full license at: L<http://www.perlfoundation.org/artistic_license_2_0>
214 =cut
215  
216 #_}
217 #_{ POD: Source Code
218  
219 =head1 Source Code
220  
221 The source code is on L<< github|https://github.com/ReneNyffenegger/perl-Geo-OSM-Primitive >>. Meaningful pull requests are welcome.
222  
223 =cut
224  
225 #_}
226  
227 1 'tq84';
↑ Summary Primitive/Relation.pm »