Outdated version of the documentation. Find the latest one here.

Support de projections

Système de coordonnées de référence

Les systèmes de coordonnées de référence (SCR) sont encapsulés par la classe : class: QgsCoordinateReferenceSystem . Les instances de cette classe peuvent être créées de différentes façons :

  • specify CRS by its ID:

    # PostGIS SRID 4326 is allocated for WGS84
    crs = QgsCoordinateReferenceSystem(4326, \
      QgsCoordinateReferenceSystem.PostgisCrsId)
    

    QGIS utilise trois identifiants différents pour chaque système de référence:

    • PostgisCrsId - IDs used within PostGIS databases.
    • InternalCrsId - IDs internally used in QGIS database.
    • EpsgCrsId - IDs assigned by the EPSG organization

    Sauf indication contraire dans le deuxième paramètre, le SRID de PostGIS est utilisé par défaut.

  • specify CRS by its well-known text (WKT):

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

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

Il faut vérifier si la création (c’est à dire la recherche dans la base de données) du SCR a réussi. La fonction :: func: isValid doit retourner : const: true.

Note that for initialization of spatial reference systems QGIS needs to lookup 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 QGIS python console or developing a plugin you do not care: everything is already set up for you.

Accessing spatial reference system information:

print "QGIS CRS ID:", crs.srsid()
print "PostGIS SRID:", crs.srid()
print "EPSG ID:", crs.epsg()
print "Description:", crs.description()
print "Projection Acronym:", crs.projectionAcronym()
print "Ellipsoid Acronym:", crs.ellipsoidAcronym()
print "Proj4 String:", crs.proj4String()
# check whether it's geographic or projected coordinate system
print "Is geographic:", crs.geographicFlag()
# check type of map units in this CRS (values defined in QGis::units enum)
print "Map units:", crs.mapUnits()

Projections

You can do transformation between different spatial reference systems by using QgsCoordinateTransform class. The easiest way to use it is to create source and destination CRS and construct QgsCoordinateTransform instance with them. 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:

crsSrc = QgsCoordinateReferenceSystem(4326)    # WGS 84
crsDest = QgsCoordinateReferenceSystem(32633)  # WGS 84 / UTM zone 33N
xform = QgsCoordinateTransform(crsSrc, crsDest)

# forward transformation: src -> dest
pt1 = xform.transform(QgsPoint(18,5))
print "Transformed point:", pt1

# inverse transformation: dest -> src
pt2 = xform.transform(pt1, QgsCoordinateTransform.ReverseTransform)
print "Transformed back:", pt2