2011-07-22 133 views
7

我在MySQL中有一列WKT POLYGON值(我繼承了db)。在Google地圖上查詢和呈現多邊形。由於Google地圖多邊形疊加需要一系列點,因此以前的用戶將WKT值轉換爲座標對並將它們存儲在另一列中。這實際上工作得很好,但不夠好。將MySQL中的WKT多邊形轉換爲Google Maps多邊形

其中之一,轉換偶爾會出現故障,對於兩個,我正在尋找方法使其更快。

Re。第一個問題,我不得不重新實現這個,並且正在尋找一個轉換器,它將把WKT poly轉換成一串座標。我想我可以使用它來編寫一個存儲過程,它將查詢WKT列,並吐出一串JSON文本,可以很容易地轉換成Google Maps多邊形,甚至預處理所有的WKT多邊形並將它們存儲爲文本它已經完成了,但是這次有了正確的值。

所以,我真的找對WKT轉換成它的組成點座標的字符串函數,還挺像那麼

SELECT AsStringOfCoords(WKT_Column) FROM table WHERE condition 

其中AsStringOfCoords()將是我的自定義功能。

+0

您可以顯示您的WKT POLYGON色譜柱的樣品嗎?一些文字嘗試一些轉換 –

+0

...並可能是實際的存儲過程來轉換此,可能是它有一個錯誤。 –

+0

我還必須將多邊形從mysql讀入gmaps中 - 我的解決方案是將多邊形作爲文本進行查詢,然後通過正則表達式(內部圓圈......)過濾掉座標組。所以我的查詢就像「SELECT poly_column as text ...」。 – Tarsis

回答

0

我寫了一個小小的C++程序來做KML多邊形的MySQL WKT多邊形。 這件事的工作原理如下:

  • 從數據庫
  • 閱讀信息創建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腳本可以用點來做同樣的事情。