-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (95 loc) · 4.96 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require("dotenv").config();
const axios = require("axios");
const fs = require("fs");
const readline = require("readline");
const PORT = process.env.PORT || 8080;
const API_KEY = process.env.API_KEY;
const csvFilePath = "./inputs/hardiness_zone_input_data.csv";
// actual data in file: hardiness_zone_input_data.csv
// test data file: sample_input.csv
// bigger test data file: test_input.csv
function zoneParameters () {
console.log(`function running on ${PORT} 🚀`)
// establish input (csv input variable, readstream file, etc)
const stream = fs.createReadStream(csvFilePath);
const reader = readline.createInterface({ input: stream });
// read row of csv
reader.on("line", row => {
// store data in array
let rawData = [];
rawData.push(row.split(","));
let data = rawData.flat()
// store zone in zone variable
let zone = data[2]
// establish output variables (using zone variable where appropriate)
let output = `./outputs/zone${zone}.csv`
if (data[1] === "Province/Territory") {
return
}
if (data[3] === "-999") {
fs.appendFile("./outputs/input-no-zone.csv", `${data.toString()} zone empty,\n`, "utf8", function(err){
if (err) {
console.log(err, data)
}
})
console.log(data[0], data[1], "error: no zone")
return
}
if (zone === "") {
fs.appendFile("./outputs/input-errors.csv", `${data.toString()} zone empty,\n`, "utf8", function(err){
if (err) {
console.log(err, data)
}
})
console.log(data[0], data[1], "error: no zone")
return
}
// axios request to geocoding API for municipality boundary coordinates (request is for "geometry")
// to search by address use this axios.get request: (use this first)
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${data[0]}&${data[1]}&canada&key=${API_KEY}`)
// to search by administrative area use this axios.get request:
// axios.get(`https://maps.googleapis.com/maps/api/geocode/json?components=administrative_area:${data[0]}+${data[1]}|country:ca&key=${API_KEY}`)
.then (response => {
// retrieve municipality boundary coordinates for responses that use bounds
// use this first
const southEastParam = [response.data.results[0].geometry.bounds.northeast.lng, response.data.results[0].geometry.bounds.southwest.lat];
const southWestParam = [response.data.results[0].geometry.bounds.southwest.lng, response.data.results[0].geometry.bounds.southwest.lat];
const northEastParam = [response.data.results[0].geometry.bounds.northeast.lng, response.data.results[0].geometry.bounds.northeast.lat];
const northWestParam = [response.data.results[0].geometry.bounds.southwest.lng, response.data.results[0].geometry.bounds.northeast.lat];
// retrieve municipality boundary coordinates for responses that use viewport
// const southEastParam = [response.data.results[0].geometry.viewport.northeast.lng, response.data.results[0].geometry.viewport.southwest.lat];
// const southWestParam = [response.data.results[0].geometry.viewport.southwest.lng, response.data.results[0].geometry.viewport.southwest.lat];
// const northEastParam = [response.data.results[0].geometry.viewport.northeast.lng, response.data.results[0].geometry.viewport.northeast.lat];
// const northWestParam = [response.data.results[0].geometry.viewport.southwest.lng, response.data.results[0].geometry.viewport.northeast.lat];
// store coordinates in array using correct format (should match grow-a-pear backend data)
const param = [southWestParam, southEastParam, northEastParam, northWestParam]
console.log(data[0], data[1], zone, "param", param)
// push array to csv output (output stored in correct variable from before)
fs.appendFile(output, `[[${param[0].toString()}], [${param[1].toString()}], [${param[2].toString()}], [${param[3].toString()}]],\n`, "utf8", function(err){
if (err) {
console.log(err)
}
})
fs.appendFile("./inputs/completed_input.csv", `\n${data.toString()}, DONE`, "utf8", function(err){
if (err) {
console.log(err)
}
})
})
.catch (error => {
// add to error output csv file
fs.appendFile("./outputs/input-errors.csv", `${data.toString()} params undefined, \n`, "utf8", function(err){
if (err) {
console.log(err)
console.log(data[0], data[1], zone, "error with google API")
}
})
console.error("Error:", error.message)
})
});
reader.on("close", () => {
// Reached the end of file
console.log("function complete 🥳")
});
}
zoneParameters()