-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
178 lines (147 loc) · 4.93 KB
/
utilities.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import fs from 'fs'
import dotenv from 'dotenv'
dotenv.config()
import csvtojson from 'csvtojson'
import { to as copyTo } from 'pg-copy-streams'
import { pipeline } from 'node:stream/promises'
import { wktToGeoJSON } from '@terraformer/wkt'
import ogr2ogr from 'ogr2ogr'
import { pool } from './dbConnection.js'
import path from 'path'
import json from 'big-json'
import { table } from 'node:console'
/**
* Converts degrees to radians
* @param {*} degrees - degrees to be converted to radians
* @returns - the value of degrees in radians
*/
export const degreesToRadians = degrees => {
return (degrees * Math.PI) / 180
}
/**
* Converts degrees to MVT x and y values
* @param {*} radians - radians to be converted to degrees
* @returns - the value of radians in degrees
* */
export const degreesToMVTCoords = (latitude, longitude, zoom) => {
const latitudeRadians = degreesToRadians(latitude)
const n = 2 ** zoom
const xTile = Math.floor(((longitude + 180) / 360) * n)
const yTile = Math.floor(
((1 - Math.asinh(Math.tan(latitudeRadians)) / Math.PI) / 2) * n
)
return { x: xTile, y: yTile }
}
/**
* Gets the MVT bounds for a given set of bounds and zoom level
* @param {*} bounds - the bounds to be converted to MVT bounds
* @param {*} zoom - the zoom level to be used in the conversion
* @returns - the MVT bounds
* */
export const getMVTBounds = (bounds, zoom) => {
const { x: minX, y: maxY } = degreesToMVTCoords(
bounds.miny,
bounds.minx,
zoom
)
const { x: maxX, y: minY } = degreesToMVTCoords(
bounds.maxy,
bounds.maxx,
zoom
)
const mvtBounds = { minX, maxX, minY, maxY }
return mvtBounds
}
export const getLatLngBoundsFromDatabase = async () => {
const query = `SELECT ST_XMin(extent) AS minx, ST_YMin(extent) AS miny, ST_XMax(extent) AS maxx, ST_YMax(extent) AS maxy FROM (SELECT ST_Extent(ST_Transform(geom, 4326)) as extent FROM ${process.env.DB_TABLE});`
const result = await pool.query(query)
const bounds = result.rows[0]
return bounds
}
export const downloadDBTableCSV = async () => {
// First check if we already have the CSV file
if (fs.existsSync(`./temp/${process.env.DB_TABLE}.csv`)) return
const client = await pool.connect()
const columns = process.env.DB_COLUMNS
const tableName = process.env.DB_TABLE
fs.mkdirSync(`./temp`, { recursive: true })
const queryTemplate = fs.readFileSync('./queries/generateCSV.sql', 'utf8')
const query = queryTemplate
.replace(/{{table_name}}/g, tableName)
.replace(/{{columns}}/g, columns)
try {
const stream = client.query(copyTo(`copy (${query}) TO STDOUT CSV HEADER`))
await pipeline(stream, fs.createWriteStream(`./temp/${tableName}.csv`))
} finally {
client.release()
}
await pool.end()
return
}
export const readGeoJSON = async () => {
const tableName = process.env.DB_TABLE
const readStream = fs.createReadStream(`./temp/${tableName}.geojson`)
const parseStream = json.createParseStream()
const geoJSON = await new Promise((resolve, reject) => {
parseStream.on('data', data => {
resolve(data)
})
readStream.pipe(parseStream)
})
return geoJSON
}
export const csvToGeoJSON = async (returnGeoJSON = false) => {
const tableName = process.env.DB_TABLE
// First check if we already have the GeoJSON file
if (fs.existsSync(`./temp/${tableName}.geojson`)) {
return returnGeoJSON ? readGeoJSON() : null
}
// If the CSV file doesn't already exist, it will be created
await downloadDBTableCSV()
// Use ogr2ogr to convert the CSV to GeoJSON
await ogr2ogr(`./temp/${tableName}.csv`, {
destination: `./temp/${tableName}.geojson`,
options: [
'-oo',
'GEOM_POSSIBLE_NAMES=geom',
'-oo',
'KEEP_GEOM_COLUMNS=NO',
'-oo',
'MAX_LINE_SIZE=-1'
]
})
return returnGeoJSON ? readGeoJSON() : null
}
export const downloadAsGeoJSON = async () => {
const tableName = process.env.DB_TABLE
// First check if we already have the GeoJSON file
if (fs.existsSync(`./temp/${tableName}.geojson`)) {
return JSON.parse(fs.readFileSync(`./temp/${tableName}.geojson`))
}
// If the CSV file doesn't already exist, it will be created
await downloadDBTableCSV()
const geoJSONFeatures = []
const jsonArray = await csvtojson().fromFile(`./temp/${tableName}.csv`)
jsonArray.forEach(element => {
if (element.geom) {
const geometry = wktToGeoJSON(element.geom)
const feat = {
type: 'Feature',
geometry,
properties: (({ geom, ...o }) => o)(element)
}
geoJSONFeatures.push(feat)
}
})
const geoJSONFeatureStrings = geoJSONFeatures.map(feature =>
JSON.stringify(feature)
)
const geoJSONFeaturesString = geoJSONFeatureStrings.join(',')
const geoJSONString = `{"type": "FeatureCollection", "features": [${geoJSONFeaturesString}]}`
const geoJSON = {
type: 'FeatureCollection',
features: geoJSONFeatures
}
fs.writeFileSync(`./temp/${tableName}.geojson`, geoJSONString)
return geoJSON
}