Jiawei Lu
Jiawei Lu

Reputation: 577

Extract coordinates from a string using regex in Python

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.

Upvotes: 3

Views: 514

Answers (2)

Kirk Strauser
Kirk Strauser

Reputation: 30937

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.

Upvotes: 0

Red
Red

Reputation: 27557

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

Upvotes: 2

Related Questions