47.112408,11.271119
</googlemap>
'''
- center, zoom, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
- assert center == (11.272337, 47.113291)
- assert zoom == 15
+ attributes, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
+ assert attributes['lon'] == 11.272337
+ assert attributes['lat'] == 47.113291
+ assert attributes['zoom'] == 15
assert coords == [
(11.266026, 47.114958, 'Parkplatz', 'Erster Parkplatz'),
(11.266262, 47.114715, 'Gasthaus', u'Alt Bärnbad (Gasthaus)')]
def test_googlemap_to_wrmap():
wikitext = u'''
- <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15">
+ <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15" height="450">
(Parkplatz)47.114958,11.266026
Erster Parkplatz
47.112408,11.271119
</googlemap>
'''
- center, zoom, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
- json = wrpylib.wrmwmarkup.googlemap_to_wrmap(center, zoom, coords, paths)
+ attributes, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
+ json = wrpylib.wrmwmarkup.googlemap_to_wrmap(attributes, coords, paths)
assert json['properties']['lon'] == 11.272337
assert json['properties']['lat'] == 47.113291
assert json['properties']['zoom'] == 15
+ assert json['properties']['height'] == 450
assert json['features'][0]['properties']['type'] == 'parkplatz'
assert json['features'][0]['properties']['name'] == 'Erster Parkplatz'
assert json['features'][0]['geometry']['coordinates'] == [11.266026, 47.114958]
47.112408,11.271119
</googlemap>
'''
- :returns: the tuple (center, zoom, coords, paths).
- center is the tuple (lon, lat) of the google maps or (None, None) if not provided
- zoom is the google zoom level as integer or None if not provided
+ :returns: The tuple (attributes, coords, paths) is returned.
+ attributes is a dict that contains the attribues that are present
+ (e.g. lon, lat, zoom, width, height) converted to float (lon, lat) or int.
coords is a list of (lon, lat, symbol, title) tuples.
paths is a list of (style, coords) tuples.
- coords is again a list of (lot, lat, symbol, title) tuples."""
+ coords is again a list of (lon, lat, symbol, title) tuples."""
def is_coord(line):
"""Returns True if the line contains a coordinate."""
start, content, endtag, end = find_tag(wikitext, 'googlemap')
if start is None:
- raise ParseError('<googlemap> tag not found.')
+ raise ParseError(u'<googlemap> tag not found.')
if content is None:
xml_only = wikitext[start:endtag]
else:
gm = xml.etree.ElementTree.XML(xml_only.encode('UTF8'))
except xml.etree.ElementTree.ParseError as e:
row, column = e.position
- raise ParseError("XML parse error in <googlemap ...>.")
- zoom = gm.get('zoom')
- lon = gm.get('lon')
- lat = gm.get('lat')
- if not zoom is None: zoom = int(zoom)
- if not lon is None: lon = float(lon)
- if not lat is None: lat = float(lat)
- center = (lon, lat)
+ raise ParseError(u"XML parse error in <googlemap ...>.")
+ # parse attributes
+ attributes = {}
+ try:
+ for key in ['lon', 'lat']:
+ if gm.get(key) is not None:
+ attributes[key] = float(gm.get(key))
+ for key in ['zoom', 'width', 'height']:
+ if gm.get(key) is not None:
+ attributes[key] = int(gm.get(key))
+ except ValueError as error:
+ raise ParseError(u'Error at parsing attribute {0} of <googlemap>: {1}'.format(key, unicode(error)))
+
+ # parse points and lines
coords = []
paths = []
lines = wikitext[content:endtag].split("\n")
continue
raise ParseError(u'Unknown line syntax: ' + line)
- return (center, zoom, coords, paths)
+
+ return (attributes, coords, paths)
return results
-def googlemap_to_wrmap(center, zoom, coords, paths):
+def googlemap_to_wrmap(attributes, coords, paths):
"""Converts the output of parse_googlemap to the GeoJSON format wrmap uses.
:returns: (GeoJSON as nested Python datatypes)
"""
geojson = {
'type': 'FeatureCollection',
'features': json_features,
- 'properties': {'lon': center[0], 'lat': center[1], 'zoom': zoom}}
+ 'properties': attributes}
return geojson