Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementation of the "Add New Spatialite Layer/Database" feature.
Squashed commit of the following:

commit e6e01ec0b3955d1cf9cb2756dfcf1aac27a0725e
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:45:05 2010 -0800

    Implementation of the add new spatialite layer dialog with ability to search for epsg srid

commit c1d46e35715e4f8baa137800c88483f612ebcb89
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:44:23 2010 -0800

    Spatialite database template used when creating a new database

commit 6d4fd8d80e610d6f0f6d22ed49815134292924c5
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:43:31 2010 -0800

    Context help for the Add New Spatialite Layer dialog

commit 1a6398be174d62df1bf74428bcf898b8d1ea8455
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:42:51 2010 -0800

    Dialog for finding an SRID from the spatialite spatial_ref_sys table

commit 197a65c134ed06b12fd6c8d96900dcf8197f802d
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sat Apr 3 11:32:58 2010 -0800

    Renamed the spatialite srids dialog selector to the proper form

commit 66c7cafa78f71b7f06504fe3b7bd4577614bfe67
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 22:25:09 2010 -0800

    Implemented create new db. Can now create a new db and an empty layer in
    one pass.

commit 088730293ad4722e7d6045e8661e6c28488b8cae
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 20:43:12 2010 -0800

    Adjust new spatialite layer dialog. Add field for geometry column name

commit d7c60012ddce6a70d0512bc9195524e6d672b372
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 12:46:49 2010 -0800

    New spatialite layer menu item does not show up if qgis is built with internal spatialite

commit 1343dfe7ac7868c38e8b7bd17ef1dd5a4a04ebad
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 11:35:26 2010 -0800

    Test of geometry column creation in the dialog

commit c8785f4aaee9d5b08fe0c0dd6f6bbfcd9cf6c7a5
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:39:03 2010 -0800

    Added multi geometry types to the radio button group

commit 06350192b9dfa6d6cc5c6ac9c4d0b1a65f260476
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:13:20 2010 -0800

    SQL for creating the new spatialite table complete---still needs the code to actually push it to the database

commit 0e05d6ab9904d003d9c7c7704f53392fc478cee1
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:12:25 2010 -0800

    Added field for the layer name to be created

commit 3c372b4da3097981d824c2dbb7a661c916d9cf7b
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Tue Mar 30 21:39:10 2010 -0800

    Removed unneeded fields from the spatialite dialog (width and precision). Added methods for getting the attributes

commit 1871db18a5d87f22caf9a34001c879eaf7f58342
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 20:51:34 2010 -0800

    Renamed ui file

commit a42e578afac13d2a9e984faedc79d0d206b48c66
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 20:51:00 2010 -0800

    Added dialog for displaying and selecting a spatialite srid

commit d0c48bc2317d175fa13c4e52fb6d4f0b10a58518
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 19:41:07 2010 -0800

    More work on the dialog to create a new spatialite layer

commit cc6d8a4e48e42c865eec9da3d2ad1e900fbbe295
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Thu Mar 18 20:51:08 2010 -0800

    Wire the new spatialite layer dialog into qgisapp

commit c5a54601fd9b76e46aeb534f92a605cd744ae768
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Thu Mar 18 20:50:18 2010 -0800

    Dialog implementation for creating a new spatialite layer

git-svn-id: http://svn.osgeo.org/qgis/trunk@13240 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
gsherman committed Apr 5, 2010
1 parent 868a157 commit f36bfa5
Show file tree
Hide file tree
Showing 14 changed files with 1,449 additions and 8 deletions.
10 changes: 5 additions & 5 deletions resources/CMakeLists.txt
@@ -1,5 +1,5 @@

INSTALL(FILES srs.db qgis.db qgis_help.db symbology-ng-style.xml
DESTINATION ${QGIS_DATA_DIR}/resources)

SUBDIRS(context_help)

INSTALL(FILES srs.db qgis.db qgis_help.db symbology-ng-style.xml spatialite.db
DESTINATION ${QGIS_DATA_DIR}/resources)

