OSM.pm

filter: Statementstmt 59.8% 70 / 117 Branchbran 42.5% 23 / 54 Conditioncond 38.4% 15 / 39 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 66.6% 12 / 18 Podpod 100% 8 / 8 totaltotal 51.3% 134 / 261 CC 45 SCAR 41.5
CC
45
Cov
51%
object_to_xml
132.0
parse
77.5
seek_to
30.0
CRAP 63.7
« OSM/Multipass.pm ↑ Summary OSM/Singlepass.pm »
1 #
2 # $Id$
3 #
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Geo::Parse::OSM;
10 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $Geo::Parse::OSM::VERSION = '0.42';
12 }
13 2 ▶use base qw{ Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our @EXPORT_OK = qw(
16 object_to_xml
17 );
18  
19 =head1 NAME
20  
21 Geo::Parse::OSM - OpenStreetMap XML file regexp parser
22  
23 =head1 VERSION
24  
25 version 0.42
26  
27 =head1 SYNOPSIS
28  
29 use Geo::Parse::OSM;
30  
31 my $osm = Geo::Parse::OSM->new( 'planet.osm.gz' );
32 $osm->seek_to_relations;
33 $osm->parse( sub{ warn $_[0]->{id} if $_[0]->{user} eq 'Alice' } );
34  
35 =cut
36  
37  
38 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use IO::Uncompress::AnyUncompress qw($AnyUncompressError);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44  
45  
46  
47 =head1 METHODS
48  
49 =head2 new
50  
51 Creates parser instance and opens file
52  
53 my $osm = Geo::Parse::OSM->new( 'planet.osm' );
54  
55 Compressed files (.gz, .bz2) are also supported.
56  
57 =cut
58  
59 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
60 1 my $class = shift;
61  
62 1 my $self = {
63 file => shift,
64 node => undef,
65 way => undef,
66 relation => undef,
67 };
68  
69 1 ▶ $self->{stream} = IO::Uncompress::AnyUncompress->new( $self->{file}, MultiStream => 1 )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"stream"} = "IO::Uncompress::AnyUncompress"->new($self->{"file"}, "MultiStream", 1) or croak(qq[Error with $self->{"file"}: $Geo::Parse::OSM::AnyUncompressError]) 1 0 1
70 or croak "Error with $self->{file}: $AnyUncompressError";
71  
72 1 bless ($self, $class);
73 1 return $self;
74 }
75  
76  
77  
78  
79 =head2 parse
80  
81 Parses file and executes callback function for every object.
82 Stops parsing if callback returns 'stop'
83  
84 $osm->parse( sub{ warn $_[0]->{id} and return 'stop' } );
85  
86 It's possible to filter out unnecessary object types
87  
88 $osm->parse( sub{ ... }, only => 'way' );
89 $osm->parse( sub{ ... }, only => [ 'way', 'relation' ] );
90  
91 =cut
92  
93 2 my @obj_types = qw{ node way relation bound bounds };
94 2 my $obj_types = join q{|}, @obj_types;
95  
96 1 ▶sub parse {
Subroutine parse: called CC=30 SCAR=43.5
Pod documented
97  
98 1 my $self = shift;
99 1 my $callback = shift;
100  
101 1 my %prop = @_;
102  
103 1 my %filter;
104 0 ▶ %filter = map { $_ => 1 } @{ $prop{only} } if exists $prop{only} && ref $prop{only};
Branch: 1 branch
BranchTrueFalseTotal
if exists $prop{"only"} and ref $prop{"only"} 0 1 1
Condition33%1/3
exists $prop{"only"} and ref $prop{"only"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $prop{"only"} and ref $prop{"only"}
exists $prop{"only"} ref $prop{"only"}
Truth table33%1/3
A and B
A= exists $prop{"only"}B= ref $prop{"only"}
ABresult
0X0
100
111
105 1 ▶ %filter = ( $prop{only} => 1 ) if exists $prop{only} && !ref $prop{only};
Branch: 1 branch
BranchTrueFalseTotal
if exists $prop{"only"} and not ref $prop{"only"} 0 1 1
Condition33%1/3
exists $prop{"only"} and not ref $prop{"only"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $prop{"only"} and not ref $prop{"only"}
exists $prop{"only"} not ref $prop{"only"}
Truth table33%1/3
A and not(B)
A= exists $prop{"only"}B= not ref $prop{"only"}
ABresult
0X0
100
111
106  
107 1 my %object;
108  
109 1 ▶ if ( exists $self->{saved} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"saved"}) 0 1 1
110 0 my $res = &$callback( $self->{saved} );
111  
112 0 ▶ delete $self->{saved} unless defined $res && $res eq 'stop' && $prop{save};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res and $res eq "stop" and $prop{"save"} 0 0 0
Condition0%0/6
defined $res and $res eq "stop" and $prop{"save"}
!ll&&!rl&&r
000
defined $res and $res eq "stop"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $res and $res eq "stop" and $prop{"save"}
defined $res $res eq "stop" $prop{"save"}
Truth table0%0/4
A and B and C
A= defined $resB= $res eq "stop"C= $prop{"save"}
ABCresult
0XX0
10X0
1100
1111
113 0 ▶ return if defined $res && $res eq 'stop';
Branch: 1 branch
BranchTrueFalseTotal
if defined $res and $res eq "stop" 0 0 0
Condition0%0/3
defined $res and $res eq "stop"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and $res eq "stop"
defined $res $res eq "stop"
Truth table0%0/3
A and B
A= defined $resB= $res eq "stop"
ABresult
0X0
100
111
114 }
115  
116 1 my $pos = tell $self->{stream};
117  
118 LINE:
119 1 ▶ while ( my $line = decode( 'utf8', $self->{stream}->getline() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = &decode("utf8", scalar $self->{"stream"}->getline)) 17 1 18
120  
121 # start of object
122 17 ▶ if ( my ($obj) = $line =~ m{ ^\s* <($obj_types) }xo ) {
Branch: 4 branches
BranchTrueFalseTotal
if (my($obj) = $line =~ / ^\s* <($obj_types) /ox) { } 5 12 17
elsif (%object and $line =~ / ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> /x) { } 3 9 12
elsif (%object and $line =~ / ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> /x) { } 5 4 9
elsif (%object and $line =~ / ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> /x) { } 0 7 7
123  
124 5 ▶ $self->{$obj} = $pos unless defined $self->{$obj};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$obj} 2 3 5
125
126 5 ▶ next LINE if %filter && !exists $filter{$obj};
Branch: 1 branch
BranchTrueFalseTotal
if %filter and not exists $filter{$obj} 0 5 5
Condition33%1/3
%filter and not exists $filter{$obj}
!ll&&!rl&&r
500
MC/DC0%0/2
%filter and not exists $filter{$obj}
%filter not exists $filter{$obj}
Truth table33%1/3
A and not(B)
A= %filterB= not exists $filter{$obj}
ABresult
0X0
100
111
127  
128 5 %object = ( type => $obj );
129
130 # ALL attributes
131 5 my @res = $line =~ m{ (?<attr>\w+) = (?<q>['"]) (?<val>.*?) \k<q> }gx;
132 5 ▶ while (@res) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) 43 5 48
133 43 my ( $attr, undef, $val ) = ( shift @res, shift @res, shift @res );
134 43 $object{$attr} = decode_entities( $val );
135 }
136 }
137 # tag
138 ▶ elsif ( %object && $line =~ m{ ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> }x ) {
Condition100%3/3
%object and $line =~ / ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> /x
!ll&&!rl&&r
363
MC/DC100%2/2
%object and $line =~ / ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> /x
%object $line =~ / ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> /x
Truth table100%3/3
A and B
A= %objectB= $line =~ / ^\s* <tag.*? \bk = (?<q>["']) (?<key>.*?) \k<q> .*? \bv = \k<q> (?<val>.*?) \k<q> /x
ABresult
0X0
100
111
139 3 $object{tag}->{ $+{key} } = decode_entities( $+{val} );
140 }
141 # node ref
142 ▶ elsif ( %object && $line =~ m{ ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> }x ) {
Condition100%3/3
%object and $line =~ / ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> /x
!ll&&!rl&&r
315
MC/DC100%2/2
%object and $line =~ / ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> /x
%object $line =~ / ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> /x
Truth table100%3/3
A and B
A= %objectB= $line =~ / ^\s* <nd.*? \bref = (?<q>["']) (?<nref>.*?) \k<q> /x
ABresult
0X0
100
111
143 5 push @{$object{chain}}, $+{nref};
144 }
145 # relation member
146 ▶ elsif ( %object && $line =~ m{ ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> }x ) {
Condition66%2/3
%object and $line =~ / ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> /x
!ll&&!rl&&r
310
MC/DC0%0/2
%object and $line =~ / ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> /x
%object $line =~ / ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> /x
Truth table66%2/3
A and B
A= %objectB= $line =~ / ^\s* <member.*? \btype = (?<q>["']) (?<type>.*?) \k<q> .*? \bref = \k<q> (?<ref>.*?) \k<q> .*? \brole = \k<q> (?<role>.*?) \k<q> /x
ABresult
0X0
100
111
147 0 push @{$object{members}}, { type => $+{type}, ref => $+{ref}, role => $+{role} };
148 }
149  
150  
151 # end of object
152 17 ▶ if ( %object && $line =~ m{ ^\s* <(?: / $object{type} | $object{type} .* / > ) }x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%object and $line =~ m[ ^\s* <(?: / $object{"type"} | $object{"type"} .* / > ) ]x) 5 12 17
Condition100%3/3
%object and $line =~ m[ ^\s* <(?: / $object{"type"} | $object{"type"} .* / > ) ]x
!ll&&!rl&&r
395
MC/DC100%2/2
%object and $line =~ m[ ^\s* <(?: / $object{"type"} | $object{"type"} .* / > ) ]x
%object $line =~ m[ ^\s* <(?: / $object{"type"} | $object{"type"} .* / > ) ]x
Truth table100%3/3
A and B
A= %objectB= $line =~ m[ ^\s* <(?: / $object{"type"} | $object{"type"} .* / > ) ]x
ABresult
0X0
100
111
153 5 my $res = &$callback( \%object );
154 5 ▶ if ( defined $res && $res eq 'stop' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res eq "stop") 0 5 5
Condition33%1/3
defined $res and $res eq "stop"
!ll&&!rl&&r
050
MC/DC0%0/2
defined $res and $res eq "stop"
defined $res $res eq "stop"
Truth table33%1/3
A and B
A= defined $resB= $res eq "stop"
ABresult
0X0
100
111
155 0 ▶ $self->{saved} = \%object if $prop{save};
Branch: 1 branch
BranchTrueFalseTotal
if $prop{"save"} 0 0 0
156 0 return;
157 }
158 5 %object = ();
159 }
160  
161 17 } continue { $pos = tell $self->{stream} }
162  
163 1 for my $type ( qw{ node way relation } ) {
164 3 ▶ $self->{$type} = $pos unless defined $self->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$type} 1 2 3
165 }
166 }
167  
168  
169 =head2 seek_to
170  
171 Seeks to the file position or to the first object of selected type.
172  
173 $osm->seek_to( 0 );
174 $osm->seek_to( 'way' );
175  
176 Can be slow on compressed files!
177  
178 =cut
179  
180 0 ▶sub seek_to {
Subroutine seek_to: not called CC=5 SCAR=34.0
Pod documented
181 0 my $self = shift;
182 0 my $obj = shift;
183  
184 0 ▶ if ( !exists $self->{$obj} || defined $self->{$obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{$obj} or defined $self->{$obj}) { } 0 0 0
Condition0%0/3
not exists $self->{$obj} or defined $self->{$obj}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{$obj} or defined $self->{$obj}
not exists $self->{$obj} defined $self->{$obj}
Truth table0%0/3
not(A) or B
A= not exists $self->{$obj}B= defined $self->{$obj}
ABresult
1X1
011
000
185 0 delete $self->{saved};
186 0 ▶ my $pos = exists $self->{$obj} ? $self->{$obj} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$obj} ? : 0 0 0
187 $self->{stream} = IO::Uncompress::AnyUncompress->new( $self->{file}, MultiStream => 0|1 )
188 0 ▶ if tell $self->{stream} > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if tell $self->{"stream"} > $pos 0 0 0
189 0 seek $self->{stream}, $pos, 0;
190 }
191 else {
192 0 ▶ parse( $self, sub{ 'stop' }, only => $obj, save => 1 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 }
194 }
195  
196  
197 =head2 seek_to_nodes
198  
199 =head2 seek_to_ways
200  
201 =head2 seek_to_relations
202  
203 $osm->seek_to_ways; # same as seek_to('way');
204  
205 =cut
206  
207 0 ▶sub seek_to_nodes { return seek_to( shift(), 'node' ) }
Subroutine seek_to_nodes: not called CC=1 SCAR=6.9
Pod documented
208  
209 0 ▶sub seek_to_ways { return seek_to( shift(), 'way' ) }
Subroutine seek_to_ways: not called CC=1 SCAR=6.9
Pod documented
210  
211 0 ▶sub seek_to_relations { return seek_to( shift(), 'relation' ) }
Subroutine seek_to_relations: not called CC=1 SCAR=6.9
Pod documented
212  
213  
214 =head2 parse_file
215  
216 Class method - creates parser instance and does one parser() pass.
217 Returns created parser object.
218  
219 use Data::Dumper;
220 Geo::Parse::OSM->parse_file( 'planet.osm', sub{ print Dumper $_[0] } );
221  
222 =cut
223  
224 1 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
225 1 my $class = shift;
226  
227 1 my ( $file, $callback ) = @_;
228  
229 1 my $obj = $class->new( $file );
230 1 $obj->parse( $callback );
231  
232 1 return $obj;
233 }
234  
235  
236  
237 =head1 FUNCTIONS
238  
239 =head2 object_to_xml
240  
241 Returns xml representation of the callback object.
242  
243 sub callback {
244 print Geo::Parse::OSM::object_to_xml( shift @_);
245 }
246 Geo::Parse::OSM->parse_file( 'planet.osm', \&callback );
247  
248 =cut
249  
250 2 my @attrorder = qw(
251 action
252 id
253 lat
254 lon
255 version
256 changeset
257 visible
258 timestamp
259 user
260 uid
261 );
262  
263 2 my %attrorder = map { $attrorder[$_-1] => $_ } ( 1 .. scalar @attrorder );
264  
265 2 my $enc = q{\x00-\x19<>&"'};
266  
267  
268 0 ▶sub object_to_xml {
Subroutine object_to_xml: not called CC=11 SCAR=48.8
Pod documented
269 0 my %obj = %{ shift() };
270  
271 0 my $type = $obj{type};
272 0 delete $obj{type};
273  
274 my $res = qq{ <$type }
275 . join( q{ },
276 0 map { qq{$_="} . encode( 'utf8', encode_entities( $obj{$_}, $enc ) ) . q{"} }
277 0 grep { ! ref $obj{$_} }
278 0 ▶ sort { (exists $attrorder{$a} ? $attrorder{$a} : 999) <=> (exists $attrorder{$b} ? $attrorder{$b} : 999) or $a cmp $b } keys %obj );
Branch: 2 branches
BranchTrueFalseTotal
exists $attrorder{$a} ? : 0 0 0
exists $attrorder{$b} ? : 0 0 0
Condition0%0/3
(exists $attrorder{$a} ? $attrorder{$a} : 999) <=> (exists $attrorder{$b} ? $attrorder{$b} : 999) || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
(exists $attrorder{$a} ? $attrorder{$a} : 999) <=> (exists $attrorder{$b} ? $attrorder{$b} : 999) || $a cmp $b
(exists $attrorder{$a} ? $attrorder{$a} : 999) <=> (exists $attrorder{$b} ? $attrorder{$b} : 999) $a cmp $b
Truth table0%0/3
A || B
A= (exists $attrorder{$a} ? $attrorder{$a} : 999) <=> (exists $attrorder{$b} ? $attrorder{$b} : 999)B= $a cmp $b
ABresult
1X1
011
000
279  
280 0 ▶ if ( grep { ref } values %obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_;} values %obj) { } 0 0 0
281 0 $res .= qq{>\n};
282 0 ▶ if ( exists $obj{chain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj{"chain"}) 0 0 0
283 0 for my $nd ( @{$obj{chain}} ) {
284 0 $res .= qq{ <nd ref="$nd"/>\n};
285 }
286 }
287 0 ▶ if ( exists $obj{members} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj{"members"}) 0 0 0
288 0 for my $nd ( @{$obj{members}} ) {
289 0 $res .= qq{ <member type="$nd->{type}" ref="$nd->{ref}" role="$nd->{role}"/>\n};
290 }
291 }
292 0 ▶ if ( exists $obj{tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj{"tag"}) 0 0 0
293 0 for my $tag ( sort keys %{$obj{tag}} ) {
294 $res .= q{ <tag k="}
295 . encode( 'utf8', encode_entities($tag, $enc) )
296 . q{" v="}
297 0 . encode( 'utf8', encode_entities($obj{tag}->{$tag}, $enc) )
298 . qq{"/>\n};
299 }
300 }
301 0 $res .= qq{ </$type>\n};
302 }
303 else {
304 0 $res .= qq{/>\n};
305 }
306  
307 0 return $res;
308 }
309  
310  
311 =head1 AUTHOR
312  
313 liosha, C<< <liosha at cpan.org> >>
314  
315 =head1 BUGS
316  
317 Please report any bugs or feature requests to C<bug-geo-parse-osm at rt.cpan.org>, or through
318 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-Parse-OSM>. I will be notified, and then you'll
319 automatically be notified of progress on your bug as I make changes.
320  
321  
322 =head1 SUPPORT
323  
324 You can find documentation for this module with the perldoc command.
325  
326 perldoc Geo::Parse::OSM
327  
328  
329 You can also look for information at:
330  
331 =over 4
332  
333 =item * RT: CPAN's request tracker
334  
335 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Parse-OSM>
336  
337 =item * AnnoCPAN: Annotated CPAN documentation
338  
339 L<http://annocpan.org/dist/Geo-Parse-OSM>
340  
341 =item * CPAN Ratings
342  
343 L<http://cpanratings.perl.org/d/Geo-Parse-OSM>
344  
345 =item * Search CPAN
346  
347 L<http://search.cpan.org/dist/Geo-Parse-OSM/>
348  
349 =back
350  
351  
352 =head1 ACKNOWLEDGEMENTS
353  
354  
355 =head1 LICENSE AND COPYRIGHT
356  
357 Copyright 2010 liosha.
358  
359 This program is free software; you can redistribute it and/or modify it
360 under the terms of either: the GNU General Public License as published
361 by the Free Software Foundation; or the Artistic License.
362  
363 See http://dev.perl.org/licenses/ for more information.
364  
365  
366 =cut
367  
368 2 1; # End of Geo::Parse::OSM
« OSM/Multipass.pm ↑ Summary OSM/Singlepass.pm »