Class: LibGD::GIS::Tile

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

Overview

Represents a single map tile fetched from a remote tile provider.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(z:, x:, y:, source:) ⇒ Tile

Returns a new instance of Tile.

Parameters:

  • z (Integer)
  • x (Integer)
  • y (Integer)
  • source (String)

    remote image URL



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

#imageGD::Image? (readonly)

Returns rendered image.

Returns:

  • (GD::Image, nil)

    rendered image



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

def image
  @image
end

#xInteger (readonly)

Returns tile X coordinate.

Returns:

  • (Integer)

    tile X coordinate



15
16
17
# File 'lib/libgd_gis.rb', line 15

def x
  @x
end

#yInteger (readonly)

Returns tile Y coordinate.

Returns:

  • (Integer)

    tile Y coordinate



18
19
20
# File 'lib/libgd_gis.rb', line 18

def y
  @y
end

#zInteger (readonly)

Returns zoom level.

Returns:

  • (Integer)

    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

Parameters:

  • z (Integer)

    zoom level

  • x (Integer)

    X coordinate

  • y (Integer)

    Y coordinate

Returns:



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

#renderGD::Image

Downloads and renders the tile image

Returns:

  • (GD::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

Parameters:

  • path (String)


69
70
71
# File 'lib/libgd_gis.rb', line 69

def save(path)
  @image.save(path)
end