Importante
La traduzione è uno sforzo comunitario you can join. Questa pagina è attualmente tradotta al 36.36%.
25.2.1. Plugin DB Manager
The DB Manager Plugin is intended to be the main tool to integrate and
manage spatial database formats supported by QGIS (PostgreSQL, SpatiaLite,
GeoPackage, Oracle Spatial, Virtual layers) in one user interface.
The
DB Manager Plugin provides several features.
You can drag layers from the QGIS Browser into the DB Manager, and it
will import your layer into your spatial database.
You can drag and drop tables between spatial databases and they will
get imported.
Fig. 25.4 La finestra di dialogo DB Manager
The menu allows you to connect to an existing database, to start the SQL window and to exit the DB Manager Plugin. Once you are connected to an existing database, the menus (relevant for DBMSs, such as PostgreSQL) and will appear.
Il menu include strumenti per creare e eliminare (solo se vuoti) schemi e, se la topologia è disponibile (ad esempio con PostGIS topology), per avviare un TopoViewer.
Il menu ti permette di creare e modificare tabelle e di cancellare tabelle e viste. È anche possibile svuotare tabelle e spostare tabelle tra schemi. Puoi Run Vacuum Analyze per la tabella selezionata. Vacuum recupera lo spazio e lo rende disponibile per il riutilizzo, e analyze aggiorna le statistiche che sono usate per determinare il modo più efficiente di eseguire una query. Change Logging… ti permette di aggiungere il supporto per la registrazione delle modifiche ad una tabella. Infine, puoi eseguire Importa Layer/File… e Esporta su File….
Nota
Usando il DB Manager è possibile aggiungere commenti per tabelle e colonne di un database PostgreSQL.
La finestra Sorgenti dati elenca tutti i database esistenti supportati da QGIS. Con un doppio clic, puoi connetterti al database. Con il tasto destro del mouse, puoi rinominare e cancellare schemi e tabelle esistenti. Le tabelle possono anche essere aggiunte a QGIS con il menu contestuale.
Se connesso a un database, la finestra principale del DB Manager offre quattro schede. La scheda Informazioni fornisce informazioni sulla tabella e la sua geometria, così come su campi, vincoli e indici esistenti. Permette di creare un indice spaziale sulla tabella selezionata. La scheda Tabella mostra la tabella, e la scheda Anteprima visualizza le geometrie come anteprima. Quando si apre una Finestra SQL, questa viene inserita in una nuova scheda.
25.2.1.1. Lavorare con la Finestra SQL
Puoi usare il DB Manager per eseguire query SQL sul tuo database spaziale. Le query possono essere salvate e caricate, e lì il Costruttore di interrogazioni SQL ti aiuterà a formulare le tue query. Puoi anche visualizzare l’output spaziale selezionando Carica come nuovo layer e specificando Colonna(e) con valori univoci (ID), Colonna geometria e Nome del layer (prefisso). È possibile evidenziare una parte dell’SQL per eseguire solo quella parte quando si preme Ctrl+R o si clicca il pulsante Esegui.
QGIS also adds support for the REGEXP function in some providers.
This allows users to use regular expressions in SQL filters or expressions, for example:
SELECT * FROM places WHERE name REGEXP '^A';
This returns all features where the name field starts with the letter A.
Dopo aver eseguito la query, puoi selezionare celle specifiche nell’insieme dei risultati. Utilizza la scorciatoia Ctrl+C per copiare le celle selezionate negli appunti. I dati copiati sono disponibili come tabella formattata. In questo modopuoi incollare i dati in altre applicazioni, ad esempio in un foglio di calcolo, dove verranno visualizzati come tabella.
Il pulsante Storico delle Interrogazioni memorizza le ultime 20 interrogazioni di ogni database e provider.
Un doppio clic su una voce aggiunge la stringa alla finestra SQL.
Fig. 25.5 Eseguire SQL nella finestra di dialogo SQL DB Manager
Nota
La finestra SQL può anche essere utilizzata per creare Layer Virtuali. In questo caso, invece di selezionare un database, seleziona QGIS Layer sotto Layer Virtuali prima di aprire la finestra SQL. Per istruzioni sulla sintassi SQL da utilizzare vedi Creazione di layer virtuali.
25.2.1.2. Import Vector Layer
You can import layer or file into your database. Here are the parameters you can set for the import process:
Input: Select the layer or file to import. Using the dropdown menu, select from the list of loaded layers in QGIS or click on the … button to select a file from disk. Check the
Import only selected features to import only the selected
features of the layer.Output table: Choose the Schema and provide a name for the new table.
Options: Here are some options for the import process:
Primary key: Provide naming for the primary key field.
By default, it is named id.
Geometry column: Provide naming for the geometry column.
By default, it is named geom.
Source SRID: Define the SRID for the geometry column.
By default, it uses the layer’s CRS.
Target SRID: Define the target SRID to reproject the geometries
during the import process. By default, it uses the layer’s CRS.
Encoding: Define the encoding of the source data. By default, it uses Automatic. It is QGIS’s automatic detection mode that attempts to guess the file’s character encoding based on available metadata or system locale.
Replace destination table (if exists): If a table with the same name already exists in the selected schema,
it will be replaced.
Do not promote to multi-part: Geometries will be imported as single-part geometries.
Convert field names to lower case: All field names will be converted to lower case.
Create spatial index: A spatial index will be created on the geometry column after import.
Comment: Add comments to table. Only available for PostgreSQL databases.
Fig. 25.6 Importing a vector layer into a spatial database using DB Manager
25.2.1.3. Export to Vector File
To export a table from your database to a vector file, select the desired Format and Save as location. Under Options, you can set the following parameters:
Source SRID: Define the source SRID of the geometry column.
By default, it uses the layer’s CRS.
Target SRID: Define the target SRID to reproject the geometries
during the export process. By default, it uses the layer’s CRS.
Replace destination table (if exists): If a table with the same name already exists at the selected location,
it will be replaced.
Fig. 25.7 Exporting a table to a vector file using DB Manager