Wichtig

Translation is a community effort you can join. This page is currently translated at 32.10%.

18.2. Lesson: Verwendung der DB-Verwaltung zur Arbeit mit räumlichen Datenbanken in QGIS

Wir haben schon eine ganze Menge von Datenbankoperationen mit Hilfe von QGIS und anderen Werkzeugen kennengelernt. Jetzt wollen wir uns die DB-Verwaltung ansehen. Sie enthält viele ähnliche Funktionalitäten sowie zusätzlich Werkzeuge zum Management.

Ziel dieser Lektion: Zu erlernen, wie mit Hilfe der QGIS DB-Verwaltung mit räumlichen Datenbanken interagiert.

18.2.1. basic Follow Along: Managen von PostGIS Datenbanken mit der DB-Verwaltung

Öffnen Sie als Erstes die DB-Verwaltung durch Auswahl von Datenbank –> DB-Verwaltung im Menü durch Auswahl der Schaltfläche DB-Verwaltung in der Werkzeugleiste.

dbManager

You should already see the previous connections we have configured and be able to expand the myPG section and its public schema to see the tables we have worked with in previous sections.

Als Erstes fällt uns vielleicht auf, dass einige Metadaten über die in der Datenbank enthaltenen Schemas angezeigt werden.

../../../_images/db_manager_dialog.png

Schemas sind eine Möglichkeit, Datentabellen und andere Objekte in einer PostgreSQL Datenbank zu gruppieren. Sie sind ein Behältnis für Zugriffsrechte und andere Einschränkungen. Das Management von PostgreSQL Schemas geht über den Umfang dieser Anleitung hinaus. Weitere Informationen hierzu findet man unter PostgreSQL documentation on Schemas. Man kann die DB-Verwaltung zur Erstellung neuer Schemas verwenden. Allerdings sind für die effektive Verwaltung andere Werkzeuge wie pgAdmin III oder die Kommandozeile erforderlich.

Die DB-Verwaltung kann auch zur Verwaltung der Tabellen in der Datenbank verwendet werden. Wir haben bereits einige Möglichkeiten der Erstellung und Verwaltung von Tabellen mit Hilfe der Kommandozeile behandelt. Sehen wir uns nun an, wie man das mit der DB-Verwaltung erledigen kann.

Als Erstes ist es nützlich, sich die Metadaten einer Tabelle anzusehen. Man klickt dazu im Baum auf den Namen der Tabelle und schaut sich den Info Reiter an.

../../../_images/table_info.png

In diesem Bedienfeld sehen wie die Allgemeinen Informationen über die Tabelle als auch die Informationen über die Geometrie und das räumliche Bezugssystem, die von der PostGIS Erweiterung bereitgestellt werden.

Wenn man im Info Reiter weiter nach unten scrollt, sieht man weitere Informationen zu Felder, Constraints und Indexes bezüglich der aktuellen Tabelle.

../../../_images/table_info_fields.png

Der DB-Manager ist außerdem sehr nützlich, um sich Datensätze anzusehen - ähnlich der Attributtabelle eines Layers im Layerbaum. Sie können die Daten im Reiter Tabelle durchsehen.

../../../_images/table_panel.png

Es gibt auch einen Vorschau Reiter, der die Layerdaten als Kartenvorschau zeigt.

Mit Rechtsklick auf den Layer im Baum und Klick auf Zur Karte hinzufügen wird der Layer in Ihre Karte eingefügt.

Bis hierher haben wir nur die Datenbank, ihre Schemas, Tabellen und Metadaten angezeigt. Was ist nun, wenn man Tabellen ändern möchte und z.B. eine neue Spalte hinzufügen möchte? Die DB-Verwaltung erlaubt uns, dass direkt zu machen.

  1. Wählen Sie die Tabelle, die sie bearbeiten möchten im Baum

  2. Select Table ► Edit Table from the menu, to open the Table Properties dialog.

    ../../../_images/edit_table.png

Sie können der Dialog verwenden, um Spalten oder Geometriespalten hinzuzufügen, vorhandene Spalten zu bearbeiten oder Spalten komplett zu entfernen.

Mit Hilfe des Reiters Restriktionen können Sie verwalten, welche Felder als Primärschlüssel verwendet werden oder um vorhandene Einschränkungen zu entfernen.

../../../_images/constraints_panel.png

Im Reiter Indizes können normale oder räumliche Indizes hinzugefügt oder gelöscht werden.

../../../_images/indexes_panel.png

18.2.2. basic Follow Along: Erstellen einer neuen Tabelle

