3

I have multiple strings as below:

LINESTRING (-3.1 2.42, 5.21 6.1, -1.17 -2.23)
LINESTRING (1.83 9.5, 3.33 2.87)

The expected results are lists that contain corresponding coordinates in the tuple format:

[(-3.1,2.42),(5.21,6.1),(-1.17,-2.33)]
[(1.83,9.5),(3.33,2.87)]

Note that the number of coordinates in the string is unknown and variable. Now, I use the split function twice after removing characters outside the parenthesis. Is there any elegant way to exact the coordinates using Regex.

1

2 Answers 2

2

Here is how you can use a for loop:

import re

strings = ['LINESTRING (-3.1 2.42, 5.21 6.1, -1.17 -2.23)',
           'LINESTRING (1.83 9.5, 3.33 2.87)']

for string in strings:
    st = re.findall('(?<=[(,]).*?(?=[,)])', string)
    print([tuple(s.split()) for s in st])

Output:

[('-3.1', '2.42'), ('5.21', '6.1'), ('-1.17', '-2.23')]
[('1.83', '9.5'), ('3.33', '2.87')]
Sign up to request clarification or add additional context in comments.

Comments

0

Is it a requirement to use regexps? I find plain ol' string splitting to be more maintainable:

strings = [
    "LINESTRING (-3.1 2.42, 5.21 6.1, -1.17 -2.23)",
    "LINESTRING (1.83 9.5, 3.33 2.87)",
]

for s in strings:
    # Collect stuff between parentheses
    inside = s.split("(")[1].split(")")[0]

    pairs = []
    for pair in inside.split(", "):
        left, right = pair.split(" ")
        pairs.append((float(left), float(right)))

    print(pairs)

This isn't a super clever solution -- it's pretty brute force -- but if it breaks at 2AM I think I'd be able to figure out what it's actually doing.

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.