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