Class: GD::GIS::LinesLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/gd/gis/layer_lines.rb

Overview

Renders collections of line geometries onto a GD image.

A LinesLayer draws simple line strings using a fixed stroke color and width. Coordinates are expected to be in

longitude, latitude

order and are projected at render time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, stroke:, width:) ⇒ LinesLayer

Creates a new lines layer.

Parameters:

  • lines (Array<Array<Array<Float>>>)

    array of line strings ([[lng, lat], …])

  • stroke (GD::Color)

    line color

  • width (Integer)

    stroke width in pixels



21
22
23
24
25
26
# File 'lib/gd/gis/layer_lines.rb', line 21

def initialize(lines, stroke:, width:)
  @lines  = lines
  @stroke = stroke
  @width  = width
  @debug  = false
end

Instance Attribute Details

#debugBoolean

Returns enables debug rendering.

Returns:

  • (Boolean)

    enables debug rendering



13
14
15
# File 'lib/gd/gis/layer_lines.rb', line 13

def debug
  @debug
end

Instance Method Details

#render!(img, projection) ⇒ void

This method returns an undefined value.

Renders the lines onto the given image.

Parameters:

  • img (GD::Image)

    target image

  • projection (#call)

    callable converting (lng, lat) → (x, y)

Raises:

  • (RuntimeError)

    if a line is invalid



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gd/gis/layer_lines.rb', line 36

def render!(img, projection)
  @lines.each do |line|
    raise "Invalid line: #{line.inspect}" unless valid_line?(line)

    pts = line.map do |lng, lat|
      projection.call(lng, lat)
    end

    color = @debug ? ColorHelpers.random_vivid : @stroke

    pts.each_cons(2) do |a, b|
      img.line(
        a[0], a[1],
        b[0], b[1],
        color,
        thickness: @width
      )
    end
  end
end