-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapBoxCreator.py
68 lines (63 loc) · 2.74 KB
/
mapBoxCreator.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
68
import http.server
import json
import os.path
import socketserver
from conf.mapBoxConfig import MapBoxConfig
from mapBoxGeoJsonEncoder import MapBoxGeoJsonEncoder
class MapBoxCreator:
@staticmethod
def _writeData(name, buckets, lonlatRange, mapBoxPath):
dataPath = os.path.join(mapBoxPath, 'data')
if not os.path.isdir(dataPath):
os.makedirs(dataPath)
gjPolygons = MapBoxGeoJsonEncoder.encodeMany(buckets, lonlatRange, asPoints=False)
polygonsFileRelativePath = os.path.join('data', f"{name}-polygons.geojson")
with open(os.path.join(mapBoxPath, polygonsFileRelativePath), 'w') as f:
json.dump(gjPolygons, f)
gjCenters = MapBoxGeoJsonEncoder.encodeMany(buckets, lonlatRange, asPoints=True)
centersFileRelativePath = os.path.join('data', f"{name}-centers.geojson")
with open(os.path.join(mapBoxPath, centersFileRelativePath), 'w') as f:
json.dump(gjCenters, f)
return polygonsFileRelativePath, centersFileRelativePath
@staticmethod
def createMap(name, buckets, lonlatRange, kind):
mapBoxPath = os.path.join('www', 'mapbox')
polygonsFileRelativePath, centersFileRelativePath = MapBoxCreator._writeData(name, buckets, lonlatRange,
mapBoxPath)
return {
'name': name,
'polygonsFileRelativePath': polygonsFileRelativePath,
'centersFileRelativePath': centersFileRelativePath,
'geoWidth': lonlatRange,
'kind': kind,
}
@staticmethod
def createMergedMap(name, title, confLst, mapBoxPath=None):
if mapBoxPath is None:
mapBoxPath = os.path.join('www', 'mapbox')
localConfLst = list()
for conf in confLst:
localConf = conf.copy()
localConfLst.append(localConf)
conf = {
'title': title,
'accessToken': MapBoxConfig.parameters['accessToken'],
'dataSets': localConfLst,
}
confPath = os.path.join(mapBoxPath, 'conf')
if not os.path.isdir(confPath):
os.makedirs(confPath)
confFileRelativePath = os.path.join('conf', f"{name}.json")
with open(os.path.join(mapBoxPath, confFileRelativePath), 'w') as f:
json.dump(conf, f)
return {
'conf': conf,
'confFileRelativePath': confFileRelativePath,
}
@staticmethod
def serve():
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", 8000), handler) as httpd:
print("--------------------------------------------------")
print(f"Serving http at port 8000")
httpd.serve_forever()