SUBDIRS(context_help)
20 changes: 20 additions & 0 deletions resources/context_help/QgsNewSpatialiteLayerDialog-en_US
@@ -0,0 +1,20 @@
<h3>Create a New Spatialite Layer</h3>
You can use this dialog to create a new Spatialite database and/or an empty Spatialite layer for editing. See below for an explanation of the dialog inputs.
<h4>Database</h4>
Choose the database from the drop-down list. This list is created from your saved Spatialite connections. If you don't have a saved connection or want to create a new database, click on the button (<label>...</label>) to the right of the drop-down. If you create a new database you will have to add it to your Spatialite connections in the <i>Add Spatialite Table(s)</i> dialog.
<h4>Layer name</h4>
Enter a name for the layer you want to create. The name should be one word. You can use underscores in the name if you like.
<h4>Geometry column</h4>
Enter a name for the geometry column or accept the default.
<h4>Type</h4>
Choose the type of layer you want to create.
<h4>EPSG SRID</h4>
Enter the EPSG number for the spatial reference id (SRID). By default the SRID for WGS 84 is filled in for you. Click on <label>Find SRID</label> button to change the coordinate reference system of the layer if needed. The SRID must exist within the spatial_ref_sys in your Spatialite database. You can search for the SRID using partial matches on both name and SRID.
<h4>New attribute</h4>
Add the desired attributes by clicking on the <label>Add to attributes list</label> button after you have specified a name and type for the attribute. Only real, integer, and string attributes are supported.<br/>
Width and precision are irrelevant in a Spatialite database so you do not have to specify these.
<h4>Attributes list</h4>
In this section you can see the list of attributes. To delete one of them, click on it and choose <label>Remove selected attribute</label> button.

