将 MySQL 中的 WKT 多边形转换为 Google 地图多边形

发布于 2024-11-26 10:42:43 字数 524 浏览 0 评论 0原文

我在 MySQL 中有一列 WKT POLYGON 值(我继承了数据库)。在 Google 地图上查询并渲染多边形。由于 Google 地图多边形叠加需要点数组,因此之前的用户将 WKT 值转换为坐标对并将其存储在另一列中。这实际上效果相当好,但还不够好。

其一,转换偶尔会出现错误;其二,我正在寻找加快转换速度的方法。

关于。第一个问题,我必须重新实现它,并且正在寻找一个将 WKT 多边形转换为坐标字符串的转换器。我想我可以用它来编写一个存储过程来查询 WKT 列并吐出一串可以轻松转换为 Google 地图多边形的 JSON 文本,或者甚至预处理所有 WKT 多边形并将它们存储为文本,例如它已经完成了,但这次使用了正确的值。

所以,我真的在寻找一个函数来将 WKT 转换为其组成点坐标的字符串,有点像

SELECT AsStringOfCoords(WKT_Column) FROM table WHERE condition

AsStringOfCoords() 是我的自定义函数。

I have a column of WKT POLYGON values in MySQL (I inherited the db). The polys are queried and rendered on Google Maps. Since Google Maps polygon overlay requires an array of points, the previous user converted the WKT values to coordinate pairs and stored them in another column. This actually works rather well, but not well enough.

For one, the conversion was occasionally faulty, and for two, I am looking for ways to make this faster.

Re. the first issue, I have to re-implement this, and am looking for a converter that will convert a WKT poly into a string of coordinates. I am thinking I could use this to either write a stored procedure that will query the WKT column and spit out a string of JSON text that could be readily converted to Google Maps polys, or even preprocess all the WKT polys and store them as text like it is already done, but this time with correct values.

So, I am really looking for a function to convert WKT to a string of its constituent point coordinates, kinda like so

SELECT AsStringOfCoords(WKT_Column) FROM table WHERE condition

where AsStringOfCoords() would be my custom function.

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

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

发布评论

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

评论(1

心安伴我暖 2024-12-03 10:42:43

我编写了一个小 C++ 程序来将 MySQL WKT 多边形转换为 KML 多边形。
这个东西的工作原理如下:

  • 从数据库中读取信息
  • 创建一个kml文档
  • 重新排列信息并将其打印到文件中。
  • 您可以将新的 KML 调用到 googlemaps,它显示得非常好。

源代码在这里...

#include <iostream>
#include <string>
/*
* Database includes...
*/
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

#include "../iolib/IOCoreFuncs.h"
#include "../iolib/ioconfigurador.h"

using namespace std;
using namespace sql;
using namespace IOCore;

sql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre);

