2

I'm trying to find a very simple solution to reproject a vector layer into a temporary layer in pyqgis, however documentation is really extensive and I'm fairly new to pyqgis; the only answer I've found here saves the layer, which I totally want to avoid. The code I've tried:

layer = QgsVectorLayer("/path/file.gpkg", "oko", "ogr")

epsg_crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)

layer.setCoordinateSystem(epsg_crs)
Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: QgsVectorLayer.setCoordinateSystem(): too many arguments
  • The answer you mentioned writes the input to a new output, because this way coordinates are changed/transformed to the new projection, while using setCoordinateSystem() just change the definition of the CRS and leaves the geometry as it is. So the idea to write a new output layer is correct, you can do that with a memory layer if you want. – Andreas Müller Mar 20 at 10:01
  • how should I write the code to set the temporal layer? – Elio Diaz Mar 20 at 19:34
1

You can use processing algorithm native:reprojectlayer

Example:

lyr= iface.activeLayer()
parameter = {'INPUT': lyr, 'TARGET_CRS': 'EPSG:4326',
                 'OUTPUT': 'memory:Reprojected'}

result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.