-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (131 loc) · 4.47 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require("express");
const compression = require('compression')
const app = express();
const knex = require('./knex/knex.js');
const knexPostgis = require('knex-postgis');
const cors = require("cors");
const path = require("path");
var fs = require('fs');
var geobuf = require("geobuf");
var Pbf = require('pbf');
app.use(compression())
const st = knexPostgis(knex)
app.set("trust proxy", 1);
app.use(cors());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Origin", req.headers.origin);
next();
});
app.get("/api", async (req, res) => {
try {
const latestDefaults = {
"week": 1,
"month": "january",
"year": 2020,
"type": "NO2"
}
if ((req.query.week && isNaN(parseInt(req.query.week))) || (req.query.year && isNaN(parseInt(req.query.year)))) {
throw "Invalid Request";
}
const parsedweek = parseInt(req.query.week);
const parsedyear = parseInt(req.query.year);
const week = parsedweek || latestDefaults["week"]
var month = req.query.month || latestDefaults["month"];
month = month.toLowerCase();
const year = parsedyear || latestDefaults["year"];
const type = req.query.type || latestDefaults["type"];
var start = new Date().getTime();
const sql2 = await knex('GEODATA2').innerJoin('District_Polygons', function () {
this.on('GEODATA2.state', '=', 'District_Polygons.state').andOn('GEODATA2.district', '=', 'District_Polygons.district')
}).select(`GEODATA2.${type}`, st.asGeoJSON('geometry')).where({
week: week,
month: month,
year: year
})
var geojson = {
"type": "FeatureCollection",
"features": []
}
for (var i = 0; i < sql2.length; i++) {
var tobeadded = {
"type": "Feature",
"properties": {},
"geometry": JSON.parse(sql2[i]["geometry"])
}
for (var key in sql2[i]) {
if (key !== 'geometry') {
tobeadded["properties"][key] = sql2[i][key]
}
}
geojson.features.push(tobeadded)
}
var packed = geobuf.encode(geojson, new Pbf());
packed = Buffer.from(packed);
res.send(packed);
} catch (e) {
res.status(400).send(e);
console.log(e);
}
})
app.get("/api/hotspot", async (req, res) => {
try {
const latestDefaults = {
"week": 1,
"month": "january",
"year": 2020,
"type": "NO2"
}
if ((req.query.week && isNaN(parseInt(req.query.week))) || (req.query.year && isNaN(parseInt(req.query.year)))) {
throw "Invalid Request";
}
const parsedweek = parseInt(req.query.week);
const parsedyear = parseInt(req.query.year);
const week = parsedweek || latestDefaults["week"]
var month = req.query.month || latestDefaults["month"];
month = month.toLowerCase();
const year = parsedyear || latestDefaults["year"];
const sql2 = await knex('HOTSPOT').select("lat", "long").where({
week: week,
month: month,
year: year
})
res.send(sql2)
} catch (e) {
res.status(400).send(e);
console.log(e);
}
})
app.get("/api/distinfo", async (req, res) => {
try {
const lat = req.query.lat;
const long = req.query.long;
const result = await knex.select().from("HOTSPOT").where({
lat: lat,
long: long
})
console.log(result.length);
res.status(200).send(result);
} catch (e) {
console.log(e);
}
})
app.get("/api/random", async (req, res) => {
const sql = await knex.select('state', 'district', st.asGeoJSON('geometry')).table('District_Polygons');
fs.writeFile("/home/bhoomik/Desktop/polygons.json", JSON.stringify(sql), function (err) {
if (err) {
console.log(err);
}
});
res.send({});
})
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, process.env.IP, () => {
console.log(`server is running on port ${PORT}`)
})