-
Notifications
You must be signed in to change notification settings - Fork 11
/
countries.js
55 lines (47 loc) · 1.47 KB
/
countries.js
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
const csv = require('csvtojson');
const fs = require('fs');
const proj4 = require("proj4");
proj4.defs([
[
'🇪🇺',
'+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs '
],
[
'🌎',
'+proj=longlat +datum=WGS84 +no_defs '
]
]);
const catalogCountries = require('./catalogs/countries.json')
const catalogSpecies = require('./catalogs/species.json')
const FILE = "./euforestspecies.csv";
const countries = "./data/";
function transformCoordinates(x,y){
return proj4('🇪🇺','🌎', [+x, +y]);
}
function filterByCountry(rows){
catalogCountries.map( country => {
let selection = rows;
if(country != "All"){
selection = rows.filter( row=> row["COUNTRY"] === country);
}
console.log(`Country ${country} Selection ${selection.length}`);
let cleanData = selection.map(item => {
let coords = transformCoordinates(item['X'], item['Y']);
return {
lat: coords[1],
lng: coords[0],
specie: catalogSpecies.indexOf(item['SPECIES NAME'])
}
});
fs.writeFileSync(
`${countries}${country.toLowerCase().replace(/ /g,'')}.json`,
JSON.stringify(cleanData)
);
console.log(`${country} generated`);
});
}
async function init() {
let rows = await csv().fromFile(FILE);
filterByCountry(rows);
}
init();