Class: Geos::Geometry

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

Overview

This is our base module that we use for some generic methods used all over the place.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.yaml_new(klass, tag, val) ⇒ Object



11
12
13
# File 'lib/geos/yaml/syck.rb', line 11

def self.yaml_new(klass, tag, val)
  Geos.read(val['geom'])
end

Instance Method Details

#bottomObject Also known as: s, south

Southern-most Y coordinate.



158
159
160
161
162
163
164
# File 'lib/geos/geometry.rb', line 158

def bottom
  if defined?(@bottom)
    @bottom
  else
    @bottom = self.lower_left.y
  end
end

#encode_with(coder) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/geos/yaml/psych.rb', line 10

def encode_with(coder)
  # Note we enforce ASCII encoding so the geom in the YAML file is

  # readable -- otherwise psych converts it to a binary string.

  coder['geom'] = self.to_ewkt(
    :include_srid => self.srid != 0
  ).force_encoding('ASCII')
end

#init_with(coder) ⇒ Object



5
6
7
8
# File 'lib/geos/yaml/psych.rb', line 5

def init_with(coder)
  # Convert wkt to a geos pointer

  @ptr = Geos.read(coder['geom']).ptr
end

#lat_lngObject Also known as: lat_long, latlng, latlong, lat_lon, latlon

Returns the Y and X coordinates of the Geometry’s centroid in an Array.



203
204
205
# File 'lib/geos/geometry.rb', line 203

def lat_lng
  self.centroid.to_a[0, 2].reverse
end

#leftObject Also known as: w, west

Western-most X coordinate.



169
170
171
172
173
174
175
# File 'lib/geos/geometry.rb', line 169

def left
  if defined?(@left)
    @left
  else
    @left = self.lower_left.x
  end
end

#lng_latObject Also known as: long_lat, lnglat, longlat, lon_lat, lonlat

Returns the X and Y coordinates of the Geometry’s centroid in an Array.



213
214
215
# File 'lib/geos/geometry.rb', line 213

def lng_lat
  self.centroid.to_a[0, 2]
end

#lower_leftObject Also known as: sw, southwest

Returns a Point for the envelope’s lower left coordinate.



124
125
126
127
128
129
130
131
# File 'lib/geos/geometry.rb', line 124

def lower_left
  if defined?(@lower_left)
    @lower_left
  else
    cs = self.envelope.exterior_ring.coord_seq
    @lower_left = Geos::wkt_reader_singleton.read("POINT(#{cs.get_x(0)} #{cs.get_y(0)})")
  end
end

#lower_rightObject Also known as: se, southeast

Returns a Point for the envelope’s lower right coordinate.



112
113
114
115
116
117
118
119
# File 'lib/geos/geometry.rb', line 112

def lower_right
  if defined?(@lower_right)
    @lower_right
  else
    cs = self.envelope.exterior_ring.coord_seq
    @lower_right = Geos::wkt_reader_singleton.read("POINT(#{cs.get_x(1)} #{cs.get_y(1)})")
  end
end

#rightObject Also known as: e, east

Eastern-most X coordinate.



147
148
149
150
151
152
153
# File 'lib/geos/geometry.rb', line 147

def right
  if defined?(@right)
    @right
  else
    @right = self.upper_right.x
  end
end

#taguriObject



7
8
9
# File 'lib/geos/yaml/syck.rb', line 7

def taguri
  "tag:ruby.yaml.org,2002:object:#{self.class.name}"
end

#to_bbox(long_or_short_names = :long) ⇒ Object

Spits out a Hash containing the cardinal points that describe the Geometry’s bbox.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/geos/geometry.rb', line 224

def to_bbox(long_or_short_names = :long)
  case long_or_short_names
    when :long
      {
        :north => self.north,
        :east => self.east,
        :south => self.south,
        :west => self.west
      }
    when :short
      {
        :n => self.north,
        :e => self.east,
        :s => self.south,
        :w => self.west
      }
    else
      raise ArgumentError.new("Expected either :long or :short for long_or_short_names argument")
  end
end

#to_box2dObject

Spits out a PostGIS BOX2D-style representing the Geometry’s bounding box.



247
248
249
# File 'lib/geos/geometry.rb', line 247

def to_box2d
  "BOX(#{self.southwest.to_a.join(' ')}, #{self.northeast.to_a.join(' ')})"
end

#to_ewkb(options = {}) ⇒ Object

Quickly call to_wkb with :include_srid set to true.



