Class: Geos::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/geos/polygon.rb,
lib/geos/yaml/syck.rb

Instance Method Summary collapse

Instance Method Details

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

Options:

  • :interior_rings - whether to include any interior rings in the output. The default is true.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/geos/polygon.rb', line 137

def as_geojson(options = {})
  options = {
    :interior_rings => true
  }.merge(options)

  ret = {
    :type => 'Polygon',
    :coordinates => [ self.exterior_ring.coord_seq.to_a ]
  }

  if options[:interior_rings] && self.num_interior_rings > 0
    ret[:coordinates].concat self.interior_rings.collect { |r|
      r.coord_seq.to_a
    }
  end

  ret
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.

  • :interior_rings - add interior rings to the output. The default is false.

  • :style_options - any style options you want to pass along in the JSON. These options will be automatically camelized into Javascripty code.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/geos/polygon.rb', line 71

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

  style_options = Hash.new
  if options[:style_options] && !options[:style_options].empty?
    options[:style_options].each do |k, v|
      style_options[Geos::Helper.camelize(k.to_s)] = v
    end
  end

  if options[:encoded]
    ret = {
      :type => 'polygon',
      :encoded => true,
      :polylines => [ Geos::GoogleMaps::PolylineEncoder.encode(
          self.exterior_ring.coord_seq.to_a,
          options[:level]
        ).merge(:bounds => {
          :sw => self.lower_left.to_a,
          :ne => self.upper_right.to_a
        })
      ],
      :options => style_options
    }

    if options[:interior_rings] && self.num_interior_rings > 0
      (0..(self.num_interior_rings) - 1).to_a.each do |n|
        ret[:polylines] << Geos::GoogleMaps::PolylineEncoder.encode(
          self.interior_ring_n(n).coord_seq.to_a,
          options[:level]
        )
      end
    end
    ret
  else
    ret = {
      :type => 'polygon',
      :encoded => false,
      :polylines => [{
        :points => self.exterior_ring.coord_seq.to_a,
        :bounds => {
          :sw => self.lower_left.to_a,
          :ne => self.upper_right.to_a
        }
      }]
    }
    if options[:interior_rings] && self.num_interior_rings > 0
      (0..(self.num_interior_rings) - 1).to_a.each do |n|
        ret[:polylines] << {
          :points => self.interior_ring_n(n).coord_seq.to_a
        }
      end
    end
    ret
  end
end

#to_georss(*args) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/geos/polygon.rb', line 41

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

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

#to_kml(*args) ⇒ Object

Build some XmlMarkup for XML. 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. You can also include interior rings by setting :interior_rings to true. The default is false.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geos/polygon.rb', line 9

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

  xml.Polygon(: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[:altitude_mode]
    xml.outerBoundaryIs do
      xml.LinearRing do
        xml.coordinates do
          xml << self.exterior_ring.coord_seq.to_a.collect do |p|
            p.join(',')
          end.join(' ')
        end
      end
    end
    (0...self.num_interior_rings).to_a.each do |n|
      xml.innerBoundaryIs do
        xml.LinearRing do
          xml.coordinates do
            xml << self.interior_ring_n(n).coord_seq.to_a.collect do |p|
              p.join(',')
            end.join(' ')
          end
        end
      end
    end if options[:interior_rings] && self.num_interior_rings > 0
  end
end