Class: Parse::GeoJSON::Geometry

Inherits:
Model
  • Object
show all
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

LineString, MultiPolygon

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#dirty?, find_class

Methods included from Client::Connectable

#client

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

#coordinatesArray

Returns the raw coordinates array, in GeoJSON nesting and ‘[longitude, latitude]` axis order. Shape varies by subclass.

Returns:

  • (Array)

    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.

Examples:

Parse::GeoJSON::Geometry.from_geojson(line_string_hash) # => Parse::GeoJSON::LineString

Returns:

Raises:

  • (ArgumentError)


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_typeString

Returns the GeoJSON ‘type` discriminator (`“Point”`, `“LineString”`, `“Polygon”`, `“MultiPolygon”`, etc.).

Returns:

  • (String)

    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_typeObject



40
41
42
# File 'lib/parse/model/geojson.rb', line 40

def geojson_type
  self.class.geojson_type
end

#inspectObject



92
93
94
# File 'lib/parse/model/geojson.rb', line 92

def inspect
  "#<#{self.class.name} #{@coordinates.inspect}>"
end

#to_geojsonHash Also known as: as_json

Returns the standard GeoJSON ‘coordinates:` hash.

Returns:

  • (Hash)

    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.

Returns:

  • (String)

    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