21

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.

6 Answers 6

16

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)
3
10

After months later I found a solution. You should add two lines before and end of the script which is from @Domokos Endre:

import os

iface.mainWindow().blockSignals(True)

layer = QgsVectorLayer(path, "My Layer",  "ogr")
crs = layer.crs()
crs.createFromId(32637)  # Whatever CRS you want
layer.setCrs(crs)

QgsProject.instance().addMapLayer(layer)
iface.mainWindow().blockSignals(False)
7

Based on both this blog post and the answer to How can I specify the CRS of a raster layer in pyqgis?, you can achieve what you want this way:

settings = QSettings()
# Take the "CRS for new layers" config, overwrite it while loading layers and...
oldProjValue = settings.value( "/Projections/defaultBehavior", "prompt", type=str )
settings.setValue( "/Projections/defaultBehavior", "useProject" )

# YOUR CODE TO LOAD THE LAYER GOES HERE

# ... then set the "CRS for new layers" back
settings.setValue( "/Projections/defaultBehavior", oldProjValue )

Note: If you're still using QGIS v2.x (BTW, you shouldn't :P), use the variable name "/Projections/defaultBehaviour" instead.

0
5

The following code should allow you to add a vector layer without the CRS popup window from appearing (atleast it doesn't appear for me):

from qgis import *
from qgis.utils import *

#replace "shapefile_path" with your layer's path; replace "Layer_name" with whatever name you want to give the layer in the TOC
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))

#Sets canvas CRS
my_crs = core.QgsCoordinateReferenceSystem(4326, core.QgsCoordinateReferenceSystem.EpsgCrsId)
iface.mapCanvas().mapRenderer().setDestinationCrs(my_crs)

Simply change the 4326 CRS value in both cases to the Authority ID of your choice as shown in the image:

CRS options

4
  • 1
    Thanks for your answer! But the main problem is that when i load the layer a dialog appear where i could choose the crs. Is it possible to hide or disable it?
    – Hinne123
    Commented Feb 23, 2015 at 14:50
  • Edited my post, when I run the commands I do not receive a dialogue window. Hopefully you won't either!
    – Joseph
    Commented Feb 23, 2015 at 16:20
  • 2
    The CRS window only appears if the CRS has not been set in the code. As long as you set it you will be good. Commented Feb 23, 2015 at 16:27
  • @BritishSteel - Thank you for mentioning that! I did not know that was the case.
    – Joseph
    Commented Feb 23, 2015 at 16:30
1

For me any above solution doesn't work. Maybe because I'm loading csv as vector layer, not a shape file. The only way I can make it work is:

crs = QgsCoordinateReferenceSystem(2180)
self.iface.mapCanvas().mapRenderer().setDestinationCrs(crs)
vlayer = QgsVectorLayer(filePath, "layer name", "ogr")
vlayer.setCrs(QgsCoordinateReferenceSystem(2180))
0

This answer describes how to avoid this dialog by specifying the CRS in the URL used to load the file into memory in the first place. So instead of passing the Shapefile path directly to addVectorLayer(), create a QgsVectorLayer with the appropriate URL and then call addMapLayer().

import urlparse
import urllib

baseuri = urlparse.urljoin('file:', urllib.pathname2url(self.shpFilePath))
uri = baseuri + "?crs=EPSG:4326"
vlayer = QgsVectorLayer(uri, "result", "memory")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

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.