source: subversion/utils/coast_import/osm.pm @ 1545

Last change on this file since 1545 was 1545, checked in by joerg, 13 years ago

insert Man pages/Copyright Info/Copright? Stubs to some of our perl Programs

File size: 5.4 KB
Line 
1package osm;
2use WWW::Curl::easy;
3
4sub new(){bless{}};
5
6sub setup(){
7  my $self = shift();
8  $self->{Username} = shift();
9  $self->{Password} = shift();
10  $self->{UserAgent} = shift();
11 
12  # You might need to change this to Easy with a capital E
13  $self->{Curl} = new WWW::Curl::easy;
14
15}
16
17sub tempfiles(){
18  my $self = shift();
19  $self->{file1} = shift();
20  $self->{file2} = shift();
21}
22
23sub uploadWay(){
24  my ($self, $Tags, @Segments) = @_;
25  $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
26 
27  my $Segments = "";
28  foreach $Segment(@Segments){
29    $Segments .= "<seg id=\"$Segment\"/>";
30  }
31 
32  my $Way = "<way id=\"0\">$Segments$Tags</way>";
33  my $OSM = "<osm version=\"0.3\">$Way</osm>";
34  my $data = "<?xml version=\"1.0\"?>\n$OSM";
35  my $path = "way/0";
36
37  my ($response, $http_code) = $self->upload($data, $path);
38  return($response);
39}
40
41sub uploadSegment(){
42  my ($self, $Node1,$Node2,$Tags) = @_;
43  $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
44 
45  my $Segment = sprintf("<segment id=\"0\" from=\"%d\" to=\"%d\">$Tags</segment>", $Node1,$Node2);
46  my $OSM = "<osm version=\"0.3\">$Segment</osm>";
47  my $data = "<?xml version=\"1.0\"?>\n$OSM";
48  my $path = "segment/0";
49
50  my ($response, $http_code) = $self->upload($data, $path);
51 
52 
53  return($response);
54}
55
56sub uploadNode(){
57  my ($self, $Lat, $Long, $Tags) = @_;
58  $Tags .= sprintf("<tag k=\"created_by\" v=\"%s\"/>", $self->{UserAgent});
59 
60  my $Node = sprintf("<node id=\"0\" lon=\"%f\" lat=\"%f\">$Tags</node>", $Long, $Lat);
61  my $OSM = "<osm version=\"0.3\">$Node</osm>";
62  my $data = "<?xml version=\"1.0\"?>\n$OSM";
63  my $path = "node/0";
64
65  my ($response, $http_code) = $self->upload($data, $path);
66 
67  return($response);
68}
69
70
71sub upload(){
72  my($self, $data, $path) = @_;
73
74  my $login = sprintf("%s:%s", $self->{Username}, $self->{Password});
75 
76  open(my $FileToSend, ">", $self->{file1});
77  print $FileToSend $data;
78  close $FileToSend;
79 
80  my $url = "http://www.openstreetmap.org/api/0.3/$path"; 
81  my $curl = $self->{Curl};
82  open(my $TxFile, "<", $self->{file1});
83  open(my $RxFile, ">",$self->{file2});
84  $curl->setopt(CURLOPT_URL,$url);
85  $curl->setopt(CURLOPT_RETURNTRANSFER,-1);
86  $curl->setopt(CURLOPT_HEADER,0);
87  $curl->setopt(CURLOPT_USERPWD,$login);
88  $curl->setopt(CURLOPT_PUT,-1);
89  $curl->setopt(CURLOPT_INFILE,$TxFile);
90  $curl->setopt(CURLOPT_INFILESIZE, -s $self->{file1});
91  $curl->setopt(CURLOPT_FILE, $RxFile);
92 
93  $curl->perform();
94  my $http_code = $curl->getinfo(CURLINFO_HTTP_CODE);
95  my $err = $curl->errbuf;
96  $curl->close();
97  close $TxFile;
98  close $RxFile;
99 
100  open(my $ResponseFile, "<", $self->{file2});
101  my $response = int(<$ResponseFile>);
102  close $ResponseFile;
103 
104  print "Code $http_code\n" if($http_code != 200);
105 
106  return($response, $http_code);
107}
108
1091;
110
111__END__
112
113=head1 NAME
114
115Geo::OSM - Upload data to OpenStreetMap
116
117=head1 SYNOPSIS
118
119  use Geo::OSM;
120  my $osm = new Geo::OSM;
121  $osm->setup("user\@domain.com","password","my_useragentname");
122  $osm->tempfiles("temp1.txt", "temp2.txt");
123 
124  $osm->uploadNode($Lat, $Long, $Tags);
125  $osm->uploadSegment($Node1, $Node2, $Tags);
126  $osm->uploadWay($Tags, @Segments);
127
128
129=head1 DESCRIPTION
130
131This module allows you to upload data to OpenStreetMap, using its REST API
132
133=head2 Methods
134
135=over 12
136
137=item C<new>
138
139Returns a new Geo::OSM object.
140
141=item C<setup($Username,$Password,$UserAgent)>
142
143Username is typically an email address, remembering that the @ symbol must be escaped in string
144
145Password is your openstreetmap password
146
147User Agent is a name to identify your program.  It will be added in the "created_by" tag to make it easier for people to identify features your program has created
148
149=item C<tempfiles($File1, $File2)>
150
151Two files that get used by CURL for sending and receiving POST data
152
153=item C<uploadNode($Lat, $Long, $Tags)>
154
155Uploads a node to OpenStreetMap, and returns the index of the new node, or 0 if it couldn't be created.
156
157Latitude and Longitude are in degrees, reference to WGS-84
158
159Tags are of the form <tag k="amenity" v="pub"/>
160
161See "Map Features" page on the OpenStreetMap wiki for a list of tags that are typically used
162
163=item C<uploadSegment($FromNode, $ToNode, $Tags)>
164
165Uploads a segment to OpenStreetMap, and returns the index of the new segment, or 0 if it couldn't be created.
166
167FromNode and ToNode are the indexes of two nodes
168
169Tags are of the form <tag k="highway" v="motorway"/>
170
171=item C<uploadWay($Tags, @Segments)>
172
173Uploads a way (list of segments) to OpenStreetMap, and returns the index of the new way, or 0 if it couldn't be created.
174
175Tags are same as used elsewhere
176
177@Segments is an array of segment IDs
178
179=back
180
181=head1 LICENSE
182
183This program is free software; you can redistribute it and/or
184modify it under the terms of the GNU General Public License
185as published by the Free Software Foundation; either version 2
186of the License, or (at your option) any later version.
187
188This program is distributed in the hope that it will be useful,
189but WITHOUT ANY WARRANTY; without even the implied warranty of
190MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191GNU General Public License for more details.
192
193You should have received a copy of the GNU General Public License
194along with this program; if not, write to the Free Software
195Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
196
197=head1 AUTHOR
198
199Oliver White - L<http://www.blibbleblobble.co.uk/>
200
201=head1 SEE ALSO
202
203L<http://openstreetmap.org/>
204
205L<http://wiki.openstreetmap.org/>
206
207=cut
Note: See TracBrowser for help on using the repository browser.