Xem dữ liệu

Agent giúp bạn mở và xem thông tin các loại dữ liệu GIS: vector, raster, WMS, mesh, v.v.

T
Tác giả TLGeo
Thời gian đọc 7 min read
Đăng tải 2025-01-01

Xem dữ liệu (View data)

Đây là bước đầu tiên khi làm việc với QGIS: mở và xem dữ liệu. QGIS hỗ trợ rất nhiều định dạng, từ shapefile đến dữ liệu trực tuyến. TLGeo Agent giúp bạn thực hiện tác vụ này bằng ngôn ngữ tự nhiên.

Tính năng QGIS gốc: https://docs.qgis.org/3.44/en/docs/about/features.html#view-data

⚠️ TODO quan trọng: Hiện tại Agent chỉ có 1 tool liên quan là list_layers. Các tác vụ mở file, thêm layer, kết nối WMS/WFS chưa có tool riêng, Agent sẽ dùng execute_qgis_python_script để thực hiện. Bạn nên xác nhận với team dev về việc bổ sung các tool chuyên dụng.

Liệt kê dữ liệu đang mở

Liệt kê tất cả layer

Đã có tool (list_layers)

Liệt kê tất cả layer đang mở trong dự án
Có những layer nào trong dự án hiện tại?

Kết quả trả về:

📊 Tổng số layer: 8

Vector (5):
  1. ranh_gioi_tinh (Polygon) - 63 features
  2. ranh_gioi_huyen (Polygon) - 713 features
  3. lo_rung (Polygon) - 12,450 features
  4. diem_quan_trac (Point) - 234 features
  5. duong_giao_thong (LineString) - 5,678 features

Raster (3):
  6. dem_30m (Raster) - 1 band
  7. sentinel2_2024 (Raster) - 12 bands
  8. bing_satellite (Raster) - Web Map Service

Lọc theo loại

Chỉ liệt kê các layer vector
Layer nào là raster?

Đếm tổng số feature

Tổng cộng có bao nhiêu feature trong tất cả layer?

Mở dữ liệu local

Vector data

QGIS hỗ trợ nhiều định dạng vector:

  • Shapefile (.shp)
  • GeoPackage (.gpkg)
  • GeoJSON (.geojson)
  • KML/KMZ (.kml, .kmz)
  • CSV (có cột WKT cho geometry)
  • GML, MapInfo, ESRI File Geodatabase, …

Mở file:

Mở shapefile tại /data/ranh_gioi/lam_dong.shp
Mở GeoPackage tại /data/du_an.gpkg
Import tất cả shapefile trong thư mục /data/raw/

Code được sinh:

import os
from qgis.core import QgsVectorLayer, QgsProject

for shp in glob.glob('/data/raw/*.shp'):
    name = os.path.basename(shp)[:-4]
    layer = QgsVectorLayer(shp, name, 'ogr')
    if layer.isValid():
        QgsProject.instance().addMapLayer(layer)
        print(f'Added: {name}')

Raster data

QGIS hỗ trợ: GeoTIFF, ERDAS IMG, ArcInfo ASCII GRID, JPEG, PNG, …

Mở ảnh GeoTIFF tại /data/dem/dem_30m.tif
Import tất cả ảnh raster trong thư mục /data/satellite/

Mesh data

QGIS hỗ trợ TINs (Triangulated Irregular Networks) và regular grids.

Mở file mesh tại /data/wave_model.nc

Spreadsheets

QGIS có thể mở file ODS/XLSX nếu có cột WKT geometry.

Mở file Excel tại /data/diem_quan_trac.xlsx, cột WKT là "geom"

Kết nối dịch vụ OGC (WMS, WFS, WCS)

Đây là các dịch vụ bản đồ trực tuyến chuẩn OGC.

WMS (Web Map Service)

WMS trả về ảnh bản đồ (raster):

Thêm kết nối WMS tới https://ows.terrestris.de/osm/service
Thêm layer WMS "OSM" từ server https://ows.terrestris.de/osm/service

Code được sinh:

from qgis.core import QgsRasterLayer, QgsDataProvider

url = 'url=https://ows.terrestris.de/osm/service&format=image/png&layers=OSM-WMS'
layer = QgsRasterLayer(url, 'OSM WMS', 'wms')
if layer.isValid():
    QgsProject.instance().addMapLayer(layer)

WFS (Web Feature Service)

WFS trả về vector (cho phép truy vấn, chỉnh sửa):

