class Geoq::GeomReader

Constants

GH_REGEX
LAT_LON_REGEX

Attributes

wkt[R]

Public Class Methods

new(instream) click to toggle source
# File lib/geoq/geom_reader.rb, line 17
def initialize(instream)
  @instream = instream
  @wkt = RGeo::WKRep::WKTParser.new
  @factory = RGeo::Cartesian.factory
end

Public Instance Methods

decode(line) click to toggle source
# File lib/geoq/geom_reader.rb, line 29
def decode(line)
  if geohash?(line)
    (lat1, lon1), (lat2, lon2) = GeoHash.decode(line)
    p1 = factory.point(lon1, lat1)
    p2 = factory.point(lon2, lat2)
    geom = RGeo::Cartesian::BoundingBox.create_from_points(p1, p2).to_geometry
    [Geohash.new(geom, strip_whitespace(line))]
  elsif geojson?(line)
    decoded = RGeo::GeoJSON.decode(line)
    case decoded
    when RGeo::GeoJSON::FeatureCollection
      decoded.map { |f| GeoJson.new(f, line) }
    else
      [GeoJson.new(decoded, line)]
    end
  elsif latlon?(line)
    [LatLon.new(factory.point(*(strip_whitespace(line.gsub("\t", ",")).split(",").map(&:to_f).reverse)), line)]
  else
    [Wkt.new(wkt.parse(line), line)]
  end
end
each(&block) click to toggle source
# File lib/geoq/geom_reader.rb, line 23
def each(&block)
  instream.each_line do |l|
    decode(l).each(&block)
  end
end
geohash?(line) click to toggle source
# File lib/geoq/geom_reader.rb, line 55
def geohash?(line)
  !!GH_REGEX.match(strip_whitespace(line))
end
geojson?(line) click to toggle source
# File lib/geoq/geom_reader.rb, line 59
def geojson?(line)
  line.lstrip.start_with?("{")
end
latlon?(line) click to toggle source
# File lib/geoq/geom_reader.rb, line 63
def latlon?(line)
  !!LAT_LON_REGEX.match(strip_whitespace(line.gsub(/\t/, ",")))
end
strip_whitespace(line) click to toggle source
# File lib/geoq/geom_reader.rb, line 51
def strip_whitespace(line)
  line.gsub(/\s+/, "").downcase
end

Private Instance Methods

factory() click to toggle source
# File lib/geoq/geom_reader.rb, line 73
def factory
  @factory
end
instream() click to toggle source
# File lib/geoq/geom_reader.rb, line 69
def instream
  @instream
end