class Charta::GeoJSON

Represents a Geometry with SRID

Attributes

srid[R]

Public Class Methods

flatten(hash) click to toggle source
# File lib/charta/geo_json.rb, line 47
def flatten(hash)
  Coordinates.flatten hash
end
new(data, srid = :WGS84) click to toggle source
# 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
valid?(data, srid = :WGS84) click to toggle source

Test is given data is a valid GeoJSON

# File lib/charta/geo_json.rb, line 41
def valid?(data, srid = :WGS84)
  new(data, srid).valid?
rescue
  false
end

Public Instance Methods

geom() click to toggle source
# File lib/charta/geo_json.rb, line 20
def geom
  Charta.new_geometry(to_ewkt)
end
to_ewkt() click to toggle source
# File lib/charta/geo_json.rb, line 28
def to_ewkt
  "SRID=#{srid};" + EwktSerializer.object_to_ewkt(@json)
end
to_hash() click to toggle source
# File lib/charta/geo_json.rb, line 24
def to_hash
  @json
end
valid?() click to toggle source
# File lib/charta/geo_json.rb, line 32
def valid?
  to_ewkt
  true
rescue
  false
end