<p>
To create the layer, click on <label>Apply</label>. The layer will be created and you will be returned to the dialog. From here you can click <label>Close</label> or change the parameters to create additional layers.
Binary file added resources/spatialite.db
Binary file not shown.
5 changes: 5 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -24,6 +24,7 @@ SET(QGIS_APP_SRCS
qgsdelattrdialog.cpp
qgsdisplayangle.cpp
qgsfieldcalculator.cpp
qgsnewspatialitelayerdialog.cpp
qgsnewvectorlayerdialog.cpp
qgsgraduatedsymboldialog.cpp
qgshelpviewer.cpp
Expand Down Expand Up @@ -58,6 +59,7 @@ SET(QGIS_APP_SRCS
qgsnewhttpconnection.cpp
qgsnumericsortlistviewitem.cpp
qgsoptions.cpp
qgsspatialitesridsdialog.cpp
qgspastetransformations.cpp
qgspointrotationitem.cpp
qgspluginitem.cpp
Expand Down Expand Up @@ -143,6 +145,7 @@ SET (QGIS_APP_MOC_HDRS
qgsfieldcalculator.h
qgsformannotationdialog.h
qgsmaptoolmeasureangle.h
qgsnewspatialitelayerdialog.h
qgsnewvectorlayerdialog.h
qgsgraduatedsymboldialog.h
qgshelpviewer.h
Expand Down Expand Up @@ -178,6 +181,7 @@ SET (QGIS_APP_MOC_HDRS
qgswmssourceselect.h
qgssinglesymboldialog.h
qgssnappingdialog.h
qgsspatialitesridsdialog.h
qgsuniquevaluedialog.h
qgsvectorlayerproperties.h
qgsdbtablemodel.h
Expand Down Expand Up @@ -330,6 +334,7 @@ IF (WITH_INTERNAL_SPATIALITE)
INCLUDE_DIRECTORIES(../core/spatialite/headers/spatialite)
ELSE (WITH_INTERNAL_SPATIALITE)
INCLUDE_DIRECTORIES(${SQLITE3_INCLUDE_DIR})
ADD_DEFINITIONS(-DEXTERNAL_SPATIALITE)
ENDIF (WITH_INTERNAL_SPATIALITE)

IF (POSTGRES_FOUND)
Expand Down
228 changes: 225 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -214,6 +214,7 @@
#endif
#ifdef HAVE_SPATIALITE
#include "qgsspatialitesourceselect.h"
#include "qgsnewspatialitelayerdialog.h"
#endif

#include "qgspythonutils.h"
Expand Down Expand Up @@ -936,11 +937,18 @@ void QgisApp::createActions()

// Layer Menu Items

mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Vector Layer..." ), this );
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Vector Layer" ) );
mActionNewVectorLayer->setStatusTip( tr( "Create a New Vector Layer" ) );
mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Vector Layer (shapefile)..." ), this );
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Vector Layer (shapefile)" ) );
mActionNewVectorLayer->setStatusTip( tr( "Create a New Vector Layer (shapefile)" ) );
connect( mActionNewVectorLayer, SIGNAL( triggered() ), this, SLOT( newVectorLayer() ) );

#ifdef HAVE_SPATIALITE
mActionNewSpatialiteLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Spatialite Layer ..." ), this );
shortcuts->registerAction( mActionNewSpatialiteLayer, tr( "Ctrl+Shift+S", "Create a New Spatialite Layer " ) );
mActionNewSpatialiteLayer->setStatusTip( tr( "Create a New Spatialite Layer " ) );
connect( mActionNewSpatialiteLayer, SIGNAL( triggered() ), this, SLOT( newSpatialiteLayer() ) );
#endif

mActionAddOgrLayer = new QAction( getThemeIcon( "mActionAddOgrLayer.png" ), tr( "Add Vector Layer..." ), this );
shortcuts->registerAction( mActionAddOgrLayer, tr( "Ctrl+Shift+V", "Add a Vector Layer" ) );
mActionAddOgrLayer->setStatusTip( tr( "Add a Vector Layer" ) );
Expand Down Expand Up @@ -1411,7 +1419,14 @@ void QgisApp::createMenus()

mLayerMenu = menuBar()->addMenu( tr( "&Layer" ) );

#ifdef HAVE_SPATIALITE
QMenu *newLayerMenu = mLayerMenu->addMenu( tr( "New ") );
newLayerMenu->addAction( mActionNewVectorLayer );
newLayerMenu->addAction( mActionNewSpatialiteLayer );
#else
mLayerMenu->addAction( mActionNewVectorLayer );
#endif

mLayerMenu->addAction( mActionAddOgrLayer );
mLayerMenu->addAction( mActionAddRasterLayer );
#ifdef HAVE_POSTGRESQL
Expand Down Expand Up @@ -3082,6 +3097,213 @@ void QgisApp::newVectorLayer()
addVectorLayers( fileNames, enc, "file" );
}

void QgisApp::newSpatialiteLayer()
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
{
return;
}

QgsNewSpatialiteLayerDialog spatialiteDialog( this );
if ( spatialiteDialog.exec() == QDialog::Rejected )
{
return;
}

QString geometrytype = spatialiteDialog.selectedType();
//QString fileformat = geomDialog.selectedFileFormat();
QString crsId = spatialiteDialog.selectedCrsId();
QString databaseName = spatialiteDialog.databaseName();
QString newLayerName = spatialiteDialog.layerName();
//QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );

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

// Build up the sql statement for creating the table
//
QString sql = QString("create table %1 (id integer primary key autoincrement").arg(newLayerName);
// iterate through the field names and add them to the create statement
// (use indexed access since this is just as fast as iterators
for ( int i = 0; i < items->size(); ++i )
{
QStringList field = items->at(i);
sql += QString(", %1 %2").arg(field.at(0)).arg(field.at(1));
}
// complete the create table statement
sql += ")";
std::cout << "Creating table in database " << databaseName.toUtf8().data() << std::endl;
std::cout << sql.toUtf8().data() << std::endl; // OK

QString sqlAddGeom = QString("select AddGeometryColumn('%1', 'geom', %2, '%3', 2)").arg(newLayerName).arg(crsId).arg(geometrytype);
std::cout << sqlAddGeom.toUtf8().data() << std::endl; // OK
QString sqlCreateIndex = QString("select CreateSpatialIndex('%1', 'geom')").arg(newLayerName);
std::cout << sqlCreateIndex.toUtf8().data() << std::endl; // OK

sqlite3 *db;
int rc = sqlite3_open( databaseName.toUtf8(), &db );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database: %1").arg(databaseName ) );
}
else
{
char * errmsg;
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this, tr( "Error deleting bookmark" ),
tr( "Failed to create the Spatialite table %1. The database returned:\n%2" ).arg( newLayerName ).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 );
}
}
}


}


