Class: Geos::Geometry
- Inherits:
-
Object
- Object
- Geos::Geometry
- 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
-
#bottom ⇒ Object
(also: #s, #south)
Southern-most Y coordinate.
- #encode_with(coder) ⇒ Object
- #init_with(coder) ⇒ Object
-
#lat_lng ⇒ Object
(also: #lat_long, #latlng, #latlong, #lat_lon, #latlon)
Returns the Y and X coordinates of the Geometry’s centroid in an Array.
-
#left ⇒ Object
(also: #w, #west)
Western-most X coordinate.
-
#lng_lat ⇒ Object
(also: #long_lat, #lnglat, #longlat, #lon_lat, #lonlat)
Returns the X and Y coordinates of the Geometry’s centroid in an Array.
-
#lower_left ⇒ Object
(also: #sw, #southwest)
Returns a Point for the envelope’s lower left coordinate.
-
#lower_right ⇒ Object
(also: #se, #southeast)
Returns a Point for the envelope’s lower right coordinate.
-
#right ⇒ Object
(also: #e, #east)
Eastern-most X coordinate.
- #taguri ⇒ Object
-
#to_bbox(long_or_short_names = :long) ⇒ Object
Spits out a Hash containing the cardinal points that describe the Geometry’s bbox.
-
#to_box2d ⇒ Object
Spits out a PostGIS BOX2D-style representing the Geometry’s bounding box.
-
#to_ewkb(options = {}) ⇒ Object
Quickly call to_wkb with :include_srid set to true.
-
#to_ewkb_bin(options = {}) ⇒ Object
Quickly call to_wkb_bin with :include_srid set to true.
-
#to_ewkt(options = {}) ⇒ Object
Quickly call to_wkt with :include_srid set to true.
-
#to_flickr_bbox(options = {}) ⇒ Object
Spits out a bounding box the way Flickr likes it.
-
#to_geojson(options = {}) ⇒ Object
Spits out the actual stringified GeoJSON.
-
#to_wkb(options = {}) ⇒ Object
Spits the geometry out into WKB in hex.
-
#to_wkb_bin(options = {}) ⇒ Object
Spits the geometry out into WKB in binary.
-
#to_wkt(options = {}) ⇒ Object
Spits the geometry out into WKT.
- #to_yaml(opts = {}) ⇒ Object
-
#top ⇒ Object
(also: #n, #north)
Northern-most Y coordinate.
-
#upper_left ⇒ Object
(also: #nw, #northwest)
Returns a Point for the envelope’s upper left coordinate.
-
#upper_right ⇒ Object
(also: #ne, #northeast)
Returns a Point for the envelope’s upper right coordinate.
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
#bottom ⇒ Object 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_lng ⇒ Object 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 |
#left ⇒ Object 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_lat ⇒ Object 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_left ⇒ Object 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_right ⇒ Object 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 |
#right ⇒ Object 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 |
#taguri ⇒ Object
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_box2d ⇒ Object
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( = {}) = { :include_srid => true }.merge to_wkb() 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( = {}) = { :include_srid => true }.merge to_wkb_bin() 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( = {}) = { :include_srid => true }.merge to_wkt() 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( = {}) = { :precision => 1 }.merge() precision = 10.0 ** [: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( = {}) self.to_geojsonable().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( = {}) wkb_writer().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( = {}) wkb_writer().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( = {}) writer = WktWriter.new # Older versions of the Geos library don't allow for options here. args = if WktWriter.instance_method(:write).arity < -1 [ ] else [] end ret = '' if [:include_srid] srid = if [:srid] [: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 |
#top ⇒ Object 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_left ⇒ Object 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_right ⇒ Object 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 |