⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qgsspit.cpp

📁 一个非常好的GIS开源新版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
void QgsSpit::removeAllFiles(){  int i = tblShapefiles->rowCount() - 1;  for (; i >= 0; --i)    tblShapefiles->removeRow(i);  fileList.clear();  total_features = 0;}void QgsSpit::useDefaultSrid(){  if ( chkUseDefaultSrid->isChecked() )  {    defaultSridValue = spinSrid->value();    spinSrid->setValue( defSrid );    spinSrid->setEnabled( false );  }  else  {    spinSrid->setEnabled( true );    spinSrid->setValue( defaultSridValue );  }}void QgsSpit::useDefaultGeom(){  if ( chkUseDefaultGeom->isChecked() )  {    defaultGeomValue = txtGeomName->text();    txtGeomName->setText( defGeom );    txtGeomName->setEnabled( false );  }  else  {    txtGeomName->setEnabled( true );    txtGeomName->setText( defaultGeomValue );  }}// TODO: make translation of helpinfovoid QgsSpit::helpInfo(){  QString message = tr("General Interface Help:") + "\n\n";  message += QString(    tr("PostgreSQL Connections:") + "\n" ) + QString(    "----------------------------------------------------------------------------------------\n" ) + QString(    tr("[New ...] - create a new connection") + "\n" ) + QString(    tr("[Edit ...] - edit the currently selected connection") + "\n" ) + QString(    tr("[Remove] - remove the currently selected connection") + "\n" ) + QString(    tr("-you need to select a connection that works (connects properly) in order to import files") + "\n" ) + QString(    tr("-when changing connections Global Schema also changes accordingly") + "\n\n" ) + QString(    tr("Shapefile List:") + "\n" ) + QString(    "----------------------------------------------------------------------------------------\n" ) + QString(    tr("[Add ...] - open a File dialog and browse to the desired file(s) to import") + "\n" ) + QString(    tr("[Remove] - remove the currently selected file(s) from the list") + "\n" ) + QString(    tr("[Remove All] - remove all the files in the list") + "\n" ) + QString(    tr("[SRID] - Reference ID for the shapefiles to be imported") + "\n" ) + QString(    tr("[Use Default (SRID)] - set SRID to -1") + "\n" ) + QString(    tr("[Geometry Column Name] - name of the geometry column in the database") + "\n" ) + QString(    tr("[Use Default (Geometry Column Name)] - set column name to \'the_geom\'") + "\n" ) + QString(    tr("[Glogal Schema] - set the schema for all files to be imported into") + "\n\n" ) + QString(    "----------------------------------------------------------------------------------------\n" ) + QString(    tr("[Import] - import the current shapefiles in the list") + "\n" ) + QString(    tr("[Quit] - quit the program\n") ) + QString(    tr("[Help] - display this help dialog") + "\n\n" );  QgsMessageViewer * e = new QgsMessageViewer( this );  e->setMessageAsPlainText( message );  e->exec(); // deletes itself on close}PGconn* QgsSpit::checkConnection(){  QSettings settings;  PGconn * pd;  bool result = true;  QString connName = cmbConnections->currentText();  if ( connName.isEmpty() )  {    QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );    result = false;  }  else  {    QgsDataSourceURI uri;    uri.setConnection( settings.readEntry( gl_key + connName + "/host" ),      settings.readEntry( gl_key + connName + "/port" ),      settings.readEntry( gl_key + connName + "/database" ),      settings.readEntry( gl_key + connName + "/username" ),      settings.readEntry( gl_key + connName + "/password" ) );    pd = PQconnectdb( ( const char * ) uri.connInfo() );    if ( PQstatus( pd ) != CONNECTION_OK )    {      QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );      result = false;    }    int errcode=PQsetClientEncoding(pd, QString("UNICODE").toLocal8Bit());    if(errcode==0)     {      QgsDebugMsg("encoding successfully set");    }     else if(errcode==-1)     {      QgsDebugMsg("error in setting encoding");    }     else     {      QgsDebugMsg("undefined return value from encoding setting");    }  }  if (result )  {    // Check that the database actually has postgis in it.    QString sql1 = "SELECT postgis_lib_version()"; // available from v 0.9.0 onwards    QString sql2 = "SELECT postgis_version()"; // depreciated     PGresult* ver = PQexec(pd, sql1.toUtf8() );    if ( PQresultStatus(ver) != PGRES_TUPLES_OK)    {      // In case the version of postgis is older than 0.9.0, try the      // depreciated call before erroring out.      PQclear(ver);      ver = PQexec(pd, sql2.toUtf8() );      if ( PQresultStatus(ver) != PGRES_TUPLES_OK)      {        QMessageBox::warning( this, tr("PostGIS not available"),          tr("<p>The chosen database does not have PostGIS installed, "          "but this is required for storage of spatial data.</p>"));        return NULL;      }    }    return pd;  }  else    return NULL;}void QgsSpit::getSchema(){  QSettings settings;  schema_list.clear();  schema_list << "public";  PGconn* pd = checkConnection();  if ( pd != NULL )  {    QString connName = cmbConnections->currentText();    QString user = settings.readEntry( gl_key + connName + "/username" );    QString schemaSql = QString( "select nspname from pg_namespace,pg_user where nspowner=usesysid and usename=%1" )                          .arg( QgsPgUtil::quotedValue(user) );    PGresult *schemas = PQexec( pd, schemaSql.toUtf8() );    // get the schema names    if ( PQresultStatus( schemas ) == PGRES_TUPLES_OK )    {      for ( int i = 0; i < PQntuples( schemas ); i++ )      {        if ( QString( PQgetvalue( schemas, i, 0 ) ) != "public" )          schema_list << QString( PQgetvalue( schemas, i, 0 ) );      }    }    PQclear( schemas );  }  PQfinish(pd);  // install a new delegate with an updated schema list (rather than  // update the existing delegate because delegates don't seem able to  // store modifiable data).   ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);  tblShapefiles->setItemDelegate(delegate);  cmbSchema->clear();  cmbSchema->insertItems( 0, schema_list );  cmbSchema->setCurrentIndex( 0 ); // index 0 is always "public"}void QgsSpit::updateSchema(){  // install a new delegate with an updated schema list (rather than  // update the existing delegate because delegates don't seem able to  // store modifiable data).   ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);  tblShapefiles->setItemDelegate(delegate);}void QgsSpit::import(){  QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems();  for (int i = 0; i < selected.count(); ++i)    selected[i]->setSelected(false);  QString connName = cmbConnections->currentText();  QSettings settings;  bool canceled = false;  PGconn* pd = checkConnection();  QString query;  if ( total_features == 0 )  {    QMessageBox::warning( this, tr("Import Shapefiles"),       tr("You need to add shapefiles to the list first") );  }  else if ( pd != NULL )  {    PGresult * res;    QProgressDialog pro( tr("Importing files"), tr("Cancel"),       0, total_features, this);    pro.setValue( 0 );    pro.setLabelText(tr("Progress"));    pro.setAutoClose( true );    //pro->show();    qApp->processEvents();    int count=fileList.size(), successes=0;    for ( std::vector<QgsShapeFile*>::size_type i = 0; i < fileList.size() ; i++ )    {      QString error = tr("Problem inserting features from file:") + "\n" +         tblShapefiles->item( i, ColFILENAME )->text();      // if a name starts with invalid character      if ( ! tblShapefiles->item( i, ColDBRELATIONNAME )->text()[ 0 ].isLetter() )      {        QMessageBox::warning( &pro, tr("Import Shapefiles"),           error + "\n" + tr("Invalid table name.") );        pro.setValue( pro.value()           + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );        continue;      }      // if no fields detected      if ( ( fileList[ i ] ->column_names ).size() == 0 )      {        QMessageBox::warning( &pro, tr("Import Shapefiles"),           error + "\n" + tr("No fields detected.") );        pro.setValue( pro.value() +           tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );        continue;      }      // duplicate field check      std::vector<QString> names_copy = fileList[ i ] ->column_names;      names_copy.push_back( txtPrimaryKeyName->text() );      names_copy.push_back( txtGeomName->text() );      QString dupl = "";      std::sort( names_copy.begin(), names_copy.end() );      for ( std::vector<QString>::size_type k = 1; k < names_copy.size(); k++ )      {        QgsDebugMsg( tr("Checking to see if ") + names_copy[ k ] + " == " + names_copy[ k - 1 ] );        if ( names_copy[ k ] == names_copy[ k - 1 ] )          dupl += names_copy[ k ] + "\n";      }      // if duplicate field names exist      if ( dupl != "" )      {        QMessageBox::warning( &pro, tr("Import Shapefiles"), error +          "\n" + tr("The following fields are duplicates:") + "\n" + dupl );        pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );        continue;      }      // Check and set destination table       fileList[ i ] ->setTable( tblShapefiles->item( i, ColDBRELATIONNAME )->text() );      pro.setLabelText( tr("Importing files") + "\n"         + tblShapefiles->item(i, ColFILENAME)->text() );      bool rel_exists1 = false;      bool rel_exists2 = false;      query = QString("SELECT f_table_name FROM geometry_columns WHERE f_table_name=%1 AND f_table_schema=%2")                .arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text()) )                .arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text()) );      res = PQexec( pd, query.toUtf8() );      rel_exists1 = ( PQntuples( res ) > 0 );      if ( PQresultStatus( res ) != PGRES_TUPLES_OK )      {        QString err = PQresultErrorMessage( res );        QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +          tr("<p>Error while executing the SQL:</p><p>") +          query + tr("</p><p>The database said:") +          err + "</p>" );        pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );        PQclear(res);        continue;      }      PQclear( res );      query = QString("SELECT tablename FROM pg_tables WHERE tablename=%1  AND schemaname=%2")                .arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) )                .arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text() ) );      res = PQexec( pd, query.toUtf8() );      rel_exists2 = ( PQntuples( res ) > 0 );      if ( PQresultStatus( res ) != PGRES_TUPLES_OK )      {        QString err = PQresultErrorMessage( res );        QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +          tr("<p>Error while executing the SQL:</p><p>") +          query + tr("</p><p>The database said:") +          err + "</p>" );        pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -