2011-07-22 29 views
7

私はMySQLにWKT POLYGON値の列を持っています(私はdbを継承しました)。ポリゴンはクエリされ、Googleマップでレンダリングされます。 Google Mapsポリゴンオーバーレイにはポイントの配列が必要なため、以前のユーザーはWKT値を座標ペアに変換して別の列に格納しました。これは実際にはうまくいくが、十分にはうまくいかない。MySQLのWKTポリゴンをGoogleマップのポリゴンに変換する

1つは、変換が間違っていることがあり、2つの場合、これを高速化する方法を探しています。

Re。最初の問題は、私はこれを再実装しなければならず、WKTポリゴンを座標列に変換するコンバータを探しています。私はこれを使って、WKT列を照会し、Google Mapsのポリゴンに簡単に変換できるJSONテキストの文字列を吐き出すか、すべてのWKTポリゴンを前処理してテキストとして保存することができると考えていますそれはすでに行われていますが、今回は正しい値で終わります。

だから、私は本当にちょっとAsStringOfCoords()は私のカスタム関数になりそう

SELECT AsStringOfCoords(WKT_Column) FROM table WHERE condition 

のように、その構成点座標の文字列にWKTを変換する機能を探しています。

+0

WKT POLYGON列のサンプルを表示できますか?いくつかの変換を試みるテキスト –

+0

...これを変換する実際のストアドプロシージャかもしれないし、バグかもしれない。 –

+0

私はgmapsにmysqlからPolygonsを読まなければなりませんでした。私の解決策はポリゴンをテキストとして照会し、cogeグループをregex(内側の円...)でフィルタリングすることでした。ですから、私のクエリは "select poly_column as text ..."のようになります。 – Tarsis

答えて

0

私はKMLポリゴンにMySQL WKTポリゴンを行うための少しのC++プログラムを書いていました。次のように この事は動作します:

  • は、データベース
  • から情報を読み取る情報を並べ替えて、ファイルにそれをプリントアウトKMLドキュメント
  • を作成します。
  • あなたはgooglemapsに新しいkmlを呼び出すことができます。

ソースコードはここにある...

#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のストアドprocに翻訳するのは簡単ですか、PHPや他の言語の中で使用してください...私はまた、ポイントで同じことをするいくつかのPHP/javascriptスクリプトを持っています。

関連する問題