Class: GeoAPI::Geometry
- Inherits:
-
Object
- Object
- GeoAPI::Geometry
- Defined in:
- lib/geoapi/geometry.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.coords ⇒ Object
Returns the value of attribute coords.
-
.geometry_type ⇒ Object
Returns the value of attribute geometry_type.
Class Method Summary collapse
- .from_hash(hash) ⇒ Object
- .from_json(json) ⇒ Object
-
.new_from_class_name(class_name) ⇒ Object
Class methods.
Instance Method Summary collapse
-
#coordinates ⇒ Object
Instance methods.
-
#initialize(attrs) ⇒ Geometry
constructor
A new instance of Geometry.
- #to_json(options = nil) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Geometry
Returns a new instance of Geometry.
76 77 78 |
# File 'lib/geoapi/geometry.rb', line 76 def initialize attrs @geometry_type = self.class.name.split('::').last end |
Class Attribute Details
.coords ⇒ Object
Returns the value of attribute coords.
5 6 7 |
# File 'lib/geoapi/geometry.rb', line 5 def coords @coords end |
.geometry_type ⇒ Object
Returns the value of attribute geometry_type.
5 6 7 |
# File 'lib/geoapi/geometry.rb', line 5 def geometry_type @geometry_type end |
Class Method Details
.from_hash(hash) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/geoapi/geometry.rb', line 42 def self.from_hash hash unless hash.blank? geom = Geometry.new_from_class_name hash['type'] unless geom.blank? geom.geometry_type = hash['type'] geom.coords = hash['coordinates'] puts "Got geometry: #{geom}" geom end end end |
.from_json(json) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/geoapi/geometry.rb', line 24 def self.from_json json unless json.blank? attrs = Crack::JSON.parse(json) geom = Geometry.new_from_class_name json['type'] unless geom.blank? geom.geometry_type = attrs['type'] geom.coords = attrs['coordinates'] geom end end end |
.new_from_class_name(class_name) ⇒ Object
Class methods
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/geoapi/geometry.rb', line 11 def self.new_from_class_name class_name geom = case class_name.downcase.gsub('_','') when "point" then GeoAPI::Point.new when "polygon" then GeoAPI::Polygon.new when "multipoint" then GeoAPI::Polygon.new end end |
Instance Method Details
#coordinates ⇒ Object
Instance methods
64 65 66 |
# File 'lib/geoapi/geometry.rb', line 64 def coordinates coords end |
#to_json(options = nil) ⇒ Object
72 73 74 |
# File 'lib/geoapi/geometry.rb', line 72 def to_json =nil {:type=>self.geometry_type, :coordinates=>self.coords}.to_json end |
#type ⇒ Object
68 69 70 |
# File 'lib/geoapi/geometry.rb', line 68 def type geometry_type end |