Skip to content

CRSHandler

Import
from pyqgis_wrapper.projection import CRSHandler
Warning

The reprojection import the processing library and needs to be executed within an environment able to use it.

You can still construct CRS without it.

CRSHandler(layer, feedback=QgsProcessingFeedback(), context=QgsProcessingContext())

Handles CRS construction and reprojection

Attributes:

Name Type Description
ALGS Dict[QgsMapLayer, str]

Processing algorithm for reprojection.

Parameters:

Name Type Description Default
layer qgis.core.QgsMapLayer

Layer to reproject

required
feedback qgis.core.QgsProcessingFeedback

Feedback object if executed within QGIS, defaults to QgsProcessingFeedback()

qgis.core.QgsProcessingFeedback()
context qgis.core.QgsProcessingContext

Context in which the algorithm is being used, defaults to QgsProcessingContext()

qgis.core.QgsProcessingContext()
Source code in pyqgis_wrapper/projection/crs_handler.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(
    self,
    layer: QgsMapLayer,
    feedback: QgsProcessingFeedback = QgsProcessingFeedback(),
    context: QgsProcessingContext = QgsProcessingContext(),
):
    """
    :param layer: Layer to reproject
    :type layer: QgsMapLayer
    :param feedback: Feedback object if executed
    within QGIS, defaults to QgsProcessingFeedback()
    :type feedback: QgsProcessingFeedback, optional
    :param context: Context in which the algorithm is
    being used, defaults to QgsProcessingContext()
    :type context: QgsProcessingContext, optional
    """
    self.layer = layer
    self.original_crs = layer.crs()
    self.project_crs = QgsProject.instance().crs()
    self.logger = create_child_logger(__name__)
    self.feedback = feedback
    self.context = context
    # CRS bounds: [min_lon, max_lon, min_lat, max_lat]
    # Only non geographic crs should be here
    self.available_crs = self._load_available_crs()

build_crs(crs) staticmethod

Build a QgsCoordinateReferenceSystem from an epsg code

Parameters:

Name Type Description Default
epsg_code typing.Union[str, int, qgis.core.QgsCoordinateReferenceSystem]

Either an int or a string or direclt a QgsCoordinateReferenceSystem It can be : "EPSG:4326", "4326" or 4326

required

Returns:

Type Description
QgsCoordinateTransform

Constructed CRS object

Source code in pyqgis_wrapper/projection/crs_handler.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
@staticmethod
def build_crs(
    crs: Union[str, int, QgsCoordinateReferenceSystem],
) -> QgsCoordinateReferenceSystem:
    """
    Build a QgsCoordinateReferenceSystem from an epsg code

    :param epsg_code: Either an int or a string or direclt a QgsCoordinateReferenceSystem
        It can be : "EPSG:4326", "4326" or 4326
    :type epsg_code: Union[str, int, QgsCoordinateReferenceSystem]

    :return: Constructed CRS object
    :rtype: QgsCoordinateTransform
    """
    if isinstance(crs, (int, str)):
        crs = CRSHandler._build_crs_from_epsg(crs)
    elif not isinstance(crs, QgsCoordinateReferenceSystem):
        raise TypeError(
            f"Provided CRS must be a string, an int or a "
            f"QgsCoordinateReferenceSystem, got {type(crs)}."
        )

    if not crs.isValid():
        raise ValueError(
            f"Provided CRS for reprojection is invalid, Got {crs.authid()}."
        )

    return crs

reproject(layer, target_crs=None, feedback=QgsProcessingFeedback(), context=QgsProcessingContext()) classmethod

Reproject a layer to a target CRS. If there is no target CRS provided then it'll reproject to an available CRS (m) if the layer is in a geographic (°) CRS.

Parameters:

Name Type Description Default
layer qgis.core.QgsMapLayer

Layer to reproject; It can be a vector, raster or point cloud layer.

required
target_crs typing.Optional[typing.Union[str, int, qgis.core.QgsCoordinateReferenceSystem]]

CRS to reproject the layer to.

None

Returns:

Type Description
QgsMapLayer

Reprojected layer

Source code in pyqgis_wrapper/projection/crs_handler.py
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
@classmethod
def reproject(
    cls,
    layer: QgsMapLayer,
    target_crs: Optional[Union[str, int, QgsCoordinateReferenceSystem]] = None,
    feedback: QgsProcessingFeedback = QgsProcessingFeedback(),
    context: QgsProcessingContext = QgsProcessingContext(),
) -> QgsMapLayer:
    """
    Reproject a layer to a target CRS.
    If there is no target CRS provided then it'll reproject
    to an available CRS (m) if the layer is in a geographic (°) CRS.

    :param layer: Layer to reproject; It can be a vector, raster or point cloud layer.
    :type layer: QgsMapLayer
    :param target_crs: CRS to reproject the layer to.
    :type target_crs: Optional[Union[str, int, QgsCoordinateReferenceSystem]]

    :return: Reprojected layer
    :rtype: QgsMapLayer
    """
    return cls(layer, feedback, context)._reproject_layer(target_crs)