77 */
88import axios from '../libs/ajax' ;
99import 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';
1816export const FGB_VERSION = '3.0.1' ; // supported format version
1917
2018export const getFlatGeobufGeojson = ( ) => import ( 'flatgeobuf/lib/mjs/geojson' ) . then ( mod => mod ) ;
19+ export const getFlatGeobufGeneric = ( ) => import ( 'flatgeobuf/lib/mjs/generic' ) . then ( mod => mod ) ; // implement readMetadata()
2120export const getFlatGeobufOl = ( ) => import ( 'flatgeobuf/lib/mjs/ol' ) . then ( mod => mod ) ;
2221
2322function getFormat ( url ) {
@@ -28,7 +27,9 @@ function getFormat(url) {
2827
2928function 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
3435function 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//
4748export 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} ) ;
0 commit comments