Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

After creating a layer with RT_SQL_Layer plugin, I want to change some details in the query. Is it possible? or I have to aways create a new layer every time I want to change something in the query?

More specifically, I want to change the "Source for This Layer" as shown in example bellow:

enter image description here

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

You cannot change the query used in RT SQL Layer plugin. But you can try to only use a general query in RT and refine it in Layer Properties - General - Query builder. The part in Query builder can be easily adjusted while working with the same layer.

share|improve this answer
    
Thats one solution. But for the given example, where the purpose of the query is to create a 3 line label the problem persists. Do you know if it can be changed with python? –  Pablo Feb 15 '11 at 17:31
add comment

EDIT: This is not ideal but it should work if you don't need to do it in the UI. You can open and edit the qgs project files as the connection strings and sql string is stored in there. When using postgres search for <datasource> in the qgs project file. As the file is xml you can just edit the string and reload the project file.

via Python:

You can kind of get half way there using the python APIs although you need to set the layers providers datasource and also the datasource on the layer itself, which you can't do......yet.

from PyQt4.QtCore import QString
qgis.console.clearConsole()
layer = qgis.utils.iface.mapCanvas().currentLayer()
pro = layer.dataProvider()
print "The old providers datasource"
pro.dataSourceUri()
print "Set the providers datasource"
pro.setDataSourceUri(QString(u'dbname=\'postgis\' host=localhost port=5432 user=\'postgres\' key=\'property_no\' table="(SELECT \\"t_0\\".\\"property_no\\", \\"t_0\\".\\"SP_GEOMETRY\\" FROM \\"public\\".\\"Property\\" AS \\"t_0\\"\n)" (SP_GEOMETRY) sql='))
print "Print out the new datasource"
pro.dataSourceUri()

print("Reloading data")
pro.reloadData()  
layer.reload()

print "The layers datasource"
layer.publicSource()
print "God dam! It's still the old one"

It seems from the code there are two places where there is a datasource string; in the provider and in the layer. The layers datasource is not sync to the providers datasource, so you can update the provider but the layer still knows nothing.

I'll have a chat to the devs to see if there is a reason why we couldn't sync the two together.

share|improve this answer
    
Thank you, at this point is maybe an workaround (with some restrictions) would be to copy the original layer datasource string, modify it, delete the old layer and create a new one... –  Pablo Mar 14 '11 at 14:19
    
So it looks like the postgres data provider (or any of them for that matter) doesn't implement the reloadData method from its base object, which is why the URI never makes it's way to the layer above. –  Nathan W May 10 '11 at 1:21
add comment

The quickest way is to open your QGIS project file (.qgs) in your favorite text editor (e.g., Notepad.exe, vim, whatever), and do a text search-replace for the connection string fragment.

For example, in Notepad.exe, go to Edit > Replace ... Text replace

share|improve this answer
add comment

Your Answer

 
discard

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.