-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
319 lines (292 loc) · 12.7 KB
/
index.js
File metadata and controls
319 lines (292 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const fs = require('node:fs')
const fsPromise = require('node:fs/promises')
const path = require('node:path')
const { Writable } = require('node:stream')
const { pipeline, finished } = require('node:stream/promises')
const util = require('node:util')
const FormData = require('form-data')
const exec = require('./lib/exec')
const { stringify } = require('wkt')
let stopped = false
const types = {
integer: 'Integer',
number: 'Real',
string: 'String',
boolean: 'Integer'
}
/**
* @param {string} filePath
* @param {any} processingConfig
* @returns {import('node:stream').Writable[]}
*/
function getCSVStreamPipeline (filePath, processingConfig) {
const csv = require('csv-stringify')
const params = {
header: true,
quoted_string: true,
cast: { boolean: value => value ? '1' : '0' }
}
if (processingConfig.fields.length) params.columns = processingConfig.fields.map((/** @type {{key: string}} */field) => field.key)
if (params.columns.includes('_geopoint')) {
params.columns.push('latitude')
params.columns.push('longitude')
}
return [
csv.stringify(params),
fs.createWriteStream(filePath, { flags: 'w' })
]
}
/**
* @param {string} filePath
* @param {any} processingConfig
* @returns {import('node:stream').Writable[]}
*/
function getParquetStreamPipeline (filePath, processingConfig) {
const { ParquetSchema, ParquetTransformer } = require('@dsnp/parquetjs')
/** @type {Record<string, string>} */
const typeConversion = {
integer: 'INT64',
number: 'FLOAT',
string: 'UTF8',
boolean: 'BOOLEAN'
}
/** @type {Record<string, any>} */
const schemaDefinition = {}
processingConfig.fields.forEach((/** @type {{key: string, type: string}} */column) => {
schemaDefinition[column.key] = { type: typeConversion[column.type], optional: true }
})
const schema = new ParquetSchema(schemaDefinition)
return [
new ParquetTransformer(schema),
fs.createWriteStream(filePath, { flags: 'w' })
]
}
/**
* @param {string} filePath
* @param {any} processingConfig
* @returns {import('node:stream').Writable[]}
*/
function getXlsxStreamPipeline (filePath, processingConfig) {
const Excel = require('exceljs')
// const readableStreamXlsx = new ReadableStreamClone(readableStream, { objectMode: true })
const writeStreamXlsx = fs.createWriteStream(filePath, { flags: 'w' })
const workbook = new Excel.stream.xlsx.WorkbookWriter({ stream: writeStreamXlsx })
const worksheet = workbook.addWorksheet(processingConfig.label)
const columns = processingConfig.fields.map((/** @type {{key: string}} */field) => ({ header: field.key, key: field.key }))
worksheet.columns = columns
const writableXlsx = new Writable({
objectMode: true,
write (line, _, next) {
worksheet.addRow(line).commit()
next()
},
final (callback) {
worksheet.commit()
workbook.commit()
.then(() => callback())
.catch(callback)
}
})
return [writableXlsx]
}
const dataSize = 10000
/**
* @param {any} processingConfig
* @param {import('axios').AxiosInstance} axios
* @param {any} log
* @param {import('node:stream').Writable[]} writeStreams
* @returns {Promise<void>}
*/
async function fetchAndWriteData (processingConfig, axios, log, writeStreams, geomField) {
const filters2qs = (await import('@data-fair/lib-utils/filters/index.js')).filters2qs
const urlObj = new URL(processingConfig.dataset.href + '/lines')
urlObj.searchParams.set('size', dataSize.toString())
if (processingConfig.fields.length) {
urlObj.searchParams.set('select', processingConfig.fields.map((/** @type {{key: string}} */field) => field.key).join(','))
}
if (processingConfig.filters?.length) { urlObj.searchParams.set('qs', filters2qs(processingConfig.filters)) }
/** @type {string | undefined} */
let url = urlObj.href
await log.task('Téléchargement des données')
let count = 0
while (url) {
if (stopped) return
const { data } = await axios(url)
url = data.next
for (const line of data.results) {
delete line._score
for (const field of processingConfig.fields) {
if (line[field.key] === null) line[field.key] = undefined
}
if (line._geopoint) {
const [lat, lon] = line._geopoint.split(',')
line.latitude = lat
line.longitude = lon
}
if (line['_geoshape.coordinates']) {
line._geoshape = JSON.stringify({
coordinates: line['_geoshape.coordinates'],
type: line['_geoshape.type']
})
}
if (geomField && line[geomField.key]) {
line[geomField.key] = stringify(JSON.parse(line[geomField.key]))
}
for (const writeStream of writeStreams) {
// writing to the stream without piping but while still respecting backpressure
const keepWriting = writeStream.write(line)
if (!keepWriting) await new Promise(resolve => writeStream.once('drain', resolve))
}
}
count += data.results.length
await log.progress('Téléchargement des données', count, data.total)
}
for (const writeStream of writeStreams) writeStream.end()
}
/**
* @param {string} filePath
* @param {any} processingConfig
* @param {import('axios').AxiosInstance} axios
* @param {any} log
*/
async function upload (filePath, dataset, processingConfig, axios, log) {
const filename = path.parse(filePath).base
const formData = new FormData()
formData.append('attachment', fs.createReadStream(filePath), { filename })
const getLengthAsync = util.promisify(formData.getLength).bind(formData)
const contentLength = await getLengthAsync()
const { formatBytes } = await import('@data-fair/lib/format/bytes.js')
const task = `Chargement de la pièce jointe ${filename} (${formatBytes(contentLength)})`
await log.task(task)
const response = await axios({
method: 'post',
url: `${processingConfig.dataset.href}/metadata-attachments`,
data: formData,
maxContentLength: Infinity,
maxBodyLength: Infinity,
headers: { ...formData.getHeaders(), 'content-length': contentLength },
onUploadProgress: progressEvent => {
log.progress(task, progressEvent.loaded, progressEvent.total)
}
})
await log.info('Mise à jour des métadonnées')
const attachments = dataset.attachments || []
const idx = attachments.findIndex((/** @type {{name: string}} */a) => a.name === filename)
if (idx >= 0) attachments.splice(idx, 1)
dataset.attachments = [...attachments,
{
...response.data,
type: 'file',
title: processingConfig.label + ` (${filename.split('.').pop()})`
}
]
await axios({
method: 'patch',
url: processingConfig.dataset.href,
data: {
attachments: dataset.attachments
}
})
}
/**
* @param {{processingConfig: any, tmpDir: string, axios: import('axios').AxiosInstance, log: any}} processingContext
* @returns
*/
exports.run = async ({ processingConfig, tmpDir, axios, log }) => {
await log.step('Récupération des données')
/** @type {import('node:stream').Writable[][]} */
const streamPipelines = []
/** @type {string[]} */
const filePaths = []
const dataset = (await axios(processingConfig.dataset.href)).data
const geomField = dataset.schema.find(f => (f['x-concept'] && f['x-concept'].id === 'geometry'))
const latField = dataset.schema.find(f => (f['x-concept'] && f['x-concept'].id === 'latitude'))
const lonField = dataset.schema.find(f => (f['x-concept'] && f['x-concept'].id === 'longitude'))
const latLonField = dataset.schema.find(f => (f['x-concept'] && f['x-concept'].id === 'latLon'))
if (!processingConfig.fields.length) processingConfig.fields = dataset.schema.filter(f => !f['x-calculated'])
if (processingConfig.format.includes('pmtiles') || processingConfig.format.includes('shp') || processingConfig.format.includes('gpkg') || processingConfig.format.includes('geojson')) {
if (latField && lonField) {
if (!processingConfig.fields.find(f => f.key === latField.key)) processingConfig.fields.push(latField)
if (!processingConfig.fields.find(f => f.key === lonField.key)) processingConfig.fields.push(lonField)
} else if (geomField && !processingConfig.fields.find(f => f.key === geomField.key)) {
processingConfig.fields.push(geomField)
} else if (latLonField && !processingConfig.fields.find(f => f.key === latLonField.key)) {
processingConfig.fields.push(latLonField)
}
}
if (processingConfig.format.includes('csv') || processingConfig.format.includes('pmtiles') || processingConfig.format.includes('shp') || processingConfig.format.includes('gpkg') || processingConfig.format.includes('geojson')) {
const filePathCsv = path.join(tmpDir, processingConfig.filename + '.csv')
streamPipelines.push(getCSVStreamPipeline(filePathCsv, processingConfig))
if (processingConfig.format.includes('csv')) filePaths.push(filePathCsv)
}
if (processingConfig.format.includes('parquet')) {
const filePathParquet = path.join(tmpDir, processingConfig.filename + '.parquet')
streamPipelines.push(getParquetStreamPipeline(filePathParquet, processingConfig))
filePaths.push(filePathParquet)
}
if (processingConfig.format.includes('xlsx')) {
const filePathXlsx = path.join(tmpDir, processingConfig.filename + '.xlsx')
streamPipelines.push(getXlsxStreamPipeline(filePathXlsx, processingConfig))
filePaths.push(filePathXlsx)
}
const promises = streamPipelines.map(streams => {
return streams.length > 1 ? pipeline(streams) : finished(streams[0])
})
promises.push(fetchAndWriteData(processingConfig, axios, log, streamPipelines.map(streams => streams[0]), geomField))
await Promise.all(promises)
if (processingConfig.format.includes('pmtiles') || processingConfig.format.includes('shp') || processingConfig.format.includes('gpkg') || processingConfig.format.includes('geojson')) {
if (!dataset.bbox) {
await log.error('Le jeu de données n\'est pas géographique et ne peut pas être converti')
return
}
const filePathCsv = path.join(tmpDir, processingConfig.filename + '.csv')
const filePathGeojson = path.join(tmpDir, processingConfig.filename + '.geojson')
if (processingConfig.format.includes('geojson')) {
filePaths.push(filePathGeojson)
}
const vrtPath = path.join(tmpDir, processingConfig.filename + '.vrt')
await fsPromise.writeFile(vrtPath, `<OGRVRTDataSource>
<OGRVRTLayer name="${processingConfig.filename}">
<SrcDataSource>${filePathCsv}</SrcDataSource>
<GeometryType>${geomField ? 'wkbUnknown' : 'wkbPoint'}</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="${geomField ? 'WKT' : 'PointFromColumns'}" ${geomField ? `field="${geomField.key}"` : `x="${lonField?.key || 'longitude'}" y="${latField?.key || 'latitude'}"`} />
${processingConfig.fields.filter(f => f.key !== latField?.key && f.key !== lonField?.key && f.key !== geomField?.key && f.key !== latLonField?.key).map(f => `<Field name="${f.key}" type="${types[f.type]}" subtype="${f.type === 'boolean' ? 'Boolean' : 'None'}"/>`).join('\n')}
</OGRVRTLayer>
</OGRVRTDataSource>`)
const ogr2ogrOptions = ['-f', 'GEOJSON', filePathGeojson, vrtPath]
if (!geomField && !latLonField && (!latField || !lonField)) {
await log.error('Les concepts nécessaires n\'ont pas été trouvés')
return
}
await exec('ogr2ogr', ogr2ogrOptions)
if (processingConfig.format.includes('pmtiles')) {
await log.info('Génération du fichier au format pmtiles')
const filePathPmtiles = path.join(tmpDir, processingConfig.filename + '.pmtiles')
await exec('tippecanoe', ['-zg', '--projection=EPSG:4326', '--force', geomField ? '-S100' : '--drop-densest-as-needed', '-pS', '-o', filePathPmtiles, '-l', 'default', filePathGeojson])
// await exec('ogr2ogr', ['-f', 'PMTiles', '-dsco', 'MAXZOOM=12', '-skipfailures', filePathPmtiles, filePathGeojson])
filePaths.push(filePathPmtiles)
}
if (processingConfig.format.includes('shp')) {
await log.info('Génération du fichier au format shp')
const filePathShp = path.join(tmpDir, processingConfig.filename + '.zip')
await exec('ogr2ogr', ['-f', 'ESRI Shapefile', '-skipfailures', filePathShp, filePathGeojson])
filePaths.push(filePathShp)
}
if (processingConfig.format.includes('gpkg')) {
await log.info('Génération du fichier au format gpkg')
const filePathGpkg = path.join(tmpDir, processingConfig.filename + '.gpkg')
await exec('ogr2ogr', ['-f', 'GPKG', '-skipfailures', filePathGpkg, filePathGeojson])
filePaths.push(filePathGpkg)
}
}
if (stopped) return
await log.step('Chargement des pièces jointes')
for (const filePath of filePaths) {
if (stopped) return
await upload(filePath, dataset, processingConfig, axios, log)
}
}
exports.stop = async () => {
stopped = true
}