-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
93 lines (80 loc) · 2.75 KB
/
transform.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
// Usage eg:
// node transform.js basedata/VC69-VC70-OSGB36.geojson basedata/VC69-VC70-WGS64.geojson 7
// node transform.js /d/QGIS/Projects/GB-vice-countiesVC60.geojson /d/QGIS/Projects/GB-vice-countiesVC60-WGS64.geojson 7
import fs from 'fs'
import path from 'path'
import * as geotools2em from './geotools2em.js' // http://www.nearby.org.uk/tests/GeoTools2.html
import _ from 'lodash'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const osgb = new geotools2em.GT_OSGB()
let precision = 8
console.log('Transform geojson from OSGB36 to WGS84')
/* const wgs = new geotools2em.GT_WGS84()
//wgs.setDegrees(54.1223, -2.91951) // 469995 340000
wgs.setDegrees(54.25556, -3.216814) // 485115 320825
const os = wgs.getOSGB()
console.log(os.northings,os.eastings)
return
// 320825.02811229, 485115.62617599 */
function customCloner (value) {
if (Array.isArray(value) && value.length === 2 && _.isNumber(value[0])) {
// console.log(value)
osgb.setGridCoordinates(value[0], value[1])
const wsg = osgb.getWGS84()
const rv = []
rv[0] = wsg.latitude.toPrecision(precision)
rv[1] = wsg.longitude.toPrecision(precision)
// console.log('-->',rv)
return rv
}
if (_.isElement(value)) {
return value.cloneNode(true)
}
}
export async function run (argv) {
try {
if (argv.length < 5) {
console.error('usage: node transform.js <in.geojson> <out.geojson> <precision>')
return 0
}
precision = parseInt(argv[4])
let inGeojsonText = fs.readFileSync(path.resolve(__dirname, argv[2]), { encoding: 'utf8' })
if (inGeojsonText.charCodeAt(0) === 65279) { // Remove UTF-8 start character
inGeojsonText = inGeojsonText.slice(1)
}
let inGeojson = false
try {
inGeojson = JSON.parse(inGeojsonText)
} catch (e) {
console.error('inGeojson file not in JSON format')
return 0
}
const outGeojson = _.cloneDeepWith(inGeojson, customCloner)
delete outGeojson.crs
const outGeojsonText = JSON.stringify(outGeojson)
const outpath = path.resolve(__dirname, argv[3])
const writeGeoJson = new Promise((resolve, reject) => {
const stream = fs.createWriteStream(outpath)
stream.on('close', function (fd) {
resolve()
})
stream.on('open', function (fd) {
stream.write(outGeojsonText)
stream.end()
})
})
await writeGeoJson
console.error('DONE written', outGeojsonText.length)
return 1
} catch (e) {
console.error('FAILED', e.message)
return 2
}
}
/// ////////////////////////////////////////////////////////////////////////////////////
// If called from command line, then run now.
// If testing, then don't.
if (process.env.JEST_WORKER_ID === undefined) {
run(process.argv)
}