generated from the-pudding/lambda-node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getClean.js
63 lines (53 loc) · 2.06 KB
/
getClean.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
56
57
58
59
60
61
62
63
import fs from "fs";
import * as turf from "@turf/turf";
import { groups } from "d3";
import fetch from "node-fetch";
let obj;
function getFile(geo){
return new Promise((resolve, reject) => {
fs.readFile(`cleaned/${geo}.json`, 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
resolve();
});
})
}
async function getImage(id,center,geo){
return new Promise(async (resolve, reject) => {
// console.log("grabbing",id)
let key = "AIzaSyAcqwF9XAp9Jj4lWobw_86Qk3tTXwylchs";
key = "AIzaSyBZwP7LnKmyNlJezialp_o-BlepoSzrA5k"
key = "AIzaSyBc2OHaL9HYNxKJwepodR154y7kiA4UJto"
let url = `https://maps.googleapis.com/maps/api/staticmap?center=${center[0][1]},${center[0][0]}&zoom=20&scale=2&size=640x640&maptype=satellite&format=png&key=${key}`;
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// let path = `../../../Volumes/My Passport/raw_images/${geo}/google_way_${id}.png`
let path = `../../../Volumes/My Passport/raw_images/${geo}/google_way_${id}.png`
if (!fs.existsSync(`../../../Volumes/My Passport/raw_images/${geo}`)){
fs.mkdirSync(`../../../Volumes/My Passport/raw_images/${geo}`);
console.log("made new path")
}
fs.createWriteStream(path).write(buffer);
// console.log("written",geo)
resolve();
})
}
function getExisting(geo){
return new Promise((resolve, reject) => {
if (!fs.existsSync(`../../../Volumes/My Passport/raw_images/${geo}`)){
fs.mkdirSync(`../../../Volumes/My Passport/raw_images/${geo}`);
// console.log("made new path")
}
fs.readdir(`../../../Volumes/My Passport/raw_images/${geo}`, (err, files) => {
resolve(files);
});
});
}
export default async function getImages(geo){
await getFile(geo);
obj.features.forEach(d => {
d.properties.geo = geo;
})
return obj.features;
}