44
45
46
47
48
49
# File 'lib/geos/geometry.rb', line 44

def to_ewkb(options = {})
  options = {
    :include_srid => true
  }.merge options
  to_wkb(options)
end

#to_ewkb_bin(options = {}) ⇒ Object

Quickly call to_wkb_bin with :include_srid set to true.



28
29
30
31
32
33
# File 'lib/geos/geometry.rb', line 28

def to_ewkb_bin(options = {})
  options = {
    :include_srid => true
  }.merge options
  to_wkb_bin(options)
end

#to_ewkt(options = {}) ⇒ Object

Quickly call to_wkt with :include_srid set to true.



80
81
82
83
84
85
# File 'lib/geos/geometry.rb', line 80

def to_ewkt(options = {})
  options = {
    :include_srid => true
  }.merge options
  to_wkt(options)
end

#to_flickr_bbox(options = {}) ⇒ Object

Spits out a bounding box the way Flickr likes it. You can set the precision of the rounding using the :precision option. In order to ensure that the box is indeed a box and not merely a point, the southwest coordinates are floored and the northeast point ceiled.



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/geos/geometry.rb', line 183

def to_flickr_bbox(options = {})
  options = {
    :precision => 1
  }.merge(options)
  precision = 10.0 ** options[:precision]

  [
    (self.west  * precision).floor / precision,
    (self.south * precision).floor / precision,
    (self.east  * precision).ceil / precision,
    (self.north * precision).ceil / precision
  ].join(',')
end

#to_geojson(options = {}) ⇒ Object

Spits out the actual stringified GeoJSON.



198
199
200
# File 'lib/geos/geometry.rb', line 198

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

#to_wkb(options = {}) ⇒ Object

Spits the geometry out into WKB in hex.

You can set the :output_dimensions, :byte_order and :include_srid options via the options Hash.



39
40
41
# File 'lib/geos/geometry.rb', line 39

def to_wkb(options = {})
  wkb_writer(options).write_hex(self)
end

#to_wkb_bin(options = {}) ⇒ Object

Spits the geometry out into WKB in binary.

You can set the :output_dimensions, :byte_order and :include_srid options via the options Hash.



23
24
25
# File 'lib/geos/geometry.rb', line 23

def to_wkb_bin(options = {})
  wkb_writer(options).write(self)
end

#to_wkt(options = {}) ⇒ Object

Spits the geometry out into WKT. You can specify the :include_srid option to create a PostGIS-style EWKT output.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/geos/geometry.rb', line 53

def to_wkt(options = {})
  writer = WktWriter.new

  # Older versions of the Geos library don't allow for options here.
  args = if WktWriter.instance_method(:write).arity < -1
    [ options ]
  else
    []
  end

  ret = ''

  if options[:include_srid]
    srid = if options[:srid]
      options[:srid]
    else
      self.srid
    end

    ret << "SRID=#{srid};"
  end

  ret << writer.write(self, *args)
  ret
end

#to_yaml(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/geos/yaml/syck.rb', line 15

def to_yaml( opts = {} )
  YAML::quick_emit(self.object_id, opts) do |out|
    out.map(taguri) do |map|
      map.add('geom', self.to_ewkt(
        :include_srid => self.srid != 0
      ))
    end
  end
end

#topObject Also known as: n, north

Northern-most Y coordinate.



136
137
138
139
140
141
142
# File 'lib/geos/geometry.rb', line 136

def top
  if defined?(@top)
    @top
  else
    @top = self.upper_right.y
  end
end

#upper_leftObject Also known as: nw, northwest

Returns a Point for the envelope’s upper left coordinate.



88
89
90
91
92
93
94
95
# File 'lib/geos/geometry.rb', line 88

def upper_left
  if defined?(@upper_left)
    @upper_left
  else
    cs = self.envelope.exterior_ring.coord_seq
    @upper_left = Geos::wkt_reader_singleton.read("POINT(#{cs.get_x(3)} #{cs.get_y(3)})")
  end
end

#upper_rightObject Also known as: ne, northeast

Returns a Point for the envelope’s upper right coordinate.



100
101
102
103
104
105
106
107
# File 'lib/geos/geometry.rb', line 100

def upper_right
  if defined?(@upper_right)
    @upper_right
  else
    cs = self.envelope.exterior_ring.coord_seq
    @upper_right = Geos::wkt_reader_singleton.read("POINT(#{cs.get_x(2)} #{cs.get_y(2)})")
  end
end