在PHP中转换MySQL二进制GEOMETRY字段 [英] Conversion of MySQL binary GEOMETRY fields in PHP
问题描述
我正在开发一个集成mapquest地图并使用mapquest API的应用程序.
I’m working on an application integrating mapquest maps and using the mapquest API.
此刻,我要实现以下目标:注册用户必须输入纬度和经度信息,并且该信息必须存储在几何类型字段中.相反,查看其帐户的用户应看到输入的纬度和经度信息.
At this moment I want to achieve the following: the registered user has to input latitude and longitude information, and in the database this information has to be stored as geometry type field. Conversely, a user looking at their account should see the latitude and longitude information that was entered.
在较早的简化代码库中,我通过使用MySQL函数AsText
和GeomFromText
在MySQL查询中直接实现了这一点.但是现在我在CodeIgniter上,需要用PHP进行转换
In an earlier simplified codebase I achieved this directly in the MySQL query, by using the MySQL functions AsText
and GeomFromText
. But now I’m on CodeIgniter and need to do the conversion in PHP
PHP中是否有与MySQL的AsText
和GeomFromText
函数等效的东西?
Is there anything equivalent in PHP to MySQL's AsText
and GeomFromText
functions?
推荐答案
看来,PHP
unpack
函数是提取坐标信息所需要的.
It appears that the PHP
unpack
function is what is needed to extract the coordinate information.
MySQL
以WKB(众所周知的二进制)格式存储几何字段.提供适当的格式说明符时,unpack
函数能够提取该信息.以下代码来自成功提取所需信息的测试脚本.
MySQL
stores the geometry fields in the WKB (Well Known Binary) format. The unpack
function is able to extract that information when provided the proper format specifier. The following code is from a test script which successfully extracted the desired information.
请注意,我的format
拆包与参考文件略有不同.这是因为未使用AsWKB()
函数从MySQL中检索到WKB字符串,因此它包含额外的填充.
Please note that my format
for unpack differs slightly from that of the reference. This is because the WKB string wasn't retrieved from MySQL with the AsWKB()
function, so it contains extra padding.
<?php
$padding = 0;
$order = 1;
$gtype = 1;
$lon = -73.91353;
$lat = 42.80611;
$bindata = pack('LcLd2', $padding, $order, $gtype, $lat, $lon);
printf("Packed: %s\n\n", bin2hex($bindata));
$result = unpack('Lpadding/corder/Lgtype/dlatitude/dlongitude', $bindata);
var_dump($result);
?>
这篇关于在PHP中转换MySQL二进制GEOMETRY字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!