중요

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

18.2. Lesson: QGIS에서 DB 관리자를 통해 공간 데이터베이스 사용

벌써 QGIS는 물론 다른 도구들을 통해 여러 가지 데이터베이스 작업을 수행하는 방법을 살펴보았습니다. 그러나 이제 그와 동일한 기능은 물론 관리 지향 도구들을 더 많이 제공하는 DB 관리자 도구에 대해 배워볼 때입니다.

이 강의의 목표: QGIS DB 관리자를 이용해서 공간 데이터베이스와 상호 작용을 하는 방법을 배우기.

18.2.1. basic Follow Along: DB 관리자를 통해 PostGIS 데이터베이스 관리

먼저 메뉴에서 Database ► DB Manager ► DB Manager 항목을 선택하거나 툴바에서 다음 DB 관리자 아이콘을 클릭해서 DB 관리자 인터페이스를 실행해야 합니다.

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.

사용자 데이터베이스가 담고 있는 스키마에 대한 몇몇 메타데이터를 볼 수 있다는 사실을 알 수 있습니다.

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

사용자 데이터베이스 내부의 테이블을 관리하는 데에도 DB 관리자를 사용할 수 있습니다. 벌써 커맨드라인에서 테이블을 생성하고 관리하는 다양한 방법을 배웠지만, 이제 DB 관리자에서 이런 작업을 하는 방법을 배워보겠습니다.

먼저 트리에서 테이블명을 클릭한 다음, Info 탭에서 테이블의 메타데이터를 살펴보면 도움이 됩니다.

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

이 패널에서 테이블에 대한 General Info 는 물론 PostGIS 확장 프로그램이 도형 및 공간 참조 시스템에 대해 유지관리하고 있는 정보를 볼 수 있습니다.

Info 탭을 스크롤다운할 경우, 현재 보고 있는 테이블의 Fields, Constraints, Indexes 에 대한 자세한 정보를 볼 수 있습니다.

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

DB 관리자를 사용하면 레이어 트리에 있는 레이어의 속성 테이블을 보는 것과 동일한 방식으로 간단히 데이터베이스 내부의 레코드를 볼 수 있다는 것도 도움이 됩니다. Table 탭을 선택하면 데이터를 둘러볼 수 있습니다.

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

레이어의 데이터를 맵 프리뷰로 보여주는 Preview 탭도 있습니다.

트리에 있는 레이어를 오른쪽 클릭한 다음 Add to Canvas 를 선택하면 해당 레이어를 사용자 맵에 추가할 것입니다.

지금까지 데이터베이스의 스키마와 테이블의 메타데이터를 살펴보기만 했지만, 테이블을 수정하거나 새 열을 추가하려면 어떻게 해야 할까요? DB 관리자에서 직접 이런 작업을 할 수 있습니다.

  1. 트리에서 편집하고자 하는 테이블을 선택하십시오.

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

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

이 대화 창에서 열을 추가하고, 도형 열을 추가하고, 기존 열을 편집하거나 완전히 삭제할 수 있습니다.

Constraints 탭에서 어떤 필드를 기본 키로 사용할지 관리하거나 기존 제약을 드롭할 수도 있습니다.

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

Indexes 탭에서 공간 및 일반 인덱스 모두를 추가하거나 삭제할 수 있습니다.

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

18.2.2. basic Follow Along: 새 테이블 생성

이제 데이터베이스에 있는 기존 테이블을 작업하는 과정을 배웠으니, DB 관리자를 이용해서 새 테이블을 생성해봅시다.

  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. 메뉴의 Table ► Create Table 항목을 선택해서 Create Table 대화 창을 여십시오.

  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. Create spatial index 항목의 체크박스를 체크한 다음, Create 를 클릭해서 테이블을 생성합니다.

    ../../../_images/create_table.png
  8. 테이블이 생성되었다고 알려주는 대화 창을 닫고, Close 를 클릭해서 Create Table 대화 창을 닫으십시오.

이제 DB 관리자에서 새 테이블을 검사해보면 물론 어떤 데이터도 없다는 사실을 알게 될 겁니다. 이때 레이어 메뉴의 Toggle Editing 을 통해 해당 테이블에 places 데이터를 추가할 수 있습니다.

18.2.3. basic Follow Along: 기본적인 데이터베이스 관리

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.

종종 데이터베이스 테이블이 아주 커질 수도 있고 자주 수정되는 테이블의 경우 PostgreSQL이 더 이상 필요로 하지 않는 레코드 찌꺼기가 남게 될 수도 있습니다. VACUUM 명령어는 일종의 쓰레기 정리 작업을 수행하며, 또 좀 더 나은 퍼포먼스를 위해 사용자 테이블을 분석하는 옵션도 갖추고 있습니다.

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: DB 관리자에서 SQL 쿼리 실행

DB 관리자는 사용자 데이터베이스에 대해 쿼리를 작성하고 그 결과를 살펴볼 수 있는 방법도 제공합니다. 이미 Browser 패널에서 이런 유형의 기능을 배웠지만, DB 관리자에서도 다시 살펴보도록 하겠습니다.

  1. Select the lines table in the tree.

  2. DB 관리자 툴바에서 다음 SQL window 버튼을 선택하십시오.

    ../../../_images/sql_window_btn.png
  3. 쿼리 작성 공간에 다음 SQL query 를 작성하십시오.

    select * from lines where roadtype = 'major';
    
  4. Execute (F5) 버튼을 클릭해서 쿼리를 실행하십시오.

  5. 이제 Result 패널에서 쿼리와 일치하는 레코드를 볼 수 있을 겁니다.

    ../../../_images/sql_results.png
  6. 이 결과를 사용자 맵에 추가하려면 Load as new layer 항목의 체크박스를 체크하십시오.

  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. Load now! 를 클릭하십시오. 이 결과가 사용자 맵에 새 레이어로 뜰 것입니다.

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

이제 사용자 쿼리와 일치하는 레이어가 사용자 맵에 나타났습니다. 물론 이 쿼리 도구를 사용해서 이전 강의들에서 살펴보았던 많은 명령어를 포함하는 어떤 임의의 SQL 명령어라도 실행할 수 있습니다.

18.2.5. 데이터베이스에 DB 관리자를 통해 데이터 임포트

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. DB 관리자 툴바에서 다음 Import layer/file 버튼을 클릭하십시오.

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

  3. Update Options 버튼을 클릭해서 일부 서식값을 미리 채우십시오.

  4. Create new table 옵션을 반드시 선택하도록 하십시오.

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

  6. Create Spatial Index 항목의 체크박스를 체크하십시오.

  7. Click OK to perform the import

    ../../../_images/import_urban.png
  8. 임포트가 성공했다고 알려주는 대화 창을 닫으십시오.

  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

트리에 있는 해당 테이블을 오른쪽 클릭한 다음 Add to Canvas 를 선택하면 해당 레이어를 사용자 맵에 추가할 것입니다.

18.2.6. 데이터베이스에서 DB 관리자를 통해 데이터 익스포트

DB 관리자는 물론 사용자의 공간 데이터베이스에서 데이터를 익스포트할 수도 있으므로, 그 방법을 배워보겠습니다.

  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. :guilabel:`OK`를 클릭해서 익스포트를 시작하십시오.

    ../../../_images/export_to_vector.png
  5. 임포트가 성공했다고 알려주는 대화 창을 닫고, DB 관리자 창도 닫으십시오.

이제 브라우저 패널에서 방금 생성한 shapefile을 검사할 수 있습니다.

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