2

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

1
  • what about storing data in list as point objects, shapely provides method to parse your point strings for you pypi.python.org/pypi/Shapely Commented Dec 6, 2012 at 17:52

8 Answers 8

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

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))

Comments

2

If your data is always in that exact format, it's easy:

>>> def parse_data(d):
    geom, xs, ys = d.split()
    return (geom, (float(xs[1:]), float(ys[:-1])))

>>> mydata
'POINT (558750.3267372231900000 6361788.0628051758000000)'
>>> parse_data(mydata)
('POINT', (558750.32673722319, 6361788.0628051758))

Comments

1

using regex:

In [59]: g,[x,y]=re.findall(r"[A-Za-z]+",mydata)[0],
                       [float(x) for x in re.findall(r"[\d+.]+",mydata)]

In [60]: g
Out[60]: 'POINT'

In [61]: x
Out[61]: 558750.3267372232

In [62]: y
Out[62]: 6361788.062805176

using str.strip() and str.split():

In [35]: mydata='POINT (558750.3267372231900000 6361788.0628051758000000)'

In [39]: data=mydata.split(None,1)

In [40]: data
Out[40]: ['POINT', '(558750.3267372231900000 6361788.0628051758000000)']

In [41]: g,[x,y]=data[0], map(lambda x: float(x.strip("()")), data[1].split())

In [42]: g,x,y
Out[42]: ('POINT', 558750.3267372232, 6361788.062805176)

Comments

1

I would use .translate and .split:

In [126]: mydata = 'POINT (558750.3267372231900000 6361788.0628051758000000)'

In [127]: mysplitdata = mydata.translate(None, '()').split()

In [128]: mysplitdata
Out[128]: ['POINT', '558750.3267372231900000', '6361788.0628051758000000']

In [129]: g,x,y = mysplitdata[0],float(mysplitdata[1]),float(mysplitdata[2])

In [130]: outdata = (g, (x,y))

In [131]: outdata
Out[131]: ('POINT', (558750.32673722319, 6361788.0628051758))

Comments

1

Recently I created an application in python where I did almost the same thing. Here is a class I created to parse wkt files.

link

Hope you find it useful. See line number 136 for usage. You can use this class to read Linestrings and Multilinestrings as well.

Comments

1
found = re.match(r'([a-zA-Z]*) \(([0-9\.]*) ([0-9\.]*)\)', mydata)
found.group(1), (float(found.group(2)), float(found.group(3)))

That's probably the shortest one, don't know about elegant.

1 Comment

You still have to convert x and y to float.
1
v = mydata.split()
g = v[0]
x = float(v[1].strip('('))
y = float(v[2].strip(')'))
(g, (x, y))

Code saving yes, elegant not so much

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.