Skip to content

Commit

Permalink
allow creation of multiple layers in spatialite creation dialog
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13337 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Apr 20, 2010
1 parent 37e7f3a commit fa8c8cc
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 267 deletions.
29 changes: 29 additions & 0 deletions resources/context_help/QgsNewSpatialiteLayerDialog-de_DE
@@ -0,0 +1,29 @@
<h3>Einen neuen SpatiaLite-Layer erzeugen</h3>
In diesem Dialog können Sie eine neue SpatiaLite-Datenbank und/oder einen leeren SpatiaLite Layer zur Bearbeitung erzeugen. Unten werden die Eingabemöglichkeiten des Dialogs beschrieben.
<h4>Datenbank</h4>
Wählen Sie die Datenbank aus der Liste. Diese Liste wird aus den gespeicherten
SpatiaLite-Verbindungen erzeugt. Wenn sie keine gespeicherten Verbindungen
haben oder oder eine neue Datenbank erzeugen wollen Klicken Sie auf
<label>...</label> rechts der Liste.
<h4>Layername</h4>
Geben Sie dem Layer, den Sie erzeugen wollen, einen Namen. Der Name sollte nur ein Wort sein. Wenn Sie wollen, können Sie auch Unterstriche verwenden
<h4>Geometriespalte</h4>
Geben Sie der Geometriespalte einen Namen oder übernehmen Sie die Voreinstellung.
<h4>Typ</h4>
Legen Sie den Layertyp fest, den Sie erzeugen wollen.
<h4>EPSG-SRID</h4>
Geben Sie einen EPSG-Code für die SRID (räumliche Bezugskennung) an. Für die SRID wird Ihnen WGS 84 vorgeschlagen.
Klicken Sie auf den Knopf <label>SRID finden</label> um das Koordinatenbezugssystem ggf. zu ändern.
Die SRID muss in der Tabelle spatial_ref_sys in Ihre Datenbank existieren. Sie können SRIDs suchen indem Sie Suchbegriffe verwenden, die
ganz oder teilweise zu Namen oder SRIDs passen.
<h4>Einen automatisch inkrementierenden Primärschlüssel erzeugen</h4>
Das Anwählen dieses Kontrolkästchen fügt dem Layer einen Primärschlüssel hinzu. Diese Feld wird automatisch inkrementiert, sodaß Sie keinen
keinen Wert dafür beim Hinzufügen von Objekten zum Layer eingeben müssen.
<h4>Neues Attribute</h4>
Fügen Sie die gewünschten Attribute durch Klicken auf den Knopf <label>Attribute zur Liste hinzufügen</label> nachdem Sie Namen und Type des Attributes festgelegt haben. Nur Fließkomma-, ganzzahlige und Zeichenkettenattribute werden unterstützt.<br/>
Breite und Genauigkeit sind irrelevant in einer SpatiaLite-Datenbank, daher brauchen Sie sie nicht anzugeben.
<h4>Attributliste</h4>
In diesem Abschnitt sehen Sie die Attributliste. Um eins davon zu löschen, klicken Sie auf den Knopf <label>Gewähltes Attribut löschen</label>.

<p>
Klicken Sie auf <label>Ok</label> um den Layer anzulegen und den Dialog zu schließen. Ein Klick auf <label>Anwenden</label> erzeugt den Layer auch, aber hält den Dialog auf und erlaubt so mehr ähnliche Layer zu erzeugen. <label>Abbruch</label> schließt den Dialog ohne weitere Änderung.
4 changes: 2 additions & 2 deletions resources/context_help/QgsNewSpatialiteLayerDialog-en_US
@@ -1,7 +1,7 @@
<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.
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.
<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>
Expand All @@ -19,4 +19,4 @@ Width and precision are irrelevant in a Spatialite database so you do not have t
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.
Click on <label>Ok</label> to create the layer and close the dialog. Clicking <label>Apply</label> also creates the layer, but keeps the dialog open, thereby allowing you to create more similar layers. <label>Cancel</label> will close the layer without further changes.
127 changes: 1 addition & 126 deletions src/app/qgisapp.cpp
Expand Up @@ -3098,18 +3098,6 @@ void QgisApp::newVectorLayer()
addVectorLayers( fileNames, enc, "file" );
}

static QString quotedIdentifier( QString id )
{
id.replace( "\"", "\"\"" );
return id.prepend( "\"" ).append( "\"" );
}

static QString quotedValue( QString value )
{
value.replace( "'", "''" );
return value.prepend( "'" ).append( "'" );
}

#ifdef HAVE_SPATIALITE
void QgisApp::newSpatialiteLayer()
{
Expand All @@ -3118,120 +3106,7 @@ void QgisApp::newSpatialiteLayer()
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();
QString newGeometryColumn = spatialiteDialog.geometryColumn();
//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 baseSQL;
if ( spatialiteDialog.includePrimaryKey() )
{
baseSQL = "create table %1(pkuid integer primary key autoincrement, ";
}
else
{
baseSQL = "create table %1(";
}
QString sql = baseSQL.arg( quotedIdentifier( 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 );
if ( i > 0 )
sql += ",";
sql += QString( "%1 %2" ).arg( quotedIdentifier( field.at( 0 ) ) ).arg( field.at( 1 ) );
}
// complete the create table statement
sql += ")";
QgsDebugMsg( QString( "Creating table in database %1" ).arg( databaseName ) );
QgsDebugMsg( sql ); // OK

QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
.arg( quotedValue( newLayerName ) )
.arg( quotedValue( newGeometryColumn ) )
.arg( crsId )
.arg( quotedValue( geometrytype ) );
QgsDebugMsg( sqlAddGeom ); // OK

QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" ).arg( quotedValue( newLayerName ) ).arg( quotedValue( newGeometryColumn ) );
QgsDebugMsg( sqlCreateIndex ); // OK

spatialite_init( 0 );

sqlite3 *db;
int rc = sqlite3_open( databaseName.toUtf8(), &db );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this,
tr( "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 Creating SpatiaLite Table" ),
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 );
}

QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(%3) sql=" )
.arg( databaseName )
.arg( newLayerName )
.arg( newGeometryColumn ), newLayerName, "spatialite" );
if ( layer->isValid() )
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
}
else
{
QgsDebugMsg( newLayerName + " is an invalid layer - not loaded" );
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( newLayerName ) );
delete layer;
}
}
}
}
spatialiteDialog.exec();
}
#endif

Expand Down

0 comments on commit fa8c8cc

Please sign in to comment.