|
| 1 | +const fs = require('fs'); |
| 2 | +const crypto = require('crypto'); |
| 3 | +const { XMLParser } = require("fast-xml-parser") |
| 4 | + |
| 5 | +const base = "https://data.source.coop/fiboa/"; |
| 6 | +const promise = fetch(`${base}?list-type=2&prefix=data`) |
| 7 | + .then(response => response.text()) |
| 8 | + .then(text => new XMLParser().parse(text)) |
| 9 | + .then(data => data.ListBucketResult.Contents |
| 10 | + .filter(item => item.Key.includes("collection.json")) |
| 11 | + .map(async (item) => { |
| 12 | + const url = new URL(item.Key, base); |
| 13 | + try { |
| 14 | + console.log("Fetching " + url); |
| 15 | + const response = await fetch(url); |
| 16 | + const collection = await response.json(); |
| 17 | + collection.source = url.href; |
| 18 | + return collection; |
| 19 | + } catch (error) { |
| 20 | + console.error(`Failed to load ${url}: ${error}`); |
| 21 | + } |
| 22 | + })); |
| 23 | + |
| 24 | +promise.then( |
| 25 | + promises => Promise.allSettled(promises) |
| 26 | + .then(results => { |
| 27 | + results = results |
| 28 | + .filter(p => p.status === 'fulfilled' && p.value) |
| 29 | + .map(p => { |
| 30 | + const c = p.value; |
| 31 | + const data = { |
| 32 | + id: c.id, |
| 33 | + title: (c.title || "").replace('Field boundaries for ', ''), |
| 34 | + attribution: c.attribution, |
| 35 | + bbox: c.extent.spatial.bbox, |
| 36 | + source: c.source, |
| 37 | + extensions: c.vecorel_extensions?.[c.id], |
| 38 | + }; |
| 39 | + |
| 40 | + const pmtiles = c.links.find(l => l.rel === 'pmtiles'); |
| 41 | + if (pmtiles) { |
| 42 | + data.pmtiles = pmtiles.href; |
| 43 | + } |
| 44 | + |
| 45 | + if (c.assets) { |
| 46 | + const parquet = Object.values(c.assets).find(a => a.type === 'application/vnd.apache.parquet'); |
| 47 | + if (parquet && parquet['table:row_count'] > 0) { |
| 48 | + data.count = parquet['table:row_count']; |
| 49 | + } |
| 50 | + } else if (c.count) { |
| 51 | + data.count = c.count; |
| 52 | + } |
| 53 | + |
| 54 | + return data; |
| 55 | + }); |
| 56 | + fs.writeFileSync('crop/sources.js', `export default ${JSON.stringify(results, null, 2)}`); |
| 57 | + }) |
| 58 | + .catch(console.error)); |
| 59 | + |
| 60 | +function strToColor(str) { |
| 61 | + return '#' + crypto.createHash('md5').update(str).digest('hex').substring(0, 6); |
| 62 | +} |
| 63 | +const hcat = fs.readFileSync('../code/hcat/HCAT3.csv', 'utf8'); |
| 64 | +const hcatMapping = hcat.split('\n').slice(1).map((line) => { |
| 65 | + const [name, code] = line.split(','); |
| 66 | + return { name, code, color: strToColor(code || "") }; |
| 67 | +}); |
| 68 | +fs.writeFileSync('crop/codes.js', `export const hcat = ${JSON.stringify(hcatMapping, null, 2)}`); |
0 commit comments