class Geos::Geometry
This is our base module that we use for some generic methods used all over the place.
Constants
- WKB_WRITER_OPTIONS
Public Class Methods
# File lib/geos/yaml/syck.rb, line 12 def self.yaml_new(klass, tag, val) Geos.read(val['geom']) end
Public Instance Methods
Southern-most Y coordinate.
# File lib/geos/geometry.rb, line 160 def bottom if defined?(@bottom) @bottom else @bottom = self.lower_left.y end end
# File lib/geos/yaml/psych.rb, line 11 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
# File lib/geos/yaml/psych.rb, line 6 def init_with(coder) # Convert wkt to a geos pointer @ptr = Geos.read(coder['geom']).ptr end
Returns the Y and X coordinates of the Geometry's centroid in an Array.
# File lib/geos/geometry.rb, line 205 def lat_lng self.centroid.to_a[0, 2].reverse end
Western-most X coordinate.
# File lib/geos/geometry.rb, line 171 def left if defined?(@left) @left else @left = self.lower_left.x end end
Returns the X and Y coordinates of the Geometry's centroid in an Array.
# File lib/geos/geometry.rb, line 215 def lng_lat self.centroid.to_a[0, 2] end
Returns a Point
for the envelope's lower left coordinate.
# File lib/geos/geometry.rb, line 126 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
Returns a Point
for the envelope's lower right coordinate.
# File lib/geos/geometry.rb, line 114 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
Eastern-most X coordinate.
# File lib/geos/geometry.rb, line 149 def right if defined?(@right) @right else @right = self.upper_right.x end end
# File lib/geos/yaml/syck.rb, line 8 def taguri "tag:ruby.yaml.org,2002:object:#{self.class.name}" end
Spits out a Hash containing the cardinal points that describe the Geometry's bbox.
# File lib/geos/geometry.rb, line 226 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
Spits out a PostGIS BOX2D-style representing the Geometry's bounding box.
# File lib/geos/geometry.rb, line 249 def to_box2d "BOX(#{self.southwest.to_a.join(' ')}, #{self.northeast.to_a.join(' ')})" end
Quickly call to_wkb
with :include_srid set to true.
# File lib/geos/geometry.rb, line 46 def to_ewkb(options = {}) options = { :include_srid => true }.merge options to_wkb(options) end
Quickly call to_wkb_bin
with :include_srid set to true.
# File lib/geos/geometry.rb, line 30 def to_ewkb_bin(options = {}) options = { :include_srid => true }.merge options to_wkb_bin(options) end
Quickly call to_wkt
with :include_srid set to true.
# File lib/geos/geometry.rb, line 82 def to_ewkt(options = {}) options = { :include_srid => true }.merge options to_wkt(options) end
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.
# File lib/geos/geometry.rb, line 185 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
Spits out the actual stringified GeoJSON.
# File lib/geos/geometry.rb, line 200 def to_geojson(options = {}) self.to_geojsonable(options).to_json end
Spits the geometry out into WKB in hex.
You can set the :output_dimensions, :byte_order and :include_srid options via the options Hash.
# File lib/geos/geometry.rb, line 41 def to_wkb(options = {}) wkb_writer(options).write_hex(self) end
Spits the geometry out into WKB in binary.
You can set the :output_dimensions, :byte_order and :include_srid options via the options Hash.
# File lib/geos/geometry.rb, line 25 def to_wkb_bin(options = {}) wkb_writer(options).write(self) end
Spits the geometry out into WKT. You can specify the :include_srid option to create a PostGIS-style EWKT output.
# File lib/geos/geometry.rb, line 55 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 = ''.dup 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
# File lib/geos/yaml/syck.rb, line 16 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
Northern-most Y coordinate.
# File lib/geos/geometry.rb, line 138 def top if defined?(@top) @top else @top = self.upper_right.y end end
Returns a Point
for the envelope's upper left coordinate.
# File lib/geos/geometry.rb, line 90 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
Returns a Point
for the envelope's upper right coordinate.
# File lib/geos/geometry.rb, line 102 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