Skip to content

Instantly share code, notes, and snippets.

@kuba-medrek
Created August 12, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuba-medrek/41310c4880582c172e4806a7ba247785 to your computer and use it in GitHub Desktop.
Save kuba-medrek/41310c4880582c172e4806a7ba247785 to your computer and use it in GitHub Desktop.
// Following file is licensed under WTFPL public licence. It's open licence.
//npm i xlsx readline-sync
const fs = require('fs');
const xlsx = require('xlsx');
const readlineSync = require('readline-sync');
async function read() {
const gymns = String(await require('fs').promises.readFile('./secondary-schools.geojson'))
const c_list = xlsx.readFile('registery.xlsx');
const c_sheets = c_list.SheetNames;
return [
JSON.parse(gymns)['features'],
xlsx.utils.sheet_to_json(c_list.Sheets[c_sheets[0]])
]
}
function printWebsite(website) {
if(!website)
return undefined
if(website.startsWith('http')) {
return website;
} else {
return 'http://' + website;
}
}
function printName(str) {
if(!str)
return undefined
return str.toLowerCase().replace(/(?:^|\s|["'([{])+\S/g, match => match.toUpperCase());
}
function printPhone(tel) {
if(!tel)
return undefined;
return tel.replace(/ /g, '').replace('+48', '').replace(/^/, '+48 ').replace(/(\d{3})(\d{3})(\d{3})/, '$1 $2 $3')
}
console.log('reading');
(async () => {
read().then(async ([gymnasiums, correct]) => {
console.log(gymnasiums.length)
const toFix = gymnasiums.filter(it => it['properties']['addr:city'] && it['properties']['addr:street'] && it['properties']['addr:housenumber']);
console.log('read');
let i=1;
const result = await Promise.all(toFix.map(async it => {
const matches = correct.filter(school => (school['MiejscowośćGUS'] === it['properties']['addr:city'] || school['Miejscowość'] === it['properties']['addr:city']) &&
school['Ulica'].match(it['properties']['addr:street']) &&
school['Numer domu'] === it['properties']['addr:housenumber']);
console.log(`${i++} / ${toFix.length} : ${matches.length}`)
if(matches.length === 1) {
return {d: it, m: matches[0]};
}
// uncomment to manually choose from multiple matches
// else if(matches.length > 1) {
// console.log(it['properties']['name'], it['properties']['addr:street'], it['properties']['addr:housenumber'], it['properties']['website'], 'WYBIERZ:');
// let j=0;
// matches.map(school => {
// console.log(`${j++}: ${school['Nazwa placówki']} ${school['Ulica']} ${school['Numer domu']}, ${school['Adres www']} y or n:?`)
// })
// return {d: it, m: matches[readlineSync.question('Zaakceptuj wybór numer:')]};
// }
return {};
}));
const nonEmpty = await Promise.all(result.filter(n => !(Object.keys(n).length === 0 && n.constructor === Object)));
console.log(`Fixed ${nonEmpty.length} items`);
let download = [];
const features = await Promise.all(nonEmpty.map(async it => {
download.push(`${it.d.id[0]}${it.d.id.split('/')[1]}`)
return {
type: "Feature",
properties: {...it.d.properties, ...{
//'@id': undefined,
'name': printName(it.m['Nazwa placówki']),
'operator': printName(it.m['Organ prowadzący']),
'phone': printPhone(it.m['Numer telefonu'] || it.m['Drugi numer telefonu']),
'email': it.d.properties['addr:email'] || it.m['Adres email'],
'website': printWebsite(it.m['Adres www']),
'isced:level': it.m['Typ podmiotu'] === 'Przedszkole' ? 0 :
it.m['Typ podmiotu'] === 'Szkoła podstawowa' ? '1;2' :
it.m['Typ podmiotu'] === 'Liceum ogólnokształcące' ? 3 :
it.m['Typ podmiotu'] === 'Technikum' ? 3 :
it.m['Typ podmiotu'] === 'Szkoła policealna' ? 4 : undefined
}},
geometry: it.d.geometry,
id: it.d.id
}
}));
const geojson = {
type: "FeatureCollection",
generator: "custom script",
copyright: "The data included in this document is from www.openstreetmap.org and dane.gov.pl The data is made available under ODbL.",
timestamp: "2020-08-12T14:11:03Z",
features: features
};
fs.writeFileSync('./result.geojson', JSON.stringify(geojson));
fs.writeFileSync('./down', JSON.stringify(download));
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment