Reputation: 11
Convert MySQL's POINT to text in PHP
I found this question & answer and it solved my problems. But I haven't found a solution for polygons.
Does someone have a solution to convert the blob/polygon to text similar to the point example?
Upvotes: 1
Views: 2009
Reputation: 2039
As you know MySQl (and MariDB) store geometry data as follow:
MySQL stores geometry values using 4 bytes to indicate the SRID followed by the WKB representation of the value. For a description of WKB format, see Well-Known Binary (WKB) Format.
See MySQL documents for more information.
By using GeoPHP you could convert any Geo data (from my MySQL) as follow:
$data = unpack("lsrid/H*wkb", $val);
$wkb_reader = new WKB();
$geometry = $wkb_reader->read($data['wkb'], TRUE);
$wkt_writer = new WKT();
$wkt = $wkt_writer->write($geometry);
return $wkt;
Where $val is column value from MySQL table.
The first line unpack data into two part:
1- SRID: first 4 bytes 2- WKB: Others as hex strig
and convert WKB hex string into WKT with GeoPHP.
Upvotes: 1
Reputation: 57121
If you use ST_NumGeometries(from https://dev.mysql.com/doc/refman/5.7/en/gis-geometrycollection-property-functions.html#function_st-numgeometries) it should give you the number of points and then you could to use a loop and ST_GeometryN to fetch each individual point. This would still need to be decoded like you already have.
You could convert the polygon back to text (AsText) and then parse the data there.
Upvotes: 2