[GIS] Defining layer CRS and avoiding CRS dialog in PyQGIS

coordinate systempyqgisvector

There is an existing Shapefile, which I want to add.

At the moment my code looks like:

self.iface.addVectorLayer(self.shpFilePath, "Track", "ogr")

It works fine, but i want to define the crs within the code. Is it possible?
I found some similar questions like this one: Programmatically Select a CRS in QGIS 2.4 using Python

But i don't get it work anyway.

Best Answer

I solve the similar problem this way:

from qgis.core import *
from qgis.gui import *

layer = QgsVectorLayer(self.shpFilePath, "Track", "ogr")
crs = layer.crs()
crs.createFromId(4326)
layer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(layer)