Skip to content

Commit

Permalink
Implement queryables
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed May 17, 2024
1 parent b5380f2 commit 6f9a1c2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
14 changes: 4 additions & 10 deletions src/api/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,12 @@ export default class Data {
id = req.params['*'].replace(/\/queryables$/, '');
}

const collection = this.catalog.getData(id);
if (collection === null) {
const queryables = this.catalog.getSchema(id);
if (queryables === null) {
throw new Errors.CollectionNotFound();
}
res.json({
"$schema" : "https://json-schema.org/draft/2019-09/schema",
"$id" : Utils.getApiUrl(`/collections/${id}/queryables`),
"title" : "Queryables",
"type" : "object",
"properties" : {},
"additionalProperties": false
});

res.json(queryables);
}

}
72 changes: 66 additions & 6 deletions src/models/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ const commonNames = {
lwir12: [11.5, 12.5]
};

const geeSchemaMapping = {
STRING: {
type: "string"
},
DOUBLE: {
type: "number"
},
INT: {
type: "integer"
},
STRING_LIST: {
type: "array",
items: {
type: "string"
}
},
INT_LIST: {
type: "array",
items: {
type: "integer"
}
}
};

export default class DataCatalog {

constructor(context) {
Expand All @@ -46,7 +70,7 @@ export default class DataCatalog {
if (obj.type !== 'Collection') {
return;
}
const collection = this.fixData(obj);
const collection = this.fixDataOnce(obj);
if (this.supportedGeeTypes.includes(collection['gee:type'])) {
this.collections[collection.id] = collection;
}
Expand Down Expand Up @@ -99,22 +123,58 @@ export default class DataCatalog {
return await this.readLocalCatalog();
}

getData(id = null) {
getSchema(id) {
const collection = this.getData(id, true);
if (!collection) {
return null;
}

const geeSchemas = collection.summaries['gee:schema'];
const jsonSchema = {
"$schema" : "https://json-schema.org/draft/2019-09/schema",
"$id" : Utils.getApiUrl(`/collections/${id}/queryables`),
"title" : "Queryables",
"type" : "object",
"properties" : {},
"additionalProperties": false
};
if (!Array.isArray(geeSchemas)) {
return jsonSchema;
}

for(const geeSchema of geeSchemas) {
const s = {
description: geeSchema.description || ""
};
Object.assign(s, geeSchemaMapping[geeSchema.type] || {});
jsonSchema.properties[geeSchema.name] = s;
}

return jsonSchema;
}

getData(id = null, withSchema = false) {
if (id !== null) {
if (typeof this.collections[id] !== 'undefined') {
return this.fixLinks(this.collections[id]);
return this.updateCollection(this.collections[id], withSchema);
}
else {
return null;
}
}
else {
return Object.values(this.collections).map(c => this.fixLinks(c));
return Object.values(this.collections).map(c => this.updateCollection(c, withSchema));
}
}

fixLinks(c) {
updateCollection(c, withSchema = false) {
c = Object.assign({}, c);
if (!withSchema) {
c.summaries = Utils.omitFromObject(c.summaries, ['gee:schema']);
}
c.links = c.links.slice(0);
c.links = c.links.map(l => {
l = Object.assign({}, l);
switch(l.rel) {
case 'self':
l.href = Utils.getApiUrl("/collections/" + c.id);
Expand All @@ -137,7 +197,7 @@ export default class DataCatalog {
return c;
}

fixData(c) {
fixDataOnce(c) {
// Fix invalid headers in markdown
if (typeof c.description === 'string') {
c.description = c.description.replace(/^(#){2,6}([^#\s].+)$/img, '$1 $2');
Expand Down

0 comments on commit 6f9a1c2

Please sign in to comment.