Class: GD::GIS::Style

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

Overview

Defines visual styling rules for map rendering.

A Style object encapsulates all visual configuration used during rendering, including colors, stroke widths, fonts, and layer ordering.

Styles are typically loaded from YAML files and applied to a Map instance before rendering.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Style

Creates a new style from a definition hash.

Parameters:

  • definition (Hash)

    style definition with optional sections: :global, :roads, :rails, :water, :parks, :points, :order, :track



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gd/gis/style.rb', line 49

def initialize(definition)
  @global = definition[:global] || {}
  @point = definition[:point] || {}
  @roads = definition[:roads] || {}
  @rails = definition[:rails] || {}
  @water = definition[:water] || {}
  @parks = definition[:parks] || {}
  @points = definition[:points] || {}
  @track = definition[:track] || {}
  @order = definition[:order] || []
end

Instance Attribute Details

#globalHash (readonly)

Returns global styling rules.

Returns:

  • (Hash)

    global styling rules



18
19
20
# File 'lib/gd/gis/style.rb', line 18

def global
  @global
end

#orderArray<Symbol> (readonly)

Returns drawing order of semantic layers.

Returns:

  • (Array<Symbol>)

    drawing order of semantic layers



42
43
44
# File 'lib/gd/gis/style.rb', line 42

def order
  @order
end

#parksHash (readonly)

Returns park styling rules.

Returns:

  • (Hash)

    park styling rules



33
34
35
# File 'lib/gd/gis/style.rb', line 33

def parks
  @parks
end

#pointHash (readonly)

Returns point styling rules.

Returns:

  • (Hash)

    point styling rules



21
22
23
# File 'lib/gd/gis/style.rb', line 21

def point
  @point
end

#pointsHash (readonly)

Returns point styling rules.

Returns:

  • (Hash)

    point styling rules



36
37
38
# File 'lib/gd/gis/style.rb', line 36

def points
  @points
end

#railsHash (readonly)

Returns rail styling rules.

Returns:

  • (Hash)

    rail styling rules



27
28
29
# File 'lib/gd/gis/style.rb', line 27

def rails
  @rails
end

#roadsHash (readonly)

Returns road styling rules.

Returns:

  • (Hash)

    road styling rules



24
25
26
# File 'lib/gd/gis/style.rb', line 24

def roads
  @roads
end

#trackHash (readonly)

Returns track styling rules.

Returns:

  • (Hash)

    track styling rules



39
40
41
# File 'lib/gd/gis/style.rb', line 39

def track
  @track
end

#waterHash (readonly)

Returns water styling rules.

Returns:

  • (Hash)

    water styling rules



30
31
32
# File 'lib/gd/gis/style.rb', line 30

def water
  @water
end

Class Method Details

.deep_symbolize(obj) ⇒ Object

Recursively converts hash keys to symbols.

Parameters:

  • obj (Object)

Returns:

  • (Object)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gd/gis/style.rb', line 101

def self.deep_symbolize(obj)
  case obj
  when Hash
    obj.transform_keys(&:to_sym)
       .transform_values { |v| deep_symbolize(v) }
  when Array
    obj.map { |v| deep_symbolize(v) }
  else
    obj
  end
end

.defaultStyle

Returns a built-in default style so Map can render even if no external style is set.

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gd/gis/style.rb', line 116

def self.default
  new({
        global: {
          background: [15, 23, 42],
          font: GD::GIS::FontHelper.find("DejaVuSans"),
          font_color: [243, 244, 246],
          label: {
            color: [229, 231, 235],
            font: GD::GIS::FontHelper.find("DejaVuSans"),
            size: 12
          }
        },

    label: {
      label: {
        color: [229, 231, 235],
        font: GD::GIS::FontHelper.find("DejaVuSans"),
        size: 12
      }
    },

    roads: {
      roads: {
        color: [229, 231, 235],
        font: GD::GIS::FontHelper.find("DejaVuSans"),
        width: 6
      }
    },

    rails: {
      rails: {
        color: [156, 163, 175],
        font: GD::GIS::FontHelper.find("DejaVuSans"),
        width: 5
      }
    },

    water: {
      water: {
        color: [59, 130, 246]
      }
    },

    parks: {
      parks: {
        color: [34, 197, 94]
      }
    },

    points: {
      points: {
        color: [239, 68, 68],
        radius: 4,
        font: GD::GIS::FontHelper.find("DejaVuSans"),
        font_color: [243, 244, 246]
      }
    },

    track: {
      track: {
        stroke: [0, 85, 127, 250],
        color: [250, 204, 21],
        width: 2,
        font_color: [243, 244, 246]
      }
    },

    order: %i[water parks rails roads points track]
      })
end

.load(name, from: "styles") ⇒ Style

Loads a style definition from a YAML file.

The file name is resolved as:

<from>/<name>.yml

All keys are deep-symbolized on load.

Parameters:

  • name (String, Symbol)

    style name (without extension)

  • from (String) (defaults to: "styles")

    directory containing style files

Returns:

Raises:

  • (RuntimeError)

    if the style file does not exist

  • (Psych::SyntaxError)

    if the YAML is invalid



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gd/gis/style.rb', line 77

def self.load(name, from: "styles")
  path = File.join(from, "#{name}.yml")
  raise "Style not found: #{path}" unless File.exist?(path)

  data = YAML.load_file(path)
  data = deep_symbolize(data)

  new(
    global: data[:global],
    point: data[:point],
    roads: data[:roads],
    rails: data[:rail] || data[:rails],
    track: data[:track],
    water: data[:water],
    parks: data[:park] || data[:parks],
    points: data[:points],
    order: (data[:order] || []).map(&:to_sym)
  )
end

Instance Method Details

#normalize_color(color) ⇒ GD::Color

Normalizes a color definition into a GD::Color.

Accepted formats:

  • GD::Color instance

  • r, g, b
  • r, g, b, a
  • nil (generates a random vivid color)

Parameters:

  • color (GD::Color, Array<Integer>, nil)

Returns:

  • (GD::Color)

Raises:

  • (ArgumentError)

    if the format is invalid



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/gd/gis/style.rb', line 198

def normalize_color(color)
  case color
  when GD::Color
    color

  when Array
    case color.length
    when 3
      GD::Color.rgb(*color)
    when 4
      GD::Color.rgba(*color)
    else
      raise ArgumentError,
            "Style error: color array must be [r,g,b] or [r,g,b,a]"
    end

  when nil
    GD::GIS::ColorHelpers.random_vivid

  else
    raise ArgumentError,
          "Style error: invalid color format (#{color.inspect})"
  end
end