213
213
#include " postgres/qgspgsourceselect.h"
214
214
#endif
215
215
#ifdef HAVE_SPATIALITE
216
+ extern " C"
217
+ {
218
+ #include < sqlite3.h>
219
+ #include < spatialite.h>
220
+ }
216
221
#include " qgsspatialitesourceselect.h"
217
222
#include " qgsnewspatialitelayerdialog.h"
218
223
#endif
@@ -937,15 +942,15 @@ void QgisApp::createActions()
937
942
938
943
// Layer Menu Items
939
944
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 " ) );
943
948
connect ( mActionNewVectorLayer , SIGNAL ( triggered () ), this , SLOT ( newVectorLayer () ) );
944
949
945
950
#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 " ) );
949
954
connect ( mActionNewSpatialiteLayer , SIGNAL ( triggered () ), this , SLOT ( newSpatialiteLayer () ) );
950
955
#endif
951
956
@@ -1420,13 +1425,13 @@ void QgisApp::createMenus()
1420
1425
mLayerMenu = menuBar ()->addMenu ( tr ( " &Layer" ) );
1421
1426
1422
1427
#ifdef HAVE_SPATIALITE
1423
- QMenu *newLayerMenu = mLayerMenu ->addMenu ( tr ( " New " ) );
1428
+ QMenu *newLayerMenu = mLayerMenu ->addMenu ( tr ( " New" ) );
1424
1429
newLayerMenu->addAction ( mActionNewVectorLayer );
1425
1430
newLayerMenu->addAction ( mActionNewSpatialiteLayer );
1426
1431
#else
1427
1432
mLayerMenu ->addAction ( mActionNewVectorLayer );
1428
1433
#endif
1429
-
1434
+
1430
1435
mLayerMenu ->addAction ( mActionAddOgrLayer );
1431
1436
mLayerMenu ->addAction ( mActionAddRasterLayer );
1432
1437
#ifdef HAVE_POSTGRESQL
@@ -3097,6 +3102,18 @@ void QgisApp::newVectorLayer()
3097
3102
addVectorLayers ( fileNames, enc, " file" );
3098
3103
}
3099
3104
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
+
3100
3117
void QgisApp::newSpatialiteLayer ()
3101
3118
{
3102
3119
if ( mMapCanvas && mMapCanvas ->isDrawing () )
@@ -3115,195 +3132,103 @@ void QgisApp::newSpatialiteLayer()
3115
3132
QString crsId = spatialiteDialog.selectedCrsId ();
3116
3133
QString databaseName = spatialiteDialog.databaseName ();
3117
3134
QString newLayerName = spatialiteDialog.layerName ();
3135
+ QString newGeometryColumn = spatialiteDialog.geometryColumn ();
3118
3136
// QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
3119
3137
3120
3138
// Get the list containing the name/type pairs for each attribute
3121
3139
QList<QStringList> * items = spatialiteDialog.attributes ( );
3122
3140
3123
3141
// Build up the sql statement for creating the table
3124
3142
//
3125
- QString sql = QString (" create table %1 (id integer primary key autoincrement " ).arg (newLayerName);
3143
+ QString sql = QString ( " create table %1( " ).arg ( quotedIdentifier ( newLayerName ) );
3126
3144
// iterate through the field names and add them to the create statement
3127
3145
// (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 )
3129
3147
{
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 ) );
3132
3152
}
3133
3153
// complete the create table statement
3134
3154
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
3137
3164
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 );
3142
3169
3143
3170
sqlite3 *db;
3144
3171
int rc = sqlite3_open ( databaseName.toUtf8 (), &db );
3145
3172
if ( rc != SQLITE_OK )
3146
3173
{
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 ) );
3148
3177
}
3149
3178
else
3150
3179
{
3151
3180
char * errmsg;
3152
- rc = sqlite3_exec ( db, sql.toUtf8 (), NULL , NULL , &errmsg);
3181
+ rc = sqlite3_exec ( db, sql.toUtf8 (), NULL , NULL , &errmsg );
3153
3182
if ( rc != SQLITE_OK )
3154
3183
{
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 ) );
3157
3187
sqlite3_free ( errmsg );
3158
3188
}
3159
3189
else
3160
3190
{
3161
3191
// 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 );
3163
3193
if ( rc != SQLITE_OK )
3164
3194
{
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 ) );
3167
3198
sqlite3_free ( errmsg );
3168
3199
}
3169
3200
else
3170
3201
{
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
+ }
3277
3211
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
+ }
3292
3227
}
3293
3228
}
3294
- else
3295
- {
3296
- QgsDebugMsg( "Resolving newEmptyDataSource(...) failed" );
3297
- }
3298
3229
}
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
- */
3306
3230
}
3231
+
3307
3232
void QgisApp::fileOpen ()
3308
3233
{
3309
3234
if ( mMapCanvas && mMapCanvas ->isDrawing () )
@@ -5026,6 +4951,7 @@ void QgisApp::loadPythonSupport()
5026
4951
mActionPluginSeparator2 = mPluginMenu ->addSeparator ();
5027
4952
mPluginMenu ->addAction ( mActionShowPythonDialog );
5028
4953
std::cout << " Python support ENABLED :-) " << std::endl; // OK
4954
+
5029
4955
}
5030
4956
}
5031
4957
0 commit comments