@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
98 lines (97 loc) • 4.07 kB
JavaScript
import { GeoJSON } from 'ol/format';
import StateManager from '../state/statemanager';
import OgcApiClient from './ogcapiclient';
// POC demo variables
export const DEMO_INSTANCES = {
GEORAMA: {
url: 'https://demo.georama.io/features',
name: 'Georama Rivers',
collectionId: '60c4f0d6-2d57-4db1-bf7e-0f0f8f9c1e36',
geometryType: 'MultiLineString',
attributeName: 'FNODE_',
attributeType: 'number',
crs: 'http://www.opengis.net/def/crs/EPSG/0/2056', // NOSONAR
credentials: 'editor:6YPhizVs4BZPvm', // Credentials for demo env, not a risk
serverType: 'georama'
},
GMF: {
url: 'https://geomapfish-demo-2-9.camptocamp.com/mapserv_proxy/QGIS_Server/wfs3',
name: 'GeoMapFish Points',
collectionId: 'points',
geometryType: 'Point',
attributeName: 'name',
attributeType: 'text',
crs: 'http://www.opengis.net/def/crs/EPSG/0/2056', // NOSONAR
credentials: undefined,
serverType: 'gmf'
}
};
const default_crs = 'https://www.opengis.net/def/crs/OGC/1.3/CRS84';
export default class OgcApiFeaturesClient extends OgcApiClient {
constructor(options) {
super(options);
this.url = options.url;
this.stateManager = StateManager.getInstance();
}
async getCollections() {
throw new Error('Not implemented');
}
async getCollection(_collectionId) {
throw new Error('Not implemented');
}
async getSchema(collectionId) {
const relation = 'http://www.opengis.net/def/rel/ogc/1.0/schema'; // NOSONAR
return this.getByRelationAndType(collectionId, relation, 'application/schema+json');
}
async getQueryables(collectionId) {
const relation = 'http://www.opengis.net/def/rel/ogc/1.0/queryables'; // NOSONAR
return this.getByRelationAndType(collectionId, relation, 'application/schema+json');
}
async getItems(collectionId, extent, crs = default_crs) {
let currentUrl = `${this.url}/collections/${collectionId}/items?crs=${crs}&f=json&limit=500`;
if (extent) {
currentUrl += `&bbox=${extent.join(',')}`;
currentUrl += `&bbox-crs=${crs}`;
}
const fetchOptions = this.getFetchOptions();
const features = [];
const responses = await this.fetchAll(currentUrl, fetchOptions);
const geoJsonReader = new GeoJSON();
responses.forEach((response) => {
features.push(...geoJsonReader.readFeatures(response, {}));
});
return features;
}
async getItem(collectionId, itemId, crs = default_crs) {
const url = `${this.url}/collections/${collectionId}/items/${itemId}?crs=${crs}&f=json`;
const response = await fetch(url, this.getFetchOptions());
if (!response.ok) {
throw new Error(`Failed to get item: ${response.status} - ${response.statusText}`);
}
const responseJson = await response.json();
let createdFeature = new GeoJSON().readFeature(responseJson);
if (Array.isArray(createdFeature)) {
createdFeature = createdFeature[0];
}
return createdFeature;
}
async createItem(collectionId, item) {
const url = `${this.url}/collections/${collectionId}/items`;
const geoJson = new GeoJSON().writeFeatureObject(item);
const fetchOptions = this.getFetchOptions('POST');
fetchOptions.headers = new Headers(fetchOptions?.headers);
fetchOptions.headers.set('Content-Type', 'application/geo+json');
fetchOptions.body = JSON.stringify(geoJson);
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`Failed to create feature: ${response.status} - ${response.statusText}`);
}
return true;
}
async updateItem(_collectionId, _itemId, _item) {
throw new Error('Not implemented');
}
async deleteItem(_collectionId, _itemId) {
throw new Error('Not implemented');
}
}