The code snippets on this page need the following imports if you’re outside the pyqgis console:

1from qgis.core import (
2    QgsCoordinateReferenceSystem,
3    QgsCoordinateTransform,
4    QgsProject,
5    QgsPointXY,

8. Supporto alle proiezioni

8.1. Sistemi di riferimento delle coordinate

I sistemi di riferimento delle coordinate (SR) sono incapsulati dalla classe QgsCoordinateReferenceSystem. Le istanze di questa classe possono essere create in diversi modi:

  • Specifica SR con il suo ID

    # EPSG 4326 is allocated for WGS84
    crs = QgsCoordinateReferenceSystem("EPSG:4326")

    QGIS supporta diversi identificatori SR con i seguenti formati:

Se non viene specificato alcun prefisso, viene assunta la definizione WKT.

  • specificare il SR con il suo well-known text (WKT)

    1wkt = 'GEOGCS["WGS84", DATUM["WGS84", SPHEROID["WGS84", 6378137.0, 298.257223563]],' \
    2      'PRIMEM["Greenwich", 0.0], UNIT["degree",0.017453292519943295],' \
    3      'AXIS["Longitude",EAST], AXIS["Latitude",NORTH]]'
    4crs = QgsCoordinateReferenceSystem(wkt)
  • create an invalid CRS and then use one of the create* functions to initialize it. In the following example we use a Proj string to initialize the projection.

    crs = QgsCoordinateReferenceSystem()
    crs.createFromProj("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

It’s wise to check whether creation (i.e. lookup in the database) of the CRS has been successful: isValid() must return True.

Note that for initialization of spatial reference systems QGIS needs to look up appropriate values in its internal database srs.db. Thus in case you create an independent application you need to set paths correctly with QgsApplication.setPrefixPath(), otherwise it will fail to find the database. If you are running the commands from the QGIS Python console or developing a plugin you do not care: everything is already set up for you.

Accessing spatial reference system information:

 1crs = QgsCoordinateReferenceSystem("EPSG:4326")
 3print("QGIS CRS ID:", crs.srsid())
 4print("PostGIS SRID:", crs.postgisSrid())
 5print("Description:", crs.description())
 6print("Projection Acronym:", crs.projectionAcronym())
 7print("Ellipsoid Acronym:", crs.ellipsoidAcronym())
 8print("Proj String:", crs.toProj())
 9# check whether it's geographic or projected coordinate system
10print("Is geographic:", crs.isGeographic())
11# check type of map units in this CRS (values defined in QGis::units enum)
12print("Map units:", crs.mapUnits())

In uscita:

1QGIS CRS ID: 3452
2PostGIS SRID: 4326
3Description: WGS 84
4Projection Acronym: longlat
5Ellipsoid Acronym: EPSG:7030
6Proj String: +proj=longlat +datum=WGS84 +no_defs
7Is geographic: True
8Map units: 6

8.2. Trasformazione SR

You can do transformation between different spatial reference systems by using the QgsCoordinateTransform class. The easiest way to use it is to create a source and destination CRS and construct a QgsCoordinateTransform instance with them and the current project. Then just repeatedly call transform() function to do the transformation. By default it does forward transformation, but it is capable to do also inverse transformation.

 1crsSrc = QgsCoordinateReferenceSystem("EPSG:4326")    # WGS 84
 2crsDest = QgsCoordinateReferenceSystem("EPSG:32633")  # WGS 84 / UTM zone 33N
 3transformContext = QgsProject.instance().transformContext()
 4xform = QgsCoordinateTransform(crsSrc, crsDest, transformContext)
 6# forward transformation: src -> dest
 7pt1 = xform.transform(QgsPointXY(18,5))
 8print("Transformed point:", pt1)
10# inverse transformation: dest -> src
11pt2 = xform.transform(pt1, QgsCoordinateTransform.ReverseTransform)
12print("Transformed back:", pt2)

In uscita:

Transformed point: <QgsPointXY: POINT(832713.79873844375833869 553423.98688333143945783)>
Transformed back: <QgsPointXY: POINT(18 5)>