Class: Charta::GeoJSON
- Inherits:
-
Object
- Object
- Charta::GeoJSON
- Defined in:
- lib/charta/geo_json.rb
Overview
Represents a Geometry with SRID
Instance Attribute Summary collapse
-
#srid ⇒ Object
readonly
Returns the value of attribute srid.
Class Method Summary collapse
- .flatten(hash) ⇒ Object
-
.valid?(data, srid = :WGS84) ⇒ Boolean
Test is given data is a valid GeoJSON.
Instance Method Summary collapse
- #geom ⇒ Object
-
#initialize(data, srid = :WGS84) ⇒ GeoJSON
constructor
A new instance of GeoJSON.
- #to_ewkt ⇒ Object
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(data, srid = :WGS84) ⇒ GeoJSON
Returns a new instance of GeoJSON.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/charta/geo_json.rb', line 8 def initialize(data, srid = :WGS84) srid ||= :WGS84 @json = Coordinates.flatten(data.is_a?(Hash) ? data : JSON.parse(data)) lsrid = @json['crs']['properties']['name'] if @json.is_a?(Hash) && @json['crs'].is_a?(Hash) && @json['crs']['properties'].is_a?(Hash) lsrid ||= srid @srid = ::Charta.find_srid(lsrid) @json = Coordinates.normalize_4326_geometry(@json) if @srid.to_i == 4326 end |
Instance Attribute Details
#srid ⇒ Object (readonly)
Returns the value of attribute srid.
6 7 8 |
# File 'lib/charta/geo_json.rb', line 6 def srid @srid end |
Class Method Details
.flatten(hash) ⇒ Object
47 48 49 |
# File 'lib/charta/geo_json.rb', line 47 def flatten(hash) Coordinates.flatten hash end |
.valid?(data, srid = :WGS84) ⇒ Boolean
Test is given data is a valid GeoJSON
41 42 43 44 45 |
# File 'lib/charta/geo_json.rb', line 41 def valid?(data, srid = :WGS84) new(data, srid).valid? rescue false end |
Instance Method Details
#geom ⇒ Object
20 21 22 |
# File 'lib/charta/geo_json.rb', line 20 def geom Charta.new_geometry(to_ewkt) end |
#to_ewkt ⇒ Object
28 29 30 |
# File 'lib/charta/geo_json.rb', line 28 def to_ewkt "SRID=#{srid};" + EwktSerializer.object_to_ewkt(@json) end |
#to_hash ⇒ Object
24 25 26 |
# File 'lib/charta/geo_json.rb', line 24 def to_hash @json end |
#valid? ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/charta/geo_json.rb', line 32 def valid? to_ewkt true rescue false end |