@data-fair/catalog-onegeo-suite 0.1.3 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/imports.ts +27 -22
- package/lib/list.ts +20 -19
- package/package.json +1 -1
- package/types/importConfig/.type/index.js +25 -8
- package/types/importConfig/schema.json +25 -8
package/lib/imports.ts
CHANGED
|
@@ -1,36 +1,44 @@
|
|
|
1
1
|
import type { CatalogPlugin, GetResourceContext } from '@data-fair/types-catalogs'
|
|
2
2
|
import type { OneGeoSuiteConfig, Link } from '#types'
|
|
3
|
+
import { apiList, formatsList, sortList } from './list.ts'
|
|
3
4
|
|
|
4
5
|
import axios from '@data-fair/lib-node/axios.js'
|
|
5
6
|
|
|
6
7
|
export const getResource = async ({ catalogConfig, importConfig, resourceId, tmpDir, log }: GetResourceContext<OneGeoSuiteConfig>): ReturnType<CatalogPlugin['getResource']> => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const catalog = (await axios.get(new URL(`fr/indexer/elastic/_search/?q=
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
let service: string = importConfig.service
|
|
9
|
+
let format: string = service ? importConfig.format : (importConfig.format2 || undefined)
|
|
10
|
+
const catalog = (await axios.get(new URL(`fr/indexer/elastic/_search/?q=uuid.keyword:${resourceId}%20AND%20is_metadata:true`, catalogConfig.url).href)).data.hits.hits[0]
|
|
11
|
+
if (!format) {
|
|
12
|
+
if (!service) {
|
|
13
|
+
const links = catalog._source['metadata-fr'].link.filter((x: any) => { return apiList.includes(x.service) && x.formats.find((y: string) => { return formatsList.includes(y) }) })
|
|
14
|
+
service = sortList(links.map((x: Link) => x.service), apiList)[0]
|
|
15
|
+
}
|
|
16
|
+
format = sortList(catalog._source['metadata-fr'].link.find((x: Link) => { return (x.service === service || x.url === service) && x.formats.find((y: string) => { return formatsList.includes(y) }) }).formats, formatsList)[0]
|
|
17
|
+
if (!format) throw Error(`resource not found for service ${service}`)
|
|
18
|
+
} else {
|
|
19
|
+
if (!service) {
|
|
20
|
+
const links = catalog._source['metadata-fr'].link.filter((x: Link) => { return x.formats.includes(format) })
|
|
21
|
+
service = sortList(links.map((x: Link) => x.service), apiList)[0]
|
|
22
|
+
if (!service) throw Error(`resource not found for format ${format}`)
|
|
23
|
+
}
|
|
13
24
|
}
|
|
14
25
|
|
|
26
|
+
if (!catalog) throw Error(`resource not found for ${resourceId} in ${catalogConfig.url}`)
|
|
27
|
+
if (!service) throw Error('resource not found')
|
|
28
|
+
if (!format) throw Error(`resource not found for service ${service}`)
|
|
29
|
+
|
|
15
30
|
// filter links by format and service
|
|
16
31
|
const source: Link = catalog._source['metadata-fr'].link.find((x: Link) => {
|
|
17
32
|
return x.service === service || x.url === service
|
|
18
33
|
})
|
|
19
|
-
|
|
20
|
-
// table of format for make AFS url
|
|
21
|
-
const afsTable: Record<string, string> = {
|
|
22
|
-
CSV: 'text/csv',
|
|
23
|
-
JSON: 'application/json',
|
|
24
|
-
GeoJSON: 'application/Geo%2Bjson',
|
|
25
|
-
GML: 'application/gml%2Bxml',
|
|
26
|
-
KML: 'application/vnd.google-earth.kml%2Bxml',
|
|
27
|
-
}
|
|
34
|
+
if (!source) throw Error('resource not found')
|
|
28
35
|
// table of format for make WFS url
|
|
29
36
|
const wfsTable: Record<string, string> = {
|
|
30
37
|
CSV: 'csv',
|
|
38
|
+
JSON: 'application/json',
|
|
31
39
|
GeoJSON: 'application/json',
|
|
32
40
|
'Shapefile (zip)': 'SHAPE-ZIP',
|
|
33
|
-
|
|
41
|
+
'SHAPE-ZIP': 'SHAPE-ZIP',
|
|
34
42
|
KML: 'kml',
|
|
35
43
|
}
|
|
36
44
|
// table of format
|
|
@@ -39,23 +47,20 @@ export const getResource = async ({ catalogConfig, importConfig, resourceId, tmp
|
|
|
39
47
|
GeoJSON: '.geojson',
|
|
40
48
|
JSON: '.json',
|
|
41
49
|
'Shapefile (zip)': '.zip',
|
|
42
|
-
ZIP: '.zip',
|
|
43
|
-
GML: '.gml',
|
|
50
|
+
'SHAPE-ZIP': '.zip',
|
|
44
51
|
KML: '.kml',
|
|
45
|
-
XML: '.xml',
|
|
46
|
-
ODS: '.ods',
|
|
47
52
|
'Excel non structuré': '.xlsx',
|
|
48
53
|
'Microsoft Excel': '.xls',
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
let downloadUrl: string
|
|
52
57
|
if (source.service === 'WS') {
|
|
58
|
+
if (extensionTable[format] === undefined) throw Error(`Format ${format} not valid for ${service}`)
|
|
53
59
|
downloadUrl = `${source.url}/${source.name}/all${extensionTable[format]}`
|
|
54
60
|
} else if (source.service === undefined) {
|
|
55
61
|
downloadUrl = `${source.url}`
|
|
56
|
-
} else if (source.service === 'AFS') {
|
|
57
|
-
downloadUrl = `${source.url}${source.name}/items?&crs=${source.projections![0]}&f=${afsTable[format]}&sortby=gid`
|
|
58
62
|
} else if (source.service === 'WFS') {
|
|
63
|
+
if (wfsTable[format] === undefined) throw Error(`Format ${format} not valid for ${service}`)
|
|
59
64
|
downloadUrl = `${source.url}?SERVICE=WFS&VERSION=2.0.0&request=GetFeature&typename=${source.name}&outputFormat=${wfsTable[format]}&startIndex=0&sortby=gid`
|
|
60
65
|
} else {
|
|
61
66
|
downloadUrl = `${source.url}`
|
package/lib/list.ts
CHANGED
|
@@ -5,10 +5,9 @@ import axios from '@data-fair/lib-node/axios.js'
|
|
|
5
5
|
|
|
6
6
|
type ResourceList = Awaited<ReturnType<CatalogPlugin['list']>>['results']
|
|
7
7
|
|
|
8
|
-
const apiList: Array<string | undefined> = ['WS', '
|
|
9
|
-
const formatsList = [
|
|
10
|
-
'CSV', '
|
|
11
|
-
'ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'XML', 'GML', 'KML']
|
|
8
|
+
export const apiList: Array<string | undefined> = ['WS', 'WFS', undefined]
|
|
9
|
+
export const formatsList = [
|
|
10
|
+
'CSV', 'Excel non structuré', 'Microsoft Excel', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML']
|
|
12
11
|
|
|
13
12
|
const extensionTable: Record<string, string> = {
|
|
14
13
|
CSV: '.csv',
|
|
@@ -16,22 +15,18 @@ const extensionTable: Record<string, string> = {
|
|
|
16
15
|
JSON: '.json',
|
|
17
16
|
'Shapefile (zip)': '.zip',
|
|
18
17
|
'SHAPE-ZIP': '.zip',
|
|
19
|
-
ZIP: '.zip',
|
|
20
|
-
GML: '.gml',
|
|
21
18
|
KML: '.kml',
|
|
22
|
-
XML: '.xml',
|
|
23
|
-
ODS: '.ods',
|
|
24
19
|
'Excel non structuré': '.xlsx',
|
|
25
20
|
'Microsoft Excel': '.xls',
|
|
26
21
|
}
|
|
27
22
|
|
|
28
|
-
const
|
|
23
|
+
export const sortList = (formats: any[], reference: any[]) => {
|
|
29
24
|
return [...formats].sort((a, b) =>
|
|
30
|
-
(
|
|
31
|
-
(
|
|
25
|
+
(reference.indexOf(a) === -1 ? reference.length : reference.indexOf(a)) -
|
|
26
|
+
(reference.indexOf(b) === -1 ? reference.length : reference.indexOf(b))
|
|
32
27
|
)
|
|
33
28
|
}
|
|
34
|
-
const baseReqDataset = (input: string = '*', size: number =
|
|
29
|
+
const baseReqDataset = (input: string = '*', size: number = 500, from: number = 1) => {
|
|
35
30
|
return {
|
|
36
31
|
from: (from - 1) * size,
|
|
37
32
|
size: Math.min(size, 10000),
|
|
@@ -52,7 +47,7 @@ const baseReqDataset = (input: string = '*', size: number = 100, from: number =
|
|
|
52
47
|
}
|
|
53
48
|
}]
|
|
54
49
|
}
|
|
55
|
-
}, { term: { is_metadata: true } }, {
|
|
50
|
+
}, { term: { is_metadata: true } }, { term: { 'editorial-metadata.isOpenAccess': true } }, {
|
|
56
51
|
bool: {
|
|
57
52
|
should: [
|
|
58
53
|
...apiList.filter((x: any) => { return x }).map((x: any) => { return { term: { 'metadata-fr.link.service.keyword': x } } }),
|
|
@@ -92,7 +87,7 @@ const countReq = (input: string = '*') => {
|
|
|
92
87
|
}
|
|
93
88
|
}]
|
|
94
89
|
}
|
|
95
|
-
}, { term: { is_metadata: true } }, {
|
|
90
|
+
}, { term: { is_metadata: true } }, { term: { 'editorial-metadata.isOpenAccess': true } }, {
|
|
96
91
|
bool: {
|
|
97
92
|
should: [
|
|
98
93
|
...apiList.filter((x: any) => x).map((x: any) => ({ term: { 'metadata-fr.link.service.keyword': x } })),
|
|
@@ -126,8 +121,14 @@ const countReq = (input: string = '*') => {
|
|
|
126
121
|
export const list = async ({ catalogConfig, params }: ListContext<OneGeoSuiteConfig, OneGeoCapabilities>): ReturnType<CatalogPlugin['list']> => {
|
|
127
122
|
const url = catalogConfig.url
|
|
128
123
|
const listResources = async (params: Record<any, any>) => {
|
|
129
|
-
|
|
130
|
-
|
|
124
|
+
let catalogs
|
|
125
|
+
try {
|
|
126
|
+
catalogs = (await axios.post(new URL('fr/indexer/elastic/_search/', url).href, baseReqDataset(params.q || '*', params.size, params.page))).data.hits.hits
|
|
127
|
+
} catch (e) {
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
throw Error(`Axios error: ${e?.status ?? ''} ${e?.message}`)
|
|
130
|
+
}
|
|
131
|
+
const count = (await axios.post(new URL('fr/indexer/elastic/_search/', url).href, countReq(params.q))).data.aggregations.unique_datasets.value
|
|
131
132
|
const res = []
|
|
132
133
|
|
|
133
134
|
for (const catalog of catalogs) {
|
|
@@ -135,8 +136,8 @@ export const list = async ({ catalogConfig, params }: ListContext<OneGeoSuiteCon
|
|
|
135
136
|
|
|
136
137
|
// sort source by priority (services / format)
|
|
137
138
|
sources.sort((x: Link, y: Link) => {
|
|
138
|
-
const bestFormatX = formatsList.indexOf(
|
|
139
|
-
const bestFormatY = formatsList.indexOf(
|
|
139
|
+
const bestFormatX = formatsList.indexOf(sortList(x.formats, formatsList)[0]) === -1 ? formatsList.length : formatsList.indexOf(sortList(x.formats, formatsList)[0])
|
|
140
|
+
const bestFormatY = formatsList.indexOf(sortList(y.formats, formatsList)[0]) === -1 ? formatsList.length : formatsList.indexOf(sortList(y.formats, formatsList)[0])
|
|
140
141
|
|
|
141
142
|
if (bestFormatX !== bestFormatY) {
|
|
142
143
|
return bestFormatX - bestFormatY
|
|
@@ -156,7 +157,7 @@ export const list = async ({ catalogConfig, params }: ListContext<OneGeoSuiteCon
|
|
|
156
157
|
formats = formats.map(x => extensionTable[x]?.slice(1) ?? x)
|
|
157
158
|
|
|
158
159
|
res.push({
|
|
159
|
-
id: `${catalog.
|
|
160
|
+
id: `${catalog._source.uuid}`,
|
|
160
161
|
title: catalog._source['metadata-fr'].title,
|
|
161
162
|
description: sources[0].description,
|
|
162
163
|
format: formats.slice(0, 3).join(', ') + (formats.length ? '..' : ''),
|
package/package.json
CHANGED
|
@@ -14,10 +14,7 @@ export const schema = {
|
|
|
14
14
|
"title": "ImportConfig",
|
|
15
15
|
"type": "object",
|
|
16
16
|
"additionalProperties": false,
|
|
17
|
-
"required": [
|
|
18
|
-
"service",
|
|
19
|
-
"format"
|
|
20
|
-
],
|
|
17
|
+
"required": [],
|
|
21
18
|
"properties": {
|
|
22
19
|
"service": {
|
|
23
20
|
"type": "string",
|
|
@@ -32,8 +29,8 @@ export const schema = {
|
|
|
32
29
|
"layout": {
|
|
33
30
|
"cols": 6,
|
|
34
31
|
"getItems": {
|
|
35
|
-
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=
|
|
36
|
-
"itemsResults": "(function() { const links = data.hits.hits[0]._source['metadata-fr'].link; return links.filter(l => l.formats.find(f => ['CSV', '
|
|
32
|
+
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=uuid.keyword:${context.resourceId}%20AND%20is_metadata:true",
|
|
33
|
+
"itemsResults": "(function() { const links = data.hits.hits[0]._source['metadata-fr'].link; return links.filter(l => l.formats.find(f => ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML'].includes(f)) && ['WS', 'WFS', undefined].includes(l.service)).map((l, i) => [l, i] ); })()",
|
|
37
34
|
"itemTitle": "item[0].service?? 'Local N°' + (item[1] + 1)",
|
|
38
35
|
"itemValue": "item[0].service?? item[0].url"
|
|
39
36
|
}
|
|
@@ -42,12 +39,32 @@ export const schema = {
|
|
|
42
39
|
"format": {
|
|
43
40
|
"type": "string",
|
|
44
41
|
"title": "Format",
|
|
42
|
+
"description": "Import format, if no service is set, the possible formats are unknown.",
|
|
43
|
+
"x-i18n-description": {
|
|
44
|
+
"fr": "Format d’importation, si aucun service n’est défini, les formats possibles sont inconnus."
|
|
45
|
+
},
|
|
45
46
|
"layout": {
|
|
46
47
|
"if": "parent.data.service != null",
|
|
47
48
|
"cols": 6,
|
|
48
49
|
"getItems": {
|
|
49
|
-
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=${parent.data.service}&q=
|
|
50
|
-
"itemsResults": "(function(){const service = data.hits.hits[0]._source['metadata-fr'].link.find(x => x.service === parent.data.service || x.url === parent.data.service); let formats = service.formats; if (['WS', 'WFS'
|
|
50
|
+
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=${parent.data.service}&q=uuid.keyword:${context.resourceId}%20AND%20is_metadata:true",
|
|
51
|
+
"itemsResults": "(function(){ const service = data.hits.hits[0]._source['metadata-fr'].link.find(x => x.service === parent.data.service || x.url === parent.data.service); let formats = service.formats; if (['WS', 'WFS'].includes(service.service) && !formats.includes('CSV')) {formats.push('CSV');} return formats.filter(f => ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML'].includes(f));})()"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"format2": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"title": "Format",
|
|
58
|
+
"description": "Import format, if no service is set, the possible formats are unknown.",
|
|
59
|
+
"x-i18n-description": {
|
|
60
|
+
"fr": "Format d’importation, si aucun service n’est défini, les formats possibles sont inconnus."
|
|
61
|
+
},
|
|
62
|
+
"layout": {
|
|
63
|
+
"if": "parent.data.service == null",
|
|
64
|
+
"cols": 6,
|
|
65
|
+
"getItems": {
|
|
66
|
+
"url": "https://httpbin.org/get",
|
|
67
|
+
"itemsResults": "(function(){return ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML']})()"
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
70
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
"title": "ImportConfig",
|
|
7
7
|
"type": "object",
|
|
8
8
|
"additionalProperties": false,
|
|
9
|
-
"required": [
|
|
10
|
-
"service",
|
|
11
|
-
"format"
|
|
12
|
-
],
|
|
9
|
+
"required": [],
|
|
13
10
|
"properties": {
|
|
14
11
|
"service": {
|
|
15
12
|
"type": "string",
|
|
@@ -24,8 +21,8 @@
|
|
|
24
21
|
"layout": {
|
|
25
22
|
"cols": 6,
|
|
26
23
|
"getItems": {
|
|
27
|
-
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=
|
|
28
|
-
"itemsResults": "(function() { const links = data.hits.hits[0]._source['metadata-fr'].link; return links.filter(l => l.formats.find(f => ['CSV', '
|
|
24
|
+
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=uuid.keyword:${context.resourceId}%20AND%20is_metadata:true",
|
|
25
|
+
"itemsResults": "(function() { const links = data.hits.hits[0]._source['metadata-fr'].link; return links.filter(l => l.formats.find(f => ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML'].includes(f)) && ['WS', 'WFS', undefined].includes(l.service)).map((l, i) => [l, i] ); })()",
|
|
29
26
|
"itemTitle": "item[0].service?? 'Local N°' + (item[1] + 1)",
|
|
30
27
|
"itemValue": "item[0].service?? item[0].url"
|
|
31
28
|
}
|
|
@@ -34,12 +31,32 @@
|
|
|
34
31
|
"format": {
|
|
35
32
|
"type": "string",
|
|
36
33
|
"title": "Format",
|
|
34
|
+
"description": "Import format, if no service is set, the possible formats are unknown.",
|
|
35
|
+
"x-i18n-description": {
|
|
36
|
+
"fr": "Format d’importation, si aucun service n’est défini, les formats possibles sont inconnus."
|
|
37
|
+
},
|
|
37
38
|
"layout": {
|
|
38
39
|
"if": "parent.data.service != null",
|
|
39
40
|
"cols": 6,
|
|
40
41
|
"getItems": {
|
|
41
|
-
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=${parent.data.service}&q=
|
|
42
|
-
"itemsResults": "(function(){const service = data.hits.hits[0]._source['metadata-fr'].link.find(x => x.service === parent.data.service || x.url === parent.data.service); let formats = service.formats; if (['WS', 'WFS'
|
|
42
|
+
"url": "${context.catalogConfig.url}/fr/indexer/elastic/_search/?q=${parent.data.service}&q=uuid.keyword:${context.resourceId}%20AND%20is_metadata:true",
|
|
43
|
+
"itemsResults": "(function(){ const service = data.hits.hits[0]._source['metadata-fr'].link.find(x => x.service === parent.data.service || x.url === parent.data.service); let formats = service.formats; if (['WS', 'WFS'].includes(service.service) && !formats.includes('CSV')) {formats.push('CSV');} return formats.filter(f => ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML'].includes(f));})()"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"format2": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"title": "Format",
|
|
50
|
+
"description": "Import format, if no service is set, the possible formats are unknown.",
|
|
51
|
+
"x-i18n-description": {
|
|
52
|
+
"fr": "Format d’importation, si aucun service n’est défini, les formats possibles sont inconnus."
|
|
53
|
+
},
|
|
54
|
+
"layout": {
|
|
55
|
+
"if": "parent.data.service == null",
|
|
56
|
+
"cols": 6,
|
|
57
|
+
"getItems": {
|
|
58
|
+
"url": "https://httpbin.org/get",
|
|
59
|
+
"itemsResults": "(function(){return ['CSV', 'Excel non structuré', 'Microsoft Excel','ZIP', 'Shapefile (zip)', 'SHAPE-ZIP', 'GeoJSON', 'JSON', 'KML']})()"
|
|
43
60
|
}
|
|
44
61
|
}
|
|
45
62
|
}
|