Important

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

18.2. Lesson: Utilizarea DB Manager din QGIS, în lucrul cu bazele de date spațiale

Am văzut deja cum se pot efectua în QGIS multe operații cu bazele de date, la fel de simplu ca și cu celelalte instrumente, dar acum este timpul să ne uităm la instrumentul DB Manager, care oferă o mare parte din aceeași funcționalitate, similar cu alte instrumente dedicate managementului.

Scopul acestei lecții: De a se învăța interacțiunea cu bazele de date raster, folosind interfața DB Manager din QGIS.

18.2.1. basic Follow Along: Gestionarea Bazelor de date PostGIS cu ajutorul DB Manager

Ar trebui să deschideți, mai întâi, interfața DB Manager, mergând la Baza de date –> DB Manager –> DB Manager din meniu sau prin selectarea pictogramei DB Manager de pe bara de instrumente.

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.

Primul lucru care se remarcă este faptul că, de acum, puteți vedea unele metadate ale Schemelor conținute în baza de date.

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

Schemas are a way of grouping data tables and other objects in a PostgreSQL database and a container for permissions and other constraints. Managing PostgreSQL schemas is beyond the scope of this manual, but you can find more information about them in the PostgreSQL documentation on Schemas. You can use the DB Manager to create new Schemas, but will need to use a tool like pgAdmin III or the command line interface to manage them effectively.

De asemenea, DB Manager se poate folosi pentru a administra tabelele din baza de date. Am analizat deja diferite moduri de creare și gestionare a tabelelor din linia de comandă, dar acum dorim să vedem cum se poate face acest lucru în DB Manager.

First, its useful to just look at a table’s metadata by clicking on its name in tree and looking in the Info tab.

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

In this panel you can see the General Info about the table as well the information that the PostGIS extension maintains about the geometry and spatial reference system.

If you scroll down in the Info tab, you can see more information about the Fields, Constraints and Indexes for the table you are viewing.

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

Its also very useful to use DB Manager to simply look at the records in the database in much the same way you might do this by viewing the attribute table of a layer in the Layer Tree. You can browse the data by selecting the Table tab.

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

Există, de asemenea o filă Preview, care vă va arăta datele stratului într-o hartă de previzualizare.

Click-dreapta pe unul dintre straturi și, făcând clic pe Add to Canvas, acesta se va adăuga pe hartă.

So far we have only been viewing the database its schemas and tables and their metadata, but what if we wanted to alter the table to add an additional column perhaps? DB Manager allows you to do this directly.

  1. Selectați din arbore tabela pe care doriți să o editați

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

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

Puteți folosi acest dialog pentru a Adăuga Coloane, Coloane pentru geometrii, pentru a edita coloanele existente sau pentru a elimina complet o coloană.

Using the Constraints tab, you can manage which fields are used as the primary key or to drop existing constraints.

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

Fila Indecșilor poate fi folosită pentru a adăuga și șterge atât indicii spațiali, cât și cei normali.

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

18.2.2. |hard| Follow Along: Crearea unei Noi Tabele

Acum, că am trecut prin procesul de lucru cu tabelele existente în baza noastră de date, haideți să folosim DB Manager pentru a crea o nouă tabelă.

  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. Selectați meniul :guilabel:`Table –> Create Table`pentru a deschide dialogul de Creare a Tabelei.

  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. Faceți clic pe caseta de bifare de Creare a indexului spațial, apoi pe Create pentru a crea tabela.

    ../../../_images/create_table.png
  8. Închideți dialogul care vă informează că tabela s-a creat cu succes, apoi faceți clic pe Close pentru a închide Dialogul de Creare a Tabelei.

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: Tehnici de bază pentru administrarea bazei de date

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.

Database tables can often become quite large and tables which are being modified frequently can end up leaving around remnants of records that are no longer needed by PostgreSQL. The VACUUM command takes care of doing a kind of garbage collection to compact and optional analyze your tables for better performance.

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.

You can find more information about the VACUUM ANALYZE process in the PostgreSQL Documentation on VACUUM ANALYZE.

18.2.4. basic Follow Along: Executarea Interogărilor SQL cu ajutorul 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. Selectați butonul SQL window din bara de instrumente DB Manager.

    ../../../_images/sql_window_btn.png
  3. Compuneți următoarea Interogare SQL în spațiul furnizat:

    select * from lines where roadtype = 'major';
    
  4. Clic pe butonul Execute (F5) pentru a rula interogarea.

  5. Ar trebui să vedeți acum înregistrările care corespund panoului Rezultate.

    ../../../_images/sql_results.png
  6. Faceți clic pe caseta de bifare Load as new layer pentru a adăuga rezultatele în harta dvs.

  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. Faceți clic pe Load now! pentru a încărca rezultatele ca un nou strat în harta dvs.

    ../../../_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. Importarea datelor dintr-o Bază de date cu ajutorul 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. Clic pe butonul Import layer/file din Bara de Instrumente a dialogului DB Manager.

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

  3. Clic pe butonul Opțiunilor de actualizare pentru a pre-completa unele din valorile formularului.

  4. Asigurați-vă că este selectată opțiunea Creare tabelă nouă.

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

  6. Activați caseta de bifare pentru a Crea îndexul Spațial

  7. Click OK to perform the import

    ../../../_images/import_urban.png
  8. Închideți dialogul care vă informează că importul a avut loc cu succes

  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

Click-dreapta pe unul dintre straturile din Arbore și apoi, făcând clic pe Adăugare la Canevas, tabela se va adăuga pe hartă, sub formă de strat.

18.2.6. Exportul datelor cu DB Manager dintr-o Bază de date

De asemenea, DB Manager se poate utiliza pentru exportul datelor din bazele de date spațiale, așa că haideți să aruncăm o privire la modul în care se face aceasta.

  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. Clic OK pentru a inițializa exportul.

    ../../../_images/export_to_vector.png
  5. Închideți dialogul care vă informează că exportul a avut loc cu succes, apoi închideți DB Manager.

Puteți inspecta de acum fișierul shape pe care l-ați creat cu panoul de Răsfoire.

../../../_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.