QgsVectorLayer* QgsSentDataSourceBuilder::vectorLayerFromSentVDS( const QDomElement& sentVDSElem, QList<QTemporaryFile*>& filesToRemove, QList<QgsMapLayer*>& layersToRemove ) const { if ( sentVDSElem.attribute( "format" ) == "GML" ) { QTemporaryFile* tmpFile = new QTemporaryFile(); if ( tmpFile->open() ) { filesToRemove.push_back( tmpFile ); //make sure the temporary file gets deleted after each request QTextStream tempFileStream( tmpFile ); sentVDSElem.save( tempFileStream, 4 ); tmpFile->close(); } else { return 0; } QgsVectorLayer* theVectorLayer = new QgsVectorLayer( tmpFile->fileName(), layerNameFromUri( tmpFile->fileName() ), "WFS" ); if ( !theVectorLayer || !theVectorLayer->isValid() ) { QgsMSDebugMsg( "invalid maplayer" ); return 0; } QgsMSDebugMsg( "returning maplayer" ); layersToRemove.push_back( theVectorLayer ); //make sure the layer gets deleted after each request if ( !theVectorLayer || !theVectorLayer->isValid() ) { return 0; } return theVectorLayer; } return 0; }
void SaSourceSelect::setSql( const QModelIndex &index ) { if ( !index.parent().isValid() ) { SaDebugMsg( "schema item found" ); return; } QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( mProxyModel.mapToSource( index ) ), "querybuilder", "sqlanywhere" ); if ( !vlayer->isValid() ) { delete vlayer; return; } // create a query builder object SaQueryBuilder *qb = new SaQueryBuilder( vlayer, this ); if ( qb->exec() ) { mTableModel.setSql( mProxyModel.mapToSource( index ), qb->sql() ); } delete qb; delete vlayer; }
void TestQgsHistogram::fromLayer() { QgsHistogram h; QVERIFY( !h.setValues( 0, QString() ) ); QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=col1:real" ), QStringLiteral( "layer" ), QStringLiteral( "memory" ) ); QVERIFY( layer->isValid() ); QgsFeatureList features; for ( int i = 1; i <= 10; ++i ) { QgsFeature f( layer->dataProvider()->fields(), i ); f.setAttribute( QStringLiteral( "col1" ), i ); features << f; } layer->dataProvider()->addFeatures( features ); QVERIFY( !h.setValues( layer, QString() ) ); QVERIFY( h.setValues( layer, QString( "col1" ) ) ); QList<int>counts = h.counts( 5 ); QList<int> expected; expected << 2 << 2 << 2 << 2 << 2; QCOMPARE( counts, expected ); delete layer; }
QgsVectorLayer* QgsRemoteDataSourceBuilder::vectorLayerFromRemoteVDS( const QDomElement& remoteVDSElem, const QString& layerName, QList<QTemporaryFile*>& filesToRemove, QList<QgsMapLayer*>& layersToRemove, bool allowCaching ) const { QString providerString; QString formatString = remoteVDSElem.attribute( "format" ); if ( formatString.compare( "gml", Qt::CaseInsensitive ) == 0 ) { providerString = "WFS"; } else { providerString = formatString; } //load file with QgsHttpTransaction QByteArray fileContents; QString uri = remoteVDSElem.text(); QgsVectorLayer* vl = 0; if ( loadData( uri, fileContents ) != 0 ) { return 0; } //store content into temporary file QTemporaryFile* tmpFile = new QTemporaryFile(); if ( tmpFile->open() ) { tmpFile->write( fileContents ); tmpFile->flush(); } else { delete tmpFile; return 0; } //create vector layer //SOS has a special datasource key... if ( formatString.compare( "SOS", Qt::CaseInsensitive ) == 0 ) { QString url = "url=" + tmpFile->fileName() + " method=FILE xml="; vl = new QgsVectorLayer( url, layerNameFromUri( tmpFile->fileName() ), providerString ); } else { vl = new QgsVectorLayer( tmpFile->fileName(), layerNameFromUri( tmpFile->fileName() ), providerString ); } if ( !( vl->isValid() ) ) { QgsMapServerLogger::instance()->printMessage( "vl is not valid" ); } layersToRemove.push_back( vl ); filesToRemove.push_back( tmpFile ); return vl; }
bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString& file ) { QList< QPair<QString, QString> > attributes; attributes.append( qMakePair( QString( "FeatureID" ), QString( "String;10;" ) ) ); attributes.append( qMakePair( QString( "ErrorDesc" ), QString( "String;80;" ) ) ); QLibrary ogrLib( QgsProviderRegistry::instance()->library( "ogr" ) ); if ( !ogrLib.load() ) { return false; } typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * ); createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) ); if ( !createEmptyDataSource ) { return false; } if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, &mFeaturePool->getLayer()->crs() ) ) { return false; } QgsVectorLayer* layer = new QgsVectorLayer( file, QFileInfo( file ).baseName(), "ogr" ); if ( !layer->isValid() ) { delete layer; return false; } int fieldFeatureId = layer->fieldNameIndex( "FeatureID" ); int fieldErrDesc = layer->fieldNameIndex( "ErrorDesc" ); for ( int row = 0, nRows = ui.tableWidgetErrors->rowCount(); row < nRows; ++row ) { QgsGeometryCheckError* error = ui.tableWidgetErrors->item( row, 0 )->data( Qt::UserRole ).value<QgsGeometryCheckError*>(); QgsFeature f( layer->pendingFields() ); f.setAttribute( fieldFeatureId, error->featureId() ); f.setAttribute( fieldErrDesc, error->description() ); f.setGeometry( new QgsGeometry( error->location().clone() ) ); layer->dataProvider()->addFeatures( QgsFeatureList() << f ); } // Remove existing layer with same uri QStringList toRemove; foreach ( QgsMapLayer* maplayer, QgsMapLayerRegistry::instance()->mapLayers() ) { if ( dynamic_cast<QgsVectorLayer*>( maplayer ) && static_cast<QgsVectorLayer*>( maplayer )->dataProvider()->dataSourceUri() == layer->dataProvider()->dataSourceUri() ) { toRemove.append( maplayer->id() ); } } if ( !toRemove.isEmpty() ) { QgsMapLayerRegistry::instance()->removeMapLayers( toRemove ); } QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << layer ); return true; }
void QgsOSMExportDialog::onOK() { if ( !openDatabase() ) return; QgsOSMDatabase::ExportType type; if ( radPoints->isChecked() ) type = QgsOSMDatabase::Point; else if ( radPolylines->isChecked() ) type = QgsOSMDatabase::Polyline; else type = QgsOSMDatabase::Polygon; buttonBox->setEnabled( false ); QApplication::setOverrideCursor( Qt::WaitCursor ); QStringList tagKeys; QStringList notNullTagKeys; for ( int i = 0; i < mTagsModel->rowCount(); ++i ) { QStandardItem* item = mTagsModel->item( i, 0 ); if ( item->checkState() == Qt::Checked ) tagKeys << item->text(); QStandardItem* item2 = mTagsModel->item( i, 2 ); if ( item2->checkState() == Qt::Checked ) notNullTagKeys << item->text(); } bool res = mDatabase->exportSpatiaLite( type, editLayerName->text(), tagKeys, notNullTagKeys ); // load the layer into canvas if that was requested if ( chkLoadWhenFinished->isChecked() ) { QgsDataSourceUri uri; uri.setDatabase( editDbFileName->text() ); uri.setDataSource( QString(), editLayerName->text(), "geometry" ); QgsVectorLayer* vlayer = new QgsVectorLayer( uri.uri(), editLayerName->text(), "spatialite" ); if ( vlayer->isValid() ) QgsMapLayerRegistry::instance()->addMapLayer( vlayer ); } QApplication::restoreOverrideCursor(); buttonBox->setEnabled( true ); if ( res ) { QMessageBox::information( this, tr( "OpenStreetMap export" ), tr( "Export has been successful." ) ); } else { QMessageBox::critical( this, tr( "OpenStreetMap export" ), tr( "Failed to export OSM data:\n%1" ).arg( mDatabase->errorString() ) ); } mDatabase->close(); }
static QgsVectorLayer* make_layer( const QStringList& wkts ) { QgsVectorLayer* vl = new QgsVectorLayer( "LineString", "x", "memory" ); Q_ASSERT( vl->isValid() ); vl->startEditing(); Q_FOREACH ( const QString& wkt, wkts ) { QgsFeature f( make_feature( wkt ) ); vl->addFeature( f, false ); }
int main(int argc, char ** argv) { // Start the Application QgsApplication app(argc, argv, true); QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; QString myLayerPath = "/home/timlinux/gisdata/brazil/BR_Cidades/"; QString myLayerBaseName = "Brasil_Cap"; QString myProviderName = "ogr"; // Instantiate Provider Registry QgsProviderRegistry::instance(myPluginsDir); // create a maplayer instance QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType()); QList <QgsMapCanvasLayer> myLayerSet; mypLayer->setRenderer(mypRenderer); if (mypLayer->isValid()) { qDebug("Layer is valid"); } else { qDebug("Layer is NOT valid"); } // Add the Vector Layer to the Layer Registry QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); // Add the Layer to the Layer Set myLayerSet.append(QgsMapCanvasLayer(mypLayer, TRUE)); // Create the Map Canvas QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0); mypMapCanvas->setExtent(mypLayer->extent()); mypMapCanvas->enableAntiAliasing(true); mypMapCanvas->setCanvasColor(QColor(255, 255, 255)); mypMapCanvas->freeze(false); // Set the Map Canvas Layer Set mypMapCanvas->setLayerSet(myLayerSet); mypMapCanvas->setVisible(true); mypMapCanvas->refresh(); // Start the Application Event Loop return app.exec(); }
int qgis_QgsVectorLayer_open(void) { QString path = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)); QString basename = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)); QString provider = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)); YAP_Term out; QgsVectorLayer *layer = new QgsVectorLayer(path, basename, provider); if (layer->isValid()) { cerr << "Layer Type: " << layer->geometryType() << endl; cerr << "Feature Count: " << layer->featureCount() << endl; return TRUE; } else { delete layer; return FALSE; } }
void QgsSpatiaLiteSourceSelect::setSql( const QModelIndex &index ) { QModelIndex idx = mProxyModel.mapToSource( index ); QString tableName = mTableModel.itemFromIndex( idx.sibling( idx.row(), 0 ) )->text(); QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( idx ), tableName, "spatialite" ); if ( !vlayer->isValid() ) { delete vlayer; return; } // create a query builder object QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this ); if ( gb->exec() ) { mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() ); } delete gb; delete vlayer; }
bool QgsNewGeoPackageLayerDialog::apply() { QString fileName( mDatabase->filePath() ); if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) ) fileName += QLatin1String( ".gpkg" ); bool createNewDb = false; if ( QFile( fileName ).exists( fileName ) ) { bool overwrite = false; switch ( mBehavior ) { case Prompt: { QMessageBox msgBox; msgBox.setIcon( QMessageBox::Question ); msgBox.setWindowTitle( tr( "The File Already Exists." ) ); msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) ); QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole ); QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole ); msgBox.setStandardButtons( QMessageBox::Cancel ); msgBox.setDefaultButton( addNewLayerButton ); bool cancel = false; if ( property( "hideDialogs" ).toBool() ) { overwrite = property( "question_existing_db_answer_overwrite" ).toBool(); if ( !overwrite ) cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool(); } else { int ret = msgBox.exec(); if ( ret == QMessageBox::Cancel ) cancel = true; if ( msgBox.clickedButton() == overwriteButton ) overwrite = true; } if ( cancel ) { return false; } break; } case Overwrite: overwrite = true; break; case AddNewLayer: overwrite = false; break; } if ( overwrite ) { QFile( fileName ).remove(); createNewDb = true; } } else { createNewDb = true; } OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" ); if ( !hGpkgDriver ) { if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), tr( "GeoPackage driver not found" ) ); return false; } gdal::ogr_datasource_unique_ptr hDS; if ( createNewDb ) { hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(), nullptr ) ); if ( !hDS ) { QString msg( tr( "Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) ); if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), msg ); return false; } } else { OGRSFDriverH hDriver = nullptr; hDS.reset( OGROpen( fileName.toUtf8().constData(), true, &hDriver ) ); if ( !hDS ) { QString msg( tr( "Opening of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) ); if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), msg ); return false; } if ( hDriver != hGpkgDriver ) { QString msg( tr( "Opening of file succeeded, but this is not a GeoPackage database" ) ); if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), msg ); return false; } } QString tableName( mTableNameEdit->text() ); bool overwriteTable = false; if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) ) { if ( property( "hideDialogs" ).toBool() ) { overwriteTable = property( "question_existing_layer_answer_overwrite" ).toBool(); } else if ( QMessageBox::question( this, tr( "Existing layer" ), tr( "A table with the same name already exists. Do you want to overwrite it?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes ) { overwriteTable = true; } if ( !overwriteTable ) { return false; } } QString layerIdentifier( mLayerIdentifierEdit->text() ); QString layerDescription( mLayerDescriptionEdit->text() ); OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType> ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() ); // z-coordinate & m-value. if ( mGeometryWithZCheckBox->isChecked() ) wkbType = OGR_GT_SetZ( wkbType ); if ( mGeometryWithMCheckBox->isChecked() ) wkbType = OGR_GT_SetM( wkbType ); OGRSpatialReferenceH hSRS = nullptr; // consider spatial reference system of the layer QgsCoordinateReferenceSystem srs = mCrsSelector->crs(); if ( wkbType != wkbNone && srs.isValid() ) { QString srsWkt = srs.toWkt(); hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() ); } // Set options char **options = nullptr; if ( overwriteTable ) options = CSLSetNameValue( options, "OVERWRITE", "YES" ); if ( !layerIdentifier.isEmpty() ) options = CSLSetNameValue( options, "IDENTIFIER", layerIdentifier.toUtf8().constData() ); if ( !layerDescription.isEmpty() ) options = CSLSetNameValue( options, "DESCRIPTION", layerDescription.toUtf8().constData() ); QString featureId( mFeatureIdColumnEdit->text() ); if ( !featureId.isEmpty() ) options = CSLSetNameValue( options, "FID", featureId.toUtf8().constData() ); QString geometryColumn( mGeometryColumnEdit->text() ); if ( wkbType != wkbNone && !geometryColumn.isEmpty() ) options = CSLSetNameValue( options, "GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() ); if ( wkbType != wkbNone ) options = CSLSetNameValue( options, "SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ? "YES" : "NO" ); OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options ); CSLDestroy( options ); if ( hSRS ) OSRRelease( hSRS ); if ( !hLayer ) { QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) ); if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), msg ); return false; } QTreeWidgetItemIterator it( mAttributeView ); while ( *it ) { QString fieldName( ( *it )->text( 0 ) ); QString fieldType( ( *it )->text( 1 ) ); QString fieldWidth( ( *it )->text( 2 ) ); OGRFieldType ogrType( OFTString ); if ( fieldType == QLatin1String( "text" ) ) ogrType = OFTString; else if ( fieldType == QLatin1String( "integer" ) ) ogrType = OFTInteger; else if ( fieldType == QLatin1String( "integer64" ) ) ogrType = OFTInteger64; else if ( fieldType == QLatin1String( "real" ) ) ogrType = OFTReal; else if ( fieldType == QLatin1String( "date" ) ) ogrType = OFTDate; else if ( fieldType == QLatin1String( "datetime" ) ) ogrType = OFTDateTime; int ogrWidth = fieldWidth.toInt(); gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( fieldName.toUtf8().constData(), ogrType ) ); OGR_Fld_SetWidth( fld.get(), ogrWidth ); if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE ) { if ( !property( "hideDialogs" ).toBool() ) { QMessageBox::critical( this, tr( "Layer creation failed" ), tr( "Creation of field %1 failed (OGR error: %2)" ) .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) ); } return false; } ++it; } // In GDAL >= 2.0, the driver implements a deferred creation strategy, so // issue a command that will force table creation CPLErrorReset(); OGR_L_ResetReading( hLayer ); if ( CPLGetLastErrorType() != CE_None ) { QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) ); if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Layer creation failed" ), msg ); return false; } hDS.reset(); QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) ); QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier ); QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) ); if ( layer->isValid() ) { // register this layer with the central layers registry QList<QgsMapLayer *> myList; myList << layer; //addMapLayers returns a list of all successfully added layers //so we compare that to our original list. if ( myList == QgsProject::instance()->addMapLayers( myList ) ) return true; } else { if ( !property( "hideDialogs" ).toBool() ) QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) ); delete layer; } return false; }
void QgsBrowserLayerProperties::setItem( QgsDataItem* item ) { QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( item ); if ( !layerItem ) return; mNoticeLabel->clear(); QgsMapLayer::LayerType type = layerItem->mapLayerType(); QString layerMetadata = tr( "Error" ); QgsCoordinateReferenceSystem layerCrs; // temporarily override /Projections/defaultBehaviour to avoid dialog prompt QSettings settings; QString defaultProjectionOption = settings.value( "/Projections/defaultBehaviour", "prompt" ).toString(); if ( settings.value( "/Projections/defaultBehaviour", "prompt" ).toString() == "prompt" ) { settings.setValue( "/Projections/defaultBehaviour", "useProject" ); } // find root item // we need to create a temporary layer to get metadata // we could use a provider but the metadata is not as complete and "pretty" and this is easier QgsDebugMsg( QString( "creating temporary layer using path %1" ).arg( layerItem->path() ) ); if ( type == QgsMapLayer::RasterLayer ) { QgsDebugMsg( "creating raster layer" ); // should copy code from addLayer() to split uri ? QgsRasterLayer* layer = new QgsRasterLayer( layerItem->uri(), layerItem->uri(), layerItem->providerKey() ); if ( layer ) { if ( layer->isValid() ) { layerCrs = layer->crs(); layerMetadata = layer->metadata(); } delete layer; } } else if ( type == QgsMapLayer::VectorLayer ) { QgsDebugMsg( "creating vector layer" ); QgsVectorLayer* layer = new QgsVectorLayer( layerItem->uri(), layerItem->name(), layerItem->providerKey() ); if ( layer ) { if ( layer->isValid() ) { layerCrs = layer->crs(); layerMetadata = layer->metadata(); } delete layer; } } else if ( type == QgsMapLayer::PluginLayer ) { // TODO: support display of properties for plugin layers return; } // restore /Projections/defaultBehaviour if ( defaultProjectionOption == "prompt" ) { settings.setValue( "/Projections/defaultBehaviour", defaultProjectionOption ); } mNameLabel->setText( layerItem->name() ); mUriLabel->setText( layerItem->uri() ); mProviderLabel->setText( layerItem->providerKey() ); QString myStyle = QgsApplication::reportStyleSheet(); mMetadataTextBrowser->document()->setDefaultStyleSheet( myStyle ); mMetadataTextBrowser->setHtml( layerMetadata ); // report if layer was set to to project crs without prompt (may give a false positive) if ( defaultProjectionOption == "prompt" ) { QgsCoordinateReferenceSystem defaultCrs = QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs(); if ( layerCrs == defaultCrs ) mNoticeLabel->setText( "NOTICE: Layer srs set from project (" + defaultCrs.authid() + ')' ); } if ( mNoticeLabel->text().isEmpty() ) { mNoticeLabel->hide(); } }
bool QgsNewSpatialiteLayerDialog::apply() { // Build up the sql statement for creating the table QString sql = QString( "create table %1(" ).arg( quotedIdentifier( leLayerName->text() ) ); QString delim = ""; if ( checkBoxPrimaryKey->isChecked() ) { sql += "pkuid integer primary key autoincrement,"; } QTreeWidgetItemIterator it( mAttributeView ); while ( *it ) { sql += delim + QString( "%1 %2" ).arg( quotedIdentifier(( *it )->text( 0 ) ) ).arg(( *it )->text( 1 ) ); delim = ","; ++it; } // complete the create table statement sql += ")"; QgsDebugMsg( QString( "Creating table in database %1" ).arg( mDatabaseComboBox->currentText() ) ); QgsDebugMsg( sql ); // OK QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" ) .arg( quotedValue( leLayerName->text() ) ) .arg( quotedValue( leGeometryColumn->text() ) ) .arg( mCrsId.split( ':' ).value( 1, "0" ).toInt() ) .arg( quotedValue( selectedType() ) ); QgsDebugMsg( sqlAddGeom ); // OK QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" ) .arg( quotedValue( leLayerName->text() ) ) .arg( quotedValue( leGeometryColumn->text() ) ); QgsDebugMsg( sqlCreateIndex ); // OK sqlite3 *db; int rc = QgsSLConnect::sqlite3_open( mDatabaseComboBox->currentText().toUtf8(), &db ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "SpatiaLite Database" ), tr( "Unable to open the database: %1" ).arg( mDatabaseComboBox->currentText() ) ); } else { char * errmsg; rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating SpatiaLite Table" ), tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text() ).arg( errmsg ) ); sqlite3_free( errmsg ); } else { // create the geometry column and the spatial index rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating Geometry Column" ), tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) ); sqlite3_free( errmsg ); } else { // create the spatial index rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating Spatial Index" ), tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) ); sqlite3_free( errmsg ); } QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(%3) sql=" ) .arg( mDatabaseComboBox->currentText() ) .arg( leLayerName->text() ) .arg( leGeometryColumn->text() ), leLayerName->text(), "spatialite" ); if ( layer->isValid() ) { // register this layer with the central layers registry QList<QgsMapLayer *> myList; myList << layer; //addMapLayers returns a list of all successfully added layers //so we compare that to our original list. if ( myList == QgsMapLayerRegistry::instance()->addMapLayers( myList ) ) return true; } else { QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" ); QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) ); delete layer; } } } QgsSLConnect::sqlite3_close( db ); } return false; }
void MainWindow::addLayer() { QString myLayerPath = "../data"; QString myLayerBaseName = "test"; QString myProviderName = "ogr"; QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); if (mypLayer->isValid()) { qDebug("Layer is valid"); } else { qDebug("Layer is NOT valid"); return; } //set up a renderer for the layer QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType()); QList<QgsMapCanvasLayer> myLayerSet; mypLayer->setRenderer(mypRenderer); // //set up labelling for the layer // //get the label instance associated with the layer QgsLabel * mypLabel; mypLabel = mypLayer->label(); //and the label attributes associated with the label QgsLabelAttributes * mypLabelAttributes; mypLabelAttributes = mypLabel->layerAttributes(); //note in QGIS 1.4 and up you should use mypLabel->labelAttributes rather //get the field list associated with the layer //we'll print the names out to console for diagnostic purposes QgsFieldMap myFields = mypLayer->dataProvider()->fields(); for (unsigned int i = 0; i < myFields.size(); i++ ) { qDebug("Field Name: " + QString(myFields[i].name()).toLocal8Bit() ); } //just use the last field's name in the fields list as the label field! qDebug("set label field to " + QString(myFields[myFields.size()-1].name()).toLocal8Bit()); mypLabel->setLabelField( QgsLabel::Text, myFields.size()-1); //set the colour of the label text mypLabelAttributes->setColor(Qt::black); //create a 'halo' effect around each label so it //can still be read on dark backgrounds mypLabelAttributes->setBufferEnabled(true); mypLabelAttributes->setBufferColor(Qt::yellow); int myType = QgsLabelAttributes::PointUnits; mypLabelAttributes->setBufferSize(1,myType); /* * Here are a bunch of other things you can set based on values on a database field * the second parameter in each case would be the field name from which the * attribute can be retrieved. mypLabel->setLabelField( QgsLabel::Family, "fontFamily" ); mypLabel->setLabelField( QgsLabel::Bold, "fontIsBold" ); mypLabel->setLabelField( QgsLabel::Italic, "fontIsItalic" ); mypLabel->setLabelField( QgsLabel::Underline, "fontIsUnderlined" ); mypLabel->setLabelField( QgsLabel::Size, "fontSize" ); mypLabel->setLabelField( QgsLabel::BufferSize,"fontBufferSize" ); mypLabel->setLabelField( QgsLabel::XCoordinate, "labelX" ); mypLabel->setLabelField( QgsLabel::YCoordinate, "labelY"); mypLabel->setLabelField( QgsLabel::XOffset, "labelXOffset"); mypLabel->setLabelField( QgsLabel::YOffset, "labelYOffset"); mypLabel->setLabelField( QgsLabel::Alignment, "labelAlignment" ); mypLabel->setLabelField( QgsLabel::Angle, "labelAngle"); */ //lastly we enable labelling! mypLayer->enableLabels(true); // Add the Vector Layer to the Layer Registry QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); // Add the Layer to the Layer Set myLayerSet.append(QgsMapCanvasLayer( mypLayer ) ); // set teh canvas to the extent of our layer mpMapCanvas->setExtent(mypLayer->extent()); // Set the Map Canvas Layer Set mpMapCanvas->setLayerSet(myLayerSet); }
bool QgsPGConnectionItem::handleDrop( const QMimeData *data, const QString &toSchema ) { if ( !QgsMimeDataUtils::isUriList( data ) ) return false; // TODO: probably should show a GUI with settings etc QgsDataSourceUri uri = QgsPostgresConn::connUri( mName ); QStringList importResults; bool hasError = false; QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data ); const auto constLst = lst; for ( const QgsMimeDataUtils::Uri &u : constLst ) { // open the source layer bool owner; QString error; QgsVectorLayer *srcLayer = u.vectorLayer( owner, error ); if ( !srcLayer ) { importResults.append( tr( "%1: %2" ).arg( u.name, error ) ); hasError = true; continue; } if ( srcLayer->isValid() ) { uri.setDataSource( QString(), u.name, srcLayer->geometryType() != QgsWkbTypes::NullGeometry ? QStringLiteral( "geom" ) : QString() ); QgsDebugMsg( "URI " + uri.uri( false ) ); if ( !toSchema.isNull() ) { uri.setSchema( toSchema ); } QVariantMap options; options.insert( QStringLiteral( "forceSinglePartGeometryType" ), true ); std::unique_ptr< QgsVectorLayerExporterTask > exportTask( new QgsVectorLayerExporterTask( srcLayer, uri.uri( false ), QStringLiteral( "postgres" ), srcLayer->crs(), options, owner ) ); // when export is successful: connect( exportTask.get(), &QgsVectorLayerExporterTask::exportComplete, this, [ = ]() { // this is gross - TODO - find a way to get access to messageBar from data items QMessageBox::information( nullptr, tr( "Import to PostGIS database" ), tr( "Import was successful." ) ); refreshSchema( toSchema ); } ); // when an error occurs: connect( exportTask.get(), &QgsVectorLayerExporterTask::errorOccurred, this, [ = ]( int error, const QString & errorMessage ) { if ( error != QgsVectorLayerExporter::ErrUserCanceled ) { QgsMessageOutput *output = QgsMessageOutput::createMessageOutput(); output->setTitle( tr( "Import to PostGIS database" ) ); output->setMessage( tr( "Failed to import some layers!\n\n" ) + errorMessage, QgsMessageOutput::MessageText ); output->showMessage(); } refreshSchema( toSchema ); } ); QgsApplication::taskManager()->addTask( exportTask.release() ); } else { importResults.append( tr( "%1: Not a valid layer!" ).arg( u.name ) ); hasError = true; } } if ( hasError ) { QgsMessageOutput *output = QgsMessageOutput::createMessageOutput(); output->setTitle( tr( "Import to PostGIS database" ) ); output->setMessage( tr( "Failed to import some layers!\n\n" ) + importResults.join( QStringLiteral( "\n" ) ), QgsMessageOutput::MessageText ); output->showMessage(); } return true; }
void QgsProjectFileTransform::transform0110to1000() { if ( ! mDom.isNull() ) { QDomNodeList layerList = mDom.elementsByTagName( "maplayer" ); for ( int i = 0; i < layerList.size(); ++i ) { QDomElement layerElem = layerList.at( i ).toElement(); QString typeString = layerElem.attribute( "type" ); if ( typeString != "vector" ) { continue; } //datasource QDomNode dataSourceNode = layerElem.namedItem( "datasource" ); if ( dataSourceNode.isNull() ) { return; } QString dataSource = dataSourceNode.toElement().text(); //provider key QDomNode providerNode = layerElem.namedItem( "provider" ); if ( providerNode.isNull() ) { return; } QString providerKey = providerNode.toElement().text(); //create the layer to get the provider for int->fieldName conversion QgsVectorLayer* theLayer = new QgsVectorLayer( dataSource, "", providerKey, false ); if ( !theLayer->isValid() ) { delete theLayer; return; } QgsVectorDataProvider* theProvider = theLayer->dataProvider(); if ( !theProvider ) { return; } QgsFields theFields = theProvider->fields(); //read classificationfield QDomNodeList classificationFieldList = layerElem.elementsByTagName( "classificationfield" ); for ( int j = 0; j < classificationFieldList.size(); ++j ) { QDomElement classificationFieldElem = classificationFieldList.at( j ).toElement(); int fieldNumber = classificationFieldElem.text().toInt(); if ( fieldNumber >= 0 && fieldNumber < theFields.count() ) { QDomText fieldName = mDom.createTextNode( theFields[fieldNumber].name() ); QDomNode nameNode = classificationFieldElem.firstChild(); classificationFieldElem.replaceChild( fieldName, nameNode ); } } } } }
void QgsOfflineEditing::synchronize() { // open logging db sqlite3* db = openLoggingDb(); if ( db == NULL ) { return; } emit progressStarted(); // restore and sync remote layers QList<QgsMapLayer*> offlineLayers; QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers(); for ( QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin() ; layer_it != mapLayers.end(); ++layer_it ) { QgsMapLayer* layer = layer_it.value(); if ( layer->customProperty( CUSTOM_PROPERTY_IS_OFFLINE_EDITABLE, false ).toBool() ) { offlineLayers << layer; } } for ( int l = 0; l < offlineLayers.count(); l++ ) { QgsMapLayer* layer = offlineLayers[l]; emit layerProgressUpdated( l + 1, offlineLayers.count() ); QString remoteSource = layer->customProperty( CUSTOM_PROPERTY_REMOTE_SOURCE, "" ).toString(); QString remoteProvider = layer->customProperty( CUSTOM_PROPERTY_REMOTE_PROVIDER, "" ).toString(); QString remoteName = layer->name(); remoteName.remove( QRegExp( " \\(offline\\)$" ) ); QgsVectorLayer* remoteLayer = new QgsVectorLayer( remoteSource, remoteName, remoteProvider ); if ( remoteLayer->isValid() ) { // TODO: only add remote layer if there are log entries? QgsVectorLayer* offlineLayer = qobject_cast<QgsVectorLayer*>( layer ); // copy style copySymbology( offlineLayer, remoteLayer ); // register this layer with the central layers registry QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << remoteLayer, true ); // apply layer edit log QString qgisLayerId = layer->id(); QString sql = QString( "SELECT \"id\" FROM 'log_layer_ids' WHERE \"qgis_id\" = '%1'" ).arg( qgisLayerId ); int layerId = sqlQueryInt( db, sql, -1 ); if ( layerId != -1 ) { remoteLayer->startEditing(); // TODO: only get commitNos of this layer? int commitNo = getCommitNo( db ); for ( int i = 0; i < commitNo; i++ ) { // apply commits chronologically applyAttributesAdded( remoteLayer, db, layerId, i ); applyAttributeValueChanges( offlineLayer, remoteLayer, db, layerId, i ); applyGeometryChanges( remoteLayer, db, layerId, i ); } applyFeaturesAdded( offlineLayer, remoteLayer, db, layerId ); applyFeaturesRemoved( remoteLayer, db, layerId ); if ( remoteLayer->commitChanges() ) { // update fid lookup updateFidLookup( remoteLayer, db, layerId ); // clear edit log for this layer sql = QString( "DELETE FROM 'log_added_attrs' WHERE \"layer_id\" = %1" ).arg( layerId ); sqlExec( db, sql ); sql = QString( "DELETE FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId ); sqlExec( db, sql ); sql = QString( "DELETE FROM 'log_removed_features' WHERE \"layer_id\" = %1" ).arg( layerId ); sqlExec( db, sql ); sql = QString( "DELETE FROM 'log_feature_updates' WHERE \"layer_id\" = %1" ).arg( layerId ); sqlExec( db, sql ); sql = QString( "DELETE FROM 'log_geometry_updates' WHERE \"layer_id\" = %1" ).arg( layerId ); sqlExec( db, sql ); // reset commitNo QString sql = QString( "UPDATE 'log_indices' SET 'last_index' = 0 WHERE \"name\" = 'commit_no'" ); sqlExec( db, sql ); } else { showWarning( remoteLayer->commitErrors().join( "\n" ) ); } } // remove offline layer QgsMapLayerRegistry::instance()->removeMapLayers( ( QStringList() << qgisLayerId ) ); // disable offline project QString projectTitle = QgsProject::instance()->title(); projectTitle.remove( QRegExp( " \\(offline\\)$" ) ); QgsProject::instance()->title( projectTitle ); QgsProject::instance()->removeEntry( PROJECT_ENTRY_SCOPE_OFFLINE, PROJECT_ENTRY_KEY_OFFLINE_DB_PATH ); remoteLayer->reload(); //update with other changes } } emit progressStopped(); sqlite3_close( db ); }
bool QgsSLConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction ) { if ( !QgsMimeDataUtils::isUriList( data ) ) return false; // TODO: probably should show a GUI with settings etc QgsDataSourceURI destUri; destUri.setDatabase( mDbPath ); qApp->setOverrideCursor( Qt::WaitCursor ); QProgressDialog *progress = new QProgressDialog( tr( "Copying features..." ), tr( "Abort" ), 0, 0, nullptr ); progress->setWindowTitle( tr( "Import layer" ) ); progress->setWindowModality( Qt::WindowModal ); progress->show(); QStringList importResults; bool hasError = false; bool cancelled = false; QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data ); Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst ) { if ( u.layerType != "vector" ) { importResults.append( tr( "%1: Not a vector layer!" ).arg( u.name ) ); hasError = true; // only vectors can be imported continue; } // open the source layer QgsVectorLayer* srcLayer = new QgsVectorLayer( u.uri, u.name, u.providerKey ); if ( srcLayer->isValid() ) { destUri.setDataSource( QString(), u.name, srcLayer->geometryType() != Qgis::NoGeometry ? "geom" : QString() ); QgsDebugMsg( "URI " + destUri.uri() ); QgsVectorLayerImport::ImportError err; QString importError; err = QgsVectorLayerImport::importLayer( srcLayer, destUri.uri(), "spatialite", srcLayer->crs(), false, &importError, false, nullptr, progress ); if ( err == QgsVectorLayerImport::NoError ) importResults.append( tr( "%1: OK!" ).arg( u.name ) ); else if ( err == QgsVectorLayerImport::ErrUserCancelled ) cancelled = true; else { importResults.append( QString( "%1: %2" ).arg( u.name, importError ) ); hasError = true; } } else { importResults.append( tr( "%1: OK!" ).arg( u.name ) ); hasError = true; } delete srcLayer; } delete progress; qApp->restoreOverrideCursor(); if ( cancelled ) { QMessageBox::information( nullptr, tr( "Import to SpatiaLite database" ), tr( "Import cancelled." ) ); refresh(); } else if ( hasError ) { QgsMessageOutput *output = QgsMessageOutput::createMessageOutput(); output->setTitle( tr( "Import to SpatiaLite database" ) ); output->setMessage( tr( "Failed to import some layers!\n\n" ) + importResults.join( "\n" ), QgsMessageOutput::MessageText ); output->showMessage(); } else { QMessageBox::information( nullptr, tr( "Import to SpatiaLite database" ), tr( "Import was successful." ) ); refresh(); } return true; }
bool QgsSLConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction ) { if ( !QgsMimeDataUtils::isUriList( data ) ) return false; // TODO: probably should show a GUI with settings etc QgsDataSourceURI destUri; destUri.setDatabase( mDbPath ); qApp->setOverrideCursor( Qt::WaitCursor ); QStringList importResults; bool hasError = false; QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data ); foreach ( const QgsMimeDataUtils::Uri& u, lst ) { if ( u.layerType != "vector" ) { importResults.append( tr( "%1: Not a vector layer!" ).arg( u.name ) ); hasError = true; // only vectors can be imported continue; } // open the source layer QgsVectorLayer* srcLayer = new QgsVectorLayer( u.uri, u.name, u.providerKey ); if ( srcLayer->isValid() ) { destUri.setDataSource( QString(), u.name, "geom" ); QgsDebugMsg( "URI " + destUri.uri() ); QgsVectorLayerImport::ImportError err; QString importError; err = QgsVectorLayerImport::importLayer( srcLayer, destUri.uri(), "spatialite", &srcLayer->crs(), false, &importError ); if ( err == QgsVectorLayerImport::NoError ) importResults.append( tr( "%1: OK!" ).arg( u.name ) ); else { importResults.append( QString( "%1: %2" ).arg( u.name ).arg( importError ) ); hasError = true; } } else { importResults.append( tr( "%1: OK!" ).arg( u.name ) ); hasError = true; } delete srcLayer; } qApp->restoreOverrideCursor(); if ( hasError ) { QgsMessageOutput *output = QgsMessageOutput::createMessageOutput(); output->setTitle( tr( "Import to SpatiaLite database" ) ); output->setMessage( tr( "Failed to import some layers!\n\n" ) + importResults.join( "\n" ), QgsMessageOutput::MessageText ); output->showMessage(); } else { QMessageBox::information( 0, tr( "Import to SpatiaLite database" ), tr( "Import was successful." ) ); refresh(); } return true; }
QgsVectorLayer* QgsRemoteOWSBuilder::sosLayer( const QDomElement& remoteOWSElem, const QString& url, const QString& layerName, QList<QgsMapLayer*>& layersToRemove, bool allowCaching ) const { //url for sos provider is: "url=... method=... xml=.... QString method = remoteOWSElem.attribute( QStringLiteral( "method" ), QStringLiteral( "POST" ) ); //possible GET/POST/SOAP //search for <LayerSensorObservationConstraints> node that is sibling of remoteOSW element //parent element of <remoteOWS> (normally <UserLayer>) QDomElement parentElem = remoteOWSElem.parentNode().toElement(); if ( parentElem.isNull() ) { return nullptr; } //Root element of the request (can be 'GetObservation' or 'GetObservationById' at the moment, probably also 'GetFeatureOfInterest' or 'GetFeatureOfInterestTime' in the future) QDomElement requestRootElem; QDomNodeList getObservationNodeList = parentElem.elementsByTagName( QStringLiteral( "GetObservation" ) ); if ( !getObservationNodeList.isEmpty() ) { requestRootElem = getObservationNodeList.at( 0 ).toElement(); } else //GetObservationById? { QDomNodeList getObservationByIdNodeList = parentElem.elementsByTagName( QStringLiteral( "GetObservationById" ) ); if ( !getObservationByIdNodeList.isEmpty() ) { requestRootElem = getObservationByIdNodeList.at( 0 ).toElement(); } } if ( requestRootElem.isNull() ) { return nullptr; } QDomDocument requestDoc; QDomNode newDocRoot = requestDoc.importNode( requestRootElem, true ); requestDoc.appendChild( newDocRoot ); QString providerUrl = "url=" + url + " method=" + method + " xml=" + requestDoc.toString(); //check if layer is already in cache QgsVectorLayer* sosLayer = nullptr; if ( allowCaching ) { sosLayer = qobject_cast<QgsVectorLayer*>( QgsMSLayerCache::instance()->searchLayer( providerUrl, layerName ) ); if ( sosLayer ) { return sosLayer; } } sosLayer = new QgsVectorLayer( providerUrl, QStringLiteral( "Sensor layer" ), QStringLiteral( "SOS" ) ); if ( !sosLayer->isValid() ) { delete sosLayer; return nullptr; } else { if ( allowCaching ) { QgsMSLayerCache::instance()->insertLayer( providerUrl, layerName, sosLayer ); } else { layersToRemove.push_back( sosLayer ); } } return sosLayer; }
bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString &file ) { QList< QPair<QString, QString> > attributes; attributes.append( qMakePair( QStringLiteral( "Layer" ), QStringLiteral( "String;30;" ) ) ); attributes.append( qMakePair( QStringLiteral( "FeatureID" ), QStringLiteral( "String;10;" ) ) ); attributes.append( qMakePair( QStringLiteral( "ErrorDesc" ), QStringLiteral( "String;80;" ) ) ); QFileInfo fi( file ); QString ext = fi.suffix(); QString driver = QgsVectorFileWriter::driverForExtension( ext ); QLibrary ogrLib( QgsProviderRegistry::instance()->library( QStringLiteral( "ogr" ) ) ); if ( !ogrLib.load() ) { return false; } typedef bool ( *createEmptyDataSourceProc )( const QString &, const QString &, const QString &, QgsWkbTypes::Type, const QList< QPair<QString, QString> > &, const QgsCoordinateReferenceSystem & ); createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) ); if ( !createEmptyDataSource ) { return false; } if ( !createEmptyDataSource( file, driver, "UTF-8", QgsWkbTypes::Point, attributes, QgsProject::instance()->crs() ) ) { return false; } QgsVectorLayer *layer = new QgsVectorLayer( file, QFileInfo( file ).baseName(), QStringLiteral( "ogr" ) ); if ( !layer->isValid() ) { delete layer; return false; } int fieldLayer = layer->fields().lookupField( QStringLiteral( "Layer" ) ); int fieldFeatureId = layer->fields().lookupField( QStringLiteral( "FeatureID" ) ); int fieldErrDesc = layer->fields().lookupField( QStringLiteral( "ErrorDesc" ) ); for ( int row = 0, nRows = ui.tableWidgetErrors->rowCount(); row < nRows; ++row ) { QgsGeometryCheckError *error = ui.tableWidgetErrors->item( row, 0 )->data( Qt::UserRole ).value<QgsGeometryCheckError *>(); QgsVectorLayer *srcLayer = mChecker->featurePools()[error->layerId()]->layer(); QgsFeature f( layer->fields() ); f.setAttribute( fieldLayer, srcLayer->name() ); f.setAttribute( fieldFeatureId, error->featureId() ); f.setAttribute( fieldErrDesc, error->description() ); QgsGeometry geom( new QgsPoint( error->location() ) ); f.setGeometry( geom ); layer->dataProvider()->addFeatures( QgsFeatureList() << f ); } // Remove existing layer with same uri QStringList toRemove; for ( QgsMapLayer *maplayer : QgsProject::instance()->mapLayers() ) { if ( dynamic_cast<QgsVectorLayer *>( maplayer ) && static_cast<QgsVectorLayer *>( maplayer )->dataProvider()->dataSourceUri() == layer->dataProvider()->dataSourceUri() ) { toRemove.append( maplayer->id() ); } } if ( !toRemove.isEmpty() ) { QgsProject::instance()->removeMapLayers( toRemove ); } QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << layer ); return true; }
void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, const QString& offlineDbPath ) { if ( layer == NULL ) { return; } QString tableName = layer->name(); // create table QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName ); QString delim = ""; const QgsFields& fields = layer->dataProvider()->fields(); for ( int idx = 0; idx < fields.count(); ++idx ) { QString dataType = ""; QVariant::Type type = fields[idx].type(); if ( type == QVariant::Int ) { dataType = "INTEGER"; } else if ( type == QVariant::Double ) { dataType = "REAL"; } else if ( type == QVariant::String ) { dataType = "TEXT"; } else { showWarning( tr( "Unknown data type %1" ).arg( type ) ); } sql += delim + QString( "'%1' %2" ).arg( fields[idx].name() ).arg( dataType ); delim = ","; } sql += ")"; // add geometry column QString geomType = ""; switch ( layer->wkbType() ) { case QGis::WKBPoint: geomType = "POINT"; break; case QGis::WKBMultiPoint: geomType = "MULTIPOINT"; break; case QGis::WKBLineString: geomType = "LINESTRING"; break; case QGis::WKBMultiLineString: geomType = "MULTILINESTRING"; break; case QGis::WKBPolygon: geomType = "POLYGON"; break; case QGis::WKBMultiPolygon: geomType = "MULTIPOLYGON"; break; default: showWarning( tr( "QGIS wkbType %1 not supported" ).arg( layer->wkbType() ) ); break; }; QString sqlAddGeom = QString( "SELECT AddGeometryColumn('%1', 'Geometry', %2, '%3', 2)" ) .arg( tableName ) .arg( layer->crs().authid().startsWith( "EPSG:", Qt::CaseInsensitive ) ? layer->crs().authid().mid( 5 ).toLong() : 0 ) .arg( geomType ); // create spatial index QString sqlCreateIndex = QString( "SELECT CreateSpatialIndex('%1', 'Geometry')" ).arg( tableName ); int rc = sqlExec( db, sql ); if ( rc == SQLITE_OK ) { rc = sqlExec( db, sqlAddGeom ); if ( rc == SQLITE_OK ) { rc = sqlExec( db, sqlCreateIndex ); } } if ( rc == SQLITE_OK ) { // add new layer QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(Geometry) sql=" ) .arg( offlineDbPath ).arg( tableName ), tableName + " (offline)", "spatialite" ); if ( newLayer->isValid() ) { // mark as offline layer newLayer->setCustomProperty( CUSTOM_PROPERTY_IS_OFFLINE_EDITABLE, true ); // store original layer source newLayer->setCustomProperty( CUSTOM_PROPERTY_REMOTE_SOURCE, layer->source() ); newLayer->setCustomProperty( CUSTOM_PROPERTY_REMOTE_PROVIDER, layer->providerType() ); // copy style bool hasLabels = layer->hasLabelsEnabled(); if ( !hasLabels ) { // NOTE: copy symbology before adding the layer so it is displayed correctly copySymbology( layer, newLayer ); } // register this layer with the central layers registry QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer *>() << newLayer ); if ( hasLabels ) { // NOTE: copy symbology of layers with labels enabled after adding to project, as it will crash otherwise (WORKAROUND) copySymbology( layer, newLayer ); } // TODO: layer order // copy features newLayer->startEditing(); QgsFeature f; // NOTE: force feature recount for PostGIS layer, else only visible features are counted, before iterating over all features (WORKAROUND) layer->setSubsetString( "" ); QgsFeatureIterator fit = layer->getFeatures(); emit progressModeSet( QgsOfflineEditing::CopyFeatures, layer->featureCount() ); int featureCount = 1; QList<QgsFeatureId> remoteFeatureIds; while ( fit.nextFeature( f ) ) { remoteFeatureIds << f.id(); // NOTE: Spatialite provider ignores position of geometry column // fill gap in QgsAttributeMap if geometry column is not last (WORKAROUND) int column = 0; QgsAttributes attrs = f.attributes(); QgsAttributes newAttrs( attrs.count() ); for ( int it = 0; it < attrs.count(); ++it ) { newAttrs[column++] = attrs[it]; } f.setAttributes( newAttrs ); newLayer->addFeature( f, false ); emit progressUpdated( featureCount++ ); } if ( newLayer->commitChanges() ) { emit progressModeSet( QgsOfflineEditing::ProcessFeatures, layer->featureCount() ); featureCount = 1; // update feature id lookup int layerId = getOrCreateLayerId( db, newLayer->id() ); QList<QgsFeatureId> offlineFeatureIds; QgsFeatureIterator fit = newLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( QgsAttributeList() ) ); while ( fit.nextFeature( f ) ) { offlineFeatureIds << f.id(); } // NOTE: insert fids in this loop, as the db is locked during newLayer->nextFeature() sqlExec( db, "BEGIN" ); int remoteCount = remoteFeatureIds.size(); for ( int i = 0; i < remoteCount; i++ ) { addFidLookup( db, layerId, offlineFeatureIds.at( i ), remoteFeatureIds.at( remoteCount - ( i + 1 ) ) ); emit progressUpdated( featureCount++ ); } sqlExec( db, "COMMIT" ); } else { showWarning( newLayer->commitErrors().join( "\n" ) ); } // remove remote layer QgsMapLayerRegistry::instance()->removeMapLayers( QStringList() << layer->id() ); } } }
bool QgsNewSpatialiteLayerDialog::apply() { // Build up the sql statement for creating the table QString sql = QStringLiteral( "create table %1(" ).arg( quotedIdentifier( leLayerName->text() ) ); QString delim; if ( checkBoxPrimaryKey->isChecked() ) { sql += QLatin1String( "pkuid integer primary key autoincrement" ); delim = QStringLiteral( "," ); } QTreeWidgetItemIterator it( mAttributeView ); while ( *it ) { sql += delim + QStringLiteral( "%1 %2" ).arg( quotedIdentifier( ( *it )->text( 0 ) ), ( *it )->text( 1 ) ); delim = QStringLiteral( "," ); ++it; } // complete the create table statement sql += ')'; QgsDebugMsg( QStringLiteral( "Creating table in database %1" ).arg( mDatabaseComboBox->currentText() ) ); QgsDebugMsg( sql ); spatialite_database_unique_ptr database; int rc = database.open( mDatabaseComboBox->currentText() ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "SpatiaLite Database" ), tr( "Unable to open the database: %1" ).arg( mDatabaseComboBox->currentText() ) ); return false; } char *errmsg = nullptr; // create the table rc = sqlite3_exec( database.get(), sql.toUtf8(), nullptr, nullptr, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating SpatiaLite Table" ), tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text(), errmsg ) ); sqlite3_free( errmsg ); return false; } // create the geometry column and the spatial index if ( mGeometryTypeBox->currentIndex() != 0 ) { QString sqlAddGeom = QStringLiteral( "select AddGeometryColumn(%1,%2,%3,%4,%5)" ) .arg( QgsSqliteUtils::quotedString( leLayerName->text() ), QgsSqliteUtils::quotedString( leGeometryColumn->text() ) ) .arg( mCrsId.split( ':' ).value( 1, QStringLiteral( "0" ) ).toInt() ) .arg( QgsSqliteUtils::quotedString( selectedType() ) ) .arg( QgsSqliteUtils::quotedString( selectedZM() ) ); QgsDebugMsg( sqlAddGeom ); rc = sqlite3_exec( database.get(), sqlAddGeom.toUtf8(), nullptr, nullptr, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating Geometry Column" ), tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) ); sqlite3_free( errmsg ); return false; } QString sqlCreateIndex = QStringLiteral( "select CreateSpatialIndex(%1,%2)" ) .arg( QgsSqliteUtils::quotedString( leLayerName->text() ), QgsSqliteUtils::quotedString( leGeometryColumn->text() ) ); QgsDebugMsg( sqlCreateIndex ); rc = sqlite3_exec( database.get(), sqlCreateIndex.toUtf8(), nullptr, nullptr, &errmsg ); if ( rc != SQLITE_OK ) { QMessageBox::warning( this, tr( "Error Creating Spatial Index" ), tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) ); sqlite3_free( errmsg ); return false; } } const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() }; QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "dbname='%1' table='%2'%3 sql=" ) .arg( mDatabaseComboBox->currentText(), leLayerName->text(), mGeometryTypeBox->currentIndex() != 0 ? QStringLiteral( "(%1)" ).arg( leGeometryColumn->text() ) : QString() ), leLayerName->text(), QStringLiteral( "spatialite" ), options ); if ( layer->isValid() ) { // Reload connections to refresh browser panel QgisApp::instance()->reloadConnections(); // register this layer with the central layers registry QList<QgsMapLayer *> myList; myList << layer; //addMapLayers returns a list of all successfully added layers //so we compare that to our original list. if ( myList == QgsProject::instance()->addMapLayers( myList ) ) return true; } else { QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" ); QMessageBox::critical( this, tr( "SpatiaLite Database" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) ); delete layer; } return false; }