2

I created a little plugin to geocode addresses using the HERE API and QGIS 3.

Especially in batch mode, I can see that the plugin is iterating over my features in a table and my messageBar progress is working as expected.

After a few seconds, QGIS gets unresponsive and the progressbar stops. This happens in a non-reproducible way.

The relevant code section is:

    progressMessageBar = iface.messageBar().createMessage("Looping through " + str(layer.featureCount()) +" records ...")
    progress = QProgressBar()
    progress.setMaximum(layer.featureCount())
    progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
    progressMessageBar.layout().addWidget(progress)
    iface.messageBar().pushWidget(progressMessageBar, level=1)
    for id in range(0, layer.featureCount()-1):
        urlPart=""
        oldAddress=""
        for key in addressLists.keys():
            if key != "oldIds":
                urlPart+="&" + key +  "=" + addressLists[key][id]
                oldAddress += addressLists[key][id] + ","
        url = "https://geocoder.api.here.com/6.2/geocode.json?app_id=" + appId + "&app_code=" + appCode + urlPart
        r = requests.get(url)
        if r.status_code == 200:
            if len(json.loads(r.text)["Response"]["View"])>0:
            #as the response may hold more than one result we only use the best one:
                responseAddress = json.loads(r.text)["Response"]["View"][0]["Result"][0]
                geocodeResponse = self.convertGeocodeResponse(responseAddress)
                lat = responseAddress["Location"]["DisplayPosition"]["Latitude"]
                lng = responseAddress["Location"]["DisplayPosition"]["Longitude"]
                ResultFet = QgsFeature()
                ResultFet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(lng,lat)))
                ResultFet.setAttributes([
                    addressLists["oldIds"][id],
                    etc...

                ])
                ResultFeatureList.append(ResultFet)
        progress.setValue(id)
        iface.mainWindow().repaint()
    pr.addFeatures(ResultFeatureList)
    iface.messageBar().clearWidgets()

Here is the behaviour in a gif: QGIS plugin stalls

The main functionality below is embedded in a function which is connected with the button. The connection is defined in the main run function of the plugin:

    def run(self):
        self.dlg.show()
        self.dlg.LayerSelect_2.currentIndexChanged.connect(self.searchFieldsPopulate)
        self.dlg.batchGeocodeFieldsButton.clicked.connect(self.batchGeocodeFields)
        result = self.dlg.exec_()

        if result:
            pass

UPDATE When adding a short sleep prior and after progressbar update the progress bar seems to update better and the system is responding and not showing any unresponsive messages in the plugin/QGIS application.

    time.sleep(0.2)
    progress.setValue(id)
    time.sleep(0.2)

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.