return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, json
-def parse_wrmap_coordinates(coordinates_str):
- return [[0, 0]] # TODO
+def parse_wrmap_coordinates(coords):
+ '''gets a string coordinates and returns an array of lon/lat coordinate pairs, e.g.
+ 47.12 N 11.87 E
+ 47.13 N 11.70 E
+ ->
+ [[11.87, 47.12], [11.70, 47.13]]'''
+ result = []
+ pos = 0
+ for match in re.finditer(r'\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*', coords):
+ if match.start() != pos:
+ break
+ result.append([float(match.groups()[1]), float(match.groups()[0])])
+ pos = match.end()
+ else:
+ if pos == len(coords):
+ return result
+ raise RuntimeError('Wrong coordinate format: {}'.format(coords))
def parse_wrmap(wikitext):
if len(wrong_properties) > 0:
raise RuntimeError("The attribute '{}' is not allowed at <{}>.".format(list(wrong_properties)[0], feature.tag))
if feature.attrib.has_key('farbe'):
- # TODO: check value
+ if not re.match('#[0-9a-fA-F]{6}$', feature.attrib['farbe']):
+ raise RuntimeError('The attribute "farbe" has to have a format like "#a0bb43".')
properties['strokeColor'] = feature.attrib['farbe'] # e.g. #a200b7
if feature.attrib.has_key('dicke'):
try:
properties['strokeWidth'] = int(feature.attrib['dicke']) # e.g. 6
except ValueError:
- raise RuntimeError('The attribute "farbe" has to be an integer.')
+ raise RuntimeError('The attribute "dicke" has to be an integer.')
json_features.append({
'type': 'feature',
'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
'type': 'FeatureCollection',
'features': json_features}
- # attributes # TODO: check
+ # attributes
attributes = {}
- attributes['latitude'] = float(wrmap_xml.attrib.get('lat', 47.267648)) # center lat
- attributes['longitude'] = float(wrmap_xml.attrib.get('lon', 11.404655)) # center lon
- attributes['zoom'] = int(wrmap_xml.attrib.get('zoom', 10)) # Google Zoom Level
- attributes['width'] = int(wrmap_xml.attrib['width']) if wrmap_xml.attrib.has_key('width') else None # None corresponds to 100%
- attributes['height'] = int(wrmap_xml.attrib.get('height', 450)) # map height in px
+ try:
+ attributes['lat'] = float(wrmap_xml.attrib.get('lat', 47.267648)) # center lat
+ except ValueError:
+ raise RuntimeError('Attribute "lat" has to be a float value.')
+ try:
+ attributes['lon'] = float(wrmap_xml.attrib.get('lon', 11.404655)) # center lon
+ except ValueError:
+ raise RuntimeError('Attribute "lon" has to be a float value.')
+ try:
+ attributes['zoom'] = int(wrmap_xml.attrib.get('zoom', 10)) # Google Zoom Level
+ except ValueError:
+ raise RuntimeError('Attribute "zoom" has to be an integer value.')
+ try:
+ attributes['width'] = int(wrmap_xml.attrib['width']) if wrmap_xml.attrib.has_key('width') else None # None corresponds to 100%
+ except ValueError:
+ raise RuntimeError('Attribute "width" has to be an integer value.')
+ try:
+ attributes['height'] = int(wrmap_xml.attrib.get('height', 450)) # map height in px
+ except ValueError:
+ raise RuntimeError('Attribute "height" has to be an integer value.')
# show_sledruns = (wrmap_xml.tag == 'wrgmap')
return attributes, json