| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | package Geo::Parse::OSM::Singlepass; | ||
| 9 | BEGIN { | ||
| 10 | $Geo::Parse::OSM::Singlepass::VERSION = '0.42'; | ||
| 11 | } | ||
| 12 | use base qw{ Geo::Parse::OSM }; | ||
| 13 | |||
| 14 | use List::Util qw{ first }; | ||
| 15 | use List::MoreUtils qw{ true first_index }; | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | my %role_type = ( | ||
| 20 | q{} => 'outer', | ||
| 21 | outer => 'outer', | ||
| 22 | border => 'outer', | ||
| 23 | exclave => 'outer', | ||
| 24 | inner => 'inner', | ||
| 25 | enclave => 'inner', | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $class = shift; | ||
| 31 | |||
| 32 | my $self = $class->SUPER::new( shift ); | ||
| 33 | |||
| 34 | $self->{latlon} = {}; | ||
| 35 | $self->{waychain} = {}; | ||
| 36 | $self->{mpoly} = {}; | ||
| 37 | |||
| 38 | our %param = @_; | ||
| 39 | |||
| 40 | ## Single pass - load node coords and way chains | ||
| 41 | |||
| 42 | my $osm_pass = sub { | ||
| 43 | my ($obj, $self) = @_; | ||
| 44 | |||
| 45 | if ( $obj->{type} eq 'node' ) { | ||
| 46 | $self->{latlon}->{ $obj->{id} } = pack 'Z*Z*', $obj->{lat}, $obj->{lon}; | ||
| 47 | } | ||
| 48 | elsif ( $obj->{type} eq 'way' ) { | ||
| 49 | $self->{waychain}->{ $obj->{id} } = $obj->{chain}; | ||
| 50 | delete $self->{ways_to_load}->{$obj->{id}}; | ||
| 51 | } | ||
| 52 | elsif ( $obj->{type} eq 'relation' && (true { $_->{role} eq 'outer' } @{ $obj->{members} }) == 1 ) { | ||
| 53 | my $outer = first { $_->{role} eq 'outer' } @{ $obj->{members} }; | ||
| 54 | $self->{mpoly}->{$outer->{ref}} = | ||
| 55 | [ map { $_->{ref} } grep { $_->{role} eq 'inner' } @{ $obj->{members} } ]; | ||
| 56 | } | ||
| 57 | |||
| 58 | &{ $param{pass1} }( $obj, $self ) if exists $param{pass1}; | ||
| 59 | }; | ||
| 60 | |||
| 61 | $self->parse( $osm_pass ); | ||
| 62 | |||
| 63 | $self->seek_to(0); | ||
| 64 | |||
| 65 | bless ($self, $class); | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub parse { | ||
| 71 | |||
| 72 | our $self = shift; | ||
| 73 | our $callback = shift; | ||
| 74 | |||
| 75 | my $parse_extent = sub { | ||
| 76 | my ($obj) = @_; | ||
| 77 | |||
| 78 | # closed ways and old-style multipolygons | ||
| 79 | if ( $obj->{type} eq 'way' && $obj->{chain}->[0] eq $obj->{chain}->[-1] ) { | ||
| 80 | # $obj->{outer} = [ [ @{$obj->{chain}} ] ]; | ||
| 81 | $obj->{outer} = [ $obj->{chain} ]; | ||
| 82 | if ( exists $self->{mpoly}->{ $obj->{id} } ) { | ||
| 83 | for my $inner ( @{ $self->{mpoly}->{ $obj->{id} } } ) { | ||
| 84 | next unless exists $self->{waychain}->{$inner}; | ||
| 85 | next unless $self->{waychain}->{$inner}->[0] eq $self->{waychain}->{$inner}->[-1]; | ||
| 86 | push @{ $obj->{inner} }, $self->{waychain}->{$inner}; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | # advanced multipolygons | ||
| 92 | if ( $obj->{type} eq 'relation' && exists $obj->{tag}->{type} && $obj->{tag}->{type} =~ /multipolygon|boundary/ ) { | ||
| 93 | |||
| 94 | for my $contour_type ( 'outer', 'inner' ) { | ||
| 95 | |||
| 96 | my @list = | ||
| 97 | grep { exists $self->{waychain}->{$_} } | ||
| 98 | map { $_->{ref} } | ||
| 99 | grep { $_->{type} eq 'way' | ||
| 100 | && exists $role_type{$_->{role}} | ||
| 101 | && $role_type{$_->{role}} eq $contour_type } | ||
| 102 | @{ $obj->{members} }; | ||
| 103 | |||
| 104 | LIST: | ||
| 105 | while ( @list ) { | ||
| 106 | |||
| 107 | my $id = shift @list; | ||
| 108 | my @contour = @{ $self->{waychain}->{$id} }; | ||
| 109 | |||
| 110 | CONTOUR: | ||
| 111 | while ( 1 ) { | ||
| 112 | # closed way | ||
| 113 | if ( $contour[0] eq $contour[-1] ) { | ||
| 114 | push @{ $obj->{$contour_type} }, [ @contour ]; | ||
| 115 | next LIST; | ||
| 116 | } | ||
| 117 | |||
| 118 | my $add = first_index { $contour[-1] eq $self->{waychain}->{$_}->[0] } @list; | ||
| 119 | if ( $add > -1 ) { | ||
| 120 | pop @contour; | ||
| 121 | push @contour, @{ $self->{waychain}->{ $list[$add] } }; | ||
| 122 | |||
| 123 | splice @list, $add, 1; | ||
| 124 | next CONTOUR; | ||
| 125 | } | ||
| 126 | |||
| 127 | $add = first_index { $contour[-1] eq $self->{waychain}->{$_}->[-1] } @list; | ||
| 128 | if ( $add > -1 ) { | ||
| 129 | pop @contour; | ||
| 130 | push @contour, reverse @{ $self->{waychain}->{ $list[$add] } }; | ||
| 131 | |||
| 132 | splice @list, $add, 1; | ||
| 133 | next CONTOUR; | ||
| 134 | } | ||
| 135 | |||
| 136 | last CONTOUR; | ||
| 137 | } #contour | ||
| 138 | } # members | ||
| 139 | } # outers/inners | ||
| 140 | } # advanced multipolygon | ||
| 141 | |||
| 142 | &$callback( $obj, $self ); | ||
| 143 | }; | ||
| 144 | |||
| 145 | $self->SUPER::parse( $parse_extent, @_ ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub latlon { | ||
| 149 | my $self = shift; | ||
| 150 | my ($node_id) = @_; | ||
| 151 | |||
| 152 | return exists $self->{latlon}->{$node_id} | ||
| 153 | ? ( unpack 'Z*Z*', $self->{latlon}->{$node_id} ) | ||
| 154 | : undef; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | =head1 NAME | ||
| 161 | |||
| 162 | Geo::Parse::OSM::Singlepass - Single pass OpenStreetMap file parser | ||
| 163 | |||
| 164 | |||
| 165 | =head1 VERSION | ||
| 166 | |||
| 167 | version 0.42 | ||
| 168 | |||
| 169 | =head1 SYNOPSIS | ||
| 170 | |||
| 171 | Geo::Parse::OSM::Singlepass extends Geo::Parse::OSM class to resolve geometry on one pass. | ||
| 172 | It faster than ::Multipass, but uses much more memory and fails on old-style | ||
| 173 | multipolygons. | ||
| 174 | |||
| 175 | |||
| 176 | use Geo::Parse::OSM::Singlepass; | ||
| 177 | |||
| 178 | my $osm = Geo::Parse::OSM::Singlepass->new( 'planet.osm.gz' ); | ||
| 179 | $osm->seek_to_relations; | ||
| 180 | $osm->parse( sub{ warn $_[0]->{id} if $_[0]->{user} eq 'Alice' } ); | ||
| 181 | |||
| 182 | |||
| 183 | =head1 METHODS | ||
| 184 | |||
| 185 | =head2 new | ||
| 186 | |||
| 187 | my $osm = Geo::Parse::OSM::Singlepass->new( 'planet.osm' ); | ||
| 188 | |||
| 189 | Creates parser instance and makes one pass to load node coords, | ||
| 190 | way chains and multipolygon lists. | ||
| 191 | |||
| 192 | You can add extra custom callback function 'pass1' for every object | ||
| 193 | |||
| 194 | my $osm = Geo::Parse::OSM::Singlepass->new( 'planet.osm', pass1 => sub{ ... } ); | ||
| 195 | |||
| 196 | =head2 parse | ||
| 197 | |||
| 198 | Same as in Geo::Parse::OSM, but callback object has additional fields for multipolygon objects: | ||
| 199 | |||
| 200 | * outer - list of outer rings (ring is a closed list of node ids) | ||
| 201 | * inner - inner rings | ||
| 202 | |||
| 203 | =head2 latlon | ||
| 204 | |||
| 205 | Returns coordinates of node. | ||
| 206 | |||
| 207 | my ($lat,$lon) = $osm->latlon( '1234578' ); | ||
| 208 | |||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | liosha, C<< <liosha at cpan.org> >> | ||
| 213 | |||
| 214 | =head1 BUGS | ||
| 215 | |||
| 216 | Please report any bugs or feature requests to C<bug-geo-parse-osm at rt.cpan.org>, or through | ||
| 217 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-Parse-OSM>. I will be notified, and then you'll | ||
| 218 | automatically be notified of progress on your bug as I make changes. | ||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | =head1 SUPPORT | ||
| 224 | |||
| 225 | You can find documentation for this module with the perldoc command. | ||
| 226 | |||
| 227 | perldoc Geo::Parse::OSM | ||
| 228 | |||
| 229 | |||
| 230 | You can also look for information at: | ||
| 231 | |||
| 232 | =over 4 | ||
| 233 | |||
| 234 | =item * RT: CPAN's request tracker | ||
| 235 | |||
| 236 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Parse-OSM> | ||
| 237 | |||
| 238 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 239 | |||
| 240 | L<http://annocpan.org/dist/Geo-Parse-OSM> | ||
| 241 | |||
| 242 | =item * CPAN Ratings | ||
| 243 | |||
| 244 | L<http://cpanratings.perl.org/d/Geo-Parse-OSM> | ||
| 245 | |||
| 246 | =item * Search CPAN | ||
| 247 | |||
| 248 | L<http://search.cpan.org/dist/Geo-Parse-OSM/> | ||
| 249 | |||
| 250 | =back | ||
| 251 | |||
| 252 | |||
| 253 | =head1 ACKNOWLEDGEMENTS | ||
| 254 | |||
| 255 | |||
| 256 | =head1 LICENSE AND COPYRIGHT | ||
| 257 | |||
| 258 | Copyright 2010 liosha. | ||
| 259 | |||
| 260 | This program is free software; you can redistribute it and/or modify it | ||
| 261 | under the terms of either: the GNU General Public License as published | ||
| 262 | by the Free Software Foundation; or the Artistic License. | ||
| 263 | |||
| 264 | See http://dev.perl.org/licenses/ for more information. | ||
| 265 | |||
| 266 | |||
| 267 | =cut |