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
|
# web/lib/utils.rb
# ------------------------------------------------------------------------------
# patch some convenience methods into base classes
class Fixnum
# convert to string with thin space as thousand separator
def to_s_with_ts
self.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ")
end
end
class String
def titlecase
self[0,1].upcase + self[1,self.size].downcase
end
end
class Numeric
def to_bytes
if self >= 1024*1024
unit = 'MB'
value = self / (1024*1024)
elsif self >= 1024
unit = 'kB'
value = self / 1024
else
unit = 'B'
end
value.to_i.to_s + ' ' + unit
end
end
class Float
def round_to(n=0)
(self * (10.0 ** n)).round * (10.0 ** (-n))
end
end
# ------------------------------------------------------------------------------
def title
@title = [] if @title.nil?
@title = [@title] unless @title.is_a?(Array)
@title << TaginfoConfig.get('instance.name', 'OpenStreetMap Taginfo')
@title.join(' | ')
end
def section(id)
@section = id.to_s
@section_title = (@section =~ /^(keys|tags)$/) ? t.osm[@section] : t.taginfo[@section]
end
def json_opts(format)
if format == 'json_pretty'
return { :indent => ' ', :space => ' ', :object_nl => "\n" }
else
return {}
end
end
# ------------------------------------------------------------------------------
# Escape tag key or value for XAPI according to
# http://wiki.openstreetmap.org/wiki/XAPI#Escaping
def xapi_escape(text)
text.gsub(/([|\[\]*\/=()\\])/, '\\\\\1')
end
def xapi_url(element, key, value=nil)
predicate = xapi_escape(key) + '='
if value.nil?
predicate += '*'
else
predicate += xapi_escape(value)
end
TaginfoConfig.get('xapi.url_prefix', 'http://www.informationfreeway.org/api/0.6/') + "#{ element }[#{ Rack::Utils::escape(predicate) }]"
end
def xapi_link(element, key, value=nil)
'<span class="button">' + external_link('xapi_button', 'XAPI', xapi_url(element, key, value), true) + '</span>'
end
def josm_link(element, key, value=nil)
'<span class="button">' + external_link('josm_button', 'JOSM', 'http://localhost:8111/import?url=' + Rack::Utils::escape(xapi_url(element, key, value)), true) + '</span>'
end
def quote_double(text)
text.gsub(/["\\]/, "\\\\\\0")
end
def turbo_link(count, filter, key, value=nil)
if count <= TaginfoConfig.get('turbo.max_auto', 100)
key = quote_double(key)
if value.nil?
value = '*'
else
value = '"' + quote_double(value) + '"'
end
if filter != 'all'
filter_condition = ' and type:' + filter.chop
end
url = TaginfoConfig.get('turbo.wizard_url_prefix', 'http://overpass-turbo.eu/master?') + 'w=' + Rack::Utils::escape('"' + key + '"=' + value + filter_condition.to_s + ' ' + TaginfoConfig.get('turbo.wizard_area', 'global')) + '&R'
else
template = 'key';
parameters = { :key => key }
unless value.nil?
parameters[:value] = value;
template += '-value'
end
if filter != 'all'
template += '-type'
parameters[:type] = filter.chop
end
parameters[:template] = template
url = TaginfoConfig.get('turbo.url_prefix', 'http://overpass-turbo.eu/?') + Rack::Utils::build_query(parameters)
end
return '<span class="button">' + external_link('turbo_button', '<img src="/img/turbo.png"/> overpass turbo', url, true) + '</span>'
end
def level0_link()
return '<span class="button">' + external_link('level0_button', 'Level0 Editor', '#', true) + '</span>'
end
def external_link(id, title, link, new_window=false)
target = new_window ? 'target="_blank" ' : ''
%Q{<a id="#{id}" #{target}rel="nofollow" class="extlink" href="#{link}">#{title}</a>}
end
def wiki_link(title)
prefix = '//wiki.openstreetmap.org/wiki/'
external_link('wikilink_' + title.gsub(%r{[^A-Za-z0-9]}, '_'), title, prefix + title)
end
# ------------------------------------------------------------------------------
def tagcloud_size(tag)
x = tag['scale1'].to_f / 20 + tag['pos'] / 4
(x * 40 + 12).to_i
end
def get_filter
f = params[:filter].to_s == '' ? 'all' : params[:filter]
if f !~ /^(all|nodes|ways|relations)$/
raise ArgumentError, "unknown filter"
end
f
end
def get_total(type)
key = {
'all' => 'objects',
'nodes' => 'nodes_with_tags',
'ways' => 'ways',
'relations' => 'relations' }[type]
return @db.stats(key)
end
# ------------------------------------------------------------------------------
# Escape % and _ special characters with @.
# The @ was chosen because it is not a special character in SQL, in Regexes,
# and isn't seen often in OSM tags. You must use "ESCAPE '@'" clause with LIKE!
def like_escape(param)
param.to_s.gsub(/[_%@]/, '@\0')
end
def like_prefix(param)
like_escape(param) + '%'
end
def like_contains(param)
'%' + like_escape(param) + '%'
end
# ------------------------------------------------------------------------------
# Like the 'get' method but will add a redirect for the same path with trailing / added
def get!(path, &block)
get path, &block
get path + '/' do
redirect path
end
end
# Like the 'get' method but specific for API calls, includes documentation for API calls
def api(version, path, doc=nil, &block)
API.new(version, path, doc) unless doc.nil?
get("/api/#{version}/#{path}", &block)
end
# ------------------------------------------------------------------------------
# Used in wiki api calls
def get_wiki_result(res)
return JSON.generate(res.map{ |row| {
:lang => row['lang'],
:language => ::Language[row['lang']].native_name,
:language_en => ::Language[row['lang']].english_name,
:title => row['title'],
:description => row['description'] || '',
:image => {
:title => row['image'],
:width => row['width'].to_i,
:height => row['height'].to_i,
:mime => row['mime'],
:image_url => row['image_url'],
:thumb_url_prefix => row['thumb_url_prefix'],
:thumb_url_suffix => row['thumb_url_suffix']
},
:on_node => row['on_node'].to_i == 1,
:on_way => row['on_way'].to_i == 1,
:on_area => row['on_area'].to_i == 1,
:on_relation => row['on_relation'].to_i == 1,
:tags_implies => row['tags_implies' ].split(','),
:tags_combination => row['tags_combination'].split(','),
:tags_linked => row['tags_linked' ].split(',')
}
}, json_opts(params[:format]))
end
# Used in josm api calls
def get_josm_style_rules_result(total, res)
return JSON.generate({
:page => @ap.page,
:rp => @ap.results_per_page,
:total => total,
:url => request.url,
:data => res.map{ |row| {
:key => row['k'],
:value => row['v'],
:value_bool => row['b'],
:rule => row['rule'],
:area_color => row['area_color'] ? row['area_color'].sub(/^.*#/, '#') : '',
:line_color => row['line_color'] ? row['line_color'].sub(/^.*#/, '#') : '',
:line_width => row['line_width'] ? row['line_width'].to_i : 0,
:icon => row['icon_source'] && row['icon_source'] != 'misc/deprecated.png' && row['icon_source'] != 'misc/no_icon.png' ? row['icon_source'] : ''
} }
}, json_opts(params[:format]))
end
def paging_results(array)
return [
[ :total, :INT, 'Total number of results.' ],
[ :page, :INT, 'Result page number (first has page number 1).' ],
[ :rp, :INT, 'Results per page.' ],
[ :url, :STRING, 'URL of the request.' ],
[ :data, :ARRAY_OF_HASHES, 'Array with results.', array ]
];
end
def no_paging_results(array)
return [
[ :total, :INT, 'Total number of results.' ],
[ :url, :STRING, 'URL of the request.' ],
[ :data, :ARRAY_OF_HASHES, 'Array with results.', array ]
];
end
MAX_IMAGE_WIDTH = 300
def build_image_url(row)
w = row['width'].to_i
h = row['height'].to_i
if w <= MAX_IMAGE_WIDTH
return row['image_url']
end
if w > 0 && h > 0
return "#{row['thumb_url_prefix']}#{ h <= w ? MAX_IMAGE_WIDTH : (MAX_IMAGE_WIDTH * w / h).to_i }#{ row['thumb_url_suffix'] }"
end
return nil
end
|