2

I want to create a layer which use the same CRS as another layer I already have in the project. The idea is to avoid the CRS dialog.

I tried the things in this link: How to define layer's CRS and avoid the CRS dialog in PyQGIS?

But it doesn't work because as soon as I create the layer the program launches the dialog.

qgis.utils.iface.addVectorLayer("shapefile_path", "Layer_name", "ogr")

#Use loaded layer as active layer to set the CRS
myLayer = qgis.utils.iface.activeLayer()
myLayer.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))

I tried something like

crs = originalLayer().crs().authid()
layer2 = QgsVectorLayer("Point", "auxlayer", "memory")
layer2.setCrs(QgsCoordinateReferenceSystem(crs))

And it works but only after the dialog appears.

1 Answer 1

4

This should work:

crs = originalLayer.crs().authid() # without () after originalLayer
layer2 = QgsVectorLayer("Point?crs={0}".format(crs), "auxlayer", "memory")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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