This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile_engine.rb
400 lines (321 loc) · 10.9 KB
/
tile_engine.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/ruby
require "rubygems"
require "tempfile"
require "thread"
require "http_client_tools"
###
# Notes
# **** tile Layout ****************
# Tiles are referenced like x/y/z, where z is the zoom level
# Example:
# 0/2/1 | 1/2/1 | 2/2/1
# 0/1/1 | 1/1/1 | 2/1/1
# 0/0/1 | 1/0/1 | 2/0/1
#
# This is the oposide from google maps, in the y dir
# Google maps looks like this:
# 0/0/1 | 1/0/1 | 2/0/1
# 0/1/1 | 1/1/1 | 2/1/1
# 0/2/1 | 1/2/1 | 2/2/1
class TileEngine
#local cache path..
PATH_FORMAT = "%02d/%03d/%03d/%09d/%09d/%09d_%09d_%09d.%s"
WAIT_TIME = 0.5
def initialize (cfg, logger)
@cfg = cfg
@log = logger
@storage_format = cfg["storage_format"]
@requests = {}
# Save some stuff for easy (shorter typing) access later..
@x_size = cfg["tiles"]["x_size"]
@y_size = cfg["tiles"]["y_size"]
@x_count = cfg["tiles"]["x_count"]
@y_count = cfg["tiles"]["y_count"]
@num_colors = cfg["tiles"]["colors"] - 4 if ( cfg["tiles"]["colors"] )
#to Debug or not to Debug
# debug flag
@tile_debug = cfg["debug"] if (cfg["debug"])
end
##
# Returns the path to a tile
def get_tile (x,y,z)
path = get_path(x,y,z)
tile_gen(x,y,z) if (!File.exists?(path))
tile_gen(x,y,z) if (File.exists?(path) && (File.size?(path) == nil))
#puts("size of #{path} -> #{File.size?(path)}")
return path
end
def is_fiddle (x,y,z)
return false if (!@cfg["tiles"]["fiddle"])
return false if ( x ==0 || y == 0 )
return true if ( (x < (2**z-1)) && (y < 2**z-1))
return false
end
##
# Note -> z level = log(width of world / width of request)/log(2)
# takes a bbox, returns the tile that it repersents..
# Todo: Handle case where things to not match..
def min_max_to_xyz(min_x,min_y, max_x,max_y)
@log.loginfo("TileEngine:min_max_to_xyz (#{min_x},#{min_y},#{max_x}, #{max_y})..")
dx = max_x - min_x
dy = max_y - min_y
zx = Math.log( (@cfg["base_extents"]["xmax"] - @cfg["base_extents"]["xmin"])/ dx) / Math.log(2)
zy = Math.log( (@cfg["base_extents"]["ymax"] - @cfg["base_extents"]["ymin"])/ dy) / Math.log(2)
x = (min_x - @cfg["base_extents"]["xmin"])/dx
y = (min_y - @cfg["base_extents"]["ymin"])/dy
x = x.to_i
y = y.to_i
@log.msgdebug("TileEngine:min_max_to_xyz:zlevels.. (#{zx},#{zy})..")
@log.msgdebug("TileEngine:min_max_to_xyz:results (#{x},#{y},#{zx})..")
return x,y,zx.to_i
end
###
# Fixer/waiter - checks to see if a tile has been generated, if not waits until it shows up..
def check_and_wait(x,y,z)
path= get_path(x,y,z)
while ( !File.exists?(path) || File.size?(path) == 0 )
@log.msgdebug("TileEngine:"+"check_and_wait -> waiting on #{x},#{y},#{z}")
sleep(WAIT_TIME)
end
end
##
# Returns path to an (x,y,z) set..
def get_path (x,y,z)
return @cfg["cache_dir"] + sprintf(PATH_FORMAT, z,x%128,y%128,x,y,x,y,z,@storage_format)
end
private
##
# creates the path for a tile..
def mk_path(x,y,z)
splits = File.dirname(get_path(x,y,z)).split("/")
start = splits.first
splits.delete_at(0)
splits.each do |x|
start += "/" + x
if ( !File.exists?(start))
@log.msgdebug("mk_path: making #{start}")
begin
Dir.mkdir(start)
rescue => e
@log.msgerror("mk_path: Something when wrong, probibly allready there..#{e.to_s}")
end
end
end
end
##
# Stub - don't call directly, subclass
def tile_gen(x,y,z)
exit(-1)
end
##
# Takes a x,y,z, return the tiles bounding box
def x_y_z_to_map_x_y ( x,y,z)
w_x = (@cfg["base_extents"]["xmax"] - @cfg["base_extents"]["xmin"])/(2.0 **(z.to_f))
w_y = (@cfg["base_extents"]["ymax"] - @cfg["base_extents"]["ymin"])/(2.0 **(z.to_f))
x_min = @cfg["base_extents"]["xmin"] + x*w_x
return { "x_min" => @cfg["base_extents"]["xmin"] + x*w_x,
"y_min" => @cfg["base_extents"]["ymin"] + y*w_y,
"x_max" => @cfg["base_extents"]["xmin"] + (x+1)*w_x,
"y_max" => @cfg["base_extents"]["ymin"] + (y+1)*w_y}
end
#does a block w/upper left and path - used to loop though tiles for cutting them up..
def each_tile_ul(x,y,z)
if (is_fiddle(x,y,z))
0.upto(@x_count) do |i|
0.upto(@y_count) do |j|
mk_path(i+x,j+y,z)
path = get_path(x+i,y+j,z)
next if ( i == 0 || j ==0 || i == @x_count|| i == @y_count)
yield( i*@x_size, ((@y_count+1) - j - 1)*@y_size, path)
end
end
else
0.upto(@x_count-1) do |i|
0.upto(@y_count-1) do |j|
mk_path(i+x,j+y,z)
path = get_path(x+i,y+j,z)
yield( i*@x_size, (@y_count - j - 1)*@y_size, path)
end
end
end
end
#shifts x + y to align with grid..
def shift_x_y(x,y)
x = (x / @x_count)*@x_count
y = (y / @y_count)*@y_count
return x,y
end
#get url - returns the url for a bounding box
def get_url_for_x_y_z(x,y,z)
# x,y,z to bounding box
bbox = x_y_z_to_map_x_y(x,y,z)
# bounding box of end tile set
bbox_big = x_y_z_to_map_x_y(x+@x_count-1,y+@y_count-1,z)
x_count = @x_count
y_count = @y_count
# If fiddle is turned on, inlargen request by 1 tile in each direction
if (is_fiddle(x,y,z))
bbox = x_y_z_to_map_x_y(x-1,y-1,z)
bbox_big = x_y_z_to_map_x_y(x+@x_count,y+@y_count,z)
x_count += 2
y_count += 2
end
#Format the url..
sprintf(@cfg["source_url"], @x_size*x_count , @y_size*y_count, bbox["x_min"],bbox["y_min"], bbox_big["x_max"], bbox_big["y_max"] )
end
end
####
# Newer tilesetup
class ExternalTileEngine < TileEngine
require "idler"
#@@idler = Idler.new(1)
@@idler = nil
def initialize (cfg, logger )
super(cfg,logger)
@lt = "ExternalTileEngine"
@command_path = File.dirname(__FILE__) + "/external_tiler"
@@idler = Idler.new(1) if ( ! @@idler ) #Only create an idler if a instance is instaicated. My spelling sucks. So does my coding.
end
def make_tiles(x,y,z)
path = get_path(x,y,z)
# Check to see if the tile has allready been generated (prevous request made it after this request was queed)
return path if ( File.size?(path) != nil)
command = [@command_path, @cfg["config_path"], @cfg["title"], x.to_s, y.to_s, z.to_s]
@log.msginfo(@lt+"running -> #{command.join(" ")}")
@log.msginfo(@lt+"Starting subtiler (#{x},#{y}.#{z})..")
#using backticks
results = YAML.load(`#{command.join(" ")}`)
#using popen
#results = YAML.load(IO.popen(command.join(" ") {|f| f.readlines}))
@log.msginfo(@lt+"Subtiler finished (#{x},#{y}.#{z}).")
if (results["error"])
#output from the external tiler should include backtrace, logs, and reason..
#({"error"=>true, "reason" => e, "backtrace" => e.backtrace, "logs"=>logs }, STDOUT)
raise "external tiler error, reason -> #{results["reason"]}, backtrace -> #{results["backtrace"]}, command line -> '#{command.join(" ")}'"
end
return path
end
private
# Makes a tile.
def tile_gen (x,y,z)
path = get_path(x,y,z)
# Check to see if the tile has allready been generated (prevous request made it after this request was queed)
return path if ( File.size?(path) != nil)
##
# Queue up everything around the request, to get maximise data generation.
(-1).upto(1) {|dx| (-1).upto(1) {|dy| @@idler.add(self, x+dx*@cfg["tiles"]["x_count"],y+dy*@cfg["tiles"]["y_count"],z) if (!(dy == 0 && dx == 0)) }}
make_tiles(x,y,z)
end
end
##
# A per tile clocking sceme... fun for all..
class TileLocker
def initialize (log)
@log = log
@locker = {}
@wait = 0.3
@m_lock = Mutex.new
@lt = "TileLocker"
end
###
# This is confusing.. pay attention!
# This function is used to control requests, if a request is in progress, it waits until the request is finished, then returns true, with the lock not set/altered.
# If a request is not in progress, it returns false and sets the lock.
def check_and_wait (x,y,z)
busy = false
while ( !check_lock(x,y,z))
@log.msgdebug(@lt+"check_and_wait -> waiting on #{x},#{y},#{z}")
sleep(@wait)
busy = true
end
release_lock(x,y,z) if (busy) # Release lock, and data has allready been generated..
return busy
end
# Returns true if locked, false otherwise..
def check_lock(x,y,z)
token = "#{x}_#{y}_#{z}"
@m_lock.synchronize do
if (!@locker[token])
@log.msgdebug(@lt+"locking on #{x},#{y},#{z}")
@locker[token] = Time.now
return true
else
return false
end
end
end
#to be called after check_and_wait..
def release_lock (x,y,z)
token = "#{x}_#{y}_#{z}"
@m_lock.synchronize {@locker.delete(token) }
end
end
class TileLockerFile
WAIT_TIME= (60*3)
def initialize (log)
@log = log
@lock_dir = "./locks/"
@wait = 0.3
@m_lock = Mutex.new
@lt = "TileLockerFile:"
end
###
# This is confusing.. pay attention!
# This function is used to control requests, if a request is in progress, it waits until the request is finished, then returns true, with the lock not set/altered.
# If a request is not in progress, it returns false and sets the lock.
# Ok, I lied, it should always return with a lock in place..
def check_and_wait (x,y,z)
busy = false
while ( !check_lock(x,y,z))
@log.msgdebug(@lt+"check_and_wait -> waiting on #{x},#{y},#{z}")
sleep(@wait)
busy = true
end
return busy
end
# Returns true if locked, false otherwise..
def check_lock(x,y,z)
if (!locked(x,y,z))
@log.msgdebug(@lt+"locking on #{x},#{y},#{z}")
return true
else
return false
end
end
#to be called after check_and_wait..
def release_lock (x,y,z)
@log.msgdebug(@lt+"releasing #{x},#{y},#{z}")
@m_lock.synchronize do
begin
File.delete(getpath(x,y,z))
rescue => e
@log.msgerror(@lt+"release lock -> colision at #{x},#{y},#{z} (#{e.to_s})")
end
end
end
def getpath(x,y,z)
return("#{@lock_dir}#{x}_#{y}_#{z}")
end
def locked(x,y,z)
@m_lock.synchronize do
path = getpath(x,y,z)
begin #this section deals with old locks - if lock file exists and is old, delete..
if ( File.exists?(path) && ((Time.now - File.mtime(path)) > WAIT_TIME) )
@log.msgerror(@lt +"Lock timeout on #{x},#{y},#{z} ")
File.delete(path)
end
rescue
# Do nothing - means file is gone.
end
# Normal path
if (File.exists?(path))
return true
else
File.open(path, "w" ) {|fl| fl.write(Time.now.to_s); fl.flush()}
return false
end
end
end
end
require "rmagick_tile_engine"
require "idler"