Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there a way to source a shapefile to a layer file in QGIS much like how you do this in the properties of an ArcGIS layer? (i.e. Go to layer properties, source tab and then just depress the 'Set Dat Source' button)

I've been all over the layer properties in QGIS and I don't see this anywhere...

Thanks, Mike

EDIT: I've received comments explaining that you cannot do this with shapefiles, but if I were to change the name of a shapefile and then open a pre-existing project file which contains this shapefile as a layer, I will get the 'Handle Bad Layers' dialogue, which allows me to navigate and re-source the layer to any shapefile I want to. I find it odd that you can resource in that scenario, but you cannot do it manually through the layer properties dialogue box.

share|improve this question

5 Answers 5

Currently this isn't possible, but there is a ticket for it. You can however change the datasource in the .qgs (project file) and reopen the project.

<projectlayers layercount="1">
    <maplayer minimumScale="-4.65661e-10" maximumScale="1e+08" minLabelScale="0" maxLabelScale="1e+08" geometry="Point" type="vector" hasScaleBasedVisibilityFlag="0" scaleBasedLabelVisibilityFlag="0">

Change the <datasource> line

share|improve this answer
Can you link to the ticket so others will know if/when it is complete? – RyanDalton Jun 3 '13 at 23:12
Always good when you say there is one then can't find the thing :) – Nathan W Jun 3 '13 at 23:17
Thanks,I looked at doing it this way, but the problem I have is that my users are not GIS people, they just need the project to view some data. I wanted to set up a project file and set all the symbology and labeling and then just havethem re-source the data layer for different sites. This method is beyond her skill set. I'm thinking I might be able to write a script that allows her to use a plugin to update the layers. – Mike Jun 4 '13 at 15:34
What about a DOS batch that simply copies one of the different shapefiles to a dummy one that is referenced in the project file, then start the project file. I don't know what happens if layer CRS or extents are different. – AndreJ Jun 5 '13 at 17:12
Probably easier to teach them how to add the data, style it and label. You can copy the style and paste it so that should be easy, don't know about labels. Since what you are describing isnt really GIS or editing it shouldn't be that difficult. Alternative maybe be to make a standalone program to replace source as a qgs file is just text – Antonio Locandro May 15 at 23:48

in python it is possible to directly modify datasource using QgsVectorLayer.writeLayerXML and QgsVectorLayer.readLayerXML modifying the DOM document on the fly and reloading layer as from example below.

from PyQt4.QtXml import *
from qgis.core import *
from PyQt4.QtXml import *

layer = self.iface.legendInterface().currentLayer()
newDatasource = "NEW DATASOURCE STRING" # get datasource from layer properties general     tab
newDatasourceProvider = "ogr" # possible values: (ogr, )

# read layer DOM definition
XMLDocument = QDomDocument("style")
XMLMapLayers = QDomElement()
XMLMapLayers = XMLDocument.createElement("maplayers")
XMLMapLayer = QDomElement()
XMLMapLayer = XMLDocument.createElement("maplayer")

# modify DOM element with new layer reference

# reload layer definition

# apply to canvas and legend

The feature has been included in PickLayer plugin that allows to perform actions on clicked layer and feature

share|improve this answer

If you work with shapefiles, no (due to the specific characteristics of the format). If you use PostGIS or SpatiaLite layers, a single vector layer (geometry_columns table) can be connected to multiple attributes tables, and inverse (with foreign keys or Views).

It is implemented natively in GRASS GIS, multiple tables for one vector layer and changing data source (

share|improve this answer

From release 2.10 QGis Api include QgsVectorLayer::setDataSource method:

void QgsVectorLayer::setDataSource  (   QString     dataSource,
        QString     baseName,
        QString     provider,
        bool    loadDefaultStyleFlag = false 

Update the data source of the layer.

The layer's renderer and legend will be preserved only if the geometry type of the new data source matches the current geometry type of the layer.

    dataSource  new layer data source
    baseName    base name of the layer
    provider    provider string
    loadDefaultStyleFlag    set to true to reset the layer's style to the default for the data source 
share|improve this answer

This can now be done easily with a plugin:


It adds a button when you right click on a vector layer "Change vector datasource", so just as easy as Arc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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