Class: GD::GIS::PolygonsLayer

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

Overview

Renders polygon geometries onto a GD image.

A PolygonsLayer draws filled polygons with optional stroke outlines. Polygons are expected to be provided as arrays of rings in [longitude, latitude] order.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(polygons, fill:, stroke: nil, width: nil) ⇒ PolygonsLayer

Creates a new polygons layer.

Parameters:

  • polygons (Array<Array<Array<Array<Float>>>>)

    array of polygons, each consisting of one or more rings

  • fill (GD::Color)

    fill color

  • stroke (GD::Color, nil) (defaults to: nil)

    stroke color

  • width (Integer, nil) (defaults to: nil)

    stroke width in pixels



22
23
24
25
26
27
28
# File 'lib/gd/gis/layer_polygons.rb', line 22

def initialize(polygons, fill:, stroke: nil, width: nil)
  @polygons = polygons
  @fill = fill
  @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_polygons.rb', line 13

def debug
  @debug
end

Class Method Details

.from_lines(features, stroke:, fill:, width:) ⇒ PolygonsLayer

Builds a polygon layer by buffering line features.

This is a convenience constructor that converts line geometries into polygon buffers using a naive geometric approximation.

Parameters:

  • features (Array<Hash>)

    GeoJSON-like features with LineString geometries

  • stroke (GD::Color)
  • fill (GD::Color)
  • width (Numeric)

    buffer width (approximate)

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gd/gis/layer_polygons.rb', line 44

def self.from_lines(features, stroke:, fill:, width:)
  polys = []

  features.each do |f|
    coords = f["geometry"]["coordinates"]
    poly = Geometry.buffer_line(coords, width)
    polys << poly
  end

  new(polys, fill: fill, stroke: stroke)
end

Instance Method Details

#render!(img, projection) ⇒ void

This method returns an undefined value.

Renders all polygons onto the image.

Parameters:

  • img (GD::Image)

    target image

  • projection (#call)

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gd/gis/layer_polygons.rb', line 63

def render!(img, projection)
  @polygons.each do |polygon|
    # polygon = [ ring, ring, ... ]
    polygon.each_with_index do |ring, idx|
      pts = ring.map do |lng, lat|
        projection.call(lng, lat)
      end

      @stroke = GD::GIS::ColorHelpers.random_vivid if @debug
      @fill   = GD::GIS::ColorHelpers.random_vivid if @debug

      if idx.zero?
        # ring exterior
        img.filled_polygon(pts, @fill)
      end

      next unless @stroke

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