QgsCoordinateReferenceSystem (QgsCRS)¶
The QgsCoordinateReferenceSystem (often abbreviated as QgsCRS) class in PyQGIS is used to represent and manipulate Coordinate Reference Systems (CRS). A CRS defines how the two-dimensional, projected map in your GIS relates to real places on the earth.
🔑 Key Responsibilities¶
- Define and store coordinate reference system information.
- Support for both EPSG codes and PROJ strings.
- Conversion between different CRS definitions.
- Check validity of CRS.
- Provide information about units, ellipsoid, and projections.
📌 Creating a CRS¶
From EPSG Code¶
From PROJ String¶
From WKT (Well-Known Text)¶
wkt = 'GEOGCS["WGS 84",DATUM["WGS_1984",...]]'
crs = QgsCoordinateReferenceSystem()
crs.createFromWkt(wkt)
📖 Useful Methods¶
| Method | Description |
|---|---|
isValid() |
Checks if the CRS is valid |
authid() |
Returns the authority ID (e.g., "EPSG:4326") |
description() |
Returns a human-readable description |
mapUnits() |
Returns units of measurement (e.g., degrees, meters) |
toWkt() |
Converts CRS to WKT format |
toProj() |
Returns PROJ string |
ellipsoidAcronym() |
Returns ellipsoid name/acronym |
⚙️ Example Usage¶
# Load CRS by EPSG code
crs = QgsCoordinateReferenceSystem("EPSG:32638")
if crs.isValid():
print("CRS is valid")
print("Auth ID:", crs.authid())
print("Description:", crs.description())
print("Units:", crs.mapUnits())
Output:
🔄 Reprojecting Layers to Another CRS¶
from qgis.core import QgsProject, QgsVectorLayer, QgsCoordinateTransform, QgsCoordinateReferenceSystem
# Load a vector layer
layer = QgsVectorLayer("path/to/shapefile.shp", "My Layer", "ogr")
# Define source and destination CRS
src_crs = layer.crs()
dest_crs = QgsCoordinateReferenceSystem("EPSG:4326")
# Setup transformation
transform = QgsCoordinateTransform(src_crs, dest_crs, QgsProject.instance())
# Transform a point
point = QgsPointXY(500000, 4649776)
reprojected_point = transform.transform(point)
print("Reprojected point:", reprojected_point)
🎯 Notes for Students¶
- Always check CRS validity with
isValid(). - EPSG codes are the most common and recommended way to define CRS.
- CRS mismatches are a common source of errors in GIS projects.
- Understanding CRS ensures accurate distance, area, and spatial analysis.