module Charta::Coordinates

Public Class Methods

flatten(hash) click to toggle source

Force coordinates to 2D

# File lib/charta/coordinates.rb, line 6
def flatten(hash)
  map_coordinates(hash) { |position| position[0..1] }
end
map_coordinates(hash, &block) click to toggle source
# File lib/charta/coordinates.rb, line 10
def map_coordinates(hash, &block)
  case hash['type']
  when 'FeatureCollection'
    map_feature_collection_coordinates hash, &block
  when 'Feature'
    map_feature_coordinates hash, &block
  else
    map_geometry_coordinates hash, &block
  end
end
normalize_4326_geometry(json) click to toggle source
# File lib/charta/coordinates.rb, line 21
def normalize_4326_geometry(json)
  map_coordinates json do |(x, y)|
    [((x + 180.to_d) % 360.to_d) - 180.to_d, ((y + 90.to_d) % 180.to_d) - 90.to_d]
  end
end

Private Class Methods

map_feature_collection_coordinates(hash, &block) click to toggle source
# File lib/charta/coordinates.rb, line 29
def map_feature_collection_coordinates(hash, &block)
  hash.merge 'features' => hash['features'].map { |feature| map_feature_coordinates feature, &block }
end
map_feature_coordinates(hash, &block) click to toggle source
# File lib/charta/coordinates.rb, line 33
def map_feature_coordinates(hash, &block)
  hash.merge 'geometry' => map_geometry_coordinates(hash['geometry'], &block)
end
map_geometry_collection_coordinates(hash, &block) click to toggle source
# File lib/charta/coordinates.rb, line 60
def map_geometry_collection_coordinates(hash, &block)
  hash.merge 'geometries' => hash['geometries'].map { |geometry| map_geometry_coordinates(geometry, &block) }
end
map_geometry_coordinates(hash, &block) click to toggle source
# File lib/charta/coordinates.rb, line 37
def map_geometry_coordinates(hash, &block)
  if hash['type'] == 'GeometryCollection'
    map_geometry_collection_coordinates hash, &block
  else
    coordinates = hash['coordinates']
    mapped =
      case hash['type']
      when 'Point'
        block.call coordinates
      when 'MultiPoint', 'LineString'
        coordinates.map(&block)
      when 'MultiLineString', 'Polygon'
        coordinates.map { |line| line.map(&block) }
      when 'MultiPolygon'
        coordinates.map { |poly| poly.map { |line| line.map(&block) } }
      else
        raise StandardError.new("Cannot handle: #{hash['type'].inspect}. In #{hash.inspect}")
      end

    hash.merge 'coordinates' => mapped
  end
end