When you run the following Python code in the Python Console of QGIS 2.4,

uri = "file:///C:/data.csv?type=csv&xField=lng&yField=lat&spatialIndex=no&subsetIndex=no&watchFile=no"
vlayer = QgsVectorLayer(uri, 'Points', "delimitedtext")

you are presented with the Coordinate Reference System Selector popup window as shown below.

Is there a way to programmatically select a CRS instead of having a user manually select it?

enter image description here

Tried the following but the CRS Selector dialog window still appears.

uri = "file:///C:/data.csv?type=csv&xField=lng&yField=lat&spatialIndex=no&subsetIndex=no&watchFile=no"
vlayer = QgsVectorLayer(uri, 'Points', "delimitedtext")
vlayer.setCrs( QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) )
  • Theres setCoordinateSystem(...) which can be called on a vector layer object, but that probably doesnt stop the dialog from showing. qgis.org/api/…
    – til_b
    Oct 30, 2014 at 15:00
  • You could use the option "Use project CRS" from Options->CRS->CRS for new layers. Also you could change that settings by code before to call QgsVectorLayer accessing to the QSettings class
    – lrssvt
    Oct 30, 2014 at 15:17
  • @til_b Tried your suggestion vlayer.setCrs( QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) ) and the dialog window still appears. I think its because .setCRS() is called after QgsVectorLayer().
    – Nyxynyx
    Oct 30, 2014 at 16:25

2 Answers 2


In your case you can add the item to define the crs string to the URI like &crs=EPSG:4326.

Your code should look as follows:

uri = "file:///C:/data.csv?type=csv&xField=lng&yField=lat&spatialIndex=no&subsetIndex=no&watchFile=no&crs=EPSG:4326"
vlayer = QgsVectorLayer(uri, 'Points', "delimitedtext")


As said above, you have an alternative by accesing to the settings of QGIS, the follow snippet does what I suggest before and uses the CRS defined in your project:

from PyQt4.QtCore import QSettings

s = QSettings()
## possible values are: prompt, useProject, useGlobal
s.setValue("/Projections/defaultBehaviour", "useProject")

## here your code ##

Be careful if you change the QSettings as mentionned above, you should restore the default value after your process. I would really hate a plugin if this plugin is changing my preferences in the background.

You should do something like that :

from qgis.PyQt.QtCore import QSettings

s = QSettings()
# possible values are: prompt, useProject, useGlobal
default_value = s.value("/Projections/defaultBehaviour")
s.setValue("/Projections/defaultBehaviour", "useProject")

# here, add your layer to the mapcanvas

s.setValue("/Projections/defaultBehaviour", default_value)
  • 1
    For the records, to be extra-safe: put the second setValue into a finally: block while everything starting from the first setValue call is in the preceding try: block. Dec 27, 2016 at 21:26

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.