int main(int argc, char **argv) {
    string            qry, arproc;
    Connection         * dbCon;
    Statement        * stmt;
    IOConfigurador    * miConf;
    ResultSet        * rs;
    //Cargar configuración...
    if (argc == 3) {
        arproc = argv[2];
    } else {
        cout << "Using mode: sqltokml <polygon id> <file kml to export>\n";
        return 1;
    }

    dbCon = conectaDB("dbserver", "dbuser"), "dbpasswd", "dbname");
    stmt = dbCon->createStatement();
    qry = "SELECT name, astext(geoarea) from " + "table name" + " where id = '" + argv[1] + "';";
    rs = stmt->executeQuery(qry);
    if (rs->rowsCount() > 0) {
        string polnombre, polcoords;
        string salida;
        while (rs->next()) {
            ofstream sale;
            polnombre = rs->getString(1);
            polcoords = rs->getString(2);
            salida = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"
            "<Document>\n"
            "<name>" + polnombre + ".kml</name>\n"
            "<Style id=\"sh_ylw-pushpin3\">\n"
                "<IconStyle>\n"
                    "<scale>1.3</scale>\n"
                    "<Icon>\n"
                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n"
                    "</Icon>\n"
                    "<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n"
                "</IconStyle>\n"
                "<LineStyle>\n"
                    "<color>467f5500</color>\n"
                    "<width>3</width>\n"
                "</LineStyle>\n"
                "<PolyStyle>\n"
                    "<color>46ff5555</color>\n"
                "</PolyStyle>\n"
            "</Style>\n"
            "<StyleMap id=\"msn_ylw-pushpin10\">\n"
                "<Pair>\n"
                    "<key>normal</key>\n"
                    "<styleUrl>#sn_ylw-pushpin30</styleUrl>\n"
                "</Pair>\n"
                "<Pair>\n"
                    "<key>highlight</key>\n"
                    "<styleUrl>#sh_ylw-pushpin3</styleUrl>\n"
                "</Pair>\n"
            "</StyleMap>\n"
            "<Style id=\"sn_ylw-pushpin30\">\n"
                "<IconStyle>\n"
                    "<scale>1.1</scale>\n"
                    "<Icon>\n"
                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n"
                    "</Icon>\n"
                    "<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n"
                "</IconStyle>\n"
                "<LineStyle>\n"
                    "<color>467f5500</color>\n"
                    "<width>3</width>\n"
                "</LineStyle>\n"
                "<PolyStyle>\n"
                    "<color>46ff5555</color>\n"
                "</PolyStyle>\n"
            "</Style>\n"
            "<Folder>\n"
                "<name>" + polnombre + "</name>\n"
                "<Placemark>\n"
                    "<name>" + polnombre + "</name>\n"
                    "<styleUrl>#msn_ylw-pushpin10</styleUrl>\n"
                    "<Polygon>\n"
                        "<tessellate>1</tessellate>\n"
                            "<outerBoundaryIs>\n"
                                "<LinearRing>\n"
                                    "<coordinates>\n";
            //Coordinates tranformation...
            polcoords = polcoords.substr(9, polcoords.size() - 11);
            vector< string > lascoords = split(polcoords, ",");
            for (unsigned i = 0; i < lascoords.size(); i++) {
                salida += lascoords[i].substr(0, lascoords[i].find(" ")) + ",";
                salida += lascoords[i].substr(lascoords[i].find(" ") + 1) + ",0 ";
            }
            salida +=                "\n</coordinates>\n" 
                                "</LinearRing>\n" 
                            "</outerBoundaryIs>\n" 
                        "</Polygon>\n" 
                    "</Placemark>\n" 
                "</Folder>\n" 
            "</Document>\n" 
            "</kml>";
            sale.open(arproc.c_str(), ios::out | ios::app);
            sale << salida ;
            sale.close();
        }
    }
    rs->close();
    stmt->close();
    dbCon->close();

}

sql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre)
{
    sql::Connection * retval;
    sql::Driver *ctrl;

    try {
        ctrl = get_driver_instance();
        retval = ctrl->connect(dbSvr, dbUsr, dbPwd);
        retval->setSchema(dbNombre);
    } catch (sql::SQLException &err) {
        cout<<"Errors... :( "<<err.what()<<"\ngoing out\n";
        retval = 0;
    }
    return retval;
}

我希望这可以帮助你。很容易将其转换为 MySQL 存储过程,或者在 PHP 或其他语言中使用它......我也有一些 php/javascript 脚本来用点做同样的事情。

I'd wrote a little C++ program to do MySQL WKT polygons to KML polygons.
This thing works as follows:

  • Read the information from the database
  • create a kml document
  • Rearrange the information and print it out to the file.
  • You can call the new kml to googlemaps and it shows pretty nice.

The source code is here...

#include <iostream>
#include <string>
/*
* Database includes...
*/
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

#include "../iolib/IOCoreFuncs.h"
#include "../iolib/ioconfigurador.h"

using namespace std;
using namespace sql;
using namespace IOCore;

sql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre);

