-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathcheck_DB.js
More file actions
62 lines (54 loc) · 1.86 KB
/
check_DB.js
File metadata and controls
62 lines (54 loc) · 1.86 KB
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
const { Client } = require('pg');
const fs = require('fs');
const path = require('path');
const client = new Client({
host: 'localhost',
port: 5434,
database: 'postgis_34_sample',
user: 'postgres',
password: '12345678'
});
function checkDatabaseConnection(callback) {
client.connect(callback);
}
const checkQuery = `
SELECT *, ST_AsGeoJSON(geom) as geojson_geom
FROM xian_vector."CHN_xian"
WHERE code = $1;
`;
checkDatabaseConnection(err => {
if (err) {
console.error('Failed to connect to the database:', err.stack);
client.end();
return;
}
const code = 512022;
client.query(checkQuery, [code], (err, res) => {
if (err) {
console.error('Error executing query:', err.stack);
client.end();
} else {
if (res.rows.length) {
console.log(`A feature with code ${code} exists in xian_vector.CHN_xian.`);
// Convert result to GeoJSON format
const geoJSONData = {
type: "FeatureCollection",
features: res.rows.map(row => {
return {
type: "Feature",
properties: row,
geometry: JSON.parse(row.geojson_geom) // Using parsed geojson_geom column.
};
})
};
// Write GeoJSON data to a file
const filePath = path.join(__dirname, 'public', 'shp', `${code}.gson`);
fs.writeFileSync(filePath, JSON.stringify(geoJSONData, null, 2));
console.log(`GeoJSON data saved to ${filePath}`);
} else {
console.log(`No feature with code ${code} found in xian_vector.CHN_xian.`);
}
client.end();
}
});
});