Nachdem wir den Prozess zur Arbeit mit vorhandenen Tabellen durchgegangen sind, nutzen wir die DB-Verwaltung jetzt zur Erstellung einer neuen Tabelle.

  1. If it is not already open, open the DB Manager window, and expand the tree until you see the list of tables already in your database.

  2. Wählen Sie Tabelle –> Tabelle erzeugen um den Dialog zu öffnen.

  3. Use the default Public schema and name the table places.

  4. Add the id, place_name, and elevation fields as shown below

  5. Make sure the id field is set as the primary key.

  6. Click the checkbox to Create geometry column and make sure it is set to a POINT type and leave it named geom and specify 4326 as the SRID.

  7. Setzen Sie den Haken bei Räumlichen Index erzeugen und klicken dann auf Erzeugen, um die Tabelle zu erstellen.

    ../../../_images/create_table.png
  8. Dismiss the dialog letting you know that the table was created and click Close to close the Create Table Dialog.

You can now inspect your table in the DB Manager and you will of course find that there is no data in it. From here you can Toggle Editing on the layer menu and begin to add places to your table.

18.2.3. basic Follow Along: Einfache Datenbankadministration

The DB Manager will also let you do some basic database administration tasks. It is certainly not a substitute for a more complete database administration tool, but it does provide some functionality that you can use to maintain your database.

Datenbanktabellen können oftmals sehr groß werden und oft veränderte Tabellen können Überbleibsel zurücklassen, die nicht mehr von PostgreSQL benötigt werden. Der Befehl VACUUM stellet eine Art Müllabfuhr dar. Er verringert die Größe der Tabellen. Bei Bedarf analysiert er die Tabellen, um eine höhere Geschwindigkeit der Datenbank zu erreichen.

Let us take a look at how we can perform a VACUUM ANALYZE command from within DB Manager.

  1. Select one of your tables in the DB Manager Tree

  2. Select Table ► Run Vacuum Analyze from the menu

PostgreSQL will now perform the operation. Depending on how big your table is, this may take some time to complete.

Weitere Informationen zum Prozess VACUUM ANALYZE finden Sie unter PostgreSQL Documentation on VACUUM ANALYZE.

18.2.4. basic Follow Along: Executing SQL Queries with DB Manager

DB Manager also provides a way for you to write queries against your database tables and to view the results. We have already seen this type of functionality in the Browser panel, but lets look at it again here with DB Manager.

  1. Select the lines table in the tree.

  2. Select the SQL window button in the DB Manager toolbar.

    ../../../_images/sql_window_btn.png
  3. Compose the following SQL query in the space provided:

    select * from lines where roadtype = 'major';
    
  4. Click the Execute (F5) button to run the query.

  5. You should now see the records that match in the Result panel.

    ../../../_images/sql_results.png
  6. Click the checkbox for Load as new layer to add the results to your map.

  7. Select the id column as the Column with unique integer values and the geom column as the Geometry column.

  8. Enter roads_primary as the Layer name (prefix).

  9. Click Load now! to load the results as a new layer into your map.

    ../../../_images/sql_add_to_map.png

The layers that matched your query are now displayed on your map. You can of course use this query tool to execute any arbitrary SQL command including many of the ones we looked at in previous modules and sections.

18.2.5. Importing Data into a Database with DB Manager

We have already looked at how to import data into a spatial database using command line tools, so now let’s learn how to use DB Manager to do imports.

  1. Click the Import layer/file button on the toolbar in the DB Manager dialog.

    ../../../_images/import_layer_btn.png
  2. Select the urban_33S.shp file from exercise_data/projected_data as the input dataset

  3. Click the Update Options button to pre-fill some of the form values.

  4. Make sure that the Create new table option is selected

  5. Specify the Source SRID as 32722 and the Target SRID as 4326

  6. Enable the checkbox to Create Spatial Index

  7. Click OK to perform the import

    ../../../_images/import_urban.png
  8. Dismiss the dialog letting you know that the import was successful

  9. Click the Refresh button on the DB Manager Toolbar

You can now inspect the table in your database by clicking on it in the Tree. Verify that the data has been reprojected by checking that the Spatial ref: is listed as WGS 84 (4326).

../../../_images/urban_info.png

Right clicking on the table in the Tree and a selecting Add to Canvas will add the table as a layer in your map.

18.2.6. Exporting Data from a Database with DB Manager

Of course DB Manager can also be used to export data from your spatial databases, so lets take a look at how that is done.

  1. Select the lines layer in the Tree and click the Export to File button on the toolbar to open the Export to vector file dialog.

  2. Click the button to select the Output file and save the data to your exercise_data directory as urban_4326.

  3. Set the Target SRID as 4326.

  4. Click OK to initialize the export.

    ../../../_images/export_to_vector.png
  5. Dismiss the dialog letting you know the export was successful and close the DB Manager.

You can now inspect the shapefile you created with the Browser panel.

../../../_images/inspect_vector_output.png

18.2.7. In Conclusion

You have now seen how to use the DB Manager interface in QGIS to manage your spatial databases, to execute SQL queries against your data and how to import and export data.

18.2.8. What’s Next?

Next, we will look at how to use many of these same techniques with SpatiaLite databases.