Skip to content

Commit

Permalink
Add Item & connect to description
Browse files Browse the repository at this point in the history
  • Loading branch information
abkfenris committed Apr 10, 2022
1 parent cd8eb50 commit 26737df
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export class Catalog extends CatalogCollectionCommon implements ICatalog {
for (const child of children) {
result += '\n' + (await child.describe(depth - 1, spaces + ' '))
}

const items = await this.get_items()
for (const item of items) {
result += '\n' + (await item.describe(depth - 1, spaces + ' '))
}
}

return result
Expand Down
5 changes: 5 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export class Collection extends CatalogCollectionCommon implements ICollection {
for (const child of children) {
result += '\n' + (await child.describe(depth - 1, spaces + ' '))
}

const items = await this.get_items()
for (const item of items) {
result += '\n' + (await item.describe(depth - 1, spaces + ' '))
}
}

return result
Expand Down
2 changes: 1 addition & 1 deletion src/collection_catalog_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export abstract class CatalogCollectionCommon extends STACObject
return children
}

async get_items(): Promise<IItem[]> {
get_items = async (): Promise<IItem[]> => {
if (!this.store) {
throw new Error('There is no store configured, cannot load items')
}
Expand Down
86 changes: 85 additions & 1 deletion src/item.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
import { Link } from './link'
import { STACObject } from './stac_object'
import { IItem, IItemData, ISTAC, ICatalog, ICollection } from './types'

export class Item extends STACObject {}
export class Item extends STACObject implements IItem {
type = 'Feature' as const
stac_version: string
stac_extensions: string[]
title?: string | undefined
description?: string | undefined
id: string
links: Link[]
geometry?: {
type: string
coordinates: number[][]
}
assets: { [key: string]: object }
bbox?: number[] | undefined
collection?: string | undefined

extra_fields?: { [key: string]: any }

store?: ISTAC | undefined
self_href?: string | undefined
parent?: ICatalog | ICollection | undefined
root?: ICatalog | undefined

constructor(
item_obj: IItemData & {
store?: ISTAC
self_href?: string
root?: ICatalog
parent?: ICatalog | ICollection
} & object
) {
super()

const {
stac_version,
stac_extensions,
title,
description,
id,
links,
geometry,
assets,
bbox,
collection,
store,
self_href,
parent,
root,

...extra
} = item_obj

this.stac_version = stac_version
this.stac_extensions = stac_extensions ?? []
this.title = title
this.description = description
this.id = id
this.links = links.map(l => new Link(l))
this.geometry = geometry
this.assets = assets
this.bbox = bbox
this.collection = collection
this.store = store
this.self_href = self_href
this.parent = parent
this.root = root
this.extra_fields = extra
}

describe = async (_: number = 0, spaces: string = '') => {
return `${spaces}* <Item ${this.id}>`
}

// get_collection(): Promise<ICollection | ICatalog> {
// if (this.parent) {
// return this.parent
// }

// if (this.collection && this.store && this.root) {

// }
// }
}
15 changes: 11 additions & 4 deletions src/reference-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import {
* @returns STAC object
*/
export const fetcher: IFetchFn = async (url: string) => {
const result = await fetch(url)
const data = await result.json()

return data
try {
const result = await fetch(url)
try {
const data = await result.json()
return data
} catch (e) {
throw new Error(`Error converting ${url} to JSON ${e}`)
}
} catch (e) {
throw new Error(`Error loading ${url} ${e}`)
}
}
9 changes: 5 additions & 4 deletions src/stac_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
ICatalogData,
ICollectionData,
IFetchFn,
IItemData,
ISTAC,
IStoreChild,
IStoreChilden,
} from './types'

import { Catalog } from './catalog'
import { Collection } from './collection'
import { Item } from './item'

export class STAC implements ISTAC {
private children: IStoreChilden = {}
Expand All @@ -33,10 +35,9 @@ export class STAC implements ISTAC {
const result = await this.fetcher(url)
let data: IStoreChild

// if ('geometry' in result) {
// Must be an item
// } else
if ('license' in result) {
if ('geometry' in result) {
data = new Item(result as IItemData)
} else if ('license' in result) {
data = new Collection(result as ICollectionData)
} else {
data = new Catalog(result as ICatalogData)
Expand Down
19 changes: 19 additions & 0 deletions test/item.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Item } from '../src/item'

describe('Item', () => {
it('Can be created from an object', () => {
const item = new Item(itemData)

expect(item.id).toEqual('LC80101172015002LGN00')
})

it('Can be described', async () => {
const item = new Item(itemData)

const description = await item.describe()

expect(description).toEqual('* <Item LC80101172015002LGN00>')
})
})

const itemData = require('./test_data/landsat-8-l1/010/117/2015-01-02/LC80101172015002LGN00.json')
5 changes: 5 additions & 0 deletions test/mock_landsat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ const urlData = {
]]: JSON.stringify(
require('./test_data/landsat-8-l1/010/120/2015-01-02/LC80101202015002LGN00.json')
),
[landsatUrls['landsat-8-l1']['010']['120']['2021-02-03'][
'LC80101202021034.json'
]]: JSON.stringify(
require('./test_data/landsat-8-l1/010/120/2021-02-03/LC80101202021034.json')
),

[landsatUrls['landsat-8-l1']['026']['catalog.json']]: JSON.stringify(
require('./test_data/landsat-8-l1/026/catalog.json')
Expand Down
27 changes: 27 additions & 0 deletions test/stac_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,36 @@ describe('STAC Store', () => {
expect(description).toContain(' * <Collection landsat-8-l1>')
expect(description).toContain(' * <Catalog 010>')
expect(description).toContain(' * <Catalog 117>')
expect(description).toContain(' * <Item LC80101172015002LGN00>')
expect(description).toContain(' * <Item LC80101172015018LGN00>')
expect(description).toContain(' * <Catalog 120>')
expect(description).toContain(' * <Item LC80101202015002LGN00>')
expect(description).toContain(' * <Item LC80101202021034>')
expect(description).toContain(' * <Catalog 026>')
expect(description).toContain(' * <Catalog 039>')
expect(description).toContain(' * <Item LC80260392015002LGN00>')
expect(description).toContain(' * <Item LC80260392015018LGN00>')
})

it('Can description will be limited by depth', async () => {
const stac = new STAC(fetcher)
const catalog = await stac.get_root_catalog(catalogUrl)

const description = await catalog.describe(3)

expect(description).toContain('* <Catalog landsat-stac>')
expect(description).toContain(' * <Collection landsat-8-l1>')
expect(description).toContain(' * <Catalog 010>')
expect(description).toContain(' * <Catalog 117>')
expect(description).not.toContain(' * <Item LC80101172015002LGN00>')
expect(description).not.toContain(' * <Item LC80101172015018LGN00>')
expect(description).toContain(' * <Catalog 120>')
expect(description).not.toContain(' * <Item LC80101202015002LGN00>')
expect(description).not.toContain(' * <Item LC80101202021034>')
expect(description).toContain(' * <Catalog 026>')
expect(description).toContain(' * <Catalog 039>')
expect(description).not.toContain(' * <Item LC80260392015002LGN00>')
expect(description).not.toContain(' * <Item LC80260392015018LGN00>')
})

// it('Can load a collection and items', async () => {
Expand Down

0 comments on commit 26737df

Please sign in to comment.