1 |
##################################################################
|
2 |
package Geo::OSM::Write;
|
3 |
##################################################################
|
4 |
|
5 |
use Exporter;
|
6 |
@ISA = qw( Exporter );
|
7 |
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
|
8 |
@EXPORT = qw( write_osm_file );
|
9 |
|
10 |
use strict;
|
11 |
use warnings;
|
12 |
|
13 |
use Math::Trig;
|
14 |
use Data::Dumper;
|
15 |
|
16 |
use Geo::Geometry;
|
17 |
use Utils::Debug;
|
18 |
use Utils::File;
|
19 |
use Utils::Math;
|
20 |
|
21 |
|
22 |
# ------------------------------------------------------------------
|
23 |
sub tags2osm($){
|
24 |
my $obj = shift;
|
25 |
|
26 |
my $erg = "";
|
27 |
for my $k ( keys %{$obj->{tag}} ) {
|
28 |
my $v = $obj->{tag}{$k};
|
29 |
if ( ! defined $v ) {
|
30 |
warn "incomplete Object: ".Dumper($obj);
|
31 |
}
|
32 |
#next unless defined $v;
|
33 |
|
34 |
# character escaping as per http://www.w3.org/TR/REC-xml/
|
35 |
$v =~ s/&/&/g;
|
36 |
$v =~ s/\'/'/g;
|
37 |
$v =~ s/</</g;
|
38 |
$v =~ s/>/>/g;
|
39 |
$v =~ s/\"/"/g;
|
40 |
|
41 |
$erg .= " <tag k=\'$k\' v=\'$v\' />\n";
|
42 |
}
|
43 |
return $erg;
|
44 |
}
|
45 |
|
46 |
sub write_osm_file($$) { # Write an osm File
|
47 |
my $filename = shift;
|
48 |
my $osm = shift;
|
49 |
|
50 |
my $osm_nodes = $osm->{nodes};
|
51 |
my $osm_ways = $osm->{ways};
|
52 |
|
53 |
$osm->{tool} ||= "OSM-Tool";
|
54 |
my $count_nodes = 0;
|
55 |
my $count_ways = 0;
|
56 |
|
57 |
my $generate_ways=$main::generate_ways;
|
58 |
|
59 |
my $start_time=time();
|
60 |
|
61 |
printf STDERR ("Writing OSM File $filename\n") if $VERBOSE >1 || $DEBUG>1;
|
62 |
|
63 |
my $fh;
|
64 |
if ( $filename eq "-" ) {
|
65 |
$fh = IO::File->new('>&STDOUT');
|
66 |
$fh or die("cannot open STDOUT: $!");
|
67 |
} else {
|
68 |
$fh = IO::File->new(">$filename");
|
69 |
}
|
70 |
$fh->binmode(':utf8');
|
71 |
|
72 |
print $fh "<?xml version='1.0' encoding='UTF-8'?>\n";
|
73 |
print $fh "<osm version=\'0.5\' generator=\'".$osm->{tool}."\'>\n";
|
74 |
if ( defined ( $osm->{bounds} ) ) {
|
75 |
my $bounds = $osm->{bounds};
|
76 |
my $bounds_sting = "$bounds->{lat_min},$bounds->{lon_min},$bounds->{lat_max},$bounds->{lon_max}";
|
77 |
# -90,-180,90,180
|
78 |
print $fh " <bound box=\"$bounds_sting\" origin=\"OSM-perl-writer\" />\n";
|
79 |
|
80 |
}
|
81 |
|
82 |
# --- Nodes
|
83 |
for my $node_id ( sort keys %{$osm_nodes} ) {
|
84 |
next unless $node_id;
|
85 |
my $node = $osm_nodes->{$node_id};
|
86 |
my $lat = $node->{lat};
|
87 |
my $lon = $node->{lon};
|
88 |
unless ( defined($lat) && defined($lon)){
|
89 |
printf STDERR "Node '$node_id' not complete\n";
|
90 |
next;
|
91 |
}
|
92 |
print $fh " <node id=\'$node_id\' ";
|
93 |
print $fh " timestamp=\'".$node->{timestamp}."\' "
|
94 |
if defined $node->{timestamp};
|
95 |
print $fh " changeset=\'".$node->{changeset}."\' "
|
96 |
if defined $node->{changeset};
|
97 |
print $fh " lat=\'$lat\' ";
|
98 |
print $fh " lon=\'$lon\' ";
|
99 |
print $fh ">\n";
|
100 |
print $fh tags2osm($node);
|
101 |
print $fh " </node>\n";
|
102 |
$count_nodes++;
|
103 |
}
|
104 |
|
105 |
# --- Ways
|
106 |
for my $way_id ( sort keys %{$osm_ways} ) {
|
107 |
next unless $way_id;
|
108 |
my $way = $osm_ways->{$way_id};
|
109 |
next unless scalar( @{$way->{nd}} )>1;
|
110 |
|
111 |
print $fh " <way id=\'$way_id\'";
|
112 |
print $fh " timestamp=\'".$way->{timestamp}."\'"
|
113 |
if defined $way->{timestamp};
|
114 |
print $fh ">";
|
115 |
|
116 |
for my $way_nd ( @{$way->{nd}} ) {
|
117 |
next unless $way_nd;
|
118 |
print $fh " <nd ref=\'$way_nd\' />\n";
|
119 |
}
|
120 |
print $fh tags2osm($way);
|
121 |
print $fh " </way>\n";
|
122 |
$count_ways++;
|
123 |
|
124 |
}
|
125 |
|
126 |
print $fh "</osm>\n";
|
127 |
$fh->close();
|
128 |
|
129 |
if ( $VERBOSE || $DEBUG ) {
|
130 |
printf STDERR "%-35s: ",$filename;
|
131 |
printf STDERR " Wrote OSM File ".
|
132 |
"($count_nodes Nodes, $count_ways Ways)";
|
133 |
print_time($start_time);
|
134 |
}
|
135 |
|
136 |
}
|
137 |
|
138 |
1;
|
139 |
|
140 |
=head1 NAME
|
141 |
|
142 |
Geo::OSM::Write
|
143 |
|
144 |
=head1 COPYRIGHT
|
145 |
|
146 |
Copyright 2006, Jörg Ostertag
|
147 |
|
148 |
This program is free software; you can redistribute it and/or
|
149 |
modify it under the terms of the GNU General Public License
|
150 |
as published by the Free Software Foundation; either version 2
|
151 |
of the License, or (at your option) any later version.
|
152 |
|
153 |
This program is distributed in the hope that it will be useful,
|
154 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
155 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
156 |
GNU General Public License for more details.
|
157 |
|
158 |
You should have received a copy of the GNU General Public License
|
159 |
along with this program; if not, write to the Free Software
|
160 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
161 |
|
162 |
=head1 AUTHOR
|
163 |
|
164 |
Jörg Ostertag (planet-count-for-openstreetmap@ostertag.name)
|
165 |
|
166 |
=head1 SEE ALSO
|
167 |
|
168 |
http://www.openstreetmap.org/
|
169 |
|
170 |
=cut
|