Class: Aerospike::GeoJSON

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/geo_json.rb

Overview

Wrapper for GeoJSON data. GeoJSON data needs to be wrapped to allow the client to distinguish geospatial data from string (or hash) data. Geospatial data from a record’s bin will be returned as an instance of this class. The wrapper accepts GeoJSON data either as a String or a Hash.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ GeoJSON

Returns a new instance of GeoJSON.



30
31
32
33
34
35
36
37
38
# File 'lib/aerospike/geo_json.rb', line 30

def initialize(data)
  self.json_data =
    case data
    when String
      data
    else
      data.to_json
    end
end

Class Method Details

.circle(lng, lat, radius) ⇒ Object



103
104
105
# File 'lib/aerospike/geo_json.rb', line 103

def self.circle(lng, lat, radius)
  new(type: 'AeroCircle', coordinates: [[lng, lat], radius])
end

.point(lng, lat) ⇒ Object



99
100
101
# File 'lib/aerospike/geo_json.rb', line 99

def self.point(lng, lat)
  new(type: 'Point', coordinates: [lng, lat])
end

.polygon(coordinates) ⇒ Object



107
108
109
# File 'lib/aerospike/geo_json.rb', line 107

def self.polygon(coordinates)
  new(type: 'Polygon', coordinates: coordinates)
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
# File 'lib/aerospike/geo_json.rb', line 50

def ==(other)
  return false unless other.class == self.class
  other.to_json == self.to_json
end

#circle?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/aerospike/geo_json.rb', line 91

def circle?
  type == 'AeroCircle'
end

#coordinatesObject



79
80
81
# File 'lib/aerospike/geo_json.rb', line 79

def coordinates
  to_h['coordinates']
end

#latObject



64
65
66
67
68
69
70
71
# File 'lib/aerospike/geo_json.rb', line 64

def lat
  case type
  when 'Point'
    coordinates.last
  when 'AeroCircle'
    coordinates.first.last
  end
end

#lngObject



55
56
57
58
59
60
61
62
# File 'lib/aerospike/geo_json.rb', line 55

def lng
  case type
  when 'Point'
    coordinates.first
  when 'AeroCircle'
    coordinates.first.first
  end
end

#point?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/aerospike/geo_json.rb', line 87

def point?
  type == 'Point'
end

#polygon?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/aerospike/geo_json.rb', line 95

def polygon?
  type == 'Polygon'
end

#radiusObject



73
74
75
76
77
# File 'lib/aerospike/geo_json.rb', line 73

def radius
  return nil unless circle?

  coordinates.last
end

#to_circle(radius) ⇒ Object

Raises:

  • (TypeError)


111
112
113
114
115
# File 'lib/aerospike/geo_json.rb', line 111

def to_circle(radius)
  raise TypeError, 'Cannot create a Circle from a Polygon' if polygon?

  self.class.circle(lng, lat, radius)
end

#to_hashObject Also known as: to_h



45
46
47
# File 'lib/aerospike/geo_json.rb', line 45

def to_hash
  JSON.parse(json_data)
end

#to_jsonObject Also known as: to_s



40
41
42
# File 'lib/aerospike/geo_json.rb', line 40

def to_json
  json_data
end

#to_pointObject

Raises:

  • (TypeError)


117
118
119
120
121
122
# File 'lib/aerospike/geo_json.rb', line 117

def to_point
  return self if point?
  raise TypeError, 'Cannot create a Point from a Polygon' if polygon?

  self.class.point(lng, lat)
end

#typeObject



83
84
85
# File 'lib/aerospike/geo_json.rb', line 83

def type
  to_h['type']
end