PHP 检索 PostGIS 地理类型

发布于 2024-10-14 22:34:31 字数 1220 浏览 1 评论 0原文

我有一个带有地理列的 PostGIS 数据库。我希望能够使用 PHP 来选择地理位置,这样我就可以使用 ST_Distance 运行另一个查询来获取点之间的距离。然而,当我运行时,

SELECT geog from locations;

我得到一个奇怪的值 6 而不是十六进制输出(类似于 0101000020E6100000C442AD69DEAD5740575BB1BFEC6E3D40 之类的东西是我所期望的)。当我在 phpPgAdmin 中使用相同的查询时,我得到了预期的输出,但不是来自我的 PHP 脚本:(

我真的很感激任何帮助:)

谢谢你,

elshae

编辑

$locations_result = @pg_query($DBConnect, "SELECT geog from locations;");

    if (!$locations_result) {
        echo "An error occurred upon getting data from the locations table.\n";
        exit;
    }
    $i = 0;
    while ($locations_arr = pg_fetch_row($locations_result)) {

        $locations['location'][$i] = $locations_arr[0];

                echo $locations['location'][$i];
        $i++;
    }

编辑2 所以现在我尝试将地理直接选择到 ST_Distance 函数中,以便它可以直接获取地理结果,例如:

SELECT ST_Distance(geog_a, geog_b) FROM(SELECT geog AS geog_a FROM location WHERE id=123 UNION SELECT geog AS geog_b FROM location WHERE id=345);

但是我的 SQL 语法是错误的,我还没有弄清楚如何获得我的结果旨在. UNION 将这些值放入一列中,这就是我已经拥有的。相反,我需要类似的东西:

geog_a |geog_b
----------------
hdsklgh|shfksh

I have a PostGIS database with a geography column. I'd like to be able to use PHP to SELECT the geography so then I could run another query using ST_Distance to get the distance between the points. However, when I run

SELECT geog from locations;

I get a weird value of 6 instead of the HEX output (something like 0101000020E6100000C442AD69DEAD5740575BB1BFEC6E3D40 is what I'm expecting). When I use the same query in phpPgAdmin I get the expected output, but not from my PHP script :(

I'd really appreciate any help :)

Thank you,

elshae

EDIT

$locations_result = @pg_query($DBConnect, "SELECT geog from locations;");

    if (!$locations_result) {
        echo "An error occurred upon getting data from the locations table.\n";
        exit;
    }
    $i = 0;
    while ($locations_arr = pg_fetch_row($locations_result)) {

        $locations['location'][$i] = $locations_arr[0];

                echo $locations['location'][$i];
        $i++;
    }

EDIT2
So now I am trying to select the geography right into the ST_Distance function so that it can just get the geography result directly, something like:

SELECT ST_Distance(geog_a, geog_b) FROM(SELECT geog AS geog_a FROM location WHERE id=123 UNION SELECT geog AS geog_b FROM location WHERE id=345);

But my SQL syntax is wrong and I have yet to figure out how I can get the result I'm aiming for. The UNION is putting these values into one column, which is what I have already. Instead I need something like:

geog_a |geog_b
----------------
hdsklgh|shfksh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2024-10-21 22:34:31

是你想要的wkb格式吗?

select ST_AsWKB('geog') from locations

但为什么要先取出数据来计算距离呢?

/Nicklas

更新

好的,

我不知道为什么您的查询没有得到正确的结果。我php太差了。我猜测结果由于某种原因被推入某种不正确的数据类型。我的意思是从 ST_AsText 你应该只得到一个字符串,没有什么比这更奇怪的了。

但为了获得距离,您不应该拉出点。您可以通过自连接来做到这一点。这就是您在使用 PostGIS 并在一个表中比较不同几何图形时一直所做的事情。

假设第一个地理区域的 id=1,第二个地理区域的 id=2,则查询可能类似于:

SELECT ST_Distance(a.the_geog, b.the_geog) as dist 
from locations a, locations b WHERE a.id=1 and b.id = 2;

如果您想要从 id=1 的点到所有点(或其他任何点)的距离,您可以这样写:

SELECT  ST_Distance(a.the_geog, b.the_geog) as dist
from locations a inner join locations b on a.id != b.id WHERE a.id=1;

等等。

这将会更加有效。

/尼克拉斯

Is it the wkb-format you want?

select ST_AsWKB('geog') from locations

But why do you want to pull out the data first to do the distance-calculation?

/Nicklas

Update

Ok

I don't know why you don't get a proper result on your queries. I am too bad in php. I would guess that the result by some reason is pushed into some data type that is not the right. I mean from ST_AsText you should just get a string, nothing more strange than that.

But to get your distance you shouldn't pull out the points. You do a self join to do that. That is what you do all the time when using PostGIS and comparing different geometries in one single table.

Let's say the first geography has id=1 and the second has id=2 the query could be something like:

SELECT ST_Distance(a.the_geog, b.the_geog) as dist 
from locations a, locations b WHERE a.id=1 and b.id = 2;

If you want the distance to all points (or whatever it is) from point with id=1 you could write:

SELECT  ST_Distance(a.the_geog, b.the_geog) as dist
from locations a inner join locations b on a.id != b.id WHERE a.id=1;

and so on.

That will be very much more effective.

/Nicklas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文