Skip to content

Commit 814861c

Browse files
author
jef
committed
spatialite improvements:
- support geometry column name - remove unnecessary id field - fix windows build - better identifier quoting and utf-8 support - update python binding - fix some translation strings git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13250 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b39235e commit 814861c

12 files changed

+324
-496
lines changed

debian/control.sid

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Build-Depends: debhelper (>= 7), libgdal1-dev, libpq-dev,
88
sharutils, sip4 (>= 4.5), libqt4-core (>=4.4.0), libqt4-dev (>=4.4.0), libqt4-gui (>=4.4.0),
99
libqt4-sql (>=4.4.0), python-qt4 (>=4.1.0), python-qt4-dev (>=4.1.0),
1010
python-sip4-dev (>= 4.5.0), libfontconfig1-dev, libxi-dev, libxrandr-dev, libxrender-dev, libice-dev,
11-
libsm-dev, pyqt4-dev-tools, libqwt5-qt4-dev
11+
libsm-dev, pyqt4-dev-tools, libqwt5-qt4-dev, libspatialite-dev
1212
Build-Conflicts: libqgis-dev, qgis-dev
1313
Standards-Version: 3.8.4
1414
XS-Python-Version: current

python/core/qgsapplication.sip

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
133133
//! Returns the path to the master qgis.db file.
134134
static const QString qgisMasterDbFilePath();
135135

136+
//! Returns the path to the spatialite template db file.
137+
//! @note added in 1.5
138+
static const QString qgisSpatialiteDbTemplatePath();
139+
136140
//! Returns the path to the settings directory in user's home dir
137141
static const QString qgisSettingsDirPath();
138142

src/app/qgisapp.cpp

+82-156
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@
213213
#include "postgres/qgspgsourceselect.h"
214214
#endif
215215
#ifdef HAVE_SPATIALITE
216+
extern "C"
217+
{
218+
#include <sqlite3.h>
219+
#include <spatialite.h>
220+
}
216221
#include "qgsspatialitesourceselect.h"
217222
#include "qgsnewspatialitelayerdialog.h"
218223
#endif
@@ -937,15 +942,15 @@ void QgisApp::createActions()
937942

938943
// Layer Menu Items
939944

940-
mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Vector Layer (shapefile)..." ), this );
941-
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Vector Layer (shapefile)" ) );
942-
mActionNewVectorLayer->setStatusTip( tr( "Create a New Vector Layer (shapefile)" ) );
945+
mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Shapefile Layer..." ), this );
946+
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Shapefile layer" ) );
947+
mActionNewVectorLayer->setStatusTip( tr( "Create a New Shapefile layer" ) );
943948
connect( mActionNewVectorLayer, SIGNAL( triggered() ), this, SLOT( newVectorLayer() ) );
944949

945950
#ifdef HAVE_SPATIALITE
946-
mActionNewSpatialiteLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Spatialite Layer ..." ), this );
947-
shortcuts->registerAction( mActionNewSpatialiteLayer, tr( "Ctrl+Shift+S", "Create a New Spatialite Layer " ) );
948-
mActionNewSpatialiteLayer->setStatusTip( tr( "Create a New Spatialite Layer " ) );
951+
mActionNewSpatialiteLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New SpatiaLite Layer ..." ), this );
952+
shortcuts->registerAction( mActionNewSpatialiteLayer, tr( "Ctrl+Shift+S", "Create a New SpatiaLite Layer " ) );
953+
mActionNewSpatialiteLayer->setStatusTip( tr( "Create a New SpatiaLite Layer " ) );
949954
connect( mActionNewSpatialiteLayer, SIGNAL( triggered() ), this, SLOT( newSpatialiteLayer() ) );
950955
#endif
951956

@@ -1420,13 +1425,13 @@ void QgisApp::createMenus()
14201425
mLayerMenu = menuBar()->addMenu( tr( "&Layer" ) );
14211426

14221427
#ifdef HAVE_SPATIALITE
1423-
QMenu *newLayerMenu = mLayerMenu->addMenu( tr( "New ") );
1428+
QMenu *newLayerMenu = mLayerMenu->addMenu( tr( "New" ) );
14241429
newLayerMenu->addAction( mActionNewVectorLayer );
14251430
newLayerMenu->addAction( mActionNewSpatialiteLayer );
14261431
#else
14271432
mLayerMenu->addAction( mActionNewVectorLayer );
14281433
#endif
1429-
1434+
14301435
mLayerMenu->addAction( mActionAddOgrLayer );
14311436
mLayerMenu->addAction( mActionAddRasterLayer );
14321437
#ifdef HAVE_POSTGRESQL
@@ -3097,6 +3102,18 @@ void QgisApp::newVectorLayer()
30973102
addVectorLayers( fileNames, enc, "file" );
30983103
}
30993104

