Class: TilesCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/tiler/tiles.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TilesCollection

Returns a new instance of TilesCollection.



6
7
8
9
10
11
12
13
14
15
# File 'lib/tiler/tiles.rb', line 6

def initialize(args)
  @zoom    = args[:zoom]
  @source  = args[:source] || "sattelite"
  @start_x = args[:start_x]
  @start_y = args[:start_y]
  @end_x   = args[:end_x]
  @end_y   = args[:end_y]
  @output  = args[:output]
  save_tiles
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



3
4
5
# File 'lib/tiler/tiles.rb', line 3

def dir
  @dir
end

#endObject (readonly)

Returns the value of attribute end.



4
5
6
# File 'lib/tiler/tiles.rb', line 4

def end
  @end
end

#output_fileObject

Returns the value of attribute output_file.



3
4
5
# File 'lib/tiler/tiles.rb', line 3

def output_file
  @output_file
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/tiler/tiles.rb', line 3

def source
  @source
end

#startObject (readonly)

Returns the value of attribute start.



4
5
6
# File 'lib/tiler/tiles.rb', line 4

def start
  @start
end

#zoomObject

Returns the value of attribute zoom.



3
4
5
# File 'lib/tiler/tiles.rb', line 3

def zoom
  @zoom
end

Instance Method Details

#allObject



29
30
31
# File 'lib/tiler/tiles.rb', line 29

def all
  @tiles
end

#cleanupObject



70
71
72
73
74
75
76
# File 'lib/tiler/tiles.rb', line 70

def cleanup
  flatten.each do |tile| 
    tile.file.close
    tile.file.unlink
  end
  delete_temp_dir
end

#columnsObject



25
26
27
# File 'lib/tiler/tiles.rb', line 25

def columns
  @start_x.upto(@end_x)
end

#create_temp_dirObject



56
57
58
# File 'lib/tiler/tiles.rb', line 56

def create_temp_dir
  @dir = Dir.mktmpdir
end

#delete_temp_dirObject



61
62
63
# File 'lib/tiler/tiles.rb', line 61

def delete_temp_dir
  FileUtils.remove_entry_secure dir if dir
end

#downloadObject



65
66
67
68
# File 'lib/tiler/tiles.rb', line 65

def download
  create_temp_dir
  flatten.each{ |tile| tile.download(dir) }
end

#flattenObject



33
34
35
36
37
38
39
# File 'lib/tiler/tiles.rb', line 33

def flatten
  rows.map do |y|
    columns.map do |x|
      tile(x,y)
    end
  end.flatten
end

#rowsObject



21
22
23
# File 'lib/tiler/tiles.rb', line 21

def rows
  @start_y.upto(@end_y)
end

#save_tile(x, y, tile) ⇒ Object



41
42
43
44
# File 'lib/tiler/tiles.rb', line 41

def save_tile(x,y,tile)
  @tiles[y] = {} unless @tiles[y]
  @tiles[y][x] = tile
end

#save_tilesObject



46
47
48
49
50
51
52
53
# File 'lib/tiler/tiles.rb', line 46

def save_tiles
  @tiles = {}
  rows.map do |y|
    columns.map do |x|
      save_tile(x,y,Tile.new(x: x, y: y, z: @zoom))
    end
  end
end

#stitchObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tiler/tiles.rb', line 78

def stitch
  output_data = Magick::ImageList.new
  columns.each do |column|
    col = Magick::ImageList.new
    rows.each do |row|
      col.push(Magick::Image.read(tile(column,row).local_file_name).first)
    end
    output_data.push(col.append(true))
  end
  output_data.append(false).write(@output)
end

#tile(x, y) ⇒ Object



17
18
19
# File 'lib/tiler/tiles.rb', line 17

def tile(x,y)
  @tiles[y][x]
end