Class: GeoCli::GeomReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/geo-cli/geom_reader.rb

Constant Summary collapse

BASE_32 =
%w(0 1 2 3 4 5 6 7 8 9 b c d e f g h j
k m n p q r s t u v w x y z).flat_map { |c| [c, c.upcase].uniq }.join("")
GH_REGEX =
Regexp.new(/\A\s*[#{BASE_32}]+\s*\z/)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instream) ⇒ GeomReader

Returns a new instance of GeomReader.



100
101
102
103
104
# File 'lib/geo-cli/geom_reader.rb', line 100

def initialize(instream)
  @instream = instream
  @wkt = RGeo::WKRep::WKTParser.new
  @factory = RGeo::Cartesian.factory
end

Instance Attribute Details

#wktObject (readonly)

Returns the value of attribute wkt.



92
93
94
# File 'lib/geo-cli/geom_reader.rb', line 92

def wkt
  @wkt
end

Instance Method Details

#decode(line) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/geo-cli/geom_reader.rb', line 112

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)
  elsif geojson?(line)
    GeoJson.new(RGeo::GeoJSON.decode(line))
  else
    Wkt.new(wkt.parse(line))
  end
end

#each(&block) ⇒ Object



106
107
108
109
110
# File 'lib/geo-cli/geom_reader.rb', line 106

def each(&block)
  instream.each_line do |l|
    block.call(decode(l))
  end
end

#geohash?(line) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/geo-cli/geom_reader.rb', line 126

def geohash?(line)
  !!GH_REGEX.match(line)
end

#geojson?(line) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/geo-cli/geom_reader.rb', line 130

def geojson?(line)
  line.lstrip.start_with?("{")
end