UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

63 lines (62 loc) 2.81 kB
// SPDX-License-Identifier: Apache-2.0 import { oapifNumericTypes, oapifTemporalTypes, oapifTextTypes } from '../../models/serverogcapifeatures.js'; /** * Represents the schema for an OGC API collection feature. */ export default class OgcApiFeaturesSchema { schema; formAttributes = []; constructor(schema) { this.schema = schema; // Extract all attributes that can be edited in a form for (const [key, properties] of Object.entries(this.schema.properties)) { if (this.isGeometry(properties) || this.isPrimaryKey(properties)) continue; this.formAttributes.push([key, this.setDefaults(key, properties)]); } // Sort the attributes according to sequence information (if present) this.formAttributes.sort((a, b) => (a[1]['x-ogc-propertySeq'] ?? 0) - (b[1]['x-ogc-propertySeq'] ?? 0)); } /** * Generates a template object based on the schema's properties, excluding the geometry. */ get template() { return Object.fromEntries(Object.entries(this.schema.properties) .filter(([_, properties]) => !this.isGeometry(properties) && !this.isPrimaryKey(properties)) .map(([key, _]) => [key, null])); } get idAttribute() { const pk = Object.entries(this.schema.properties).find(([_, properties]) => this.isPrimaryKey(properties)); return pk ? pk[0] : this.formAttributes[0][0]; } get geometryAttribute() { const geometryAttributes = Object.entries(this.schema.properties).filter(([_, properties]) => this.isGeometry(properties)); if (geometryAttributes.length === 0) return undefined; if (geometryAttributes.length === 1) return geometryAttributes[0][0]; const primaryGeometry = geometryAttributes.find(([_, properties]) => properties['x-ogc-role'] === 'primary-geometry'); return primaryGeometry ? primaryGeometry[0] : undefined; } getProperties(attributeName) { return this.schema.properties[attributeName]; } isTextProperty(attributeName) { return oapifTextTypes.includes(this.getProperties(attributeName).type ?? ''); } isNumericProperty(attributeName) { return oapifNumericTypes.includes(this.getProperties(attributeName).type ?? ''); } isTemporalProperty(attributeName) { return oapifTemporalTypes.includes(this.getProperties(attributeName).type ?? ''); } isGeometry(propertySchema) { return propertySchema.format?.startsWith('geometry'); } isPrimaryKey(propertySchema) { return propertySchema['x-ogc-role'] === 'id'; } setDefaults(key, properties) { return { ...properties, title: properties.title ?? key, type: properties.type ?? 'string' }; } }