30

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...

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.

6 Answers 6

29

This can now be done easily with a plugin:

changeDataSource

https://geogear.wordpress.com/2015/09/30/changedatasource-plugin-release-1-0/

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

2
  • Unfortunately this feature won't work if the datasource you want to switch to is in an ESRI File Geodatabase (through Open FileGDB) as that data type is not listed in the subsequent pop up window.
    – user25644
    Oct 26, 2017 at 22:15
  • 5
    @hilpers: Starting with QGIS 3.6.0, this is a core functionality
    – Jake
    Apr 17, 2020 at 8:18
10

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">
        <id>graduated_classes20130603233806207</id>
        <datasource>../Downloads/Grauated_classes_test_sample/graduated_classes.shp</datasource>
        <title></title>
        <abstract></abstract>

Change the <datasource> line

5
  • 6
    Can you link to the ticket so others will know if/when it is complete? Jun 3, 2013 at 23:12
  • Always good when you say there is one then can't find the thing :)
    – Nathan W
    Jun 3, 2013 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, 2013 at 15:34
  • 1
    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, 2013 at 17:12
  • 1
    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 May 15, 2015 at 23:48
4

Right click layer, chose "Change Data Source" option since QGIS version 3.20.

2
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format.
    – PolyGeo
    May 30, 2021 at 2:05
  • There seems to be a problem that has crept in sometime after QGIS3: if I have created the project using a bunch of data sources, then moved them, when I reopen the project it opens the 'Handle unavailable layers' dialogue. But whatever I do, this dialog simply does not work. It appears to work: when I chose the new path and say 'Apply' it inserts the new path into the datasource table. But the layers still don't become visible after the dialogue closes. The layers show error buttons on the right, and I have to right click on individual layers and fix the data source all over again. Any help?
    – Sharad
    Feb 27, 2022 at 11:12
3

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")
layer.writeLayerXML(XMLMapLayer,XMLDocument)

# modify DOM element with new layer reference
XMLMapLayer.firstChildElement("datasource").firstChild().setNodeValue(newDatasource)
XMLMapLayer.firstChildElement("provider").firstChild().setNodeValue(newDatasourceProvider)
XMLMapLayers.appendChild(XMLMapLayer)
XMLDocument.appendChild(XMLMapLayers)

# reload layer definition
self.layer.readLayerXML(XMLMapLayer)
self.layer.reload()

# apply to canvas and legend
self.iface.actionDraw().trigger()
self.iface.legendInterface().refreshLayerSymbology(self.layer)

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

2

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.

Parameters
    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 
1

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 (v.to.db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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