Class: Hexagonly::GeoJson

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(features = nil) ⇒ GeoJson

Returns a new instance of GeoJson.

Parameters:

  • features (Array) (defaults to: nil)

    an array of objects that support the #to_geojson method



9
10
11
# File 'lib/hexagonly/geo_json.rb', line 9

def initialize(features = nil)
  add_features(features) unless features.nil?
end

Instance Attribute Details

#featuresObject (readonly)

Returns the value of attribute features.



6
7
8
# File 'lib/hexagonly/geo_json.rb', line 6

def features
  @features
end

Instance Method Details

#add_features(features) ⇒ Object

Adds features (Points, Spaces, Hexagons) that support the #to_geojson method.

Parameters:

  • features (Array)

    an array of objects that support the #to_geojson method



16
17
18
19
20
21
# File 'lib/hexagonly/geo_json.rb', line 16

def add_features(features)
  @features ||= []
  features.each do |feat|
    @features << feat.to_geojson
  end
end

#to_jsonString

Outputs the GeoJson string.

Returns:

  • (String)

    a valid GeoJSON string



26
27
28
29
30
31
# File 'lib/hexagonly/geo_json.rb', line 26

def to_json
  {
    :type => "FeatureCollection",
    :features => @features
  }.to_json
end