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

yaml_new(klass, tag, val) click to toggle source
# File lib/geos/yaml/syck.rb, line 12
def self.yaml_new(klass, tag, val)
  Geos.read(val['geom'])
end

Public Instance Methods

bottom() click to toggle source

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
Also aliased as: s, south
e()
Alias for: right
east()
Alias for: right
encode_with(coder) click to toggle source
# 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
init_with(coder) click to toggle source
# 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
lat_lng() click to toggle source

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
Also aliased as: lat_long, latlng, latlong, lat_lon, latlon
lat_lon()
Alias for: lat_lng
lat_long()
Alias for: lat_lng
latlng()
Alias for: lat_lng
latlon()
Alias for: lat_lng
latlong()
Alias for: lat_lng
left() click to toggle source

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
Also aliased as: w, west
lng_lat() click to toggle source

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
Also aliased as: long_lat, lnglat, longlat, lon_lat, lonlat
lnglat()
Alias for: lng_lat
lon_lat()
Alias for: lng_lat
long_lat()
Alias for: lng_lat
longlat()
Alias for: lng_lat
lonlat()
Alias for: lng_lat
lower_left() click to toggle source

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
Also aliased as: sw, southwest
lower_right() click to toggle source

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
Also aliased as: se, southeast
n()
Alias for: top
ne()
Alias for: upper_right
north()
Alias for: top
northeast()
Alias for: upper_right
northwest()
Alias for: upper_left
nw()
Alias for: upper_left
right() click to toggle source

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
Also aliased as: e, east
s()
Alias for: bottom
se()
Alias for: lower_right
south()
Alias for: bottom
southeast()
Alias for: lower_right
southwest()
Alias for: lower_left
sw()
Alias for: lower_left
taguri() click to toggle source
# File lib/geos/yaml/syck.rb, line 8
def taguri
  "tag:ruby.yaml.org,2002:object:#{self.class.name}"
end
to_bbox(long_or_short_names = :long) click to toggle source

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
to_box2d() click to toggle source

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
to_ewkb(options = {}) click to toggle source

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
to_ewkb_bin(options = {}) click to toggle source

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
to_ewkt(options = {}) click to toggle source

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
to_flickr_bbox(options = {}) click to toggle source

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
to_geojson(options = {}) click to toggle source

Spits out the actual stringified GeoJSON.

# File lib/geos/geometry.rb, line 200
def to_geojson(options = {})
  self.to_geojsonable(options).to_json
end
to_wkb(options = {}) click to toggle source

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
to_wkb_bin(options = {}) click to toggle source

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
to_wkt(options = {}) click to toggle source

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
to_yaml( opts = {} ) click to toggle source
# 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
top() click to toggle source

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
Also aliased as: n, north
upper_left() click to toggle source

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
Also aliased as: nw, northwest
upper_right() click to toggle source

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
Also aliased as: ne, northeast
w()
Alias for: left
west()
Alias for: left