Thêm kết nối WFS tới https://demo.geo-solutions.it/geoserver/wfs
Tải layer WFS "regions" từ server về dự án local

WMTS (Web Map Tile Service)

WMTS phục vụ tile (vuông) ảnh cache:

Thêm WMTS từ Google Maps satellite

⚠️ Google Maps có thể yêu cầu API key. TODO: Cần confirm flow này.

WCS (Web Coverage Service)

WCS trả về raster “thô” (cho phép phân tích):

Thêm WCS coverage "elevation" từ server

Vector Tiles

QGIS 3.x hỗ trợ Vector Tiles (.pbf, .mbtiles).

Mở file vector tile tại /data/tiles/asia.pbf
Kết nối tới OpenMapTiles vector tile service

GRASS data

QGIS có thể đọc trực tiếp dữ liệu từ GRASS database.

Mở GRASS location tại /grass_data/asia_permanent
Liệt kê các vector map có sẵn trong GRASS location "asia"

PostGIS, SpatiaLite, MS SQL Spatial, Oracle Spatial

PostGIS

Kết nối PostGIS: host=localhost port=5432 dbname=tl_geo user=admin password=xxx
Liệt kê các bảng trong schema "public" của PostGIS
Mở bảng "lo_rung" từ PostGIS vào dự án

SpatiaLite

Mở SpatiaLite database tại /data/gis.sqlite

MS SQL Spatial

Kết nối MS SQL server: server=sql.tlgeo.vn database=tl_geo

Oracle Spatial

Kết nối Oracle: host=oracle.tlgeo.vn service=ORCLPDB1

Xem thông tin dữ liệu

Thông tin cơ bản của layer

Cho tôi xem thông tin chi tiết của layer "lo_rung"

Kết quả:

📊 Layer: lo_rung
- Loại: Vector (Polygon)
- Đường dẫn: /data/forest/lo_rung.shp
- Số feature: 12,450
- CRS: EPSG:4326 - WGS 84
- Extent: 102.0, 8.0 → 109.5, 23.5
- Encoding: UTF-8
- Kích thước file: 45 MB

Cấu trúc bảng thuộc tính

Liệt kê các cột của layer "lo_rung"

Kết quả:

Tên cộtKiểuMô tả
idIntegerKhóa chính
ma_loStringMã lô rừng
loai_rungStringLoại rừng (tự nhiên/trồng)
dien_tichDoubleDiện tích (m²)
tinh_thanhStringTỉnh thành
huyenStringHuyện
xaString
trang_thaiStringTrạng thái

Thống kê nhanh

Layer "lo_rung" có bao nhiêu feature?
Tổng diện tích của layer "lo_rung" là bao nhiêu?

QGIS Browser

QGIS Browser là panel giúp duyệt file/datasource.

Mở QGIS Browser và duyệt thư mục /data
Tìm tất cả file .shp trong thư mục /data

Dữ liệu có yếu tố thời gian (Temporal)

QGIS hỗ trợ dữ liệu có chiều thời gian (time-aware).

Bật temporal panel

Bật Temporal Controller cho layer "track_2024"

Lọc theo thời gian

Lọc layer "track_2024" theo khoảng thời gian từ 2024-01-01 đến 2024-12-31

Animation

Tạo animation cho layer temporal từ 2020 đến 2024, mỗi frame 1 tháng

Authentication cho Web Services

Một số WMS/WFS yêu cầu đăng nhập.

Cấu hình authentication cho server https://secure.tlgeo.vn/wms (user: admin, password: xxx)

TODO cần bạn xác nhận

  • Nên phát triển các tool riêng cho mở layer, kết nối WMS/WFS?
  • Có cần UI confirm khi import nhiều file?
  • Workflow nào phổ biến nhất với người dùng Việt Nam (WMS từ nguồn nào)?
  • Có hỗ trợ nhập từ nguồn WFS cụ thể nào không (Vietnam admin boundaries, OSM, …)?

Lỗi thường gặp & cách xử lý

LỗiNguyên nhânCách xử lý
”File not found”Đường dẫn saiKiểm tra lại đường dẫn, dùng qgis_find_file
”Invalid CRS”File không có CRSGán CRS thủ công trong Layer Properties
”WMS connection timeout”Server chậm hoặc firewallKiểm tra URL, thử server khác
”Permission denied”Không đủ quyền đọcChạy với quyền cao hơn
”Unsupported format”Driver GDAL thiếuCài thêm GDAL driver
”Geometry invalid”Dữ liệu lỗiValidate trước khi mở (xem Bài 5)

Liên kết