i have a string as:
mydata
'POINT (558750.3267372231900000 6361788.0628051758000000)'
i wish a code saving way to convert in a list numeric as
(g, (x,y))
where:
g = geometry (POINT)
x = coordinates x
y = coordinates y
i am using
mydata.split(" ")
['POINT', '(558750.3267372231900000', '6361788.0628051758000000)']
but after that i need to use several code line to get x and y
Step by step:
>>> s = 'POINT (558750.3267372231900000 6361788.0628051758000000)'
>>> word, points = s.split(None, 1)
>>> word
'POINT'
>>> points
'(558750.3267372231900000 6361788.0628051758000000)'
>>> points = points.strip('()').split()
>>> points
['558750.3267372231900000', '6361788.0628051758000000']
>>> x, y = (float(i) for i in points)
>>> x
558750.3267372232
>>> y
6361788.062805176
Regex can spare you some typing here:
In [1]: import re
In [2]: def nice_tuple(s):
g, x, y, _ = re.split(' ?[()]?', s)
return g, tuple(map(float, (x, y)))
...:
In [3]: nice_tuple('POINT (558750.3267372231900000 6361788.0628051758000000)')
Out[3]: ('POINT', (558750.3267372232, 6361788.062805176))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With