从PGSQL解析PHP

时间:2012-07-03 01:44:36

标签: php

我有一个postgresql db的连接,它返回以下结果

O4, MULTILINESTRING((-91.4272099951079 35.6984820849707,-91.2691971234476 35.8195546559061))
I4, MULTILINESTRING((-91.4668296641028 35.5407821819705,-91.3332878428544 35.7230307833947,-91.1889662757843 35.8834271551078))

使用此查询:

select id, astext(the_geom)from trkl

我正在尝试生成如下文件,我必须首先反转35.xx和-91.xx,最后是否有一种简单的方法可以做到这一点?

"O4"
35.62200200, -88.98259200
35.62203500, -88.98240800
35.62202700, -88.98231000
35.62180000, -88.98163400
END:
"I4"
35.62200200, -88.98259200
35.62203500, -88.98240800
35.62202700, -88.98231000
35.62180000, -88.98163400
35.62175700, -88.98149000
35.62172500, -88.97881200
35.62172000, -88.97798500
35.62169800, -88.97752400
END

2 个答案:

答案 0 :(得分:0)

我不熟悉这个PGSQL以及从数据库中获取结果时的结果,但我认为这就是你要找的......

$mls = '-91.4272099951079 35.6984820849707,-91.2691971234476 35.8195546559061';
$pairs = explode(',', $mls);
foreach ($pairs as $pair) {
    $coords = explode(' ', $pair);
    printf('%s, %s', $coords[1], $coords[0]);
}

输出

35.6984820849707, -91.4272099951079
35.8195546559061, -91.2691971234476

答案 1 :(得分:0)

假设您的坐标始终使用字符串中包含的MULTILINESTRING获取:

$output = array();
foreach ($results as $row)
{
    $row = explode(', ', $row, 2);
    $output[] = '"'.$row[0].'"';
    $row = explode(',', substr(rtrim($row[1], ')'), 17);
    for ($i = 0, $c = count($row); $i < $c; $i++)
    {
        $row[$i] = explode(' ', $row[$i], 2);
        $output[] = $row[$i][1].', '.$row[$i][0];
    }
}
$output = implode("\n", $output);

有更好的解决方案,但这是最明显的解决方案,应该可以胜任。