简体   繁体   中英

How to convert points from MySql database to WKT using PHP?

In MySql database I have points (pairs of latitudes and longitudes represented as doubles) like this:

LATITUDE | LONGITUDE
---------|----------
45.045664| 46.084528
26.938933| 60.903848
       .....

Now, what I need is to get these points from my database (which is not a problem at all) and echo them in a WKT format (this part I don't know). This PHP script is going to be a part of an API used by Windows App.

So question is: how can I convert points to WKT format? Is there any plugin I can use? Which are the best approaches?

Thanks in advance.

UPDATE

Output should look like WKT line, eg:

LINESTRING(3 4,10 50,20 25)

Mysql has its own GIS conversion functions to convert data stored in its internal geometry datatypes to textual representation, such as WKT.

Specifically, use ST_AsWKT(g) or ST_AsText(g) functions to convert data to WKT format:

Converts a value in internal geometry format to its WKT representation and returns the string result.

 mysql> SET @g = 'LineString(1 1,2 2,3 3)'; mysql> SELECT ST_AsText(ST_GeomFromText(@g)); +--------------------------------+ | ST_AsText(ST_GeomFromText(@g)) | +--------------------------------+ | LINESTRING(1 1,2 2,3 3) | +--------------------------------+ 

ST_AsText(), ST_AsWKT(), AsText(), and AsWKT() are synonyms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM