/[zanavi_public1]/navit/navit/script/osm/Geo/OSM/Upload.pm
ZANavi

Contents of /navit/navit/script/osm/Geo/OSM/Upload.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File size: 3664 byte(s)
import files
1 package osm;
2
3 use strict;
4 use warnings;
5
6
7 use WWW::Curl::easy;
8
9 sub new(){bless{}};
10
11 sub setup(){
12 my $self = shift();
13 $self->{Username} = shift();
14 $self->{Password} = shift();
15 $self->{UserAgent} = shift();
16 }
17
18 sub tempfiles(){
19 my $self = shift();
20 $self->{file1} = shift();
21 $self->{file2} = shift();
22 }
23
24 sub uploadWay(){
25 my ($self, $Tags, @Segments) = @_;
26 $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
27
28 my $Segments = "";
29 foreach $Segment(@Segments){
30 $Segments .= "<seg id=\"$Segment\"/>";
31 }
32
33 my $Way = "<way id=\"0\">$Segments$Tags</way>";
34 my $OSM = "<osm version=\"0.3\">$Way</osm>";
35 my $data = "<?xml version=\"1.0\"?>\n$OSM";
36 my $path = "way/0";
37
38 my ($response, $http_code) = $self->upload($data, $path);
39 return($response);
40 }
41
42 sub uploadSegment(){
43 my ($self, $Node1,$Node2,$Tags) = @_;
44 $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
45
46 my $Segment = sprintf("<segment id=\"0\" from=\"%d\" to=\"%d\">$Tags</segment>", $Node1,$Node2);
47 my $OSM = "<osm version=\"0.3\">$Segment</osm>";
48 my $data = "<?xml version=\"1.0\"?>\n$OSM";
49 my $path = "segment/0";
50
51 my ($response, $http_code) = $self->upload($data, $path);
52
53
54 return($response);
55 }
56
57 sub uploadNode(){
58 my ($self, $Lat, $Long, $Tags) = @_;
59 $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
60
61 my $Node = sprintf("<node id=\"0\" lon=\"%f\" lat=\"%f\">$Tags</node>", $Long, $Lat);
62 my $OSM = "<osm version=\"0.3\">$Node</osm>";
63 my $data = "<?xml version=\"1.0\"?>\n$OSM";
64 my $path = "node/0";
65
66 my ($response, $http_code) = $self->upload($data, $path);
67
68 return($response);
69 }
70
71 sub upload(){
72 my($self, $data, $path) = @_;
73
74 my $curl = new WWW::Curl::easy;
75
76 my $login = sprintf("%s:%s", $self->{Username}, $self->{Password});
77
78 open(my $FileToSend, ">", $self->{file1});
79 print $FileToSend $data;
80 close $FileToSend;
81
82 my $url = "http://www.openstreetmap.org/api/0.3/$path";
83
84 open(my $TxFile, "<", $self->{file1});
85 open(my $RxFile, ">",$self->{file2});
86 $curl->setopt(CURLOPT_URL,$url);
87 $curl->setopt(CURLOPT_RETURNTRANSFER,-1);
88 $curl->setopt(CURLOPT_HEADER,0);
89 $curl->setopt(CURLOPT_USERPWD,$login);
90 $curl->setopt(CURLOPT_PUT,-1);
91 $curl->setopt(CURLOPT_INFILE,$TxFile);
92 $curl->setopt(CURLOPT_INFILESIZE, -s $self->{file1});
93 $curl->setopt(CURLOPT_FILE, $RxFile);
94
95 $curl->perform();
96 my $http_code = $curl->getinfo(CURLINFO_HTTP_CODE);
97 my $err = $curl->errbuf;
98 $curl->close();
99 close $TxFile;
100 close $RxFile;
101
102 open(my $ResponseFile, "<", $self->{file2});
103 my $response = int(<$ResponseFile>);
104 close $ResponseFile;
105
106 print "Code $http_code\n" if($http_code != 200);
107
108 return($response, $http_code);
109 }
110
111 1;
112
113
114 =head1 NAME
115
116 Geo::OSM::Upload
117
118 =head1 COPYRIGHT
119
120 Copyright 2006, Jörg Ostertag
121
122 This program is free software; you can redistribute it and/or
123 modify it under the terms of the GNU General Public License
124 as published by the Free Software Foundation; either version 2
125 of the License, or (at your option) any later version.
126
127 This program is distributed in the hope that it will be useful,
128 but WITHOUT ANY WARRANTY; without even the implied warranty of
129 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130 GNU General Public License for more details.
131
132 You should have received a copy of the GNU General Public License
133 along with this program; if not, write to the Free Software
134 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
135
136 =head1 AUTHOR
137
138 Jörg Ostertag (planet-count-for-openstreetmap@ostertag.name)
139
140 =head1 SEE ALSO
141
142 http://www.openstreetmap.org/
143
144 =cut
145

   
Visit the ZANavi Wiki