int main(int argc, char **argv) {
    string            qry, arproc;
    Connection         * dbCon;
    Statement        * stmt;
    IOConfigurador    * miConf;
    ResultSet        * rs;
    //Cargar configuración...
    if (argc == 3) {
        arproc = argv[2];
    } else {
        cout << "Using mode: sqltokml <polygon id> <file kml to export>\n";
        return 1;
    }

    dbCon = conectaDB("dbserver", "dbuser"), "dbpasswd", "dbname");
    stmt = dbCon->createStatement();
    qry = "SELECT name, astext(geoarea) from " + "table name" + " where id = '" + argv[1] + "';";
    rs = stmt->executeQuery(qry);
    if (rs->rowsCount() > 0) {
        string polnombre, polcoords;
        string salida;
        while (rs->next()) {
            ofstream sale;
            polnombre = rs->getString(1);
            polcoords = rs->getString(2);
            salida = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"
            "<Document>\n"
            "<name>" + polnombre + ".kml</name>\n"
            "<Style id=\"sh_ylw-pushpin3\">\n"
                "<IconStyle>\n"
                    "<scale>1.3</scale>\n"
                    "<Icon>\n"
                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n"
                    "</Icon>\n"
                    "<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n"
                "</IconStyle>\n"
                "<LineStyle>\n"
                    "<color>467f5500</color>\n"
                    "<width>3</width>\n"
                "</LineStyle>\n"
                "<PolyStyle>\n"
                    "<color>46ff5555</color>\n"
                "</PolyStyle>\n"
            "</Style>\n"
            "<StyleMap id=\"msn_ylw-pushpin10\">\n"
                "<Pair>\n"
                    "<key>normal</key>\n"
                    "<styleUrl>#sn_ylw-pushpin30</styleUrl>\n"
                "</Pair>\n"
                "<Pair>\n"
                    "<key>highlight</key>\n"
                    "<styleUrl>#sh_ylw-pushpin3</styleUrl>\n"
                "</Pair>\n"
            "</StyleMap>\n"
            "<Style id=\"sn_ylw-pushpin30\">\n"
                "<IconStyle>\n"
                    "<scale>1.1</scale>\n"
                    "<Icon>\n"
                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n"
                    "</Icon>\n"
                    "<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n"
                "</IconStyle>\n"
                "<LineStyle>\n"
                    "<color>467f5500</color>\n"
                    "<width>3</width>\n"
                "</LineStyle>\n"
                "<PolyStyle>\n"
                    "<color>46ff5555</color>\n"
                "</PolyStyle>\n"
            "</Style>\n"
            "<Folder>\n"
                "<name>" + polnombre + "</name>\n"
                "<Placemark>\n"
                    "<name>" + polnombre + "</name>\n"
                    "<styleUrl>#msn_ylw-pushpin10</styleUrl>\n"
                    "<Polygon>\n"
                        "<tessellate>1</tessellate>\n"
                            "<outerBoundaryIs>\n"
                                "<LinearRing>\n"
                                    "<coordinates>\n";
            //Coordinates tranformation...
            polcoords = polcoords.substr(9, polcoords.size() - 11);
            vector< string > lascoords = split(polcoords, ",");
            for (unsigned i = 0; i < lascoords.size(); i++) {
                salida += lascoords[i].substr(0, lascoords[i].find(" ")) + ",";
                salida += lascoords[i].substr(lascoords[i].find(" ") + 1) + ",0 ";
            }
            salida +=                "\n</coordinates>\n" 
                                "</LinearRing>\n" 
                            "</outerBoundaryIs>\n" 
                        "</Polygon>\n" 
                    "</Placemark>\n" 
                "</Folder>\n" 
            "</Document>\n" 
            "</kml>";
            sale.open(arproc.c_str(), ios::out | ios::app);
            sale << salida ;
            sale.close();
        }
    }
    rs->close();
    stmt->close();
    dbCon->close();

}

sql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre)
{
    sql::Connection * retval;
    sql::Driver *ctrl;

    try {
        ctrl = get_driver_instance();
        retval = ctrl->connect(dbSvr, dbUsr, dbPwd);
        retval->setSchema(dbNombre);
    } catch (sql::SQLException &err) {
        cout<<"Errors... :( "<<err.what()<<"\ngoing out\n";
        retval = 0;
    }
    return retval;
}

I hope this can help you. Is easy to translate this to MySQL stored proc, or use it inside PHP or another languages... I also have some php/javascript scripts to do the same things with points.

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