Added cut down script and fixed how data is fetched

This commit is contained in:
Alexis
2023-04-27 23:20:50 +02:00
parent ea61b307f9
commit 94f702686c

View File

@@ -20,7 +20,17 @@ app.use('/rep', async (req, res) => {
if (!lat || !lon) return
const f = fs.readFileSync(path.join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
const closestCity = await fetch(
`https://geo.api.gouv.fr/communes?lat=${lat}&lon=${lon}`
);
const body = await closestCity.json();
let codeDepartement = String(body[0]?.codeDepartement)
if (!codeDepartement || codeDepartement === "") return
codeDepartement = codeDepartement.padStart(3, "0");
const f = fs.readFileSync(path.join(__dirname, `public/cirs/${codeDepartement}.json`), 'utf-8')
const data = JSON.parse(f)
gl = new GeoJsonGeometriesLookup(data)
@@ -42,12 +52,47 @@ app.use('/rep', async (req, res) => {
const repFile = fs.readFileSync(path.join(__dirname, `public/reps/${lookupTable[code]}.json`), 'utf-8')
res.end(repFile)
})
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
/**
* CUT DOWN CIRCO JSON
*/
// const f = fs.readFileSync(path.join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
// const data = JSON.parse(f)
// const features = Object.values(data.features)
// const departements = groupBy(features, e => e.properties['REF'].substring(0, 3));
// for (const [key, value] of departements.entries()) {
// const output = {
// "type": "FeatureCollection",
// "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
// "features": value
// }
// fs.writeFileSync(path.join(__dirname, `public/cirs/${key}.json`), JSON.stringify(output))
// }
// function groupBy(list, keyGetter) {
// const map = new Map();
// list.forEach((item) => {
// const key = keyGetter(item);
// const collection = map.get(key);
// if (!collection) {
// map.set(key, [item]);
// } else {
// collection.push(item);
// }
// });
// return map;
// }
/**
* GENERATE INITIAL DATA
* Basically a bad crawler made in an hour
*/
// const output = {}
// for (let i = 500; i < lookupTable.length; i++) {
@@ -81,6 +126,3 @@ app.use('/rep', async (req, res) => {
// }
// res.json(output)
})
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));