Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 273 lines (218 sloc) 7.987 kb
0cce195 tanjeff Add ruby script which will replace the shell script in near future.
tanjeff authored
1 #!/usr/bin/ruby
2
b6f3929 Clean up script.
Tanjeff-N. Moos authored
3 ################################
4 # License
5 ################################
6
9f68e1a tanjeff Add some documentation.
tanjeff authored
7 #
47850d9 Add copyright notice and GPL as textfile.
Tanjeff-N. Moos authored
8 # Copyright 2010
9 # Tanjeff Moos <tanjeff@cccmz.de>
10 # (Chaos Computer Club Mainz e.V.)
9f68e1a tanjeff Add some documentation.
tanjeff authored
11 #
47850d9 Add copyright notice and GPL as textfile.
Tanjeff-N. Moos authored
12 #
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
9f68e1a tanjeff Add some documentation.
tanjeff authored
26
b6f3929 Clean up script.
Tanjeff-N. Moos authored
27 ################################
28 # Globals
29 ################################
9f68e1a tanjeff Add some documentation.
tanjeff authored
30
31 # This script downloads data from the OpenStreetMap database and
32 # converts it to a format suitable for the OpenLayers API.
33 #
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
34 # Data is downloaded via 'wget', requesting all nodes which are tagged
b6f3929 Clean up script.
Tanjeff-N. Moos authored
35 # with certain tags. The resulting files are in XML format. They are parsed
36 # (using REXML) and text files are constructed which are fed into an OpenLayers
37 # JavaScript program to serve as overlays which makes club-mate locations (and
38 # others) visible.
9f68e1a tanjeff Add some documentation.
tanjeff authored
39
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
40
0cce195 tanjeff Add ruby script which will replace the shell script in near future.
tanjeff authored
41 # We use REXML
42 require "rexml/document"
43
b6f3929 Clean up script.
Tanjeff-N. Moos authored
44 # Parameter -d: no download (use existing XML files). Intended for development
45 # puporse.
46 if (ARGV[0] == "-d")
47 $do_download = false
48 else
49 $do_download = true
50 end
05e96b2 Add flag for easy development
Tanjeff-N. Moos authored
51
b6f3929 Clean up script.
Tanjeff-N. Moos authored
52 # URLs, file names and counters
c61f18a tanjeff Some cleanup.
tanjeff authored
53 $URL_club_mate="http://www.informationfreeway.org/api/0.6/node[club-mate=yes]"
54 $XML_club_mate="club-mate.xml"
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
55 $TXT_club_mate="club-mate.txt"
b6f3929 Clean up script.
Tanjeff-N. Moos authored
56 $count_club_mate = 0;
0cce195 tanjeff Add ruby script which will replace the shell script in near future.
tanjeff authored
57
88194bb Add icons for served/retail and use them in machMatekate.rb
Tanjeff-N. Moos authored
58 $URL_drink_club_mate="http://www.informationfreeway.org/api/0.6/node[drink:club-mate=*]"
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
59 $XML_drink_club_mate="drink_club-mate.xml"
60 $TXT_drink_club_mate="drink_club-mate.txt"
b6f3929 Clean up script.
Tanjeff-N. Moos authored
61 $count_drink_club_mate = 0;
0cce195 tanjeff Add ruby script which will replace the shell script in near future.
tanjeff authored
62
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
63 $URL_drink_afri_cola="http://www.informationfreeway.org/api/0.6/node[drink:afri-cola=*]"
64 $XML_drink_afri_cola="drink_afri-cola.xml"
65 $TXT_drink_afri_cola="drink_afri-cola.txt"
b6f3929 Clean up script.
Tanjeff-N. Moos authored
66 $count_drink_afri_cola = 0;
c61f18a tanjeff Some cleanup.
tanjeff authored
67
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
68
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
69 ################################
b6f3929 Clean up script.
Tanjeff-N. Moos authored
70 # Helper functions
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
71 ################################
72
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
73 # Download data (max. 3 tries)
b6f3929 Clean up script.
Tanjeff-N. Moos authored
74 #
75 # Give an URL and under which name the data shall be stored.
ec02747 Begin development of statistics output
Tanjeff-N. Moos authored
76 # The result is the file 'filename'#
b6f3929 Clean up script.
Tanjeff-N. Moos authored
77 # The function exits on error!
78 def download(url, filename)
79 if $do_download
80 `wget "#{url}" -t 3 -O #{filename}`
81 if $? != 0
82 puts("Error downloading matenodes.")
83 exit 1
84 end
05e96b2 Add flag for easy development
Tanjeff-N. Moos authored
85 end
99909a4 Add counter, enable wget fetching, print summary
Tanjeff-N. Moos authored
86 end
c61f18a tanjeff Some cleanup.
tanjeff authored
87
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
88
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
89 # Parse an XML file and generate an TXT file suitable for the OpenLayers
90 # javascript program.
91 #
92 # Params:
93 # infile: the filename of the XML file. This file must not include nodes
94 # which are not relevant! For each <node> in the file an entry is
95 # added to the outfile!
96 # outfile: the filename of the TXT output file
97 # drink_tag: which tag we search for. If this tag is found in a node, we
98 # determine which icon to use.
99 # description_extra: is added to the description field (can be "")
100 # icons: an hash containing icon filenames as values. The value of the
101 # drink_tag is used as key into the hash. If the key is not found,
102 # the key "default" is used, therefore always provide an icon for
103 # "default"! The value must be
104 # "path_to_icon.png\tWIDTHxHEIGHT\tOFFSETXxOFFSETY".
105 #
106 # Return Value: number of entries written to outfile
b6f3929 Clean up script.
Tanjeff-N. Moos authored
107 def parse(infile, outfile, drink_tag, description_extra, icons)
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
108 # We count the found nodes
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
109 count = 0;
110
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
111 # Scan XML file
112 doc = REXML::Document.new(File.new(infile))
62dae8a tanjeff Improve ruby script a bit
tanjeff authored
113
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
114 # Open output file and put header
115 file = File.new(outfile, File::WRONLY|File::CREAT|File::TRUNC)
116 file << "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n"
117
118 # We inspect each <node>
b6f3929 Clean up script.
Tanjeff-N. Moos authored
119 doc.elements.each("osm/node") do | node |
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
120 # Collect needed data from the tags
b6f3929 Clean up script.
Tanjeff-N. Moos authored
121 name,street,housenumber,postcode,city = nil
122 icon = ""
123
124 node.elements.each("tag") do | tag |
125 key=tag.attributes["k"]
126 value=tag.attributes["v"]
127
128 case key
129 when drink_tag
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
130 # This is the relevant tag; we determine which icon to use
b6f3929 Clean up script.
Tanjeff-N. Moos authored
131 if icons.has_key?(value)
132 icon=icons[value]
133 else
134 icon=icons["default"]
135 end
136 when "name"
137 name=value
138 when "addr:street"
139 street=value
140 when "addr:housenumber"
141 housenumber=value
142 when "addr:postcode"
143 postcode=value
144 when "addr:city"
145 city=value
146 end
147 end
148
149 # Print position
150 file << node.attributes["lat"] + "\t"
151 file << node.attributes["lon"] + "\t"
152
153 # Print title (use name tag if it was found)
154 if name != nil
155 file << name + "\t"
156 else
157 file << "Mate-Zugangspunkt\t"
158 end
159
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
160 # Put address into description field
b6f3929 Clean up script.
Tanjeff-N. Moos authored
161 description = ""
162 description += street + " " if street
163 description += housenumber if housenumber
164 description += "<br/>" if description != ""
165 description += postcode + " " if postcode
166 description += city if city
81b4c18 Bugfix: Address was not added to textfile.
Tanjeff-N. Moos authored
167 description = "(Keine Adresse angegeben)" if description == ""
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
168 # Add description_extra to description field
b6f3929 Clean up script.
Tanjeff-N. Moos authored
169 if description_extra != ""
170 description += "<br/>" + description_extra
c61f18a tanjeff Some cleanup.
tanjeff authored
171 end
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
172 # Write description field to outfile
b6f3929 Clean up script.
Tanjeff-N. Moos authored
173 file << description + "\t"
0cce195 tanjeff Add ruby script which will replace the shell script in near future.
tanjeff authored
174
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
175 # write icon information to outfile
b6f3929 Clean up script.
Tanjeff-N. Moos authored
176 file << icon
62886f2 tanjeff Write to file instead of stdout, some cleanup.
tanjeff authored
177
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
178 # Count the entry
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
179 count += 1
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
180
181 # Put newline for next entry
182 file << "\n"
05e96b2 Add flag for easy development
Tanjeff-N. Moos authored
183 end
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
184
185 # Tidy up
b6f3929 Clean up script.
Tanjeff-N. Moos authored
186 file.close()
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
187
22c444e Add comments to ruby script.
Tanjeff-N. Moos authored
188 # Return number of found nodes.
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
189 return count
99909a4 Add counter, enable wget fetching, print summary
Tanjeff-N. Moos authored
190 end
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
191
192
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
193
b6f3929 Clean up script.
Tanjeff-N. Moos authored
194 ########################
195 # tag: club-mate=yes
196 ########################
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
197
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
198
b6f3929 Clean up script.
Tanjeff-N. Moos authored
199 # download
200 download($URL_club_mate, $XML_club_mate)
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
201
b6f3929 Clean up script.
Tanjeff-N. Moos authored
202 icons = Hash.new()
203 icons["default"] = "./icon_club-mate-obsolet_37x37_-12x-25.png\t37,37\t-12,-25"
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
204
b6f3929 Clean up script.
Tanjeff-N. Moos authored
205 # Add Note about obsolete tag
206 description_extra = "<br/>HINWEIS:<br/>"
207 description_extra += "Der Tag club-mate=yes ist obsolet. Bitte benutze statt dessen drink:club-mate=*."
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
208
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
209 $count_club_mate += parse($XML_club_mate, $TXT_club_mate, "club-mate", description_extra, icons)
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
210
211
212
213 ###########################
b6f3929 Clean up script.
Tanjeff-N. Moos authored
214 # tag: drink:club-mate=*
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
215 ###########################
216
b6f3929 Clean up script.
Tanjeff-N. Moos authored
217 # download
218 download($URL_drink_club_mate, $XML_drink_club_mate)
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
219
b6f3929 Clean up script.
Tanjeff-N. Moos authored
220 icons = Hash.new()
221 icons["retail"] = "./icon_club-mate-retail_30x40_-12x-28.png\t30,40\t-12,-28"
222 icons["served"] = "./icon_club-mate-served_32x40_-12x-28.png\t32,40\t-12,-28"
223 icons["default"] = "./icon_club-mate_24x24_-12x-12.png\t24,24\t-12,-12"
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
224 $count_drink_club_mate += parse($XML_drink_club_mate, $TXT_drink_club_mate, "drink:club-mate", "", icons)
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
225
226
227
228
b6f3929 Clean up script.
Tanjeff-N. Moos authored
229 ###########################
230 # tag: drink:afri-cola=*
231 ###########################
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
232
b6f3929 Clean up script.
Tanjeff-N. Moos authored
233 # Download data
234 download($URL_drink_afri_cola, $XML_drink_afri_cola)
c03bd2d Add support for drink:afri-cola.
Tanjeff-N. Moos authored
235
b6f3929 Clean up script.
Tanjeff-N. Moos authored
236 icons = Hash.new()
237 icons["retail"] = "./icon_afri-cola-retail_30x40_-12x-28.png\t30,40\t-12,-28"
238 icons["served"] = "./icon_afri-cola-served_32x40_-12x-28.png\t32,40\t-12,-28"
239 icons["default"] = "./icon_afri-cola_24x24_-12x-12.png\t24,24\t-12,-12"
83b978a Add counter for club-mate and afri cola.
Tanjeff-N. Moos authored
240 $count_drink_afri_cola += parse($XML_drink_afri_cola, $TXT_drink_afri_cola, "drink:afri-cola", "", icons)
8699f15 tanjeff - Add note aboute obsolete club-mate=yes tag to descriptions (tested)
tanjeff authored
241
99909a4 Add counter, enable wget fetching, print summary
Tanjeff-N. Moos authored
242
243 ###########################
14408b2 Generate HTML file while parsing nodes.
Tanjeff-N. Moos authored
244 # Generate HTML code
ec02747 Begin development of statistics output
Tanjeff-N. Moos authored
245 ###########################
14408b2 Generate HTML file while parsing nodes.
Tanjeff-N. Moos authored
246 #
247 # We read a html file and substitute the following patterns:
248 # ##count_drink_afri_cola## => number of afri-cola nodes
249 # ##count_drink_club_mate## => number of club nodes (new tag)
250 # ##count_club_mate## => number of club nodes (old tag)
251 # TBD: describe all substitutions
ec02747 Begin development of statistics output
Tanjeff-N. Moos authored
252
253 infile = File.new("matekate.html.in")
254 outfile = File.new("matekate.html", "w")
255
256 infile.each_line do |line|
14408b2 Generate HTML file while parsing nodes.
Tanjeff-N. Moos authored
257 line.gsub!(/##(.*?)##/) do | match |
258 result = $&
259
260 case $1
261 when "count_club_mate"
262 result = $count_club_mate
263 when "count_drink_club_mate"
264 result = $count_drink_club_mate
265 when "count_drink_afri_cola"
266 result = $count_drink_afri_cola
ec02747 Begin development of statistics output
Tanjeff-N. Moos authored
267 end
14408b2 Generate HTML file while parsing nodes.
Tanjeff-N. Moos authored
268 result.to_s
269 end
ec02747 Begin development of statistics output
Tanjeff-N. Moos authored
270 outfile << line
271 end
272
Something went wrong with that request. Please try again.