-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.js
124 lines (110 loc) · 3.83 KB
/
server.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
const fs = require('fs')
const path = require('path')
const http = require('http')
const url = require('url')
const shapefile = require('shapefile')
const PolygonLookup = require('polygon-lookup')
const proj4 = require('proj4')
const extract = require('extract-zip')
const async = require('async')
const debug = require('debug')('http')
const commandLineArgs = require('command-line-args')
const filenameWithoutExtension = 'Cont_AAD_CAOP2020'
const serverPort = process.env.npm_config_port ||
commandLineArgs([{ name: 'port', type: Number }]).port ||
'8080'
let geojson
let projection
// extracts zip file with shapefile and projection files
function extractZip (callback) {
const zipFile = path.join(__dirname, 'res', filenameWithoutExtension + '.zip')
extract(zipFile, { dir: path.join(__dirname, 'res') })
.then(() => {
console.log('zip file extraction complete')
callback()
})
.catch((errOnUnzip) => {
callback(Error('Error unziping file ' + zipFile + '. ' + errOnUnzip.message))
})
}
function readShapefile (callback) {
shapefile.read(
path.join(__dirname, 'res', filenameWithoutExtension + '.shp'),
path.join(__dirname, 'res', filenameWithoutExtension + '.dbf'),
{ encoding: 'utf-8' }
).then(geojsonLoc => {
geojson = geojsonLoc
console.log(`Shapefiles read from ${filenameWithoutExtension}.shp and ${filenameWithoutExtension}.dbf`)
callback()
}).catch((error) => {
console.error(error.stack)
callback(Error('Error reading shapefile'))
})
}
function readProjectionFile (callback) {
fs.readFile(
path.join(__dirname, 'res', filenameWithoutExtension + '.prj'),
'utf8',
(err, data) => {
if (err) {
callback(Error(err))
} else {
projection = data
console.log(`Projection info read from ${filenameWithoutExtension}.prj`)
callback()
}
})
}
function startServer (callback) {
http.createServer(function (req, res) {
try {
if (req.url.includes('favicon.ico')) {
res.writeHead(204) // no content
res.end()
return
}
const searchParams = url.parse(req.url, true).query // eslint-disable-line
const lat = parseFloat(searchParams.lat) // ex: 40.153687
const lon = parseFloat(searchParams.lon) // ex: -8.514602
const point = [lon, lat] // longitude, latitude
const transformedPoint = proj4(projection, point)
const lookupFreguesias = new PolygonLookup(geojson)
const freguesia = lookupFreguesias.search(transformedPoint[0], transformedPoint[1])
if (freguesia) {
debug('Found freguesia')
const local = {
freguesia: freguesia.properties.Freguesia,
concelho: freguesia.properties.Concelho,
distrito: freguesia.properties.Distrito
}
debug(local)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.write(JSON.stringify(local))
res.end()
} else {
debug('Results not found')
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.write('Results not found. Coordinates out of scope!')
res.end()
}
} catch (e) {
debug('Error no server')
debug(e)
res.writeHead(400, { 'Content-Type': 'text/plain' })
res.write(`Wrong request! Example of good request: ${req.headers.host || ''}/?lat=40.153687&lon=-8.514602`)
res.end()
}
}).listen(serverPort, () => {
console.log(`Server initiated on port ${serverPort}, check for example:`)
console.log('\x1b[36m%s\x1b[0m', `http://localhost:${serverPort}/?lat=40.153687&lon=-8.514602`)
})
}
async.series([extractZip, readShapefile, readProjectionFile, startServer],
function (err) {
if (err) {
console.error(err)
process.exitCode = 1
} else {
console.log('Everything done with success')
}
})