@@ -51,6 +51,19 @@ def parse_coordinates(value: str, cs: str = ',', ts: str = ' ',
51
51
""" Parses the the values of a gml:coordinates node to a list of
52
52
lists of floats. Takes the coordinate separator and tuple
53
53
separator into account, and also custom decimal separators.
54
+
55
+ >>> parse_coordinates('12.34 56.7,89.10 11.12')
56
+ [(12.34, 56.7), (89.1, 11.12)]
57
+ >>> parse_coordinates('12.34 56.7;89.10 11.12', cs=';')
58
+ [(12.34, 56.7), (89.1, 11.12)]
59
+ >>> parse_coordinates('12.34:56.7,89.10:11.12', ts=':')
60
+ [(12.34, 56.7), (89.1, 11.12)]
61
+ >>> parse_coordinates('12.34:56.7;89.10:11.12', cs=';', ts=':')
62
+ [(12.34, 56.7), (89.1, 11.12)]
63
+ >>> parse_coordinates(
64
+ ... '12,34:56,7;89,10:11,12', cs=';', ts=':', decimal=','
65
+ ... )
66
+ [(12.34, 56.7), (89.1, 11.12)]
54
67
"""
55
68
56
69
number_parser = _make_number_parser (decimal )
@@ -67,6 +80,15 @@ def parse_coordinates(value: str, cs: str = ',', ts: str = ' ',
67
80
def parse_poslist (value : str , dimensions : int = 2 ) -> Coordinates :
68
81
""" Parses the value of a single gml:posList to a `Coordinates`
69
82
structure.
83
+
84
+ >>> parse_poslist('12.34 56.7 89.10 11.12')
85
+ [(12.34, 56.7), (89.1, 11.12)]
86
+ >>> parse_poslist('12.34 56.7 89.10 11.12 13.14 15.16', dimensions=3)
87
+ [(12.34, 56.7, 89.1), (11.12, 13.14, 15.16)]
88
+ >>> parse_poslist('12.34 56.7 89.10 11.12', dimensions=3)
89
+ Traceback (most recent call last):
90
+ ...
91
+ ValueError: Invalid dimensionality of pos list
70
92
"""
71
93
raw = [float (v ) for v in value .split ()]
72
94
if len (raw ) % dimensions > 0 :
@@ -80,18 +102,37 @@ def parse_poslist(value: str, dimensions: int = 2) -> Coordinates:
80
102
81
103
def parse_pos (value : str ) -> Coordinate :
82
104
""" Parses a single gml:pos to a `Coordinate` structure.
105
+
106
+ >>> parse_pos('12.34 56.7')
107
+ (12.34, 56.7)
108
+ >>> parse_pos('12.34 56.7 89.10')
109
+ (12.34, 56.7, 89.1)
83
110
"""
84
111
return tuple (float (v ) for v in value .split ())
85
112
86
113
87
114
def swap_coordinate_xy (coordinate : Coordinate ) -> Coordinate :
88
115
""" Swaps the X and Y coordinates of a given coordinate
116
+
117
+ >>> swap_coordinate_xy((12.34, 56.7))
118
+ (56.7, 12.34)
119
+ >>> swap_coordinate_xy((12.34, 56.7, 89.10))
120
+ (56.7, 12.34, 89.1)
89
121
"""
90
122
return (coordinate [1 ], coordinate [0 ], * coordinate [2 :])
91
123
92
124
93
125
def swap_coordinates_xy (coordinates : Coordinates ) -> Coordinates :
94
126
""" Swaps the X and Y coordinates of a given coordinates list
127
+
128
+ >>> swap_coordinates_xy(
129
+ ... [(12.34, 56.7), (89.10, 11.12)]
130
+ ... )
131
+ [(56.7, 12.34), (11.12, 89.1)]
132
+ >>> swap_coordinates_xy(
133
+ ... [(12.34, 56.7, 89.10), (11.12, 13.14, 15.16)]
134
+ ... )
135
+ [(56.7, 12.34, 89.1), (13.14, 11.12, 15.16)]
95
136
"""
96
137
return [
97
138
(coordinate [1 ], coordinate [0 ], * coordinate [2 :])
0 commit comments