-
Notifications
You must be signed in to change notification settings - Fork 208
/
map.rb
executable file
·309 lines (264 loc) · 8.12 KB
/
map.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
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
class Map < ApplicationRecord
include ActiveModel::Validations
extend FriendlyId
friendly_id :name, use: %i(slugged static)
module Status
VALUES = [
BANNED = 0, # Usage: Status::BANNED
NORMAL = 1, # Usage: Status::NORMAL
MODERATED = 4, # Usage: Status::MODERATED
].freeze
end
attr_accessor :image_urls
validates_presence_of :name, :slug, :author, :lat, :lon
validates_uniqueness_of :slug
validates_presence_of :location, message: ' cannot be found. Try entering a latitude and longitude if this problem persists.'
# validates_format_of :slug,
# :with => /^[\w-]*$/,
# :message => " must not include spaces and must be alphanumeric, as it'll be used in the URL of your map, like: https://mapknitter.org/maps/your-map-name. You may use dashes and underscores.",
# :on => :create
# validates_format_of :tile_url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
validates_with NotAtOriginValidator
validates :lat, :lon, NotAtOrigin: true
has_many :exports, dependent: :destroy
has_many :tags, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :annotations, dependent: :destroy
belongs_to :user, optional: true
has_many :warpables
scope :active, -> { where(status: Status::NORMAL) }
scope :has_user, -> { where('user_id != ?', 0) }
def validate
lat >= -90 && lat <= 90 && lon >= -180 && lat <= 180 if name != 'untitled'
end
# Hash the password before saving the record.
def before_create
self.password = Password.update(password) if password != ""
end
def placed_warpables
warpables.where('width > 0 AND nodes <> ""')
end
def private
password != ""
end
def anonymous?
author == "anonymous" || user_id.zero?
end
def self.anonymous
Map.active.where(user_id: 0)
end
def self.bbox(minlat, minlon, maxlat, maxlon, tag = nil)
if tag.nil?
Map.active.where(
['lat > ? AND lat < ? AND lon > ? AND lon < ?',
minlat, maxlat, minlon, maxlon,]
)
else
Map.active.where(
['lat > ? AND lat < ? AND lon > ? AND lon < ?',
minlat, maxlat, minlon, maxlon,]
).joins(:tags).where("tags.name = ?", tag)
end
end
def exporting?
export&.running?
end
def export
latest_export
end
def latest_export
exports.last
end
def self.authors(limit = 50)
Map.where(status: Status::NORMAL, password: '')
.limit(limit)
.order("maps.id DESC")
.collect(&:author)
end
def self.search(query)
query = query.squeeze(' ').strip
Map.active.where([
'author LIKE ? OR name LIKE ? OR location LIKE ? OR description LIKE ?',
"%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%",
])
end
def self.featured
Map.active.joins(:warpables)
.select('maps.*, count(maps.id) as image_count')
.group('warpables.map_id')
.order('image_count DESC')
end
def self.new_maps
Map.where(status: Status::NORMAL, password: '')
.order('created_at DESC')
.limit(12)
end
def self.map
Map.where(status: Status::NORMAL, password: '')
.select('author, maps.name, lat, lon, slug, archived, maps.status as map_status,
password, users.login as user_login')
.joins(:warpables, :user)
.group('maps.id')
end
def self.featured_authors
maps = Map.active.has_user
author_counts = maps.select('user_id, author, count(1) as maps_count')
.group('author')
.order('maps_count DESC')
author_counts.map do |a|
user = User.find(a.user_id)
{ user: user, count: a.maps_count, location: user.maps.first.location } if user.status = Status::NORMAL
end
end
def self.maps_nearby(lat:, lon:, dist:)
Map.active.where([
'lat>? AND lat<? AND lon>? AND lon<?',
lat - dist, lat + dist, lon - dist, lon + dist,
])
end
def nodes
nodes = {}
warpables.each do |warpable|
if warpable.nodes
w_nodes = []
warpable.nodes.split(',').each do |node|
node_obj = Node.find(node)
w_nodes << [node_obj.lon, node_obj.lat]
end
nodes[warpable.id.to_s] = w_nodes
end
nodes[warpable.id.to_s] ||= 'none'
end
nodes
end
# find all other maps within <dist> degrees lat or lon
def nearby_maps(dist)
return [] if lat.to_f == 0.0 || lon.to_f == 0.0
Map.active.where('id != ? AND lat > ? AND lat < ? AND lon > ? AND lon < ?',
id, lat - dist, lat + dist, lon - dist, lon + dist)
.limit(10)
end
def average_scale
# determine optimal zoom level
puts '> calculating scale'
pxperms = []
placed_warpables.each do |warpable|
pxperms << 100.00 / warpable.cm_per_pixel if warpable.placed?
end
pxperms.sum / pxperms.length
end
def best_cm_per_pixel
hist = images_histogram
scores = []
(0..(hist.length - 1)).each do |i|
scores[i] = 0
scores[i] += hist[i - 3] if i > 3
scores[i] += hist[i - 2] if i > 2
scores[i] += hist[i - 1] if i > 1
scores[i] += hist[i]
scores[i] += hist[i + 1] if i < hist.length - 2
scores[i] += hist[i + 2] if i < hist.length - 3
scores[i] += hist[i + 3] if i < hist.length - 4
end
highest = 0
scores.each_with_index { |s, i| highest = i if s > scores[highest] }
highest
end
def average_cm_per_pixel
if !warpables.empty?
scales = []
count = 0
average = 0
placed_warpables.each do |warpable|
count += 1
res = warpable.cm_per_pixel
res = 1 if res.zero? # let's not ever try to go for infinite resolution
scales << res unless res.nil?
end
total_sum = scales.sum unless scales.empty?
average = total_sum / count if total_sum
average
else
0
end
end
# for sparklines graph display
def images_histogram
hist = []
warpables.each do |warpable|
res = warpable.cm_per_pixel.to_i
hist[res] = 0 if hist[res].nil?
hist[res] += 1
end
(0..hist.length - 1).each do |bin|
hist[bin] = 0 if hist[bin].nil?
end
hist
end
# for sparklines graph display
def grouped_images_histogram(binsize)
hist = []
warpables.each do |warpable|
res = warpable.cm_per_pixel
next if res.nil?
res = (warpable.cm_per_pixel / (0.001 + binsize)).to_i
hist[res] = 0 if hist[res].nil?
hist[res] += 1
end
(0..hist.length - 1).each do |bin|
hist[bin] = 0 if hist[bin].nil?
end
hist
end
# we'll eventually replace this with a JavaScript call to initiate an external export process:
def run_export(user, resolution)
key = APP_CONFIG ? APP_CONFIG["google_maps_api_key"] : "AIzaSyAOLUQngEmJv0_zcG1xkGq-CXIPpLQY8iQ"
new_export = Export.new(map_id: id) unless export
Exporter.run_export(user,
resolution,
export || new_export,
id,
slug,
Rails.root.to_s,
average_scale,
placed_warpables,
key)
end
def after_create
puts 'saving Map'
return unless Map.find_by_name(slug, order: "version DESC")
self.version = last.version + 1
end
def license_link
if license == "cc-by"
"<a href='http://creativecommons.org/licenses/by/3.0/'>Creative Commons Attribution 3.0 Unported License</a>"
elsif license == "publicdomain"
"<a href='http://creativecommons.org/publicdomain/zero/1.0/'>Public Domain</a>"
end
end
def has_tag(tagname)
!Tag.where(map_id: id, name: tagname).empty?
end
def add_tag(tagname, user)
tagname = tagname.downcase
tags.create(name: tagname, user_id: user.id, map_id: id) unless has_tag(tagname)
end
def fetch_map_data
# fetches a list of updated warpables along with their corners in a json format.
data = warpables
data.to_json
end
def authors
user_ids = []
warpables.each do |warp|
user_ids.push(warp.versions.map(&:whodunnit))
end
User.where(id: user_ids.flatten.uniq).where.not(id: user_id)
end
def spam
update!(status: Status::BANNED)
end
def publish
update!(status: Status::NORMAL)
end
end