|
| 1 | +/* |
| 2 | + * Copyright 2025, GeoSolutions Sas. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { Observable } from 'rxjs'; |
| 10 | +import { isValidURL } from '../../utils/URLUtils'; |
| 11 | + |
| 12 | +import { preprocess as commonPreprocess } from './common'; |
| 13 | + |
| 14 | +import { |
| 15 | + FGB, |
| 16 | + FGB_VERSION, |
| 17 | + FGB_LAYER_TYPE, |
| 18 | + getCapabilities |
| 19 | +} from '../FlatGeobuf'; |
| 20 | + |
| 21 | +//copy from ThreeDTiles.js |
| 22 | +const getRecords = (url, startPosition, maxRecords, text) => { |
| 23 | + return getCapabilities(url) |
| 24 | + .then(({ ...properties }) => { |
| 25 | + const records = [{ |
| 26 | + ...properties, |
| 27 | + type: FGB_LAYER_TYPE, |
| 28 | + url, |
| 29 | + }]; |
| 30 | + return { |
| 31 | + numberOfRecordsMatched: records.length, |
| 32 | + numberOfRecordsReturned: records.length, |
| 33 | + records |
| 34 | + }; |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +function validateUrl(serviceUrl) { |
| 39 | + if (isValidURL(serviceUrl)) { |
| 40 | + const parts = serviceUrl.split(/\./g); |
| 41 | + // remove query params |
| 42 | + const ext = (parts[parts.length - 1] || '').split(/\?/g)[0]; |
| 43 | + return ext === FGB |
| 44 | + ? true |
| 45 | + : false; |
| 46 | + } |
| 47 | + return false; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * Converts a FGB record into a layerNode |
| 53 | + * maybe export as util method |
| 54 | + */ |
| 55 | +const recordToLayer = (record) => { |
| 56 | + if (!record) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + const { bbox, format, properties } = record; |
| 60 | + return { |
| 61 | + type: FGB_LAYER_TYPE, |
| 62 | + url: record.url, |
| 63 | + title: record.title, |
| 64 | + visibility: true, |
| 65 | + ...(bbox && { bbox }), |
| 66 | + ...(format && { format }), |
| 67 | + ...(properties && { properties }) |
| 68 | + }; |
| 69 | +}; |
| 70 | + |
| 71 | +export const preprocess = commonPreprocess; |
| 72 | +//export const validate = (service) => Observable.of(service); |
| 73 | +export const validate = (service) => { |
| 74 | + if (service.title && validateUrl(service.url)) { |
| 75 | + return Observable.of(service); |
| 76 | + } |
| 77 | + throw new Error("catalog.config.notValidURLTemplate"); |
| 78 | +}; |
| 79 | +export const testService = (service) => Observable.of(service); |
| 80 | +export const textSearch = (url, startPosition, maxRecords, text, info) => getRecords(url, startPosition, maxRecords, text, info); |
| 81 | + |
| 82 | +export const getCatalogRecords = (response) => { |
| 83 | + return response?.records |
| 84 | + ? response.records.map(record => { |
| 85 | + const { version, bbox, format, properties } = record; |
| 86 | + const identifier = (record.url || '').split('?')[0]; |
| 87 | + return { |
| 88 | + serviceType: FGB_LAYER_TYPE, |
| 89 | + isValid: true, |
| 90 | + title: record.title, |
| 91 | + identifier, |
| 92 | + url: record.url, |
| 93 | + //...(bbox && { bbox }), //DONT PASS bbox otherwise viewport will set a fixed bbox to the layer |
| 94 | + ...(format && { format }), |
| 95 | + references: [] |
| 96 | + }; |
| 97 | + }) |
| 98 | + : null; |
| 99 | +}; |
| 100 | + |
| 101 | +export const getLayerFromRecord = (record, options, asPromise) => { |
| 102 | + if (asPromise) { |
| 103 | + return Promise.resolve(recordToLayer(record, options)); |
| 104 | + } |
| 105 | + return recordToLayer(record, options); |
| 106 | +}; |
0 commit comments