-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv_alti.py
67 lines (48 loc) · 2 KB
/
serv_alti.py
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
import http.server
import json
import math
import socketserver
import struct
from osgeo import gdal, osr
from tqdm import tqdm
class Correction:
def __init__(self):
self.vrt = 'Out.vrt'
def get_alti(self, lat, long):
src_ds = gdal.Open(self.vrt, gdal.GA_ReadOnly)
gt_forward = src_ds.GetGeoTransform()
gt_reverse = gdal.InvGeoTransform(gt_forward)
rb = src_ds.GetRasterBand(1)
w_fromcrs = osr.SpatialReference()
w_fromcrs.SetWellKnownGeogCS("WGS84")
mp_transfo = osr.CoordinateTransformation(w_fromcrs, src_ds.GetSpatialRef())
mx, my, _ = mp_transfo.TransformPoint(lat, long)
# Convert from map to pixel coordinates.
px, py = gdal.ApplyGeoTransform(gt_reverse, mx, my)
px = math.floor(px)
py = math.floor(py)
structval = rb.ReadRaster(px, py, 1, 1, buf_type=gdal.GDT_Float32)
intval = round(struct.unpack('f', structval)[0], 2)
return intval
correction = Correction()
config = json.load(open('./config.json', mode='r'))
class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
prefix_alti = '/alti/'
if self.path.startswith(prefix_alti):
xml = '<reponse>'
list_coords = [tuple(map(float, elem.split(','))) for elem in
self.path[len(prefix_alti):].split('coords=')[1].split(config['Sep'])]
for lat, lon in tqdm(list_coords):
elevation = correction.get_alti(lat, lon)
xml += f'<elevation>{elevation}</elevation>\n'
xml += '</reponse>'
self.send_response(200)
self.send_header('Content-type', 'application/xml')
self.end_headers()
self.wfile.write(bytes(xml, "utf8"))
PORT = config['PORT']
HOST = ""
with socketserver.TCPServer((HOST, PORT), HttpRequestHandler) as server_map:
print(f"Server running on port {PORT} ==> http://localhost:{PORT}")
server_map.serve_forever()