Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert MySQL's POINT to text in PHP

Using PHP, how can I convert a value in POINT datatype to a string like POINT (-34.601020 -58.371020) (an ouput in WKT or GeoJSON is preferable)

If I echo the raw value, I get weird characters.

I've tried using bin2hex and then tried to convert the hex to string but with no luck.

I'm aware of MySQL's AsText(), but I would like to do it in PHP.

like image 419
Matías Cánepa Avatar asked May 26 '16 17:05

Matías Cánepa


1 Answers

Finally I've got this working!!!

I had to use unpack in order to extract the binary data from MySQL

$point_value = $data_from_db["point_field"];

$coordinates = unpack('x/x/x/x/corder/Ltype/dlat/dlon', $point_value);

echo $coordinates['lat'];
echo $coordinates['lon'];

Here is a tutotial that helped me with this issue

like image 81
Matías Cánepa Avatar answered Nov 07 '22 11:11

Matías Cánepa