-
Notifications
You must be signed in to change notification settings - Fork 444
FlatGeobuf format support #11833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stefanocudini
wants to merge
2
commits into
geosolutions-it:master
Choose a base branch
from
stefanocudini:fix-11749-flatgeobuf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
FlatGeobuf format support #11833
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
| /* | ||
| * Copyright 2025, 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 axios from '../libs/ajax'; | ||
| import isEmpty from 'lodash/isEmpty'; | ||
|
|
||
| /** | ||
| * constants of FlatGeobuf format | ||
| */ | ||
| export const FGB = 'fgb'; // extension file and format short name | ||
| export const FGB_LAYER_TYPE = 'flatgeobuf'; | ||
| export const FGB_VERSION = '3.0.1'; // supported format version | ||
|
|
||
| export const getFlatGeobufGeojson = () => import('flatgeobuf/lib/mjs/geojson').then(mod => mod); | ||
| export const getFlatGeobufGeneric = () => import('flatgeobuf/lib/mjs/generic').then(mod => mod); // implement readMetadata() | ||
| export const getFlatGeobufOl = () => import('flatgeobuf/lib/mjs/ol').then(mod => mod); | ||
|
|
||
| function getFormat(url) { | ||
| const parts = url.split(/\./g); | ||
| const format = parts[parts.length - 1]; | ||
| return format; | ||
| } | ||
|
|
||
| function getTitleFromUrl(url) { | ||
| const parts = url.split('/'); | ||
| const filename = parts[parts.length - 1]; | ||
| const nameNoExt = filename.split('.').slice(0, -1).join('.'); | ||
| return nameNoExt || filename; | ||
| } | ||
|
|
||
| function extractCapabilities({url}) { | ||
| const version = FGB_VERSION; // flatgeobuf-ol not read file format version, it might be possible by reading the header | ||
| // https://flatgeobuf.org/doc/format-changelog.html | ||
| const format = getFormat(url || '') || FGB; | ||
| return { | ||
| version, | ||
| format | ||
| }; | ||
| } | ||
|
|
||
| // | ||
| // copy and paste in catalog for testing: https://flatgeobuf.org/test/data/countries.fgb | ||
| // | ||
| export const getCapabilities = (url) => { | ||
| return getFlatGeobufGeneric().then(async flatgeobuf => { // load lib dynamically | ||
| return axios.get(url, { | ||
| adapter: async config => { | ||
| // fetch(config.url); // use fetch adapter to get a stream | ||
| return await flatgeobuf.readMetadata(config.url); // readMetadata accept also request headers as param | ||
| } | ||
| }).then(async(metadata) => { | ||
|
|
||
| // const metadata = await flatgeobuf.readMetadata(url); | ||
| metadata.title = !isEmpty(metadata?.title) ? metadata.title : getTitleFromUrl(url); | ||
|
|
||
| const bbox = { | ||
| bounds: { | ||
| minx: metadata.envelope[0], | ||
| miny: metadata.envelope[1], | ||
| maxx: metadata.envelope[2], | ||
| maxy: metadata.envelope[3] | ||
| }, | ||
| crs: metadata.crs ? `${metadata.crs.org}:${metadata.crs.code}` : 'EPSG:4326' | ||
| }; | ||
|
|
||
| const capabilities = extractCapabilities({flatgeobuf, url}); | ||
|
|
||
| return { | ||
| ...capabilities, | ||
| ...metadata, | ||
| ...(bbox && { bbox }) | ||
| }; | ||
| }); | ||
| }); | ||
| }; | ||
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Copyright 2025, 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 expect from 'expect'; | ||
|
|
||
| import { | ||
| FGB, | ||
| FGB_VERSION, | ||
| getCapabilities | ||
| } from '../FlatGeobuf'; | ||
|
|
||
| const FGB_FILE = 'base/web/client/test-resources/flatgeobuf/UScounties_subset.fgb'; | ||
|
|
||
| describe('Test FlatGeobuf API', () => { | ||
| it('getCapabilities from FlatGeobuf file', (done) => { | ||
| getCapabilities(FGB_FILE).then(({ bbox, format, version }) => { | ||
| try { | ||
| expect(format).toBeTruthy(); | ||
| expect(format).toBe(FGB); // read from file extension | ||
| expect(version).toBeTruthy(); | ||
| expect(version).toBe(FGB_VERSION); | ||
| expect(bbox).toBeTruthy(); | ||
| expect(bbox.crs).toBe('EPSG:4326'); | ||
| // TODO add on flatgebouf upgrade > v4.4.5 | ||
| // expect(title).toBe('data'); | ||
| // expect(crs).toBe('EPSG:4326'); | ||
| // TODO get from test file | ||
| // expect(Math.round(bbox.bounds.minx)).toBe(0); | ||
| // expect(Math.round(bbox.bounds.miny)).toBe(0); | ||
| // expect(Math.round(bbox.bounds.maxx)).toBe(0); | ||
| // expect(Math.round(bbox.bounds.maxy)).toBe(0); | ||
| } catch (e) { | ||
| done(e); | ||
| } | ||
| done(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,106 @@ | ||
| /* | ||
| * Copyright 2025, 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 { Observable } from 'rxjs'; | ||
| import { isValidURL } from '../../utils/URLUtils'; | ||
|
|
||
| import { preprocess as commonPreprocess } from './common'; | ||
|
|
||
| import { | ||
| FGB, | ||
| FGB_LAYER_TYPE, | ||
| getCapabilities | ||
| } from '../FlatGeobuf'; | ||
|
|
||
| // copy from ThreeDTiles.js | ||
| const getRecords = (url) => { | ||
| return getCapabilities(url) | ||
| .then(({ ...properties }) => { | ||
| const records = [{ | ||
| ...properties, | ||
| visibility: true, | ||
| type: FGB_LAYER_TYPE, | ||
| url | ||
| }]; | ||
| return { | ||
| numberOfRecordsMatched: records.length, | ||
| numberOfRecordsReturned: records.length, | ||
| records | ||
| }; | ||
| }); | ||
| }; | ||
|
|
||
| function validateUrl(serviceUrl) { | ||
| if (isValidURL(serviceUrl)) { | ||
| const parts = serviceUrl.split(/\./g); | ||
| // remove query params | ||
| const ext = (parts[parts.length - 1] || '').split(/\?/g)[0]; | ||
| return ext === FGB | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to only use: return ext === FGB |
||
| ? true | ||
| : false; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Converts a FGB record into a layerNode | ||
| * maybe export as util method | ||
| */ | ||
| const recordToLayer = (record) => { | ||
| if (!record) { | ||
| return null; | ||
| } | ||
| const { format, properties } = record; | ||
| return { | ||
| type: FGB_LAYER_TYPE, | ||
| url: record.url, | ||
| title: record.title, | ||
| visibility: true, | ||
| ...(format && { format }), | ||
| ...(properties && { properties }) | ||
| }; | ||
| }; | ||
|
|
||
| export const preprocess = commonPreprocess; | ||
| // export const validate = (service) => Observable.of(service); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this commented? I would suggest to remove it if not necessary. |
||
| export const validate = (service) => { | ||
| if (service.title && validateUrl(service.url)) { | ||
| return Observable.of(service); | ||
| } | ||
| throw new Error("catalog.config.notValidURLTemplate"); | ||
| }; | ||
| export const testService = (service) => Observable.of(service); | ||
|
|
||
| export const textSearch = (url, startPosition, maxRecords, text, info) => getRecords(url, startPosition, maxRecords, text, info); | ||
|
|
||
| export const getCatalogRecords = (response) => { | ||
| return response?.records | ||
| ? response.records.map(record => { | ||
| const { format } = record; | ||
| const identifier = (record.url || '').split('?')[0]; | ||
| return { | ||
| serviceType: FGB_LAYER_TYPE, | ||
| isValid: true, | ||
| title: record.title, | ||
| identifier, | ||
| url: record.url, | ||
| // ...(bbox && { bbox }), //DONT PASS bbox otherwise viewport will set a fixed bbox to the layer | ||
| ...(format && { format }), | ||
| references: [] | ||
| }; | ||
| }) | ||
| : null; | ||
| }; | ||
|
|
||
| export const getLayerFromRecord = (record, options, asPromise) => { | ||
| if (asPromise) { | ||
| return Promise.resolve(recordToLayer(record, options)); | ||
| } | ||
| return recordToLayer(record, options); | ||
| }; | ||
This file contains hidden or 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,46 @@ | ||
| /* | ||
| * Copyright 2025, 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 expect from 'expect'; | ||
|
|
||
| import { | ||
| FGB, | ||
| FGB_LAYER_TYPE | ||
| } from '../../FlatGeobuf'; | ||
| import { | ||
| textSearch, | ||
| getLayerFromRecord | ||
| } from '../FlatGeobuf'; | ||
|
|
||
| const FGB_FILE = 'base/web/client/test-resources/flatgeobuf/UScounties_subset.fgb'; | ||
|
|
||
| describe('Test FlatGeobuf API catalog', () => { | ||
| it('should return a single record for flatGeobuf', (done) => { | ||
| textSearch(FGB_FILE) | ||
| .then((response) => { | ||
| expect(response.records.length).toBe(1); | ||
| expect(response.records[0].type).toBe(FGB_LAYER_TYPE); | ||
| expect(response.records[0].visibility).toBe(true); | ||
| expect(response.records[0].title).toBe('UScounties_subset'); | ||
| expect(response.records[0].format).toBe(FGB); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should extract the layer config from a catalog record', () => { | ||
| const catalogRecord = { | ||
| serviceType: FGB_LAYER_TYPE, | ||
| isValid: true, | ||
| identifier: FGB_FILE, | ||
| url: FGB_FILE | ||
| }; | ||
| const layer = getLayerFromRecord(catalogRecord); | ||
| expect(layer.visibility).toBe(true); | ||
| expect(layer.type).toEqual(FGB_LAYER_TYPE); | ||
| expect(layer.url).toEqual(FGB_FILE); | ||
| }); | ||
| }); | ||
|
|
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, we avoid using async await in mapstore project. So, I would suggest converting it to promise chaining.
cc: @allyoucanmap