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

pun*_*ish 7 mysql google-maps wkt

我在MySQL中有一列WKT POLYGON值(我继承了db).在Google地图上查询和渲染多边形.由于Google地图多边形叠加需要一个点数组,因此前一个用户将WKT值转换为坐标对并将它们存储在另一列中.这实际上运作得相当好,但还不够好.

首先,转换有时是错误的,而对于两个,我正在寻找方法来加快速度.

回覆.第一个问题,我必须重新实现这个,我正在寻找一个转换器,将WKT多边形转换为一串坐标.我想我可以使用它来编写一个存储过程来查询WKT列并吐出一串JSON文本,这些文本可以很容易地转换为Google Maps多边形,甚至可以预处理所有WKT多边形并将它们存储为文本它已经完成,但这次是正确的值.

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

SELECT AsStringOfCoords(WKT_Column) FROM table WHERE condition
Run Code Online (Sandbox Code Playgroud)

哪里AsStringOfCoords()是我的自定义功能.

小智 0

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

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

源代码在这里...

\n\n
#include <iostream>\n#include <string>\n/*\n* Database includes...\n*/\n#include <mysql_connection.h>\n#include <cppconn/driver.h>\n#include <cppconn/exception.h>\n#include <cppconn/resultset.h>\n#include <cppconn/statement.h>\n#include <cppconn/prepared_statement.h>\n\n#include "../iolib/IOCoreFuncs.h"\n#include "../iolib/ioconfigurador.h"\n\nusing namespace std;\nusing namespace sql;\nusing namespace IOCore;\n\nsql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre);\n\nint main(int argc, char **argv) {\n    string            qry, arproc;\n    Connection         * dbCon;\n    Statement        * stmt;\n    IOConfigurador    * miConf;\n    ResultSet        * rs;\n    //Cargar configuraci\xc3\xb3n...\n    if (argc == 3) {\n        arproc = argv[2];\n    } else {\n        cout << "Using mode: sqltokml <polygon id> <file kml to export>\\n";\n        return 1;\n    }\n\n    dbCon = conectaDB("dbserver", "dbuser"), "dbpasswd", "dbname");\n    stmt = dbCon->createStatement();\n    qry = "SELECT name, astext(geoarea) from " + "table name" + " where id = \'" + argv[1] + "\';";\n    rs = stmt->executeQuery(qry);\n    if (rs->rowsCount() > 0) {\n        string polnombre, polcoords;\n        string salida;\n        while (rs->next()) {\n            ofstream sale;\n            polnombre = rs->getString(1);\n            polcoords = rs->getString(2);\n            salida = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n"\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"\n            "<Document>\\n"\n            "<name>" + polnombre + ".kml</name>\\n"\n            "<Style id=\\"sh_ylw-pushpin3\\">\\n"\n                "<IconStyle>\\n"\n                    "<scale>1.3</scale>\\n"\n                    "<Icon>\\n"\n                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\\n"\n                    "</Icon>\\n"\n                    "<hotSpot x=\\"20\\" y=\\"2\\" xunits=\\"pixels\\" yunits=\\"pixels\\"/>\\n"\n                "</IconStyle>\\n"\n                "<LineStyle>\\n"\n                    "<color>467f5500</color>\\n"\n                    "<width>3</width>\\n"\n                "</LineStyle>\\n"\n                "<PolyStyle>\\n"\n                    "<color>46ff5555</color>\\n"\n                "</PolyStyle>\\n"\n            "</Style>\\n"\n            "<StyleMap id=\\"msn_ylw-pushpin10\\">\\n"\n                "<Pair>\\n"\n                    "<key>normal</key>\\n"\n                    "<styleUrl>#sn_ylw-pushpin30</styleUrl>\\n"\n                "</Pair>\\n"\n                "<Pair>\\n"\n                    "<key>highlight</key>\\n"\n                    "<styleUrl>#sh_ylw-pushpin3</styleUrl>\\n"\n                "</Pair>\\n"\n            "</StyleMap>\\n"\n            "<Style id=\\"sn_ylw-pushpin30\\">\\n"\n                "<IconStyle>\\n"\n                    "<scale>1.1</scale>\\n"\n                    "<Icon>\\n"\n                        "<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\\n"\n                    "</Icon>\\n"\n                    "<hotSpot x=\\"20\\" y=\\"2\\" xunits=\\"pixels\\" yunits=\\"pixels\\"/>\\n"\n                "</IconStyle>\\n"\n                "<LineStyle>\\n"\n                    "<color>467f5500</color>\\n"\n                    "<width>3</width>\\n"\n                "</LineStyle>\\n"\n                "<PolyStyle>\\n"\n                    "<color>46ff5555</color>\\n"\n                "</PolyStyle>\\n"\n            "</Style>\\n"\n            "<Folder>\\n"\n                "<name>" + polnombre + "</name>\\n"\n                "<Placemark>\\n"\n                    "<name>" + polnombre + "</name>\\n"\n                    "<styleUrl>#msn_ylw-pushpin10</styleUrl>\\n"\n                    "<Polygon>\\n"\n                        "<tessellate>1</tessellate>\\n"\n                            "<outerBoundaryIs>\\n"\n                                "<LinearRing>\\n"\n                                    "<coordinates>\\n";\n            //Coordinates tranformation...\n            polcoords = polcoords.substr(9, polcoords.size() - 11);\n            vector< string > lascoords = split(polcoords, ",");\n            for (unsigned i = 0; i < lascoords.size(); i++) {\n                salida += lascoords[i].substr(0, lascoords[i].find(" ")) + ",";\n                salida += lascoords[i].substr(lascoords[i].find(" ") + 1) + ",0 ";\n            }\n            salida +=                "\\n</coordinates>\\n" \n                                "</LinearRing>\\n" \n                            "</outerBoundaryIs>\\n" \n                        "</Polygon>\\n" \n                    "</Placemark>\\n" \n                "</Folder>\\n" \n            "</Document>\\n" \n            "</kml>";\n            sale.open(arproc.c_str(), ios::out | ios::app);\n            sale << salida ;\n            sale.close();\n        }\n    }\n    rs->close();\n    stmt->close();\n    dbCon->close();\n\n}\n\nsql::Connection * conectaDB(string dbSvr, string dbUsr, string dbPwd, string dbNombre)\n{\n    sql::Connection * retval;\n    sql::Driver *ctrl;\n\n    try {\n        ctrl = get_driver_instance();\n        retval = ctrl->connect(dbSvr, dbUsr, dbPwd);\n        retval->setSchema(dbNombre);\n    } catch (sql::SQLException &err) {\n        cout<<"Errors... :( "<<err.what()<<"\\ngoing out\\n";\n        retval = 0;\n    }\n    return retval;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n