-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmapml.py
61 lines (47 loc) · 2.06 KB
/
mapml.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
import logging
from xml.etree.ElementTree import Element, SubElement, tostring
from pygeoapi.formatter.base import BaseFormatter
LOGGER = logging.getLogger(__name__)
class MapMLFormatter(BaseFormatter):
"""MapML formatter"""
def __init__(self, formatter_def):
super().__init__({'name': 'mapml', 'geom': None})
self.mimetype = 'text/mapml'
def write(self, options={}, data=None):
"""
Generate data in MapML format
:returns: string representation of format
"""
mapml = Element('mapml', {'xmlns':'http://www.w3.org/1999/xhtml'})
head = SubElement(mapml, 'head')
title = SubElement(head, 'title')
body = SubElement(mapml, 'body')
if data['type'] == 'FeatureCollection':
for f in data['features']:
body.append(self.__generateFeature(f))
elif data['type'] == 'Feature':
body.append(self.__generateFeature(data))
return tostring(mapml)
def __generateFeature(self, f):
type = f['geometry']['type'].lower()
featureElem = Element('feature')
properties = SubElement(featureElem, 'properties')
name = SubElement(properties, 'h1')
name.text = f['properties']['name'] if 'name' in f['properties'] else f['properties']['stn_id']
geometry = SubElement(featureElem, 'geometry')
geometrySubtype = SubElement(geometry, type)
if type == 'multipolygon':
for p in f['geometry']['coordinates']:
polygon = SubElement(geometry, 'polygon')
polygon.text = self.__coordToStringPair(p)
else:
geometrySubtype.text = self.__coordToStringPair(f['geometry']['coordinates'])
return featureElem
def __coordToStringPair(self, coords):
if type(coords[0]) == list:
cString = ''
for pair in coords[0]:
cString += str(pair[0]) + ' ' + str(pair[1]) + ' '
return cString[:-1]
else:
return str(coords[0]) + ' ' + str(coords[1])