-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.py
79 lines (71 loc) · 2.17 KB
/
writer.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
69
70
71
72
73
74
75
76
77
78
79
# SPDX-FileCopyrightText: 2024 Cédric Bosdonnat <[email protected]>
#
# SPDX-License-Identifier: MIT
# -*- coding: utf-8 -*-
import os
from datetime import datetime
class KmlWriter:
"""
Write a gas prices KML file for organic maps.
"""
def __init__(self, filepath, name, source, color):
"""
Creates the writer with its metadata
"""
self.filepath = filepath
self._fd = open(filepath, "w")
self.name = name
self.color = color
now = datetime.now().isoformat(' ', timespec='minutes')
self._fd.write(f"""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Style id="placemark-{color}">
<IconStyle>
<Icon>
<href>https://omaps.app/placemarks/placemark-{color}.png</href>
</Icon>
</IconStyle>
</Style>
<name>{name}</name>
<visibility>1</visibility>
<ExtendedData xmlns:mwm="https://omaps.app">
<mwm:name>
<mwm:lang code="default">{name}</mwm:lang>
</mwm:name>
<mwm:annotation>
</mwm:annotation>
<mwm:description>
<mwm:lang code="en">Generated from {source} data on {now}</mwm:lang>
<mwm:lang code="fr">
Généré à partir des données de {source} à {now}
</mwm:lang>
</mwm:description>
<mwm:accessRules>Local</mwm:accessRules>
</ExtendedData>
""")
def close(self):
"""
Close the writer and its resources.
"""
self._fd.write("""</Document>
</kml>""")
self._fd.close()
def writeStation(self, price, update_time, lon, lat, vending):
"""
Write a gas station data to the file.
"""
self._fd.write(f""" <Placemark>
<name>{price}</name>
<TimeStamp><when>{update_time}</when></TimeStamp>
<styleUrl>#placemark-{self.color}</styleUrl>
<Point><coordinates>{lon},{lat}</coordinates></Point>
<ExtendedData xmlns:mwm="https://omaps.app">
<mwm:name><mwm:lang code="default">{price}</mwm:lang></mwm:name>
<mwm:description>
<mwm:lang code="default">Automate: {vending}</mwm:lang>
</mwm:description>
<mwm:icon>Gas</mwm:icon>
</ExtendedData>
</Placemark>
""")