def test_create_wrmap():
- pass
+ attributes = {'lon': 11.21408895, 'lat': 47.2417134, 'zoom': 14, 'width': 700, 'height': 400}
+ geojson = {
+ 'type': 'FeatureCollection',
+ 'features':
+ [{
+ 'type': 'feature',
+ 'geometry': {
+ 'type': 'Point',
+ 'coordinates': [11.190454, 47.240689]},
+ 'properties': {'type': 'inn', 'name': u'Rosskogelhütte', 'wiki': u'Rosskogelhütte'}
+ }, {
+ 'type': 'feature',
+ 'geometry': {
+ 'type': 'Point',
+ 'coordinates': [11.238971, 47.245789]},
+ 'properties': {'type': 'carpark'}
+ }, {
+ 'type': 'feature',
+ 'geometry': {
+ 'type': 'Point',
+ 'coordinates': [11.238283, 47.245711]},
+ 'properties': {'type': 'busstop', 'name': u'Oberperfuss Rangger Köpfl Lift'}
+ }, {
+ 'type': 'feature',
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [
+ [11.203360, 47.238587],
+ [11.230868, 47.244951],
+ [11.237853, 47.245470]]},
+ 'properties': {}
+ }]
+ }
+
+ wikitext = wrpylib.wrmwmarkup.create_wrmap(attributes, geojson)
+ assert wikitext == u'''
+ <wrmap lat="47.2417134" lon="11.21408895" zoom="14" width="700" height="400">
+ <gasthaus name="Rosskogelhütte" wiki="Rosskogelhütte">47.240689 11.190454</gasthaus>
+ <parkplatz>47.245789 11.238971</parkplatz>
+ <haltestelle name="Oberperfuss Rangger Köpfl Lift">47.245711 11.238283</haltestelle>
+ <rodelbahn>
+ 47.238587 11.203360
+ 47.244951 11.230868
+ 47.245470 11.237853
+ </rodelbahn>
+ </wrmap>'''
import wrpylib.wrvalidators
import wrpylib.mwmarkup
+WRMAP_POINT_TYPE = {'gasthaus': 'inn', 'haltestelle': 'busstop', 'parkplatz': 'carpark', 'achtung': 'attention', 'punkt': 'point'}
+WRMAP_LINE_TYPE = {'rodelbahn': 'sledrun', 'gehweg': 'walk', 'alternative': 'alternative', 'lift': 'lift', 'linie': 'line'}
def _conv(fnct, value, fieldname):
"""Internal function.
'coordinates': [[lon, lat] for lon, lat, symbol, title in entries]},
'properties': properties})
- json = {'type': 'FeatureCollection', 'features': json_features}
- return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, json
+ geojson = {'type': 'FeatureCollection', 'features': json_features}
+ return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, geojson
def parse_wrmap_coordinates(coords):
# convert XML to geojson (http://www.geojson.org/geojson-spec.html)
json_features = []
- point_type = {'gasthaus': 'inn', 'haltestelle': 'busstop', 'parkplatz': 'carpark', 'achtung': 'attention', 'punkt': 'point'}
- line_type = {'rodelbahn': 'sledrun', 'gehweg': 'walk', 'alternative': 'alternative', 'lift': 'lift', 'linie': 'line'}
for feature in wrmap_xml:
# determine feature type
- is_point = point_type.has_key(feature.tag)
- is_line = line_type.has_key(feature.tag)
+ is_point = WRMAP_POINT_TYPE.has_key(feature.tag)
+ is_line = WRMAP_LINE_TYPE.has_key(feature.tag)
if (not is_point and not is_line):
raise RuntimeError('Unknown element <{}>.'.format(feature.tag))
# point
if is_point:
- properties = {'type': point_type[feature.tag]}
+ properties = {'type': WRMAP_POINT_TYPE[feature.tag]}
allowed_properties = set(['name', 'wiki'])
wrong_properties = set(feature.attrib.keys()) - allowed_properties
if len(wrong_properties) > 0:
# line
if is_line:
- properties = {'type': line_type[feature.tag]}
+ properties = {'type': WRMAP_LINE_TYPE[feature.tag]}
allowed_properties = set(['farbe', 'dicke'])
wrong_properties = set(feature.attrib.keys()) - allowed_properties
if len(wrong_properties) > 0:
'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
'properties': properties})
- json = {
+ geojson = {
'type': 'FeatureCollection',
'features': json_features}
raise RuntimeError('Attribute "height" has to be an integer value.')
# show_sledruns = (wrmap_xml.tag == 'wrgmap')
- return attributes, json
+ return attributes, geojson
-def create_wrmap(geojson):
- """Creates a <wrmap> wikitext from geojson."""
- pass
-
+def create_wrmap_coordinates(coords):
+ result = []
+ for coord in coords:
+ result.append('{:.6f} N {:.6f} E'.format(coord[1], coord[0]))
+ return '\n'.join(result)
+
+
+def create_wrmap(attributes, geojson):
+ """Creates a <wrmap> wikitext from geojson (as python types)."""
+ wrmap_xml = xml.etree.ElementTree.Element('wrmap')
+ wrmap_xml.attrib = attributes
+
+ assert geojson['type'] == 'FeatureCollection'
+ json_features = geojson['features']
+ for json_feature in json_features:
+ geo = json_feature['geometry']
+ if geo['type'] == 'Point':
+ wrmap_type = dict(zip(WRMAP_POINT_TYPE.values(), WRMAP_POINT_TYPE.keys()))
+ feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, wrmap_type[json_feature['properties']['type']])
+ feature_xml.text = create_wrmap_coordinates([geo['coordinates']])
+ feature_xml.attrib = json_feature['properties']
+ if geo['type'] == 'LineString':
+ wrmap_type = dict(zip(WRMAP_LINE_TYPE.values(), WRMAP_LINE_TYPE.keys()))
+ feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, wrmap_type[json_feature['properties']['type']])
+ feature_xml.text = create_wrmap_coordinates(geo['coordinates'])
+ feature_xml.attrib = json_feature['properties']
+
+ return xml.etree.ElementTree.tostring(wrmap_xml)