Class: GD::GIS::PolygonsLayer
- Inherits:
-
Object
- Object
- GD::GIS::PolygonsLayer
- 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
-
#debug ⇒ Boolean
Enables debug rendering.
Class Method Summary collapse
-
.from_lines(features, stroke:, fill:, width:) ⇒ PolygonsLayer
Builds a polygon layer by buffering line features.
Instance Method Summary collapse
-
#initialize(polygons, fill:, stroke: nil, width: nil) ⇒ PolygonsLayer
constructor
Creates a new polygons layer.
-
#render!(img, projection) ⇒ void
Renders all polygons onto the image.
Constructor Details
#initialize(polygons, fill:, stroke: nil, width: nil) ⇒ PolygonsLayer
Creates a new polygons layer.
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
#debug ⇒ Boolean
Returns 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.
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.
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 |