@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
89 lines (88 loc) • 2.69 kB
JavaScript
export const xmlNumberTypesStrList = [
'byte',
'decimal',
'int',
'integer',
'long',
'negativeInteger',
'nonNegativeInteger',
'nonPositiveInteger',
'positiveInteger',
'short',
'unsignedLong',
'unsignedInt',
'unsignedShort',
'unsignedByte',
'double',
'boolean'
];
export const xmlStringTypesStrList = ['string'];
export const xmlDatetimeTypesStrList = ['date', 'dateTime'];
export const xmlGeometryTypesStrList = [
'multipoint',
'multilinestring',
'multipolygon',
'multisurface',
'multicurve',
'multigeometry',
'point',
'linestring',
'polygon',
'surface',
'curve',
'geometry',
'unknown'
];
export const xmlTypesStrList = [
...xmlNumberTypesStrList,
...xmlStringTypesStrList,
...xmlDatetimeTypesStrList,
...xmlGeometryTypesStrList
];
export function isStringNumeric(str) {
return str.trim().length > 0 && !isNaN(Number(str)); // ensure strings of whitespace fail or mixed chars and numbers like '2n'
}
export function isString(attributeType) {
return xmlStringTypesStrList.includes(attributeType);
}
export function isNumber(attributeType) {
return xmlNumberTypesStrList.includes(attributeType);
}
export function isDate(attributeType) {
return xmlDatetimeTypesStrList.includes(attributeType);
}
export function isGeometry(attributeType) {
// Check for GML namespace
return xmlGeometryTypesStrList.includes(attributeType) || attributeType.startsWith('gml:');
}
/**
* Sanitizes the given geometry type by removing the 'gml:' prefix and simplifying it to one of the types
* defined in xmlGeometryTypesStrList. Example: `gml:MultiLineStringPropertyType` -> `multilinestring`
*/
export function sanitizeGeometryType(attributeType) {
const sanitizedType = attributeType.toLowerCase().replace('gml:', '');
return xmlGeometryTypesStrList.find((type) => sanitizedType.startsWith(type)) ?? 'unknown';
}
/**
* Checks if the geometry type can be considered area-like or is unknown, useful when doing `CONTAINS` comparisons.
*/
export function isAreaLikeGeometryTypeOrUnknown(attributeType) {
return ['polygon', 'multipolygon', 'surface', 'multisurface', 'geometry', 'multigeometry', 'unknown'].includes(attributeType);
}
export function typeGroupFromAttributeType(attributeType) {
if (isString(attributeType)) {
return 'string';
}
else if (isNumber(attributeType)) {
return 'number';
}
else if (isDate(attributeType)) {
return 'date';
}
else if (isGeometry(attributeType)) {
return 'geometry';
}
else {
throw new Error('Unknown attribute type');
}
}