Class: LibGD::GIS::Tile
- Inherits:
-
Object
- Object
- LibGD::GIS::Tile
- Defined in:
- lib/libgd_gis.rb
Overview
Represents a single map tile fetched from a remote tile provider.
Instance Attribute Summary collapse
-
#image ⇒ GD::Image?
readonly
Rendered image.
-
#x ⇒ Integer
readonly
Tile X coordinate.
-
#y ⇒ Integer
readonly
Tile Y coordinate.
-
#z ⇒ Integer
readonly
Zoom level.
Class Method Summary collapse
-
.osm(z:, x:, y:) ⇒ Tile
Builds a tile using an OSM-compatible XYZ source.
Instance Method Summary collapse
-
#initialize(z:, x:, y:, source:) ⇒ Tile
constructor
A new instance of Tile.
-
#render ⇒ GD::Image
Downloads and renders the tile image.
-
#save(path) ⇒ void
Saves the rendered image to disk.
Constructor Details
#initialize(z:, x:, y:, source:) ⇒ Tile
Returns a new instance of Tile.
42 43 44 45 46 47 |
# File 'lib/libgd_gis.rb', line 42 def initialize(z:, x:, y:, source:) @z = z @x = x @y = y @source = source end |
Instance Attribute Details
#image ⇒ GD::Image? (readonly)
Returns rendered image.
21 22 23 |
# File 'lib/libgd_gis.rb', line 21 def image @image end |
#x ⇒ Integer (readonly)
Returns tile X coordinate.
15 16 17 |
# File 'lib/libgd_gis.rb', line 15 def x @x end |
#y ⇒ Integer (readonly)
Returns tile Y coordinate.
18 19 20 |
# File 'lib/libgd_gis.rb', line 18 def y @y end |
#z ⇒ Integer (readonly)
Returns zoom level.
12 13 14 |
# File 'lib/libgd_gis.rb', line 12 def z @z end |
Class Method Details
.osm(z:, x:, y:) ⇒ Tile
Builds a tile using an OSM-compatible XYZ source
29 30 31 32 33 34 35 36 |
# File 'lib/libgd_gis.rb', line 29 def self.osm(z:, x:, y:) new( z: z, x: x, y: y, source: "https://api.maptiler.com/maps/basic/#{z}/#{x}/#{y}.png?key=GetYourOwnKey" ) end |
Instance Method Details
#render ⇒ GD::Image
Downloads and renders the tile image
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/libgd_gis.rb', line 52 def render tmp = Tempfile.new(["tile", ".png"]) tmp.binmode uri = URI(@source) response = Net::HTTP.get(uri) tmp.write(response) tmp.flush @image = GD::Image.open(tmp.path) ensure tmp.close end |
#save(path) ⇒ void
This method returns an undefined value.
Saves the rendered image to disk
69 70 71 |
# File 'lib/libgd_gis.rb', line 69 def save(path) @image.save(path) end |