class STAC::Catalog

Represents STAC catalog.

STAC Catalog Specification: github.com/radiantearth/stac-spec/tree/master/catalog-spec

Attributes

description[RW]
id[RW]
title[RW]

Public Class Methods

new(id:, description:, links: [], title: nil, stac_extensions: [], **extra) click to toggle source
Calls superclass method
# File lib/stac/catalog.rb, line 27
def initialize(id:, description:, links: [], title: nil, stac_extensions: [], **extra)
  @id = id
  @description = description
  @title = title
  super(links: links, stac_extensions: stac_extensions, **extra)
end
root(id:, description:, href:, links: [], title: nil, stac_extensions: [], **extra) click to toggle source
# File lib/stac/catalog.rb, line 15
def root(id:, description:, href:, links: [], title: nil, stac_extensions: [], **extra)
  catalog = new(
    id: id, description: description, links: links, title: title, stac_extensions: stac_extensions, **extra,
  )
  catalog.self_href = href
  catalog.root = catalog
  catalog
end

Public Instance Methods

add_child(catalog, href: " click to toggle source

Adds a rel=“child” link to self and adds “self”, “root”, and “parent” links to the child catalog.

# File lib/stac/catalog.rb, line 95
def add_child(catalog, href: "#{catalog.id}/#{catalog.type.downcase}.json", title: catalog.title)
  if (base = self_href)
    catalog.self_href = Pathname(base).dirname.join(href).to_s
  end
  catalog.root = root
  catalog.parent = self
  add_link(catalog, rel: 'child', type: 'application/json', title: title)
end
add_item(item, href: " click to toggle source

Adds a rel=“item” link to self and adds “self”, “root”, and “parent” links to the given item.

# File lib/stac/catalog.rb, line 105
def add_item(item, href: "#{item.id}.json", title: item.properties.title)
  if (base = self_href)
    item.self_href = Pathname(base).dirname.join(href).to_s
  end
  item.root = root
  item.parent = self
  add_link(item, rel: 'item', type: 'application/geo+json', title: title)
end
all_collections() click to toggle source

Returns all collections from this catalog and its child catalogs/collections recursively.

# File lib/stac/catalog.rb, line 60
def all_collections
  # The last `.lazy` is not necessary with Ruby 3.1.
  # But with Ruby 3.0, it is necessary because Enumerator::Lazy#chain returns Enumerator::Chain
  # and RBS type check fails.
  collections.chain(children.flat_map(&:all_collections)).lazy
end
all_items() click to toggle source

Returns all items from this catalog and its child catalogs/collections recursively.

# File lib/stac/catalog.rb, line 80
def all_items
  # The last `.lazy` is not necessary with Ruby 3.1.
  # But with Ruby 3.0, it is necessary because Enumerator::Lazy#chain returns Enumerator::Chain
  # and RBS type check fails.
  items.chain(children.flat_map(&:all_items)).lazy
end
children() click to toggle source

Returns catalog/collection objects from rel=“child” links of this catalog.

# File lib/stac/catalog.rb, line 46
def children
  child_links.lazy.map(&:target)
end
collections() click to toggle source

Filters only collections from children.

# File lib/stac/catalog.rb, line 55
def collections
  children.select { |child| child.type == 'Collection' }
end
export(dest_dir = nil, writer: FileWriter.new) click to toggle source

Exports this catalog and all its children and items to the specified dir or each self href.

# File lib/stac/catalog.rb, line 115
def export(dest_dir = nil, writer: FileWriter.new)
  dest_pathname = Pathname(dest_dir) if dest_dir
  self_dest = dest_pathname.join(File.basename(self_href!)).to_s if dest_pathname
  save(self_dest, writer: writer)

  item_links.select(&:resolved?).each do |item_link|
    item_dest = dest_pathname.join(item_link.relative_href!).to_s if dest_pathname
    item_link.target.save(item_dest, writer: writer)
  end

  child_links.select(&:resolved?).each do |child_link|
    child_dest_dir = dest_pathname.join(child_link.relative_href!).dirname.to_s if dest_pathname
    child_link.target.export(child_dest_dir, writer: writer)
  end
end
find_child(id, recursive: false) click to toggle source

Returns the child catalog/collection with the given ID if it exists.

With option ‘recusive: true`, it will traverse all child catalogs/collections recursively.

# File lib/stac/catalog.rb, line 70
def find_child(id, recursive: false)
  (recursive ? all_children : children).find { |child| child.id == id }
end
find_item(id, recursive: false) click to toggle source

Returns the item with the given ID if it exists.

With option ‘recursive: true`, it will traverse all child catalogs/collections recursively.

# File lib/stac/catalog.rb, line 90
def find_item(id, recursive: false)
  (recursive ? all_items : items).find { |item| item.id == id }
end
items() click to toggle source

Returns item objects from rel=“item” links of this catalog.

# File lib/stac/catalog.rb, line 75
def items
  item_links.lazy.map(&:target)
end
to_h() click to toggle source

Serializes self to a Hash.

Calls superclass method STAC::HashLike#to_h
# File lib/stac/catalog.rb, line 35
def to_h
  super.merge(
    {
      'id' => id,
      'title' => title,
      'description' => description,
    }.compact,
  )
end