Class: Parse::GeoJSON::Geometry
- Defined in:
- lib/parse/model/geojson.rb
Overview
Base class for GeoJSON geometry wrappers. Subclasses define ‘TYPE` and `#valid_coordinates?` and inherit the round-trip plumbing.
Direct Known Subclasses
Constant Summary collapse
- TYPE_REGISTRY =
Dispatch table for from_geojson. Kept here at the end so subclasses are registered after their constants are defined.
{ "LineString" => LineString, "MultiPolygon" => MultiPolygon, }.freeze
Constants inherited from Model
Model::CLASS_AUDIENCE, Model::CLASS_INSTALLATION, Model::CLASS_JOB_SCHEDULE, Model::CLASS_JOB_STATUS, Model::CLASS_PRODUCT, Model::CLASS_PUSH_STATUS, Model::CLASS_ROLE, Model::CLASS_SCHEMA, Model::CLASS_SESSION, Model::CLASS_USER, Model::ID, Model::KEY_CLASS_NAME, Model::KEY_CREATED_AT, Model::KEY_OBJECT_ID, Model::KEY_UPDATED_AT, Model::OBJECT_ID, Model::TYPE_ACL, Model::TYPE_BYTES, Model::TYPE_DATE, Model::TYPE_FIELD, Model::TYPE_FILE, Model::TYPE_GEOPOINT, Model::TYPE_NUMBER, Model::TYPE_OBJECT, Model::TYPE_POINTER, Model::TYPE_POLYGON, Model::TYPE_RELATION
Instance Attribute Summary collapse
-
#coordinates ⇒ Array
The raw coordinates array, in GeoJSON nesting and ‘[longitude, latitude]` axis order.
Class Method Summary collapse
-
.from_geojson(hash) ⇒ Parse::GeoJSON::Geometry
Build any GeoJSON geometry from its wire-format Hash.
-
.geojson_type ⇒ String
The GeoJSON ‘type` discriminator (`“Point”`, `“LineString”`, `“Polygon”`, `“MultiPolygon”`, etc.).
Instance Method Summary collapse
- #==(other) ⇒ Object
- #geojson_type ⇒ Object
-
#initialize(value = nil) ⇒ Geometry
constructor
The initializer accepts either a GeoJSON Hash (‘coordinates:`), a plain coordinates Array, or another instance of the same class.
- #inspect ⇒ Object
-
#to_geojson ⇒ Hash
(also: #as_json)
The standard GeoJSON ‘coordinates:` hash.
-
#to_json(*args) ⇒ String
The JSON form, suitable for direct shipment to any GeoJSON-aware consumer.
Methods inherited from Model
Methods included from Client::Connectable
Constructor Details
#initialize(value = nil) ⇒ Geometry
The initializer accepts either a GeoJSON Hash (‘coordinates:`), a plain coordinates Array, or another instance of the same class.
46 47 48 49 |
# File 'lib/parse/model/geojson.rb', line 46 def initialize(value = nil) @coordinates = [] self.coordinates = value unless value.nil? end |
Instance Attribute Details
#coordinates ⇒ Array
Returns the raw coordinates array, in GeoJSON nesting and ‘[longitude, latitude]` axis order. Shape varies by subclass.
32 33 34 |
# File 'lib/parse/model/geojson.rb', line 32 def coordinates @coordinates end |
Class Method Details
.from_geojson(hash) ⇒ Parse::GeoJSON::Geometry
Build any GeoJSON geometry from its wire-format Hash. Dispatches to the matching subclass based on the ‘type` field.
107 108 109 110 111 112 113 114 |
# File 'lib/parse/model/geojson.rb', line 107 def self.from_geojson(hash) raise ArgumentError, "[Parse::GeoJSON::Geometry] expected a Hash." unless hash.is_a?(Hash) h = hash.respond_to?(:symbolize_keys) ? hash.symbolize_keys : hash type = (h[:type] || h["type"]).to_s klass = TYPE_REGISTRY[type] raise ArgumentError, "[Parse::GeoJSON::Geometry] unsupported GeoJSON type #{type.inspect}." if klass.nil? klass.new(h) end |
.geojson_type ⇒ String
Returns the GeoJSON ‘type` discriminator (`“Point”`, `“LineString”`, `“Polygon”`, `“MultiPolygon”`, etc.).
36 37 38 |
# File 'lib/parse/model/geojson.rb', line 36 def self.geojson_type const_get(:TYPE) end |
Instance Method Details
#==(other) ⇒ Object
87 88 89 90 |
# File 'lib/parse/model/geojson.rb', line 87 def ==(other) return false unless other.is_a?(self.class) @coordinates == other.coordinates end |
#geojson_type ⇒ Object
40 41 42 |
# File 'lib/parse/model/geojson.rb', line 40 def geojson_type self.class.geojson_type end |
#inspect ⇒ Object
92 93 94 |
# File 'lib/parse/model/geojson.rb', line 92 def inspect "#<#{self.class.name} #{@coordinates.inspect}>" end |
#to_geojson ⇒ Hash Also known as: as_json
Returns the standard GeoJSON ‘coordinates:` hash.
76 77 78 |
# File 'lib/parse/model/geojson.rb', line 76 def to_geojson { "type" => geojson_type, "coordinates" => deep_copy_array(@coordinates) } end |
#to_json(*args) ⇒ String
Returns the JSON form, suitable for direct shipment to any GeoJSON-aware consumer.
83 84 85 |
# File 'lib/parse/model/geojson.rb', line 83 def to_json(*args) to_geojson.to_json(*args) end |