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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#!/usr/bin/ruby
#------------------------------------------------------------------------------
#
# Taginfo
#
#------------------------------------------------------------------------------
#
# taginfo.rb
#
#------------------------------------------------------------------------------
#
# Copyright (C) 2010 Jochen Topf <jochen@remote.org>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#------------------------------------------------------------------------------
require 'rubygems'
require 'sinatra/base'
require 'json'
require 'sqlite3'
require 'lib/utils.rb'
require 'lib/sql.rb'
#------------------------------------------------------------------------------
TAGCLOUD_NUMBER_OF_TAGS = 200
#------------------------------------------------------------------------------
class Taginfo < Sinatra::Base
configure do
set :app_file, __FILE__
if ARGV[0]
# production
set :host, 'localhost'
set :port, ARGV[0]
set :environment, :production
else
# test
enable :logging
end
end
# make h() method for escaping HTML available
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
before do
@db = SQL::Database.new('../../data')
@stats = Hash.new
@db.execute('SELECT key, value FROM master_stats') do |row|
@stats[row[0]] = row[1].to_i
end
@stats['objects'] = @stats['nodes'] + @stats['ways'] + @stats['relations']
@stats['object_tags'] = @stats['node_tags'] + @stats['way_tags'] + @stats['relation_tags']
@stats['nodes_with_tags_percent'] = (10000.0 * @stats['nodes_with_tags'] / @stats['nodes']).to_i.to_f / 100
@stats['tags_per_node'] = ( 100.0 * @stats['node_tags'] / @stats['nodes_with_tags']).to_i.to_f / 100
@stats['tags_per_way'] = ( 100.0 * @stats['way_tags'] / @stats['ways']).to_i.to_f / 100
@stats['tags_per_relation'] = ( 100.0 * @stats['relation_tags'] / @stats['relations']).to_i.to_f / 100
@data_until = @db.select("SELECT min(data_until) FROM master_meta").get_first_value().sub(/:..$/, '')
@breadcrumbs = []
end
after do
@db.close
end
#-------------------------------------
before do
if request.path_info =~ %r{^/api/}
content_type :json
end
end
#-------------------------------------
get '/' do
@tags = @db.select("SELECT key, scale1 FROM popular_keys ORDER BY scale1 DESC LIMIT #{ TAGCLOUD_NUMBER_OF_TAGS }").
execute().
each_with_index{ |tag, idx| tag['pos'] = (TAGCLOUD_NUMBER_OF_TAGS - idx) / TAGCLOUD_NUMBER_OF_TAGS.to_f }.
sort{ |a,b| a['key'] <=> b['key'] }
erb :index
end
['about', 'contact', 'download', 'languages'].each do |page|
get '/' + page do
@title = page.capitalize
@breadcrumbs << @title
erb page.to_sym
end
end
get '/sources/?' do
@title = 'Sources'
@breadcrumbs << @title
@sources = @db.select('SELECT * FROM master_meta ORDER BY source_name').execute()
erb :'sources/index'
end
get '/api' do
@title = 'API'
@breadcrumbs << @title
erb :api
end
get '/keys' do
@title = 'Keys'
@breadcrumbs << ['Keys']
erb :keys
end
get %r{^/keys/(.*)} do
if params[:key].nil?
@key = params[:captures][0]
else
@key = params[:key]
end
@key_html = escape_html(@key)
@key_uri = escape(@key)
@key_json = @key.to_json
@key_pp = pp_key(@key)
@title = [@key_html, 'Keys']
@breadcrumbs << ['Keys', '/keys']
@breadcrumbs << @key_html
@filter_type = get_filter()
@sel = Hash.new('')
@sel[@filter_type] = ' selected="selected"'
@count_all_values = @db.select("SELECT count_#{@filter_type} FROM db.keys").condition('key = ?', @key).get_first_value().to_i
@desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang='en' AND key=? AND value IS NULL", @key).get_first_value())
@desc = '<i>no description in wiki</i>' if @desc == ''
@prevalent_values = @db.select("SELECT value, count_#{@filter_type} AS count FROM tags").
condition('key=?', @key).
condition('count > ?', @count_all_values * 0.02).
order_by([:count], :count, 'DESC').
execute().map{ |row| [{ 'value' => row['value'], 'count' => row['count'].to_i }] }
# add "(other)" label for the rest of the values
sum = @prevalent_values.inject(0){ |sum, x| sum += x[0]['count'] }
if sum < @count_all_values
@prevalent_values << [{ 'value' => '(other)', 'count' => @count_all_values - sum }]
end
erb :key
end
get %r{^/tags/(.*)} do
if params[:captures].first.match(/=/)
kv = params[:captures].first.split('=', 2)
else
kv = [ params[:captures].first, '' ]
end
if params[:key].nil?
@key = kv[0]
else
@key = params[:key]
end
if params[:value].nil?
@value = kv[1]
else
@value = params[:value]
end
@tag = @key + '=' + @value
@key_html = escape_html(@key)
@key_uri = escape(@key)
@key_json = @key.to_json
@key_pp = pp_key(@key)
@value_html = escape_html(@value)
@value_uri = escape(@value)
@value_json = @value.to_json
@value_pp = pp_value(@value)
@title = [@key_html + '=' + @value_html, 'Tags']
@breadcrumbs << ['Keys', '/keys']
@breadcrumbs << [@key_html, '/keys/' + @key_uri]
@breadcrumbs << ( @value.length > 30 ? escape_html(@value[0,20] + '...') : @value_html)
@filter_type = get_filter()
@sel = Hash.new('')
@sel[@filter_type] = ' selected="selected"'
@count_all = @db.select('SELECT count_all FROM db.tags').condition('key = ? AND value = ?', @key, @value).get_first_value().to_i
@desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang='en' AND key=? AND value=?", @key, @value).get_first_value())
@desc = '<i>no description in wiki</i>' if @desc == ''
erb :tag
end
#--------------------------------------------------------------------------
get '/search/?' do
@title = 'Search results'
@breadcrumbs << @title
@escaped_search_string = escape_html(params[:search])
@key = @db.select('SELECT key FROM keys').
condition('key = ?', params[:search]).
get_first_value()
@substring_keys = @db.select('SELECT key FROM keys').
condition("key LIKE '%' || ? || '%' AND key != ?", params[:search], params[:search]).
order_by([:key], :key, 'ASC').
execute().
map{ |row| row['key'] }
erb :search
end
#--------------------------------------------------------------------------
get '/sources/db/?' do
@title = 'Database'
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['Database']
erb :'sources/db'
end
#--------------------------------------------------------------------------
get '/sources/wiki/?' do
@title = 'Wiki'
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['Wiki']
erb :'sources/wiki/index'
end
get '/sources/wiki/keys/?' do
@title = ['Keys', 'Wiki']
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['Wiki', '/sources/wiki']
@breadcrumbs << ['Keys']
@languages = @db.execute('SELECT language FROM wiki_languages ORDER by language').map do |row|
row['language']
end
lang_lookup = Hash.new
@languages.each_with_index do |lang, idx|
lang_lookup[lang] = idx + 1
end
@languages_lookup = @languages.map{ |lang| "'#{lang}': #{lang_lookup[lang]}" }.join(', ')
erb :'sources/wiki/keys'
end
#--------------------------------------------------------------------------
get '/sources/josm/?' do
@title = 'JOSM'
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['JOSM']
erb :'sources/josm/index'
end
get '/sources/josm/styles/?' do
@title = ['Styles', 'JOSM']
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['JOSM', '/sources/josm']
@breadcrumbs << ['Styles']
erb :'sources/josm/styles'
end
get '/sources/josm/styles/:style' do
@stylename = h(params[:style])
@title = [@stylename, 'Styles', 'JOSM']
@breadcrumbs << ['Sources', '/sources']
@breadcrumbs << ['JOSM', '/sources/josm']
@breadcrumbs << ['Styles', '/sources/josm/styles']
@breadcrumbs << @stylename
erb :'sources/josm/style'
end
#--------------------------------------------------------------------------
load 'lib/api/db.rb'
load 'lib/api/wiki.rb'
load 'lib/api/josm.rb'
load 'lib/test.rb'
# run application
run!
end
|