def process_yearly_data(self, download_path, tmp_layer, data, year, indicator, indicator_name, layers, layers_not_available):

        year = str(year)
        data_yearly = get_data_by_year(data, year)

        if len(data_yearly) == 0:
            layers_not_available.append(year)

        else:

            # process layer
            layer_name = year + ' ' + indicator_name
            QgsMessageLog.logMessage(layer_name, self.QGSMESSAGEBAR_ID, QgsMessageLog.INFO)
            self.dlg.progressText.setText('Processing: ' + layer_name)

            try:

                # Create Layer
                layer, addedValue = create_layer(download_path, tmp_layer, indicator, indicator_name, data_yearly, year)

                # check if the layer has been changed
                if addedValue:
                    layers.append(year)

                else:
                    # TODO: give a message to the user. something like "data are not available for this year"
                    QgsMessageLog.logMessage("WARN: there are no data available for " + str(year), self.QGSMESSAGEBAR_ID, QgsMessageLog.WARNING)
                    layers_not_available.append(year)

            except Exception, e:
                layers_not_available.append(year)
    def process_yearly_data(self, download_path, tmp_layer, data, year,
                            indicator, indicator_name, layers,
                            layers_not_available):

        year = str(year)
        data_yearly = get_data_by_year(data, year)

        if len(data_yearly) == 0:
            layers_not_available.append(year)

        else:

            # process layer
            layer_name = year + ' ' + indicator_name
            QgsMessageLog.logMessage(layer_name, self.QGSMESSAGEBAR_ID,
                                     QgsMessageLog.INFO)
            self.dlg.progressText.setText('Processing: ' + layer_name)

            try:

                # Create Layer
                layer, addedValue = create_layer(download_path, tmp_layer,
                                                 indicator, indicator_name,
                                                 data_yearly, year)

                # check if the layer has been changed
                if addedValue:
                    layers.append(year)

                else:
                    # TODO: give a message to the user. something like "data are not available for this year"
                    QgsMessageLog.logMessage(
                        "WARN: there are no data available for " + str(year),
                        self.QGSMESSAGEBAR_ID, QgsMessageLog.WARNING)
                    layers_not_available.append(year)

            except Exception, e:
                layers_not_available.append(year)
    def process_layers(self):

        download_path = self.dlg.download_path.text()

        if self.dlg.download_path.text() is None or len(
                self.dlg.download_path.text()) == 0:
            QMessageBox.critical(None, self.tr('Error'),
                                 self.tr('Please insert the download folder'))

        elif (int(self.dlg.cbToYear.currentText())) - int(
                self.dlg.cbFromYear.currentText()) < 0:
            QMessageBox.critical(None, self.tr('Error'),
                                 self.tr('Year selection is not valid'))

        else:
            processed_layers = 0
            self.dlg.progressBar.setValue(processed_layers)
            self.dlg.progressText.setText('Fetching Data from the World Bank')

            indicator_name = self.dlg.cbIndicator.currentText()
            indicator = self.indicators[indicator_name]

            from_year = int(self.dlg.cbFromYear.currentText())
            to_year = int(self.dlg.cbToYear.currentText()) + 1
            total_years = to_year - from_year

            layers = []
            layers_not_available = []

            # create tmp layer
            tmp_layer = QgsVectorLayer("Polygon?crs=EPSG:4326", "tmp",
                                       "memory")
            tmp_data_provider = tmp_layer.dataProvider()

            # add fields
            tmp_data_provider.addAttributes(
                [QgsField("value", QVariant.Double)])

            data = get_world_bank_data(indicator, str(from_year),
                                       str(to_year - 1))

            # copy layer
            output_file = copy_layer(download_path, indicator_name)

            layer = QgsVectorLayer(output_file, "layer_name", "ogr")

            # create layer by year
            for index, year in enumerate(range(from_year, to_year)):
                self.dlg.progressText.setText('Processing ' + str(year))

                data_yearly = get_data_by_year(data, year)
                if len(data_yearly) > 0:
                    added = create_layer(layer, tmp_layer, data_yearly, year,
                                         index)
                    layers.append(str(year))
                else:
                    layers_not_available.append(str(year))

                # update procgress bar
                processed_layers += 1
                self.dlg.progressBar.setValue(
                    int((float(processed_layers) / float(total_years)) * 100))

            # commit changed on tmp layer
            tmp_layer.commitChanges()
            layer.commitChanges()

            renderer = create_join_renderer(tmp_layer, 'value', 5,
                                            QgsGraduatedSymbolRendererV2.Jenks)

            if self.dlg.open_in_qgis.isChecked():
                for index, year in enumerate(reversed(layers)):
                    l = QgsVectorLayer(output_file,
                                       indicator_name + ' (' + str(year) + ')',
                                       "ogr")
                    r = renderer.clone()
                    r.setClassAttribute(str(year))
                    l.setRendererV2(r)
                    QgsMapLayerRegistry.instance().addMapLayer(l)
                    self.iface.legendInterface().setLayerVisible(
                        l, (index == 0))

                    # for index, year in enumerate(range(from_year, to_year)):
                    #     if str(year) not in layers_not_available:
                    #         l = QgsVectorLayer(output_file, indicator_name + ' (' + str(year) + ')', "ogr")
                    #         r = renderer.clone()
                    #         r.setClassAttribute(str(year))
                    #         l.setRendererV2(r)
                    #         QgsMapLayerRegistry.instance().addMapLayer(l)
                    #         self.iface.legendInterface().setLayerVisible(l, (index == (to_year - from_year-1)))

            self.iface.mapCanvas().refresh()

            self.dlg.progressText.setText('Process Finished')

            if len(layers_not_available) > 0:
                self.iface.messageBar().pushMessage(
                    indicator_name,
                    'Data are not available for ' +
                    ', '.join(layers_not_available) + '',
                    level=QgsMessageBar.WARNING)
    def process_layers(self):

        download_path = self.dlg.download_path.text()

        if self.dlg.download_path.text() is None or len(self.dlg.download_path.text()) == 0:
            QMessageBox.critical(None, self.tr('Error'), self.tr('Please insert the download folder'))

        elif (int(self.dlg.cbToYear.currentText())) - int(self.dlg.cbFromYear.currentText()) < 0:
            QMessageBox.critical(None, self.tr('Error'), self.tr('Year selection is not valid'))

        else:
            processed_layers = 0
            self.dlg.progressBar.setValue(processed_layers)
            self.dlg.progressText.setText('Fetching Data from the World Bank')

            indicator_name = self.dlg.cbIndicator.currentText()
            indicator = self.indicators[indicator_name]

            from_year = int(self.dlg.cbFromYear.currentText())
            to_year = int(self.dlg.cbToYear.currentText()) + 1
            total_years = to_year - from_year

            layers = []
            layers_not_available = []

            # create tmp layer
            tmp_layer = QgsVectorLayer("Polygon?crs=EPSG:4326", "tmp", "memory")
            tmp_data_provider = tmp_layer.dataProvider()

            # add fields
            tmp_data_provider.addAttributes([QgsField("value", QVariant.Double)])

            data = get_world_bank_data(indicator, str(from_year), str(to_year-1))

            # copy layer
            output_file = copy_layer(download_path, indicator_name)

            layer = QgsVectorLayer(output_file, "layer_name", "ogr")

            # create layer by year
            for index, year in enumerate(range(from_year, to_year)):
                self.dlg.progressText.setText('Processing ' + str(year))

                data_yearly = get_data_by_year(data, year)
                if len(data_yearly) > 0:
                    added = create_layer(layer, tmp_layer, data_yearly, year, index)
                    layers.append(str(year))
                else:
                    layers_not_available.append(str(year))


                # update procgress bar
                processed_layers += 1
                self.dlg.progressBar.setValue(int((float(processed_layers) / float(total_years)) * 100))

            # commit changed on tmp layer
            tmp_layer.commitChanges()
            layer.commitChanges()

            renderer = create_join_renderer(tmp_layer, 'value', 5,  QgsGraduatedSymbolRendererV2.Jenks)

            if self.dlg.open_in_qgis.isChecked():
                for index, year in enumerate(reversed(layers)):
                    l = QgsVectorLayer(output_file, indicator_name + ' (' + str(year) + ')', "ogr")
                    r = renderer.clone()
                    r.setClassAttribute(str(year))
                    l.setRendererV2(r)
                    QgsMapLayerRegistry.instance().addMapLayer(l)
                    self.iface.legendInterface().setLayerVisible(l, (index == 0))

                    # for index, year in enumerate(range(from_year, to_year)):
                    #     if str(year) not in layers_not_available:
                    #         l = QgsVectorLayer(output_file, indicator_name + ' (' + str(year) + ')', "ogr")
                    #         r = renderer.clone()
                    #         r.setClassAttribute(str(year))
                    #         l.setRendererV2(r)
                    #         QgsMapLayerRegistry.instance().addMapLayer(l)
                    #         self.iface.legendInterface().setLayerVisible(l, (index == (to_year - from_year-1)))

            self.iface.mapCanvas().refresh()

            self.dlg.progressText.setText('Process Finished')

            if len(layers_not_available) > 0:
                self.iface.messageBar().pushMessage(indicator_name, 'Data are not available for ' + ', '.join(layers_not_available) + '', level=QgsMessageBar.WARNING)