Skip to main content
added 80 characters in body
Source Link
MrXsquared
  • 35.6k
  • 22
  • 71
  • 121

Yes, you can reproject a layer in use without having to create a new one.

Unfortunately, no there is (still) no such short and simple method. It would shorten up things, but basically if there would be such a method, it would also have to iterate over the geometries to reproject them. Since the code can be really short I dont see much of an issue in doing it like:

layer = iface.activeLayer()
source_crs = QgsCoordinateReferenceSystem(layer.crs().authid())
target_crs = QgsCoordinateReferenceSystem("EPSG:25832")
with edit(layer):
    for feat in layer.getFeatures():
        # you need to store the geometry in a variable to reproject it; you can not reproject without doing so
        geom = feat.geometry() 
        # transform the geometry
        geom.transform(QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance()))
        # now update the features geometry
        feat.setGeometry(geom)
        # dont forget to update the feature, otherwise the changes will have no effect
        layer.updateFeature(feat)
    # finally simply set the layers crs to the new one
    layer.setCrs(target_crs)

Also see:

Unfortunately, no there is (still) no such method. It would shorten up things, but basically if there would be such a method, it would also have to iterate over the geometries to reproject them. Since the code can be really short I dont see much of an issue in doing it like:

layer = iface.activeLayer()
source_crs = QgsCoordinateReferenceSystem(layer.crs().authid())
target_crs = QgsCoordinateReferenceSystem("EPSG:25832")
with edit(layer):
    for feat in layer.getFeatures():
        # you need to store the geometry in a variable to reproject it; you can not reproject without doing so
        geom = feat.geometry() 
        # transform the geometry
        geom.transform(QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance()))
        # now update the features geometry
        feat.setGeometry(geom)
        # dont forget to update the feature, otherwise the changes will have no effect
        layer.updateFeature(feat)
    # finally simply set the layers crs to the new one
    layer.setCrs(target_crs)

Also see:

Yes, you can reproject a layer in use without having to create a new one.

Unfortunately, no there is (still) no such short and simple method. It would shorten up things, but basically if there would be such a method, it would also have to iterate over the geometries to reproject them. Since the code can be really short I dont see much of an issue in doing it like:

layer = iface.activeLayer()
source_crs = QgsCoordinateReferenceSystem(layer.crs().authid())
target_crs = QgsCoordinateReferenceSystem("EPSG:25832")
with edit(layer):
    for feat in layer.getFeatures():
        # you need to store the geometry in a variable to reproject it; you can not reproject without doing so
        geom = feat.geometry() 
        # transform the geometry
        geom.transform(QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance()))
        # now update the features geometry
        feat.setGeometry(geom)
        # dont forget to update the feature, otherwise the changes will have no effect
        layer.updateFeature(feat)
    # finally simply set the layers crs to the new one
    layer.setCrs(target_crs)

Also see:

Source Link
MrXsquared
  • 35.6k
  • 22
  • 71
  • 121

Unfortunately, no there is (still) no such method. It would shorten up things, but basically if there would be such a method, it would also have to iterate over the geometries to reproject them. Since the code can be really short I dont see much of an issue in doing it like:

layer = iface.activeLayer()
source_crs = QgsCoordinateReferenceSystem(layer.crs().authid())
target_crs = QgsCoordinateReferenceSystem("EPSG:25832")
with edit(layer):
    for feat in layer.getFeatures():
        # you need to store the geometry in a variable to reproject it; you can not reproject without doing so
        geom = feat.geometry() 
        # transform the geometry
        geom.transform(QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance()))
        # now update the features geometry
        feat.setGeometry(geom)
        # dont forget to update the feature, otherwise the changes will have no effect
        layer.updateFeature(feat)
    # finally simply set the layers crs to the new one
    layer.setCrs(target_crs)

Also see: