5

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) )
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Nyxynyx
  • 1,687
  • 4
  • 23
  • 35
  • Theres setCoordinateSystem(...) which can be called on a vector layer object, but that probably doesnt stop the dialog from showing. http://qgis.org/api/classQgsVectorLayer.html#a511bf3d6ef9817f6972b57f9f6655142 – til_b Oct 30 '14 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 '14 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 '14 at 16:25

2 Answers2

4

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")

QgsMapLayerRegistry.instance().addMapLayer(vlayer)

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 ##
lrssvt
  • 1,921
  • 12
  • 9
2

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)
Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
etrimaille
  • 7,240
  • 34
  • 47
  • 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. – Matthias Kuhn Dec 27 '16 at 21:26