forked from dh10050160/map_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (107 loc) · 4.21 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
const express = require('express'); // import express
const { Pool } = require('pg'); // import pg module
const app = express(); // initialize express
// const port = 3000; //localhost
const port = process.env.PORT || 5000; //heroku
// 配置 PostgreSQL 連接(localhost)
// const pool = new Pool({
// user: 'ann',
// host: 'localhost',
// database: 'flood',
// password: '0000',
// port: 5432, // PostgreSQL port
// });
// 配置 PostgreSQL 連接(gcp)
const pool = new Pool({
user: 'postgres',
host: '34.31.70.151',
database: 'flooddb',
password: '0000',
port: 5432, // PostgreSQL port
});
// 定義全局變量來存儲數據
let detailsData = null;
app.use(express.static('public'));
app.get('/regions', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM region');
res.json(result.rows);
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Internal Server Error');
}
});
// Add this route after the '/regions' route
app.get('/cases', async (req, res) => {
try {
const selectedRegion = req.query.region;
const result = await pool.query(`
SELECT regionname,CAST(caseseq AS INTEGER) AS caseseq,casename,casedate
FROM floodarea, floodcase, region
WHERE floodarea.regioncode = region.code
AND floodarea.caseseq = floodcase.seq
AND regionname = $1
GROUP BY regionname,casedate,casename,caseseq
ORDER BY floodcase.casedate;
`, [selectedRegion]);
res.json(result.rows);
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Internal Server Error');
}
});
// Add this route after the '/cases' route
app.get('/details', async (req, res) => {
try {
const selectedRegion = req.query.region;
const selectedCase = req.query.case;
const result = await pool.query(`
SELECT view_24hr_max.casename,view_24hr_max.caseseq, hr1, hr3, hr6, hr12, hr24,depth,ROUND((R1.ha::NUMERIC),1) AS ha, view_24hr_max.regioncode AS region
FROM view_24hr_max,floodcase,(
SELECT regioncode,floodarea.caseseq,sum(ST_Area(geom::geography))/10000 as ha
FROM floodarea
GROUP BY regioncode,caseseq
ORDER BY regioncode,caseseq) R1
WHERE view_24hr_max.caseseq = floodcase.seq
AND R1.regioncode = view_24hr_max.regioncode
AND R1.caseseq = view_24hr_max.caseseq
AND view_24hr_max.regioncode = $1
AND view_24hr_max.caseseq <= $2
ORDER BY ABS(hr24 - (SELECT hr24 FROM view_24hr_max WHERE regioncode = $1 AND caseseq = $2 ))
LIMIT 4;
`, [selectedRegion, selectedCase]);
// 存儲數據到全局變量
detailsData = result.rows;
res.json(result.rows);
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Internal Server Error');
}
});
app.get('/spatial', async (req, res) => {
try {
// 等待 detailsData 數據更新
await new Promise(resolve => setTimeout(resolve, 100)); // 延遲 100 毫秒
const selectedRegion = req.query.region;
// const selectedCase = req.query.case;
// console.log("selectedRegion: "+selectedRegion);
// console.log("selectedCase: "+selectedCase);
const caseseqs = detailsData.map(item => item.caseseq).join(','); // Array
// console.log("detailsData: "+detailsData[0].caseseq);
console.log("caseseqs: "+caseseqs);
const result = await pool.query(`
select caseseq,tag,ST_AsGeoJSON(geom) as geomjson
from floodarea
where regioncode = $1
and caseseq IN (${caseseqs});
`, [selectedRegion]);
// console.log("result: "+result.rows);
res.json({resultRows: result.rows, detailsData: detailsData});
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`); //localhost
});