gammon-im 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gammon/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ from ._main import __version__, convert, ConversionError
2
+ __all__ = ['__version__', 'convert', 'ConversionError']
gammon/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ import sys
2
+ from ._main import _main
3
+ sys.exit(_main())
gammon/__version__.py ADDED
@@ -0,0 +1,6 @@
1
+ from importlib.metadata import PackageNotFoundError, version
2
+
3
+ try:
4
+ __version__ = version('gammon-im')
5
+ except PackageNotFoundError:
6
+ __version__ = '0.0.0.dev0'
gammon/_main.py ADDED
@@ -0,0 +1,219 @@
1
+ import lxml.etree as tree
2
+ import sys
3
+ import lxml.builder
4
+ import re
5
+ import collections
6
+ import argparse
7
+ from .__version__ import __version__
8
+
9
+
10
+ class ConversionError(Exception):
11
+ def __init__(self, message):
12
+ super().__init__(message)
13
+
14
+
15
+ E = lxml.builder.ElementMaker()
16
+
17
+ ns = {'x': 'http://www.opengis.net/kml/2.2'}
18
+ mwm = 'https://maps.me'
19
+ mwmns = {'mwm': mwm}
20
+ google_style_regex = re.compile(r'^#icon-(\d{4})-([0-9A-F]{6})')
21
+
22
+ # noinspection SpellCheckingInspection
23
+ style_map = {
24
+ 'C2185B': 'placemark-red',
25
+ 'A52714': 'placemark-red',
26
+ '0288D1': 'placemark-blue',
27
+ '673AB7': 'placemark-blue',
28
+ '006064': 'placemark-blue',
29
+ '01579B': 'placemark-blue',
30
+ '1A237E': 'placemark-blue',
31
+ '0097A7': 'placemark-blue',
32
+ '3949AB': 'placemark-blue',
33
+ '880E4F': 'placemark-purple',
34
+ '9C27B0': 'placemark-purple',
35
+ 'FBC02D': 'placemark-yellow',
36
+ 'FFD600': 'placemark-yellow',
37
+ 'FFEA00': 'placemark-yellow',
38
+ 'FF5252': 'placemark-pink',
39
+ '817717': 'placemark-brown',
40
+ '4E342E': 'placemark-brown',
41
+ 'AFB42B': 'placemark-brown',
42
+ '795548': 'placemark-brown',
43
+ '424242': 'placemark-brown',
44
+ '000000': 'placemark-brown',
45
+ '757575': 'placemark-brown',
46
+ '097138': 'placemark-green',
47
+ '558B2F': 'placemark-green',
48
+ '7CB342': 'placemark-green',
49
+ '0F9D58': 'placemark-green',
50
+ 'E65100': 'placemark-orange',
51
+ 'F57C00': 'placemark-orange',
52
+ 'F9A825': 'placemark-orange',
53
+ 'BDBDBD': 'placemark-orange',
54
+ }
55
+
56
+ icon_map = {
57
+ '1602': 'Hotel',
58
+ '1507': 'Animals',
59
+ '1743': 'Animals',
60
+ '1667': 'Buddhism',
61
+ '1668': 'Buddhism',
62
+ '1669': 'Buddhism',
63
+ '1533': 'Building',
64
+ '1546': 'Building',
65
+ '1548': 'Building',
66
+ '1717': 'Building',
67
+ '1741': 'Building',
68
+ '1603': 'Building',
69
+ '1716': 'Building',
70
+ '1670': 'Christianity',
71
+ '1709': 'Entertainment',
72
+ '1540': 'Entertainment',
73
+ '1555': 'Exchange',
74
+ '1517': 'Food',
75
+ '1534': 'Food',
76
+ '1577': 'Food',
77
+ '1581': 'Gas',
78
+ '1675': 'Judaism',
79
+ '1624': 'Medicine',
80
+ '1634': 'Mountain',
81
+ '1636': 'Museum',
82
+ '1592': 'None',
83
+ '1899': 'None',
84
+ '1673': 'Islam',
85
+ '1720': 'Park',
86
+ '1644': 'Parking',
87
+ '1684': 'Shop',
88
+ '1685': 'Shop',
89
+ '1598': 'Sights',
90
+ '1521': 'Swim',
91
+ '1701': 'Swim',
92
+ '1703': 'Water',
93
+ }
94
+
95
+
96
+ def indent(elem, level=0):
97
+ i = '\n' + level * ' '
98
+ if len(elem):
99
+ if not elem.text or not elem.text.strip():
100
+ elem.text = i + ' '
101
+ if not elem.tail or not elem.tail.strip():
102
+ elem.tail = i
103
+ for elem in elem:
104
+ indent(elem, level + 1)
105
+ if not elem.tail or not elem.tail.strip():
106
+ elem.tail = i
107
+ else:
108
+ if level and (not elem.tail or not elem.tail.strip()):
109
+ elem.tail = i
110
+
111
+
112
+ def google_to_organic_maps_icon_and_style(google_style):
113
+ icon, style = None, google_style
114
+ m = google_style_regex.match(google_style)
115
+ if m and m.group(1) in icon_map:
116
+ icon = icon_map[m.group(1)]
117
+ if m and m.group(2) in style_map:
118
+ style = '#' + style_map[m.group(2)]
119
+ return icon, style
120
+
121
+
122
+ def err(*args, **kwargs):
123
+ print(*args, file=sys.stderr, **kwargs)
124
+
125
+
126
+ def remove_old_styles(doc):
127
+ for i in doc.xpath('x:Style | x:StyleMap', namespaces=ns):
128
+ i.getparent().remove(i)
129
+
130
+
131
+ def remove_lines(doc):
132
+ for i in doc.xpath('x:Folder/x:Placemark[x:LineString]', namespaces=ns):
133
+ i.getparent().remove(i)
134
+
135
+
136
+ def remove_empty_folders(doc):
137
+ for i in doc.xpath('x:Folder[not(x:Placemark)]', namespaces=ns):
138
+ i.getparent().remove(i)
139
+
140
+
141
+ def new_ordered_set(xs):
142
+ return collections.OrderedDict((x, None) for x in xs)
143
+
144
+
145
+ def add_organic_maps_styles(doc):
146
+ unique_styles = new_ordered_set(style_map.values())
147
+ for i, name in enumerate(unique_styles):
148
+ ref = f'http://maps.me/placemarks/{name}.png'
149
+ style = E.Style(E.IconStyle(E.Icon(E.href(ref))), id=name)
150
+ doc.insert(i, style)
151
+
152
+
153
+ def google_to_organic_maps_icons(doc, verbose):
154
+ for i in doc.xpath('x:Folder/x:Placemark/x:styleUrl', namespaces=ns):
155
+ google_style = i.text
156
+ icon, i.text = google_to_organic_maps_icon_and_style(google_style)
157
+ if icon is not None:
158
+ if icon != 'None':
159
+ placemark = i.getparent()
160
+ extended = placemark.find('x:ExtendedData', ns)
161
+ if extended is None:
162
+ extended = tree.SubElement(placemark, 'ExtendedData', nsmap=mwmns)
163
+ icon_tag = tree.SubElement(extended, f'{{{mwm}}}icon')
164
+ else:
165
+ icon_tag = tree.SubElement(extended, f'{{{mwm}}}icon', nsmap=mwmns)
166
+ icon_tag.text = icon
167
+ elif verbose:
168
+ err(f'the icon from the following style is not found: {google_style}')
169
+
170
+
171
+ def process(doc, verbose):
172
+ remove_old_styles(doc)
173
+ remove_lines(doc)
174
+ remove_empty_folders(doc)
175
+ add_organic_maps_styles(doc)
176
+ google_to_organic_maps_icons(doc, verbose)
177
+
178
+
179
+ def leave_unsupported(doc):
180
+ for i in doc.xpath('x:Folder/x:Placemark/x:styleUrl', namespaces=ns):
181
+ icon, style = google_to_organic_maps_icon_and_style(i.text)
182
+ if icon is not None and style != i.text:
183
+ i.getparent().getparent().remove(i.getparent())
184
+
185
+
186
+ def convert(input_file, output_file, verbose, only_unsupported):
187
+ try:
188
+ root = tree.parse(input_file)
189
+ except tree.XMLSyntaxError:
190
+ raise ConversionError('The document is either not in XML format or is poorly formatted')
191
+
192
+ doc = root.find('.//x:Document', ns)
193
+ if doc is None:
194
+ raise ConversionError('The "Document" tag is missing')
195
+
196
+ if not only_unsupported:
197
+ process(doc, verbose)
198
+ else:
199
+ leave_unsupported(doc)
200
+
201
+ indent(root.getroot())
202
+ string = tree.tostring(root, pretty_print=True, encoding='UTF-8', xml_declaration=True)
203
+ output_file.write(string)
204
+
205
+
206
+ def _main():
207
+ parser = argparse.ArgumentParser(prog=__package__, description='Google My Maps to Organic Maps KML converter')
208
+ parser.add_argument('file', metavar='GOOGLE_KML', help='Google My Maps KML')
209
+ parser.add_argument('--verbose', action='store_true', help='verbose output')
210
+ parser.add_argument('--only-unsupported-styles', dest='only_unsupported', action='store_true',
211
+ help='leave only placemarks with unsupported styles')
212
+ parser.add_argument('-v', '--version', action='version', version=f'%(prog)s {__version__}')
213
+ args = parser.parse_args()
214
+ try:
215
+ with open(args.file, 'r') as input_file:
216
+ convert(input_file, sys.stdout.buffer, args.verbose, args.only_unsupported)
217
+ except Exception as e:
218
+ err(e)
219
+ return 1
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: gammon-im
3
+ Version: 2.0.0
4
+ Summary: Google My Maps to Organic Maps KML converter
5
+ Project-URL: Homepage, https://github.com/igrmk/gammon
6
+ Project-URL: Website, https://gammon.im
7
+ Author-email: igrmk <igrmkx@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.9
13
+ Requires-Dist: lxml
14
+ Description-Content-Type: text/markdown
15
+
16
+ Gammon — Google My Maps to Organic Maps KML converter
17
+ =====================================================
18
+
19
+ This tool adapts KML files from Google My Maps
20
+ for use with [Organic Maps](https://organicmaps.app/) (and MAPS.ME),
21
+ striving to maintain color and icon accuracy.
22
+ Although Organic Maps supports fewer colors and icons,
23
+ the tool does its best to match the original as closely as possible.
24
+ Input on new icon mappings is appreciated.
25
+
26
+ Formerly published as `mmmm`; now installed as `gammon-im` and run as `gammon` (online at
27
+ [gammon.im](https://gammon.im)).
28
+ If you installed the old package, migrate with `pipx uninstall mmmm && pipx install gammon-im`.
29
+
30
+ Usage
31
+ -----
32
+
33
+ gammon google-maps.kml > organic-maps.kml
34
+
35
+ Or go to [Gammon](https://gammon.im) and convert your KML online.
36
+
37
+ Installation
38
+ ------------
39
+
40
+ pipx install gammon-im
41
+
42
+ Development
43
+ -----------
44
+
45
+ Install [uv](https://docs.astral.sh/uv/), then sync the environment and run the converter or the linter:
46
+
47
+ uv sync
48
+ uv run gammon google-maps.kml > organic-maps.kml
49
+ uv run ruff check
50
+
@@ -0,0 +1,9 @@
1
+ gammon/__init__.py,sha256=5B0G6JAz2tq7k0TyBakXC-vWgB9TgVRnVXgmW1VQWMU,113
2
+ gammon/__main__.py,sha256=imGPIFJ1FB5eScNLGnkkF4HQpyn6Vwx7g-0HqXwFr6s,54
3
+ gammon/__version__.py,sha256=L26xsDhKXzAEpeFGnNg-qSAuayOj9oIgQBznP-HA4kQ,166
4
+ gammon/_main.py,sha256=NfvE1-uqszRGSTLSd3vFZSWZ7spMvS83PoA3pvFPSHY,6674
5
+ gammon_im-2.0.0.dist-info/METADATA,sha256=1nVArUngUKKrSehxibrhzWwwOgjhcLsGI_qZhwotLJ8,1528
6
+ gammon_im-2.0.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
7
+ gammon_im-2.0.0.dist-info/entry_points.txt,sha256=qsK70OnMiR_RpIh1FimzcRmb6pz-tUrF07E_TYdLrgs,46
8
+ gammon_im-2.0.0.dist-info/licenses/LICENSE,sha256=yfHJftVrbCeDtBMmbonoxbjtCYmoWm0Hf-nJbH6Z6T4,1062
9
+ gammon_im-2.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ gammon = gammon._main:_main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 igrmk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.