Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getCollectionItemsUrl Method for OGC API #32

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ogc-api/endpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,19 @@ describe('OgcApiEndpoint', () => {
});
});
});
describe('#getCollectionItemsUrl', () => {
it('returns the correct URL for the collection items', () => {
expect(
endpoint.getCollectionItemsUrl('airports', {
limit: 101,
query: 'name=Sumburgh Airport',
outputFormat: 'json',
})
).resolves.toEqual(
'https://my.server.org/sample-data/collections/airports/items?f=json&name=Sumburgh+Airport&limit=101'
);
});
});
});
describe('a failure happens while parsing the endpoint capabilities', () => {
beforeEach(() => {
Expand Down
57 changes: 57 additions & 0 deletions src/ogc-api/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getLinkUrl,
} from './link-utils.js';
import { EndpointError } from '../shared/errors.js';
import { BoundingBox, CrsCode, MimeType } from '../shared/models.js';

/**
* Represents an OGC API endpoint advertising various collections and services.
Expand Down Expand Up @@ -245,4 +246,60 @@ export default class OgcApiEndpoint {
})
.then(fetchDocument<OgcApiCollectionItem>);
}

/**
* Asynchronously retrieves a URL for the items of a specified collection, with optional query parameters.
* @param collectionId - The unique identifier for the collection.
* @param options - An object containing optional parameters:
* - query: Additional query parameters to be included in the URL.
* - outputFormat: The MIME type for the output format. Default is 'json'.
* - limit: The maximum number of features to include.
* - extent: Bounding box to limit the features.
* - offset: Pagination offset for the returned results.
* - outputCrs: Coordinate Reference System code for the output.
* - extentCrs: Coordinate Reference System code for the bounding box.
* @returns A promise that resolves to the URL as a string or rejects if an error occurs.
*/
getCollectionItemsUrl(
collectionId: string,
options: {
query?: string;
outputFormat?: MimeType;
limit?: number;
offset?: number;
outputCrs?: CrsCode;
extent?: BoundingBox;
extentCrs?: CrsCode;
} = {}
): Promise<string> {
return this.getCollectionDocument(collectionId)
.then((collectionDoc) => {
const baseUrl = this.baseUrl || '';
const itemsLink = getLinkUrl(collectionDoc, 'items', baseUrl);
const url = new URL(itemsLink);

// Set the format to JSON if not specified
const format = options.outputFormat || 'json';
url.searchParams.set('f', format);

if (options.query !== undefined)
url.search += (url.search ? '&' : '') + options.query;
if (options.limit !== undefined)
url.searchParams.set('limit', options.limit.toString());
if (options.offset !== undefined)
url.searchParams.set('offset', options.offset.toString());
if (options.outputCrs !== undefined)
url.searchParams.set('crs', options.outputCrs);
if (options.extent !== undefined && options.extent.length === 4)
url.searchParams.set('bbox', options.extent.join(','));
if (options.extentCrs !== undefined)
url.searchParams.set('bbox-crs', options.extentCrs);

return url.toString();
})
.catch((error) => {
console.error('Error fetching collection items URL:', error);
throw error;
});
}
}
Loading