Class: Geos::CoordinateSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/geos/coordinate_sequence.rb

Instance Method Summary collapse

Instance Method Details

#as_geojson(options = {}) ⇒ Object Also known as: to_geojsonable



79
80
81
82
83
84
# File 'lib/geos/coordinate_sequence.rb', line 79

def as_geojson(options = {})
  {
    :type => 'LineString',
    :coordinates => self.to_a
  }
end

#as_json(options = {}) ⇒ Object Also known as: to_jsonable

Returns a Hash suitable for converting to JSON.

Options:

  • :encoded - enable or disable Google Maps encoding. The default is true.

  • :level - set the level of the Google Maps encoding algorithm.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/geos/coordinate_sequence.rb', line 58

def as_json(options = {})
  options = {
    :encoded => true,
    :level => 3
  }.merge options

  if options[:encoded]
    {
      :type => 'lineString',
      :encoded => true
    }.merge(Geos::GoogleMaps::PolylineEncoder.encode(self.to_a, options[:level]))
  else
    {
      :type => 'lineString',
      :encoded => false,
      :points => self.to_a
    }
  end
end

#to_aObject

Returns a Ruby Array of Arrays of coordinates within the CoordinateSequence in the form [ x, y, z ].



6
7
8
9
10
11
12
13
14
# File 'lib/geos/coordinate_sequence.rb', line 6

def to_a
  (0...self.length).to_a.collect do |p|
    [
      self.get_x(p),
      (self.dimensions >= 2 ? self.get_y(p) : nil),
      (self.dimensions >= 3 && self.get_z(p) > 1.7e-306 ? self.get_z(p) : nil)
    ].compact
  end
end

#to_geojson(options = {}) ⇒ Object



87
88
89
# File 'lib/geos/coordinate_sequence.rb', line 87

def to_geojson(options = {})
  self.to_geojsonable(options).to_json
end

#to_georss(*args) ⇒ Object

Build some XmlMarkup for GeoRSS GML. You should include the appropriate georss and gml XML namespaces in your document.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/geos/coordinate_sequence.rb', line 37

def to_georss(*args)
  xml = Geos::Helper.xml_options(*args)[0]

  xml.georss(:where) do
    xml.gml(:LineString) do
      xml.gml(:posList) do
        xml << self.to_a.collect do |p|
          "#{p[1]} #{p[0]}"
        end.join(' ')
      end
    end
  end
end

#to_kml(*args) ⇒ Object

Build some XmlMarkup for KML. You can set various KML options like tessellate, altitudeMode, etc. Use Rails/Ruby-style code and it will be converted automatically, i.e. :altitudeMode, not :altitude_mode.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/geos/coordinate_sequence.rb', line 20

def to_kml(*args)
  xml, options = Geos::Helper.xml_options(*args)

  xml.LineString(:id => options[:id]) do
    xml.extrude(options[:extrude]) if options[:extrude]
    xml.tessellate(options[:tessellate]) if options[:tessellate]
    xml.altitudeMode(Geos::Helper.camelize(options[:altitude_mode])) if options[:altitudeMode]
    xml.coordinates do
      self.to_a.each do
        xml << (self.to_a.join(','))
      end
    end
  end
end