Module: NSWTopo::GeoJSON

Defined in:
lib/nswtopo/gis/geojson.rb,
lib/nswtopo/gis/geojson/point.rb,
lib/nswtopo/gis/geojson/polygon.rb,
lib/nswtopo/gis/geojson/collection.rb,
lib/nswtopo/gis/geojson/line_string.rb,
lib/nswtopo/gis/geojson/multi_point.rb,
lib/nswtopo/gis/geojson/multi_polygon.rb,
lib/nswtopo/gis/geojson/multi_line_string.rb

Defined Under Namespace

Classes: Collection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon

Constant Summary collapse

Error =
Class.new StandardError
TYPES =
Set.new %W[Point MultiPoint LineString MultiLineString Polygon MultiPolygon]
CLASSES =
TYPES.map do |type|
  klass = Class.new do
    def initialize(coordinates, properties = nil, &block)
      @coordinates, @properties = coordinates, properties || {}
      raise Error, "invalid feature properties" unless Hash === @properties
      instance_eval(&block) if block_given?
      @coordinates.freeze
      @properties.freeze
      freeze!
    end

    alias freeze! freeze

    attr_reader :coordinates, :properties

    extend Forwardable
    delegate %i[[] fetch values_at key? slice except dig] => :@properties
    delegate %i[empty?] => :@coordinates
    delegate %i[to_json] => :to_h
  end

  klass.define_method :to_h do
    {
      "type" => "Feature",
      "geometry" => {
        "type" => type,
        "coordinates" => @coordinates
      },
      "properties" => @properties
    }
  end

  klass.define_method :with_properties do |properties|
    klass.new @coordinates, **properties
  end

  klass.define_method :add_properties do |properties|
    klass.new @coordinates, **@properties, **properties
  end

  next type, const_set(type, klass)
end
DEFAULT_PROJECTION =
Projection.wgs84