3105+
static QString quotedIdentifier( QString id )
3106+
{
3107+
id.replace( "\"", "\"\"" );
3108+
return id.prepend( "\"" ).append( "\"" );
3109+
}
3110+
3111+
static QString quotedValue( QString value )
3112+
{
3113+
value.replace( "'", "''" );
3114+
return value.prepend( "'" ).append( "'" );
3115+
}
3116+
31003117
void QgisApp::newSpatialiteLayer()
31013118
{
31023119
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -3115,195 +3132,103 @@ void QgisApp::newSpatialiteLayer()
31153132
QString crsId = spatialiteDialog.selectedCrsId();
31163133
QString databaseName = spatialiteDialog.databaseName();
31173134
QString newLayerName = spatialiteDialog.layerName();
3135+
QString newGeometryColumn = spatialiteDialog.geometryColumn();
31183136
//QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
31193137

31203138
// Get the list containing the name/type pairs for each attribute
31213139
QList<QStringList> * items = spatialiteDialog.attributes( );
31223140

31233141
// Build up the sql statement for creating the table
31243142
//
3125-
QString sql = QString("create table %1 (id integer primary key autoincrement").arg(newLayerName);
3143+
QString sql = QString( "create table %1(" ).arg( quotedIdentifier( newLayerName ) );
31263144
// iterate through the field names and add them to the create statement
31273145
// (use indexed access since this is just as fast as iterators
3128-
for ( int i = 0; i < items->size(); ++i )
3146+
for ( int i = 0; i < items->size(); ++i )
31293147
{
3130-
QStringList field = items->at(i);
3131-
sql += QString(", %1 %2").arg(field.at(0)).arg(field.at(1));
3148+
QStringList field = items->at( i );
3149+
if ( i > 0 )
3150+
sql += ",";
3151+
sql += QString( "%1 %2" ).arg( quotedIdentifier( field.at( 0 ) ) ).arg( field.at( 1 ) );
31323152
}
31333153
// complete the create table statement
31343154
sql += ")";
3135-
std::cout << "Creating table in database " << databaseName.toUtf8().data() << std::endl;
3136-
std::cout << sql.toUtf8().data() << std::endl; // OK
3155+
QgsDebugMsg( QString( "Creating table in database %1" ).arg( databaseName ) );
3156+
QgsDebugMsg( sql ); // OK
3157+
3158+
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
3159+
.arg( quotedValue( newLayerName ) )
3160+
.arg( quotedValue( newGeometryColumn ) )
3161+
.arg( crsId )
3162+
.arg( quotedValue( geometrytype ) );
3163+
QgsDebugMsg( sqlAddGeom ); // OK
31373164

3138-
QString sqlAddGeom = QString("select AddGeometryColumn('%1', 'geom', %2, '%3', 2)").arg(newLayerName).arg(crsId).arg(geometrytype);
3139-
std::cout << sqlAddGeom.toUtf8().data() << std::endl; // OK
3140-
QString sqlCreateIndex = QString("select CreateSpatialIndex('%1', 'geom')").arg(newLayerName);
3141-
std::cout << sqlCreateIndex.toUtf8().data() << std::endl; // OK
3165+
QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" ).arg( quotedValue( newLayerName ) ).arg( quotedValue( newGeometryColumn ) );
3166+
QgsDebugMsg( sqlCreateIndex ); // OK
3167+
3168+
spatialite_init( 0 );
31423169

31433170
sqlite3 *db;
31443171
int rc = sqlite3_open( databaseName.toUtf8(), &db );
31453172
if ( rc != SQLITE_OK )
31463173
{
3147-
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database: %1").arg(databaseName ) );
3174+
QMessageBox::warning( this,
3175+
tr( "SpatiaLite Database" ),
3176+
tr( "Unable to open the database: %1" ).arg( databaseName ) );
31483177
}
31493178
else
31503179
{
31513180
char * errmsg;
3152-
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
3181+
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
31533182
if ( rc != SQLITE_OK )
31543183
{
3155-
QMessageBox::warning( this, tr( "Error deleting bookmark" ),
3156-
tr( "Failed to create the Spatialite table %1. The database returned:\n%2" ).arg( newLayerName ).arg( errmsg ) );
3184+
QMessageBox::warning( this,
3185+
tr( "Error Creating SpatiaLite Table" ),
3186+
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( newLayerName ).arg( errmsg ) );
31573187
sqlite3_free( errmsg );
31583188
}
31593189
else
31603190
{
31613191
// create the geometry column and the spatial index
3162-
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg);
3192+
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg );
31633193
if ( rc != SQLITE_OK )
31643194
{
3165-
QMessageBox::warning( this, tr( "Error Creating Geometry Column" ),
3166-
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
3195+
QMessageBox::warning( this,
3196+
tr( "Error Creating Geometry Column" ),
3197+
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
31673198
sqlite3_free( errmsg );
31683199
}
31693200
else
31703201
{
3171-
// create the spatial index
3172-
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg);
3173-
if ( rc != SQLITE_OK )
3174-
{
3175-
QMessageBox::warning( this, tr( "Error Creating Spatial Index" ),
3176-
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
3177-
sqlite3_free( errmsg );
3178-
}
3179-
}
3180-
}
3181-
3182-
3183-
}
3184-
3185-
3186-
/*
3187-
bool haveLastUsedFilter = false; // by default, there is no last
3188-
// used filter
3189-
QString enc;
3190-
QString fileName;
3191-
3192-
QSettings settings; // where we keep last used filter in
3193-
// persistent state
3194-
3195-
haveLastUsedFilter = settings.contains( "/UI/lastVectorFileFilter" );
3196-
QString lastUsedFilter = settings.value( "/UI/lastVectorFileFilter",
3197-
QVariant( QString::null ) ).toString();
3198-
3199-
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir",
3200-
"." ).toString();
3201-
3202-
QgsDebugMsg( "Saving vector file dialog without filters: " );
3203-
3204-
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( this,
3205-
tr( "Save As" ), lastUsedDir, "", QString( "" ) );
3206-
3207-
// allow for selection of more than one file
3208-
openFileDialog->setFileMode( QFileDialog::AnyFile );
3209-
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );
3210-
openFileDialog->setConfirmOverwrite( true );
3211-
3212-
if ( haveLastUsedFilter ) // set the filter to the last one used
3213-
{
3214-
openFileDialog->selectFilter( lastUsedFilter );
3215-
}
3216-
3217-
int res;
3218-
while (( res = openFileDialog->exec() ) == QDialog::Accepted )
3219-
{
3220-
fileName = openFileDialog->selectedFiles().first();
3221-
3222-
if ( fileformat == "ESRI Shapefile" )
3223-
{
3224-
if ( !isValidShapeFileName( fileName ) )
3225-
{
3226-
fileName += ".shp";
3227-
}
3228-
3229-
if ( !isValidShapeFileName( fileName ) )
3230-
{
3231-
QMessageBox::information( this,
3232-
tr( "New Shapefile" ),
3233-
tr( "Shapefiles must end on .shp" ) );
3234-
continue;
3235-
}
3236-
}
3237-
3238-
break;
3239-
}
3240-
3241-
if ( res == QDialog::Rejected )
3242-
{
3243-
delete openFileDialog;
3244-
return;
3245-
}
3246-
3247-
enc = openFileDialog->encoding();
3248-
3249-
// If the file exists, delete it otherwise we'll end up loading that
3250-
// file, which can cause problems (e.g., if the file contains
3251-
// linestrings, but we're wanting to create points, we'll end up
3252-
// with a linestring file).
3253-
if ( fileformat == "ESRI Shapefile" )
3254-
{
3255-
QgsVectorFileWriter::deleteShapeFile( fileName );
3256-
}
3257-
else
3258-
{
3259-
QFile::remove( fileName );
3260-
}
3261-
3262-
settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );
3263-
3264-
settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );
3265-
3266-
delete openFileDialog;
3267-
3268-
//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
3269-
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
3270-
QString ogrlib = pReg->library( "ogr" );
3271-
// load the data provider
3272-
QLibrary* myLib = new QLibrary( ogrlib );
3273-
bool loaded = myLib->load();
3274-
if ( loaded )
3275-
{
3276-
QgsDebugMsg( "ogr provider loaded" );
3202+
// create the spatial index
3203+
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg );
3204+
if ( rc != SQLITE_OK )
3205+
{
3206+
QMessageBox::warning( this,
3207+
tr( "Error Creating Spatial Index" ),
3208+
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
3209+
sqlite3_free( errmsg );
3210+
}
32773211

3278-
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
3279-
const std::list<std::pair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
3280-
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
3281-
if ( createEmptyDataSource )
3282-
{
3283-
if ( geometrytype != QGis::WKBUnknown )
3284-
{
3285-
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
3286-
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs );
3287-
}
3288-
else
3289-
{
3290-
QgsDebugMsg( "geometry type not recognised" );
3291-
return;
3212+
QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname=%1 table=%2(%3) sql=" )
3213+
.arg( databaseName )
3214+
.arg( newLayerName )
3215+
.arg( newGeometryColumn ), newLayerName, "spatialite" );
3216+
if ( layer->isValid() )
3217+
{
3218+
// register this layer with the central layers registry
3219+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
3220+
}
3221+
else
3222+
{
3223+
QgsDebugMsg( newLayerName + " is an invalid layer - not loaded" );
3224+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( newLayerName ) );
3225+
delete layer;
3226+
}
32923227
}
32933228
}
3294-
else
3295-
{
3296-
QgsDebugMsg( "Resolving newEmptyDataSource(...) failed" );
3297-
}
32983229
}
3299-
3300-
//then add the layer to the view
3301-
QStringList fileNames;
3302-
fileNames.append( fileName );
3303-
//todo: the last parameter will change accordingly to layer type
3304-
addVectorLayers( fileNames, enc, "file" );
3305-
*/
33063230
}
3231+
33073232
void QgisApp::fileOpen()
33083233
{
33093234
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -5026,6 +4951,7 @@ void QgisApp::loadPythonSupport()
50264951
mActionPluginSeparator2 = mPluginMenu->addSeparator();
50274952
mPluginMenu->addAction( mActionShowPythonDialog );
50284953
std::cout << "Python support ENABLED :-) " << std::endl; // OK
4954+
50294955
}
50304956
}
50314957

0 commit comments

Comments
 (0)