-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (84 loc) · 2.02 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
#!/usr/bin/env node
var { Client } = require('pg');
var fs = require('fs');
var prompt = require('prompt');
var properties = [
{
name: 'host',
description: 'PostGIS database host',
default: 'localhost'
},
{
name: 'port',
type: 'integer',
description: 'PORT',
default: 5432
},
{
name: 'database',
type: 'string',
description: 'Database name',
required: true
},
{
name: 'username',
description: 'Username',
default: 'postgres',
required: true
},
{
name: 'password',
description: 'Password',
required: true,
hidden: true,
replace: '*'
},
{
name: 'proj4function',
type: 'boolean',
description: 'Save generated result as proj4 function?',
default: true
}
]
var start_epsg = 'Proj4js.defs["EPSG:';
var end_epsg_start_proj4js = '"] = "';
var end_proj4js ='";';
prompt.start();
prompt.get(properties, function (err, result) {
if (err) { return onErr(err); }
var client = new Client({
host: result.host,
port: result.port,
database: result.database,
user: result.username,
password: result.password
});
client.connect();
client.query(`SELECT srid,trim(proj4text) as proj4text from spatial_ref_sys where proj4text != '';`, function (err, res) {
if (!err) {
var stringData = "";
if (result.proj4function) {
stringData = "module.exports = function (Proj4js) {\r\n";
res.rows.map(function(row) {
stringData = `${stringData} ${start_epsg}${row.srid}${end_epsg_start_proj4js}${row.proj4text}${end_proj4js}\r\n`
});
stringData += "}"
} else {
stringData = "export const proj4def: Record<number, string> = {\r\n"
res.rows.map(function(row) {
stringData = `${stringData} ${row.srid}: "${row.proj4text}",\r\n`
});
stringData += "}"
}
fs.writeFileSync('epsg.ts', stringData);
} else {
onErr(err);
}
client.end();
});
});
function onErr(err) {
console.log(err);
prompt.stop();
return 1;
}