class STAC::Item

Represents STAC item.

STAC Item Specification: github.com/radiantearth/stac-spec/tree/master/item-spec

Attributes

assets[R]
bbox[RW]
collection_id[RW]
geometry[RW]
id[RW]
properties[R]

Public Class Methods

from_hash(hash) click to toggle source
Calls superclass method
# File lib/stac/item.rb, line 16
def from_hash(hash)
  h = hash.transform_keys(&:to_sym)
  h[:properties] = Properties.from_hash(h.fetch(:properties, {}))
  h[:assets] = h.fetch(:assets, {}).transform_values { |v| Asset.from_hash(v) }
  super(h)
rescue KeyError => e
  raise ArgumentError, "required field not found: #{e.key}"
end
new( id:, geometry:, properties:, links: [], assets: {}, bbox: nil, collection: nil, stac_extensions: [], **extra ) click to toggle source
Calls superclass method
# File lib/stac/item.rb, line 30
def initialize(
  id:, geometry:, properties:, links: [], assets: {}, bbox: nil, collection: nil, stac_extensions: [], **extra
)
  @id = id
  @geometry = geometry
  @properties = properties
  @assets = assets
  @bbox = bbox
  case collection
  when Collection
    self.collection = collection
  else
    @collection_id = collection
  end
  super(links: links, stac_extensions: stac_extensions, **extra)
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method STAC::HashLike#[]
# File lib/stac/item.rb, line 47
def [](key)
  value = super
  if value.nil?
    properties.extra[key.to_s]
  else
    value
  end
end
add_asset(key:, href:, title: nil, description: nil, type: nil, roles: nil, **extra) click to toggle source

Adds an asset with the given key.

When the item has extendable stac_extensions, make the asset extend the extension modules.

# File lib/stac/item.rb, line 88
def add_asset(key:, href:, title: nil, description: nil, type: nil, roles: nil, **extra)
  asset = Asset.new(href: href, title: title, description: description, type: type, roles: roles, **extra)
  extensions.each do |extension|
    asset.extend(extension::Asset) if extension.const_defined?(:Asset)
  end
  assets[key] = asset
  self
end
collection() click to toggle source

Returns a rel=“collection” link as a collection object if it exists.

# File lib/stac/item.rb, line 73
def collection
  link = find_link(rel: 'collection')
  link&.target
end
collection=(collection) click to toggle source

Overwrites rel=“collection” link and collection_id attribute.

# File lib/stac/item.rb, line 79
def collection=(collection)
  @collection_id = collection.id
  remove_links(rel: 'collection')
  add_link(collection, rel: 'collection', type: 'application/json', title: collection.title)
end
to_h() click to toggle source
Calls superclass method STAC::HashLike#to_h
# File lib/stac/item.rb, line 56
def to_h
  super.merge(
    {
      'geometry' => geometry, # required but nullable
    },
  ).merge(
    {
      'id' => id,
      'bbox' => bbox,
      'properties' => properties.to_h,
      'assets' => assets.transform_values(&:to_h),
      'collection' => collection_id,
    }.compact,
  )
end