4

From a geospatial column in mysql I'm getting the following string-value which I want to convert into an array. Ultimate goal is to convert it to geoJSON.

POLYGON((4.885838 52.388063,4.891061 52.388381,4.890973 52.382909))

This string has 3 coordinate pairs with the x and y coordinate separated by a space and the pairs separated with a comma. The exact number is not known and variable. Also the POLYGON can differ to three different settings.

With my little knowledge of reg. expressions I came up with this:

$pat = '/^(POLYGON|LINESTRING|POINT)(\(\() (.....) (\)\))$/';
preg_match($pat, $str, $matches);

With the part of the coordinates with the double brackets as an uncertain part.

Could anyone help me with this?

edit Ultimately the resulting array should look like this:

$array['type'] = POLYGON | LINESTRING ....
$array['coordinates'] = array of all the coordinates.
2
  • How should the array look like? Commented May 14, 2012 at 8:39
  • Added an example result array. Thank you for looking into it! Commented May 14, 2012 at 8:43

3 Answers 3

2

You're best off tackling this in stages. Only the first stage needs to use regex:

  1. Find the entire string of coordinates as one glob, for example:

    "4.885838 52.388063,4.891061 52.388381,4.890973 52.382909"

  2. Split that string up into coordinate pairs separated by comma. In Python, we would use str.split(','). I believe your PHP equivalent is called explode().

    [ "4.885838 52.388063" , "4.891061 52.388381" , "4.890973 52.382909" ]

  3. Split each coordinate pair into two numbers separated by space: str.split(' ').

    [ ["4.885838","52.388063"] , ["4.891061","52.388381"] , ["4.890973","52.382909"] ]

  4. Convert to floats. In python we use float(): your local equivalent might be called str2float() or similar.

    [ [4.885838,52.388063] , [4.891061,52.388381] , [4.890973,52.382909] ]

To find the string of numbers for step 1, try the regex

([-]?\d+.\d+ [-]?\d+.\d+)((,[-]?\d+.\d+ [-]?\d+.\d+)+)?

which finds at least one pair of coordinates in x y format, with coordinate pairs separated by commas. You can see the regular expression in action on regexr.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I used the method you described but the function of emil Vikström.
0

I think it's easier and more maintainable to just use explode and array_map on the coordinate string:

$coordString = $matches[3];
$coordinates = array_map(function($e) { return explode(' ', $e); },
                         explode(',', $coordString));

Comments

0

I have stumbled into the same problem, however in my case I found patterns such as POLYGON ((-1.23 2.34, 3.45 4.56), (3.21 4.32, 5.43 6.54)) and the above regular expression did not work for me to perform step 1. After some head scratching I came with the following pattern: /(((([-]?\d+\.\d+) ([-]?\d+\.\d+))[, ]?)+[, ]?)+/. You would then iterate over the matches splitting on the commas and finally split on the spaces. I am not a regex guru, so maybe someone has a better solution.

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.