Class: Charta::GeoJSON

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

Overview

Represents a Geometry with SRID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#sridObject (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

Returns:

  • (Boolean)


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

#geomObject



20
21
22
# File 'lib/charta/geo_json.rb', line 20

def geom
  Charta.new_geometry(to_ewkt)
end

#to_ewktObject



28
29
30
# File 'lib/charta/geo_json.rb', line 28

def to_ewkt
  "SRID=#{srid};" + EwktSerializer.object_to_ewkt(@json)
end

#to_hashObject



24
25
26
# File 'lib/charta/geo_json.rb', line 24

def to_hash
  @json
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/charta/geo_json.rb', line 32

def valid?
  to_ewkt
  true
rescue
  false
end