Hot answers tagged

18

Documentation Here you can find what is new and what is break under the PyQGIS API. To get details about how to port Python2 to Python3 go there You can find some detail about testing from QGIS2 to QGIS3 on this question :Writing automated tests for QGIS plugins? And you will find an interesting OpenGis.ch's paper here about the migrations tools. What ...


10

According to the documentation you can add filter in the definition of your class: QString QFileDialog.getOpenFileName (QWidget parent = None, QString caption = QString(), QString directory = QString(), QString filter = QString(), Options options = 0) So with your code it should looks like : def filePath(title): qfd = QFileDialog() path = "D:\...


8

Hopefully the following will help get you started, I've added comments to try and explain how it works. Essentially, you can use a QgsMapLayerComboBox to filter out all layers loaded in QGIS and only have it list the multiband rasters. You can then define a function which will read the selected layer and populate the combo boxes for the bands (note that I ...


8

This is indeed an effect of using the print statement and having the python console open. And using the logging module as proposed is a very good approach to solving this issue. Here follow some insights into what exactly happens. It boils down to print() internally calling QCoreApplication::processEvents() and processEvents() is evil and dangerous. The ...


7

This is a simple code for connect your QgsMapToolIdentifyFeature signal. from qgis.gui import QgsMapToolIdentifyFeature def onFeatureIdentified(feature): print "feature selected : "+ str(feature.id()) mapTool = None mc=iface.mapCanvas() lyr=iface.activeLayer() mapTool = QgsMapToolIdentifyFeature(mc) mapTool.setLayer(lyr) mc.setMapTool(mapTool) mapTool....


5

Try this, pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==`gdal-config --version`


5

The reason is, you are trying to use QGIS 3 API on QGIS 2. Here is the API for QGIS 2: https://qgis.org/api/2.18/classQgsMapCanvas.html Here is the API for QGIS 3: https://qgis.org/api/classQgsMapCanvas.html There are 2 solutions to this problem: Use mapCanvas.setLayerSet Work with QGIS 3


4

This sounds like you have multiple python installations, and GDAL is installed on one and not the other. Try the following (which I have based upon this). Create a text file within your python installation site packages folder C:\Python27\Lib\site-packages called usrlocal.pth. In this text file insert one line giving the path to your second python ...


4

Import qgis.core before PyQt4.


4

dialog = QtGui.QFileDialog() fname = dialog.getOpenFileName(None, "Window name", "", "CSV files (*.csv)") Will do the job. Just pass None as 1st agrument.


4

Solved, it looks like I overused print statement while debugging in python. Switching to logging module solved the problem. Use logging module instead of print, it's much more convenient and practical. It's a nuisance in the beginning, but once you get it it's quite easy. Here are some links that were very helpful for me: http://docs.python-guide.org/en/...


4

With this documentation about PyQt5 signals, try this : self.buttonBox.accepted.connect(Configuration.accept) self.buttonBox.rejected.connect(Configuration.reject)


3

I can partly answer my own question! The following works now also in my Python plugin: The reason was that I had a differed order of xmin/xmax/ymin/ymax for PROJWIN in the plugin code. Thus the module failed but did not throw any errors. Yes sometimes you don't see the wood for the trees ;-) import processing from qgis.core import QgsRasterLayer raster = ...


3

If the uploaded file is not in image or .csv file it should thrown an message. It is not the most common approach I think. The best way to do this is to create a file dialog, that allows to choose only one extension. Example: def upload_csv(self): dialog = QtGui.QFileDialog() fname = dialog.getOpenFileName(None, "Import CSV", "", "CSV data files ...


3

If I understand well, you want to check for NULLs when the custom value for NULL is changed by the user from the Settings. In this case, you may simply use: from qgis.core import * if <what you want to check> == qgis.core.NULL: # do something The above check should compare the input with the value that the user set for the NULLs.


3

Your code is ok, but there is probably a typo in the p list. The first item value seems too big for the location you are working on (I generally work using CRS UTM 32N): probably the last digits of 33401477 are decimals. If I use other values for p: p = [318333,4993348] on this sample raster (CRS UTM 32N, EPSG:32632): I get a result without any problem (...


3

I figured out another solution (QGIS 3.4): startup.py from PyQt5.QtCore import QSettings,QStandardPaths from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QLabel, QWidget, QSplashScreen,QApplication import os try: s = QSettings() s.setValue("PythonPlugins/BufferSelection",True) except: pass try: widgets= QApplication.allWidgets() ...


3

In QT designer you need make right click on the widget and select "promote to", and write the class and header as seen in the image. after you need compile the .ui. In the class that loads this ui, to load some layer for example (this canvas have a objectname "canvas") def __init__(self): """ Contructor """ QDialog.__init__(self) self.setupUi(...


2

Got the same problem. I fixed ir by installing python-gdal using synaptic with my Debian Jessie


2

An answer is here : How to define an event for features selected in mapcanvas in qgis using python You can connect your function to the selectionChanged signal of your vector layer Example code from the related answer: lyr=iface.activeLayer() def myFunction(selFeatures): print str(len(selFeatures)) + " features were selected: " + str(selFeatures) lyr....


2

I managed to do it in the end, so I'll post the answer here in case anyone needs it later. The problem was that my image contained float32 and sometimes even complex32 data, but QImage does not support it, so I had to convert it to uint8. Another problem that came up was that my image was completely black, because the values were most of the time like 0.x, ...


2

It seems that this is more a PyQt4 question. I guess the best way of doing it is to give your actions an objectName, so that you can access them directly instead of looping through toolbars and their children. For instance, I can give the custom name mActionCopyLayersToClipboard to one of my plugin buttons: self.actionCopy = QAction( icon, u"Copy ...


2

You can call the function reset_form on closeEvent of the form. Add this code to GNATDialog class: def closeEvent(self, event): self.reset_form()


2

You can do that by using 'groupRelationsChanged' method of QgsLegendInterface to connect slot. Following code snippet is referred to pluging and works as expected: . . . def __init__(self, iface): . . . self.dlg.comboBox.clear() layers = self.iface.legendInterface().layers() layer_list = [ layer.name() for layer ...


2

You can use QgsMapRegistry signals like layersAdded and LayersRemoved. Just connect function that can modify your QComboBox. Another solution is to use QgsMapLayerComboBox: https://qgis.org/api/classQgsMapLayerComboBox.html I think it should be updated automatically.


2

To stick with your code : def onFeatureIdentified(self, ff): print("feature selected") def run(self): mCanvas = self.iface.mapCanvas() f = QgsMapToolIdentifyFeature(mCanvas, self.iface.activeLayer()) mCanvas.setMapTool(f) f.featureIdentified.connect(self.onFeatureIdentified)


2

Try something like that: try: # action for QGIS 3/PyQt5 except: # action for QGIS 2/PyQt4


2

You could put your code into a function which can then be called by the QgsMapLayerRegistry::layersAdded signal using: QgsMapLayerRegistry.instance().legendLayersAdded.connect(myF‌​unc)


2

I reply myself here, according with the solution given in the qgis developers mail list. In the initGui function I should add this line: self.dockwidget = GeoFibraDockWidget() Is the constructor for the dockwidget.


Only top voted, non community-wiki answers of a minimum length are eligible