Skip to content

Commit b62f5c3

Browse files
committed
update flatgeobuf 4.4.0 using readMetadata
1 parent c189a73 commit b62f5c3

File tree

4 files changed

+24
-55
lines changed

4 files changed

+24
-55
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"eventlistener": "0.0.1",
160160
"file-saver": "1.3.3",
161161
"filtrex": "2.1.0",
162-
"flatgeobuf": "4.3.3",
162+
"flatgeobuf": "4.4.0",
163163
"font-awesome": "4.7.0",
164164
"fs-extra": "3.0.1",
165165
"git-revision-webpack-plugin": "5.0.0",
@@ -302,4 +302,4 @@
302302
},
303303
"author": "GeoSolutions",
304304
"license": "BSD-2-Clause"
305-
}
305+
}

web/client/api/FlatGeobuf.js

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*/
88
import axios from '../libs/ajax';
99
import isEmpty from 'lodash/isEmpty';
10-
import { extend, createEmpty } from 'ol/extent';
11-
import { GeoJSON } from 'ol/format';
1210

1311
/**
1412
* constants of FlatGeobuf format
@@ -18,6 +16,7 @@ export const FGB_LAYER_TYPE = 'flatgeobuf';
1816
export const FGB_VERSION = '3.0.1'; // supported format version
1917

2018
export const getFlatGeobufGeojson = () => import('flatgeobuf/lib/mjs/geojson').then(mod => mod);
19+
export const getFlatGeobufGeneric = () => import('flatgeobuf/lib/mjs/generic').then(mod => mod); // implement readMetadata()
2120
export const getFlatGeobufOl = () => import('flatgeobuf/lib/mjs/ol').then(mod => mod);
2221

2322
function getFormat(url) {
@@ -28,7 +27,9 @@ function getFormat(url) {
2827

2928
function getTitleFromUrl(url) {
3029
const parts = url.split('/');
31-
return parts[parts.length - 2];
30+
const filename = parts[parts.length - 1];
31+
const nameNoExt = filename.split('.').slice(0, -1).join('.');
32+
return nameNoExt || filename;
3233
}
3334

3435
function extractCapabilities({url}) {
@@ -45,56 +46,25 @@ function extractCapabilities({url}) {
4546
// copy and paste in catalog for testing: https://flatgeobuf.org/test/data/countries.fgb
4647
//
4748
export const getCapabilities = (url) => {
48-
return getFlatGeobufGeojson().then(async flatgeobuf => { // load lib dynamically
49+
return getFlatGeobufGeneric().then(async flatgeobuf => { // load lib dynamically
4950
return axios.get(url, {
50-
adapter: config => {
51-
return fetch(config.url); // use fetch adapter to get a stream
51+
adapter: async config => {
52+
// fetch(config.url); // use fetch adapter to get a stream
53+
return await flatgeobuf.readMetadata(config.url); // readMetadata accept also request headers as param
5254
}
53-
}).then(async({body}) => {
55+
}).then(async(metadata) => {
5456

55-
const features = [];
56-
let metadata = {};
57-
let rect; // if undefined read all features
58-
59-
// ///// TODO replace .deserialize() with new method .readMetadata()
60-
// when available in flatgeobuf lib > v4.4.5
61-
// just merged here: https://github.com/flatgeobuf/flatgeobuf/commit/15f137cdf30495dd84afda6159e537df39d5309c
62-
63-
/**
64-
* flatgeobuf.deserialize() return a AsyncGenerator
65-
*/
66-
for await (let feature of flatgeobuf.deserialize(
67-
body,
68-
rect,
69-
function HeaderMetaFn(meta) {
70-
// sample of metadata structure /web/client/test-resources/flatgeobuf/UScounties_subset.metadata.json
71-
const crs = `${meta?.crs?.org}:${meta?.crs?.code}`;
72-
const title = !isEmpty(meta?.title) ? meta.title : getTitleFromUrl(url);
73-
metadata = {
74-
...metadata,
75-
title,
76-
crs
77-
};
78-
})
79-
) {
80-
features.push(new GeoJSON().readFeature(feature));
81-
}
82-
83-
// TODO simplify using new method readMetadata(url) when available in flatgeobuf lib > v4.4.5
84-
// that return envelope
85-
86-
const totExtent = features.reduce((extent, feature) => {
87-
return extend(extent, feature.getGeometry().getExtent());
88-
}, createEmpty());
57+
// const metadata = await flatgeobuf.readMetadata(url);
58+
metadata.title = !isEmpty(metadata?.title) ? metadata.title : getTitleFromUrl(url);
8959

9060
const bbox = {
9161
bounds: {
92-
minx: totExtent[0],
93-
miny: totExtent[1],
94-
maxx: totExtent[2],
95-
maxy: totExtent[3]
62+
minx: metadata.envelope[0],
63+
miny: metadata.envelope[1],
64+
maxx: metadata.envelope[2],
65+
maxy: metadata.envelope[3]
9666
},
97-
crs: metadata.crs || 'EPSG:4326'
67+
crs: metadata.crs ? `${metadata.crs.org}:${metadata.crs.code}` : 'EPSG:4326'
9868
};
9969

10070
const capabilities = extractCapabilities({flatgeobuf, url});

web/client/api/catalog/FlatGeobuf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const getRecords = (url) => {
2323
.then(({ ...properties }) => {
2424
const records = [{
2525
...properties,
26+
visibility: true,
2627
type: FGB_LAYER_TYPE,
2728
url
2829
}];
@@ -55,13 +56,12 @@ const recordToLayer = (record) => {
5556
if (!record) {
5657
return null;
5758
}
58-
const { bbox, format, properties } = record;
59+
const { format, properties } = record;
5960
return {
6061
type: FGB_LAYER_TYPE,
6162
url: record.url,
6263
title: record.title,
6364
visibility: true,
64-
...(bbox && { bbox }),
6565
...(format && { format }),
6666
...(properties && { properties })
6767
};
@@ -76,6 +76,7 @@ export const validate = (service) => {
7676
throw new Error("catalog.config.notValidURLTemplate");
7777
};
7878
export const testService = (service) => Observable.of(service);
79+
7980
export const textSearch = (url, startPosition, maxRecords, text, info) => getRecords(url, startPosition, maxRecords, text, info);
8081

8182
export const getCatalogRecords = (response) => {

web/client/api/catalog/__tests__/FlatGeobuf-test.jsx renamed to web/client/api/catalog/__tests__/FlatGeobuf-test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ describe('Test FlatGeobuf API catalog', () => {
2525
expect(response.records.length).toBe(1);
2626
expect(response.records[0].type).toBe(FGB_LAYER_TYPE);
2727
expect(response.records[0].visibility).toBe(true);
28+
expect(response.records[0].title).toBe('UScounties_subset');
2829
expect(response.records[0].format).toBe(FGB);
29-
// TODO test bbox on flatgeobuf upgrade > v4.4.5
3030
done();
3131
});
3232
});
@@ -38,11 +38,9 @@ describe('Test FlatGeobuf API catalog', () => {
3838
url: FGB_FILE
3939
};
4040
const layer = getLayerFromRecord(catalogRecord);
41-
expect(layer.type).toEqual("model");
41+
expect(layer.visibility).toBe(true);
42+
expect(layer.type).toEqual(FGB_LAYER_TYPE);
4243
expect(layer.url).toEqual(FGB_FILE);
43-
expect(layer.title).toEqual("Title");
44-
expect(layer.visibility).toEqual(true);
45-
expect(layer.bbox.crs).toEqual('EPSG:4326');
4644
});
4745
});
4846

0 commit comments

Comments
 (0)