-
Notifications
You must be signed in to change notification settings - Fork 3
/
machMateKate.rb
executable file
·285 lines (235 loc) · 8.27 KB
/
machMateKate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/ruby
################################
# License
################################
#
# Copyright 2010
# Tanjeff Moos <[email protected]>
# (Chaos Computer Club Mainz e.V.)
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################
# Globals
################################
# This script downloads data from the OpenStreetMap database and
# converts it to a format suitable for the OpenLayers API.
#
# Data is downloaded via 'wget', requesting all nodes which are tagged
# with certain tags. The resulting files are in XML format. They are parsed
# (using REXML) and text files are constructed which are fed into an OpenLayers
# JavaScript program to serve as overlays which makes club-mate locations (and
# others) visible.
# We use REXML
require "rexml/document"
# Parameter -d: no download (use existing XML files). Intended for development
# puporse.
if (ARGV[0] == "-d")
$do_download = false
else
$do_download = true
end
# Flag, show or hide drink_xyz elements
$show_drink_no = false;
# URLs, file names and counters
$URL_drink_club_mate="http://www.overpass-api.de/api/xapi?node[drink:club-mate=*]"
$XML_drink_club_mate="drink_club-mate.xml"
$TXT_drink_club_mate="drink_club-mate.txt"
$count_drink_club_mate = 0;
$date_drink_club_mate = "";
$URL_drink_afri_cola="http://www.overpass-api.de/api/xapi?node[drink:afri-cola=*]"
$XML_drink_afri_cola="drink_afri-cola.xml"
$TXT_drink_afri_cola="drink_afri-cola.txt"
$count_drink_afri_cola = 0;
$date_drink_afri_cola = "";
# HTML file generation
$html_infile = "matekate.html.in"
$html_outfile = "matekate.html"
################################
# Helper functions
################################
# Download data (max. 3 tries)
#
# Give an URL and under which name the data shall be stored.
# The result is the file 'filename'#
# The function exits on error!
def download(url, filename)
if $do_download
`wget "#{url}" -t 3 -O #{filename}`
if $? != 0
puts("Error downloading matenodes.")
exit 1
end
end
end
# Parse an XML file and generate an TXT file suitable for the OpenLayers
# javascript program.
#
# Params:
# infile: the filename of the XML file. This file must not include nodes
# which are not relevant! For each <node> in the file an entry is
# added to the outfile!
# outfile: the filename of the TXT output file
# drink_tag: which tag we search for. If this tag is found in a node, we
# determine which icon to use.
# description_extra: is added to the description field (can be "")
# icons: an hash containing icon filenames as values. The value of the
# drink_tag is used as key into the hash. If the key is not found,
# the key "default" is used, therefore always provide an icon for
# "default"! The value must be
# "path_to_icon.png\tWIDTHxHEIGHT\tOFFSETXxOFFSETY".
#
# Return Values: number of entries written to outfile and the date of the data
# as found in the parsed infile (attribute xapi:planetDate of <osm> element).
def parse(infile, outfile, drink_tag, description_extra, icons)
# We count the found nodes
count = 0;
# Scan XML file
doc = REXML::Document.new(File.new(infile))
# Open output file and put header
file = File.new(outfile, File::WRONLY|File::CREAT|File::TRUNC)
file << "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n"
# We inspect each <node>
doc.elements.each("osm/node") do | node |
showposition = true
# Collect needed data from the tags
name,street,housenumber,postcode,city = nil
icon = ""
node.elements.each("tag") do | tag |
key=tag.attributes["k"]
value=tag.attributes["v"]
if !$show_drink_no and key == drink_tag and value == "no"
showposition = false
end
case key
when drink_tag
# This is the relevant tag; we determine which icon to use
if icons.has_key?(value)
icon=icons[value]
else
icon=icons["default"]
end
when "name"
name=value
when "addr:street"
street=value
when "addr:housenumber"
housenumber=value
when "addr:postcode"
postcode=value
when "addr:city"
city=value
end
end
# continue with next element when this one is hidden
next if !showposition
# Print position
file << node.attributes["lat"] + "\t"
file << node.attributes["lon"] + "\t"
# Print title (use name tag if it was found)
if name != nil
file << name + "\t"
else
file << "Mate-Zugangspunkt\t"
end
# Put address into description field
description = ""
description += street + " " if street
description += housenumber if housenumber
description += "<br/>" if description != ""
description += postcode + " " if postcode
description += city if city
description = "(Keine Adresse angegeben)" if description == ""
# Add description_extra to description field
if description_extra != ""
description += "<br/>" + description_extra
end
# Write description field to outfile
file << description + "\t"
# write icon information to outfile
file << icon
# Count the entry
count += 1
# Put newline for next entry
file << "\n"
end
# Tidy up
file.close()
# Read date
#date = doc.root.attributes["xapi:planetDate"]
#date = date.slice(6,2) + "." + date.slice(4,2) + "." + date.slice(0,4)
date = "20.10.2011"
# Return number of found nodes.
return count, date
end
###########################
# tag: drink:club-mate=*
###########################
# download
download($URL_drink_club_mate, $XML_drink_club_mate)
icons = Hash.new()
icons["retail"] = "./icon_club-mate-retail_30x40_-12x-28.png\t30,40\t-12,-28"
icons["served"] = "./icon_club-mate-served_32x40_-12x-28.png\t32,40\t-12,-28"
icons["no"] = "./icon_club-mate-no_24x24_-12x-12.png\t24,24\t-12,-12"
icons["default"] = "./icon_club-mate_24x24_-12x-12.png\t24,24\t-12,-12"
$count_drink_club_mate, $date_drink_club_mate =
parse($XML_drink_club_mate, $TXT_drink_club_mate, "drink:club-mate", "", icons)
###########################
# tag: drink:afri-cola=*
###########################
# Download data
download($URL_drink_afri_cola, $XML_drink_afri_cola)
icons = Hash.new()
icons["retail"] = "./icon_afri-cola-retail_30x40_-12x-28.png\t30,40\t-12,-28"
icons["served"] = "./icon_afri-cola-served_32x40_-12x-28.png\t32,40\t-12,-28"
icons["no"] = "./icon_afri-cola-no_24x24_-12x-12.png\t24,24\t-12,-12"
icons["default"] = "./icon_afri-cola_24x24_-12x-12.png\t24,24\t-12,-12"
$count_drink_afri_cola, $date_drink_afri_cola =
parse($XML_drink_afri_cola, $TXT_drink_afri_cola, "drink:afri-cola", "", icons)
###########################
# Generate HTML code
###########################
#
# We read a html file and substitute the following patterns:
#
# ##count_drink_afri_cola## => number of afri-cola nodes
#
# ##count_drink_club_mate## => number of club nodes (new tag)
#
# ##count_club_mate## => number of club nodes (old tag)
# Open files
infile = File.new($html_infile)
outfile = File.new($html_outfile, File::WRONLY|File::CREAT|File::TRUNC)
# Read one line after another, perform pattern substitution for the current
# line and write the line to outfile
infile.each_line do |line|
line.gsub!(/##(.*?)##/) do | match |
result = $&
case $1
when "count_drink_club_mate"
result = $count_drink_club_mate
when "count_drink_afri_cola"
result = $count_drink_afri_cola
when "date_drink_club_mate"
result = $date_drink_club_mate
when "date_drink_afri_cola"
result = $date_drink_afri_cola
end
result.to_s
end
outfile << line
end
# close files
infile.close()
outfile.close()