使用 PHP pack() 将 WKT 转换为 WKB

发布于 2024-12-07 08:23:39 字数 522 浏览 1 评论 0原文

我正在使用准备好的语句将数据插入数据库,问题是我无法使用

INSERT INTO table (polygon) VALUES (GeomFromText(POLYGON((?,?,?,?,?,?))))

为什么?好吧,似乎 GeomFromText 本身被解释为文本:/ 所以我想我应该尝试将纯 WKB 字符串推入数据库,问题是我无法真正弄清楚如何将 WKT 打包到 WKB 中。

有人可以帮助我使用以下格式描述来完成此操作: http: //dev.mysql.com/doc/refman/5.0/en/gis-wkb-format.html pack() 的文档位于: http://php.net/manual/en/function.pack.php

I'm using prepared statements to insert data into my database, problem is I'm not able to use

INSERT INTO table (polygon) VALUES (GeomFromText(POLYGON((?,?,?,?,?,?))))

why? well, seems the GeomFromText itself is interpreted as text :/
so I figured I'd try pushing pure WKB strings into the db instead, problem is I can't really figure out how to pack a WKT into WKB.

Could someone help me do this with this format description: http://dev.mysql.com/doc/refman/5.0/en/gis-wkb-format.html
and the doc for pack() over at:
http://php.net/manual/en/function.pack.php

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

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

发布评论

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

评论(2

话少情深 2024-12-14 08:23:40

这非常简单。获取规范并使用“C”代表字节,“V”代表字节uint32 和“d”表示 pack 格式字符串中的双精度数。但是,我的建议是不要这样做。首先,重写内置函数是浪费时间。其次,使用 sql 传输二进制数据很容易出错(例如考虑编码问题)。第三,根据 http://dev.mysql。 com/doc/refman/5.0/en/creating-spatial-values.html 你甚至不需要GeomFromText,因为mysql已经将不带引号的wkt视为二进制。

This is pretty straightforward. Grab a specification and use "C" for bytes, "V" for uint32's and "d" for doubles in your pack format strings. However, my advice is not to do that. First, rewriting a build-in function is a waste of time. Second, transferring binary data with sql is error prone (think of encoding issues for example). And third, according to http://dev.mysql.com/doc/refman/5.0/en/creating-spatial-values.html you don't even need GeomFromText, because mysql already treats unquoted wkt as binary.

兔姬 2024-12-14 08:23:39

我看到两个问题。首先,GeomFromText 函数接受一个字符串,因此它应该类似于 GeomFromText('POLYGON((0, 1, 2))') (注意引号)。其次,由于 POLYGON... 文本是字符串文字,因此应将其替换为通配符,而不是单个片段。您的查询应如下所示:

INSERT INTO areas (name, polygon)
VALUES (?, GeomFromText(?))

然后您将在应用程序中而不是在语句中构建 POLYGON((?, ?, ?, ?, ?, ?)) 字符串。由于 PHP 具有安全的字符串处理,我建议使用 sprintf('POLYGON((%d, %d, %d, %d, %d, %d))', $var1, $var2, $var3, $ var4, $var5, $var6) (sprintf 在 C 中是危险的)。

或者,您可以使用 MySQL 的空间函数来生成点。我认为这就是您想要做的,但您没有通过 GeomFromText 传递它们。要使用 MySQL 的空间函数构建多边形,文档建议您需要执行以下操作:

INSERT INTO areas (name, polygon)
VALUES (?, Polygon(LineString(Point(?, ?), Point(?, ?), Point(?, ?))))

在 MySQL 5.1.35 之前,您需要执行以下操作:

INSERT INTO areas (name, polygon)
VALUES (?, GeomFromWKB(Polygon(LineString(Point(?, ?), Point(?, ?), Point(?, ?)))))

I see two problems. First, the GeomFromText function takes a string, so it should look like GeomFromText('POLYGON((0, 1, 2))') (note the quotation marks). Second, since the POLYGON... text is a string literal, it should be replaced with the wildcard, not the individual pieces. Your query should look like this:

INSERT INTO areas (name, polygon)
VALUES (?, GeomFromText(?))

You would then build the POLYGON((?, ?, ?, ?, ?, ?)) string in the application, not in the statement. Since PHP has safe string handling I would recommend using sprintf('POLYGON((%d, %d, %d, %d, %d, %d))', $var1, $var2, $var3, $var4, $var5, $var6) (sprintf is dangerous in C).

Alternatively, you can use MySQL's spatial functions to generate the points. I think this is what you were trying to do, but you don't pass them through GeomFromText. To build a polygon with MySQL's spatial functions, the documentation suggests you would need to do:

INSERT INTO areas (name, polygon)
VALUES (?, Polygon(LineString(Point(?, ?), Point(?, ?), Point(?, ?))))

Prior to MySQL 5.1.35 you would need to do:

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