/*
bool haveLastUsedFilter = false; // by default, there is no last
// used filter
QString enc;
QString fileName;
QSettings settings; // where we keep last used filter in
// persistent state
haveLastUsedFilter = settings.contains( "/UI/lastVectorFileFilter" );
QString lastUsedFilter = settings.value( "/UI/lastVectorFileFilter",
QVariant( QString::null ) ).toString();
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir",
"." ).toString();
QgsDebugMsg( "Saving vector file dialog without filters: " );
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( this,
tr( "Save As" ), lastUsedDir, "", QString( "" ) );
// allow for selection of more than one file
openFileDialog->setFileMode( QFileDialog::AnyFile );
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );
openFileDialog->setConfirmOverwrite( true );
if ( haveLastUsedFilter ) // set the filter to the last one used
{
openFileDialog->selectFilter( lastUsedFilter );
}
int res;
while (( res = openFileDialog->exec() ) == QDialog::Accepted )
{
fileName = openFileDialog->selectedFiles().first();
if ( fileformat == "ESRI Shapefile" )
{
if ( !isValidShapeFileName( fileName ) )
{
fileName += ".shp";
}
if ( !isValidShapeFileName( fileName ) )
{
QMessageBox::information( this,
tr( "New Shapefile" ),
tr( "Shapefiles must end on .shp" ) );
continue;
}
}
break;
}
if ( res == QDialog::Rejected )
{
delete openFileDialog;
return;
}
enc = openFileDialog->encoding();
// If the file exists, delete it otherwise we'll end up loading that
// file, which can cause problems (e.g., if the file contains
// linestrings, but we're wanting to create points, we'll end up
// with a linestring file).
if ( fileformat == "ESRI Shapefile" )
{
QgsVectorFileWriter::deleteShapeFile( fileName );
}
else
{
QFile::remove( fileName );
}
settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );
settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );
delete openFileDialog;
//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QString ogrlib = pReg->library( "ogr" );
// load the data provider
QLibrary* myLib = new QLibrary( ogrlib );
bool loaded = myLib->load();
if ( loaded )
{
QgsDebugMsg( "ogr provider loaded" );
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
const std::list<std::pair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
if ( createEmptyDataSource )
{
if ( geometrytype != QGis::WKBUnknown )
{
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs );
}
else
{
QgsDebugMsg( "geometry type not recognised" );
return;
}
}
else
{
QgsDebugMsg( "Resolving newEmptyDataSource(...) failed" );
}
}
//then add the layer to the view
QStringList fileNames;
fileNames.append( fileName );
//todo: the last parameter will change accordingly to layer type
addVectorLayers( fileNames, enc, "file" );
*/
}
void QgisApp::fileOpen()
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -265,6 +265,7 @@ class QgisApp : public QMainWindow
QAction *actionViewSeparator3() { return mActionViewSeparator3; }

QAction *actionNewVectorLayer() { return mActionNewVectorLayer; }
QAction *actionNewSpatialLiteLayer() { return mActionNewSpatialiteLayer; }
QAction *actionAddOgrLayer() { return mActionAddOgrLayer; }
QAction *actionAddRasterLayer() { return mActionAddRasterLayer; }
QAction *actionAddPgLayer() { return mActionAddPgLayer; }
Expand Down Expand Up @@ -488,6 +489,8 @@ class QgisApp : public QMainWindow
void fileNew( bool thePromptToSaveFlag );
//! Create a new empty vector layer
void newVectorLayer();
//! Create a new empty spatialite layer
void newSpatialiteLayer();
//! Print the current map view frame
void newPrintComposer();
void showComposerManager();
Expand Down Expand Up @@ -859,6 +862,7 @@ class QgisApp : public QMainWindow
QAction *mActionAnnotation;

QAction *mActionNewVectorLayer;
QAction *mActionNewSpatialiteLayer;
QAction *mActionAddOgrLayer;
QAction *mActionAddRasterLayer;
QAction *mActionAddPgLayer;
Expand Down

0 comments on commit f36bfa5

Please sign in to comment.