-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…) (#9484) * #9320 - Support reading/loading of cloud-optimized geotiff (COG) * Url validation modified * Update cog layer model * unit test (cherry picked from commit 920ff39)
- Loading branch information
Showing
21 changed files
with
316 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2023, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import get from 'lodash/get'; | ||
import { Observable } from 'rxjs'; | ||
import { isValidURL } from '../../utils/URLUtils'; | ||
|
||
export const COG_LAYER_TYPE = 'cog'; | ||
const searchAndPaginate = (layers, startPosition, maxRecords, text) => { | ||
|
||
const filteredLayers = layers | ||
.filter(({ title = "" } = {}) => !text | ||
|| title.toLowerCase().indexOf(text.toLowerCase()) !== -1 | ||
); | ||
const records = filteredLayers | ||
.filter((layer, index) => index >= startPosition - 1 && index < startPosition - 1 + maxRecords); | ||
return { | ||
numberOfRecordsMatched: filteredLayers.length, | ||
numberOfRecordsReturned: records.length, | ||
nextRecord: startPosition + Math.min(maxRecords, filteredLayers.length) + 1, | ||
records | ||
}; | ||
}; | ||
export const getRecords = (url, startPosition, maxRecords, text, info = {}) => { | ||
const service = get(info, 'options.service'); | ||
let layers = []; | ||
if (service.url) { | ||
const urls = service.url?.split(',')?.map(_url => _url?.trim()); | ||
// each url corresponds to a layer | ||
layers = urls.map((_url, index) => { | ||
const title = _url.split('/')?.pop()?.replace('.tif', '') || `COG_${index}`; | ||
return { | ||
...service, | ||
title, | ||
type: COG_LAYER_TYPE, | ||
sources: [{url: _url}], | ||
options: service.options || {} | ||
}; | ||
}); | ||
} | ||
// fake request with generated layers | ||
return new Promise((resolve) => { | ||
resolve(searchAndPaginate(layers, startPosition, maxRecords, text)); | ||
}); | ||
|
||
|
||
}; | ||
|
||
export const textSearch = (url, startPosition, maxRecords, text, info = {}) => { | ||
return getRecords(url, startPosition, maxRecords, text, info); | ||
}; | ||
|
||
const validateCog = (service) => { | ||
const urls = service.url?.split(','); | ||
const isValid = urls.every(url => isValidURL(url?.trim())); | ||
if (service.title && isValid) { | ||
return Observable.of(service); | ||
} | ||
const error = new Error("catalog.config.notValidURLTemplate"); | ||
// insert valid URL; | ||
throw error; | ||
}; | ||
export const validate = service => { | ||
return validateCog(service); | ||
}; | ||
export const testService = service => { | ||
return Observable.of(service); | ||
}; | ||
|
||
export const getCatalogRecords = (data) => { | ||
if (data && data.records) { | ||
return data.records.map(record => { | ||
return { | ||
serviceType: COG_LAYER_TYPE, | ||
isValid: record.sources?.every(source => isValidURL(source.url)), | ||
title: record.title || record.provider, | ||
sources: record.sources, | ||
options: record.options, | ||
references: [] | ||
}; | ||
}); | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* Converts a record into a layer | ||
*/ | ||
export const cogToLayer = (record) => { | ||
return { | ||
type: COG_LAYER_TYPE, | ||
visibility: true, | ||
sources: record.sources, | ||
title: record.title, | ||
options: record.options, | ||
name: record.title | ||
}; | ||
}; | ||
|
||
const recordToLayer = (record, options) => { | ||
return cogToLayer(record, options); | ||
}; | ||
|
||
export const getLayerFromRecord = (record, options, asPromise) => { | ||
if (asPromise) { | ||
return Promise.resolve(recordToLayer(record, options)); | ||
} | ||
return recordToLayer(record, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2023, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { getLayerFromRecord, getCatalogRecords, validate, COG_LAYER_TYPE} from '../COG'; | ||
import expect from 'expect'; | ||
|
||
|
||
const record = {sources: [{url: "some.tif"}], title: "some", options: []}; | ||
describe('COG (Abstraction) API', () => { | ||
beforeEach(done => { | ||
setTimeout(done); | ||
}); | ||
|
||
afterEach(done => { | ||
setTimeout(done); | ||
}); | ||
it('test getLayerFromRecord', () => { | ||
const layer = getLayerFromRecord(record, null); | ||
expect(layer.title).toBe(record.title); | ||
expect(layer.visibility).toBeTruthy(); | ||
expect(layer.type).toBe(COG_LAYER_TYPE); | ||
expect(layer.sources).toEqual(record.sources); | ||
expect(layer.name).toBe(record.title); | ||
}); | ||
it('test getLayerFromRecord as promise', () => { | ||
getLayerFromRecord(record, null, true).then((layer) => { | ||
expect(layer.title).toBe(record.title); | ||
expect(layer.visibility).toBeTruthy(); | ||
expect(layer.type).toBe(COG_LAYER_TYPE); | ||
expect(layer.sources).toEqual(record.sources); | ||
expect(layer.name).toBe(record.title); | ||
}); | ||
}); | ||
it('test getCatalogRecords - empty records', () => { | ||
const catalogRecords = getCatalogRecords(); | ||
expect(catalogRecords).toBeFalsy(); | ||
}); | ||
it('test getCatalogRecords', () => { | ||
const records = getCatalogRecords({records: [record]}); | ||
const [{serviceType, isValid, title, sources, options }] = records; | ||
expect(serviceType).toBe(COG_LAYER_TYPE); | ||
expect(isValid).toBeFalsy(); | ||
expect(title).toBe(record.title); | ||
expect(sources).toEqual(record.sources); | ||
expect(options).toEqual(record.options); | ||
}); | ||
it('test validate with invalid url', () => { | ||
const service = {title: "some", url: "some.tif"}; | ||
const error = new Error("catalog.config.notValidURLTemplate"); | ||
try { | ||
validate(service); | ||
} catch (e) { | ||
expect(e).toEqual(error); | ||
} | ||
}); | ||
it('test validate with valid url', () => { | ||
const service = {title: "some", url: "https://some.tif"}; | ||
expect(validate(service)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import Layers from '../../../../utils/openlayers/Layers'; | ||
|
||
import GeoTIFF from 'ol/source/GeoTIFF.js'; | ||
import TileLayer from 'ol/layer/WebGLTile.js'; | ||
|
||
function create(options) { | ||
return new TileLayer({ | ||
msId: options.id, | ||
style: options.style, // TODO style needs to be improved. Currently renders only predefined band and ranges when specified in config | ||
opacity: options.opacity !== undefined ? options.opacity : 1, | ||
visible: options.visibility, | ||
source: new GeoTIFF({ | ||
convertToRGB: 'auto', // CMYK, YCbCr, CIELab, and ICCLab images will automatically be converted to RGB | ||
sources: options.sources, | ||
wrapX: true | ||
}), | ||
zIndex: options.zIndex, | ||
minResolution: options.minResolution, | ||
maxResolution: options.maxResolution | ||
}); | ||
} | ||
|
||
Layers.registerType('cog', { | ||
create, | ||
update(layer, newOptions, oldOptions, map) { | ||
if (newOptions.srs !== oldOptions.srs) { | ||
return create(newOptions, map); | ||
} | ||
if (oldOptions.minResolution !== newOptions.minResolution) { | ||
layer.setMinResolution(newOptions.minResolution === undefined ? 0 : newOptions.minResolution); | ||
} | ||
if (oldOptions.maxResolution !== newOptions.maxResolution) { | ||
layer.setMaxResolution(newOptions.maxResolution === undefined ? Infinity : newOptions.maxResolution); | ||
} | ||
return null; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.