Example #1
0
    def start(self):
        while True:
            idx = Dialog().select(tr(30007), WINDOWS.values())
            if idx == -1:
                break
            window = WINDOWS.keys()[idx]

            while True:
                idx = Dialog().select(tr(30008), ACTIONS.keys())
                if idx == -1:
                    break
                category = ACTIONS.keys()[idx]

                while True:
                    curr_keymap = self._current_keymap(window, category)
                    labels = [
                        "%s - %s" % (name, key) for _, key, name in curr_keymap
                    ]
                    idx = Dialog().select(tr(30009), labels)
                    if idx == -1:
                        break
                    action, oldkey, _ = curr_keymap[idx]
                    newkey = KeyListener.record_key()
                    if newkey is None:
                        continue

                    old = (window, action, oldkey)
                    new = (window, action, newkey)
                    if old in self.userkeymap:
                        self.userkeymap.remove(old)
                    self.userkeymap.append(new)
                    if old != new:
                        self.dirty = True
Example #2
0
 def onInit(self):
     try:
         self.getControl(401).addLabel(tr(30002))
         self.getControl(402).addLabel(tr(30010) % self.TIMEOUT)
     except AttributeError:
         self.getControl(401).setLabel(tr(30002))
         self.getControl(402).setLabel(tr(30010) % self.TIMEOUT)
Example #3
0
 def onInit(self):
     try:
         self.getControl(401).addLabel(tr(30002))
         self.getControl(402).addLabel(tr(30010) % self.TIMEOUT)
     except AttributeError:
         self.getControl(401).setLabel(tr(30002))
         self.getControl(402).setLabel(tr(30010) % self.TIMEOUT)
Example #4
0
    def start(self):
        while True:
            idx = Dialog().select(tr(30007), WINDOWS.values())
            if idx == -1:
                break
            window = WINDOWS.keys()[idx]

            while True:
                idx = Dialog().select(tr(30008), ACTIONS.keys())
                if idx == -1:
                    break
                category = ACTIONS.keys()[idx]

                while True:
                    curr_keymap = self._current_keymap(window, category)
                    labels = ["%s - %s" % (name, key) for _, key, name in curr_keymap]
                    idx = Dialog().select(tr(30009), labels)
                    if idx == -1:
                        break
                    action, oldkey, _ = curr_keymap[idx]
                    newkey = KeyListener.record_key()
                    if newkey is None:
                        continue

                    old = (window, action, oldkey)
                    new = (window, action, newkey)
                    if old in self.userkeymap:
                        self.userkeymap.remove(old)
                    self.userkeymap.append(new)
                    if old != new:
                        self.dirty = True
Example #5
0
    def setxlim(self, windowsize):
        if self.x is None or self.y is None: return
        xmax = max(self.x)
        date = mdates.num2date(xmax).date()
        global global_shared
        status_bar = global_shared.get('status_bar')
        if windowsize == WindowSize.ONEDAY:
            if status_bar:
                status_bar.showMessage(
                    tr('ErrorMSG', 'No intraday data', None), 1000)
            return
        elif windowsize == WindowSize.FIVEDAY:
            if status_bar:
                status_bar.showMessage(
                    tr('ErrorMSG', 'No intraday data', None), 1000)
            return
        elif windowsize == WindowSize.ONEMONTH:
            xmin = mdates.date2num(date - timedelta(days=30))
        elif windowsize == WindowSize.THREEMONTH:
            xmin = mdates.date2num(date - timedelta(days=90))
        elif windowsize == WindowSize.SIXMONTH:
            xmin = mdates.date2num(date - timedelta(days=180))
        elif windowsize == WindowSize.ONEYEAR:
            xmin = mdates.date2num(date - timedelta(days=365))
        elif windowsize == WindowSize.TWOYEAR:
            xmin = mdates.date2num(date - timedelta(days=365 * 2))
        elif windowsize == WindowSize.FIVEYEAR:
            xmin = mdates.date2num(date - timedelta(days=365 * 5))
        elif windowsize == WindowSize.MAX:
            xmin = min(self.x)

        self.axes.set_xlim([xmin, xmax])
        self.adjust_ylim(xmin, xmax)
 def load_zonal_layer(self, zonal_layer_path):
     # Load zonal layer
     zonal_layer = QgsVectorLayer(zonal_layer_path, tr('Zonal data'), 'ogr')
     if not zonal_layer.geometryType() == QGis.Polygon:
         msg = 'Zonal layer must contain zone polygons'
         self.iface.messageBar().pushMessage(
             tr("Error"),
             tr(msg),
             level=QgsMessageBar.CRITICAL)
         return False
     # Add zonal layer to registry
     if zonal_layer.isValid():
         QgsMapLayerRegistry.instance().addMapLayer(zonal_layer)
     else:
         msg = 'Invalid zonal layer'
         self.iface.messageBar().pushMessage(
             tr("Error"),
             tr(msg),
             level=QgsMessageBar.CRITICAL)
         return None
     return zonal_layer
 def load_loss_layer(self, loss_layer_path):
     # Load loss layer
     if self.loss_layer_is_vector:
         loss_layer = QgsVectorLayer(loss_layer_path, tr('Loss map'), 'ogr')
         if not loss_layer.geometryType() == QGis.Point:
             msg = 'Loss map must contain points'
             self.iface.messageBar().pushMessage(
                 tr("Error"),
                 tr(msg),
                 level=QgsMessageBar.CRITICAL)
             return False
     else:
         loss_layer = QgsRasterLayer(loss_layer_path, tr('Loss map'))
     # Add loss layer to registry
     if loss_layer.isValid():
         QgsMapLayerRegistry.instance().addMapLayer(loss_layer)
     else:
         msg = 'Invalid loss map'
         self.iface.messageBar().pushMessage(
             tr("Error"),
             tr(msg),
             level=QgsMessageBar.CRITICAL)
         return None
     # Zoom depending on the zonal layer's extent
     return loss_layer
Example #8
0
def main():
    ## load mappings ##
    try:
        setup_keymap_folder()
    except Exception:
        traceback.print_exc()
        utils.rpc('GUI.ShowNotification',
                  title="Keymap Editor",
                  message="Failed to remove old keymap file",
                  image='error')
        return

    defaultkeymap = utils.read_keymap(default)
    userkeymap = []
    if os.path.exists(gen_file):
        try:
            userkeymap = utils.read_keymap(gen_file)
        except Exception:
            traceback.print_exc()
            utils.rpc('GUI.ShowNotification',
                      title="Keymap Editor",
                      message="Failed to load keymap file",
                      image='error')
            return

    ## main loop ##
    confirm_discard = False
    while True:
        idx = Dialog().select(tr(30000), [tr(30003), tr(30004), tr(30005)])
        if idx == 0:
            # edit
            editor = Editor(defaultkeymap, userkeymap)
            editor.start()
            confirm_discard = editor.dirty
        elif idx == 1:
            # reset
            confirm_discard = bool(userkeymap)
            userkeymap = []
        elif idx == 2:
            # save
            if os.path.exists(gen_file):
                shutil.copyfile(gen_file, gen_file + ".old")
            utils.write_keymap(userkeymap, gen_file)
            xbmc.executebuiltin("action(reloadkeymaps)")
            break
        elif idx == -1 and confirm_discard:
            if Dialog().yesno(tr(30000), tr(30006)) == 1:
                break
        else:
            break

    sys.modules.clear()
Example #9
0
    def start(self):
        while True:
            # Select context menu
            idx = Dialog().select(tr(30007), WINDOWS.values())
            if idx == -1:
                break
            window = WINDOWS.keys()[idx]

            while True:
                # Select category menu
                idx = Dialog().select(tr(30008), ACTIONS.keys())
                if idx == -1:
                    break
                category = ACTIONS.keys()[idx]

                while True:
                    # Select action menu
                    current_keymap = self._current_keymap(window, category)
                    labels = [
                        "%s - %s" % (name, key)
                        for _, key, name in current_keymap
                    ]
                    idx = Dialog().select(tr(30009), labels)
                    if idx == -1:
                        break
                    action, current_key, _ = current_keymap[idx]
                    old_mapping = (window, action, current_key)

                    # Ask what to do
                    idx = Dialog().select(tr(30000), [tr(30011), tr(30012)])
                    if idx == -1:
                        continue
                    elif idx == 1:
                        # Remove
                        if old_mapping in self.userkeymap:
                            self.userkeymap.remove(old_mapping)
                            self.dirty = True
                    elif idx == 0:
                        # Edit key
                        newkey = KeyListener.record_key()
                        if newkey is None:
                            continue

                        new_mapping = (window, action, newkey)
                        if old_mapping in self.userkeymap:
                            self.userkeymap.remove(old_mapping)
                        self.userkeymap.append(new_mapping)
                        if old_mapping != new_mapping:
                            self.dirty = True
Example #10
0
def main():
    ## load mappings ##
    try:
        setup_keymap_folder()
    except Exception:
        traceback.print_exc()
        utils.rpc('GUI.ShowNotification', title="Keymap Editor",
            message="Failed to remove old keymap file", image='error')
        return

    defaultkeymap = utils.read_keymap(default)
    userkeymap = []
    if os.path.exists(gen_file):
        try:
            userkeymap = utils.read_keymap(gen_file)
        except Exception:
            traceback.print_exc()
            utils.rpc('GUI.ShowNotification', title="Keymap Editor",
                      message="Failed to load keymap file", image='error')
            return

    ## main loop ##
    confirm_discard = False
    while True:
        idx = Dialog().select(tr(30000), [tr(30003), tr(30004), tr(30005)])
        if idx == 0:
            # edit
            editor = Editor(defaultkeymap, userkeymap)
            editor.start()
            confirm_discard = editor.dirty
        elif idx == 1:
            # reset
            confirm_discard = bool(userkeymap)
            userkeymap = []
        elif idx == 2:
            # save
            if os.path.exists(gen_file):
                shutil.copyfile(gen_file, gen_file + ".old")
            utils.write_keymap(userkeymap, gen_file)
            xbmc.executebuiltin("action(reloadkeymaps)")
            break
        elif idx == -1 and confirm_discard:
            if Dialog().yesno(tr(30000), tr(30006)) == 1:
                break
        else:
            break

    sys.modules.clear()
Example #11
0
    def start(self):
        while True:
            # Select context menu
            idx = Dialog().select(tr(30007), WINDOWS.values())
            if idx == -1:
                break
            window = WINDOWS.keys()[idx]

            while True:
                # Select category menu
                idx = Dialog().select(tr(30008), ACTIONS.keys())
                if idx == -1:
                    break
                category = ACTIONS.keys()[idx]

                while True:
                    # Select action menu
                    current_keymap = self._current_keymap(window, category)
                    labels = ["%s - %s" % (name, key) for _, key, name in current_keymap]
                    idx = Dialog().select(tr(30009), labels)
                    if idx == -1:
                        break
                    action, current_key, _ = current_keymap[idx]
                    old_mapping = (window, action, current_key)

                    # Ask what to do
                    idx = Dialog().select(tr(30000), [tr(30011), tr(30012)])
                    if idx == -1:
                        continue
                    elif idx == 1:
                        # Remove
                        if old_mapping in self.userkeymap:
                            self.userkeymap.remove(old_mapping)
                            self.dirty = True
                    elif idx == 0:
                        # Edit key
                        newkey = KeyListener.record_key()
                        if newkey is None:
                            continue

                        new_mapping = (window, action, newkey)
                        if old_mapping in self.userkeymap:
                            self.userkeymap.remove(old_mapping)
                        self.userkeymap.append(new_mapping)
                        if old_mapping != new_mapping:
                            self.dirty = True
Example #12
0
def calculate_svi(
    iface, current_layer, project_definition, indicators_operator=None, themes_operator=None, reuse_field=False
):
    """
    add an SVI attribute to the current layer
    """
    # set default
    if indicators_operator is None:
        indicators_operator = DEFAULT_COMBINATION
    if themes_operator is None:
        themes_operator = DEFAULT_COMBINATION

    themes = project_definition["children"][1]["children"]

    if reuse_field and "svi_field" in project_definition:
        svi_attr_name = project_definition["svi_field"]
        if DEBUG:
            print "Reusing %s" % svi_attr_name
    else:
        svi_attr_name = "SVI"
        svi_field = QgsField(svi_attr_name, QVariant.Double)
        svi_field.setTypeName(DOUBLE_FIELD_TYPE_NAME)
        attr_names = ProcessLayer(current_layer).add_attributes([svi_field])
        svi_attr_name = attr_names[svi_attr_name]

    # get the id of the new attribute
    svi_attr_id = ProcessLayer(current_layer).find_attribute_id(svi_attr_name)

    discarded_feats_ids = []
    try:
        with LayerEditingManager(current_layer, "Add SVI", DEBUG):
            for feat in current_layer.getFeatures():
                # If a feature contains any NULL value, discard_feat will
                # be set to True and the corresponding SVI will be set to
                # NULL
                discard_feat = False
                feat_id = feat.id()

                # init svi_value to the correct value depending on
                # themes_operator
                if themes_operator in SUM_BASED_COMBINATIONS:
                    svi_value = 0
                elif themes_operator in MUL_BASED_COMBINATIONS:
                    svi_value = 1

                # iterate all themes of SVI
                for theme in themes:
                    indicators = theme["children"]

                    # init theme_result to the correct value depending on
                    # indicators_operator
                    if indicators_operator in SUM_BASED_COMBINATIONS:
                        theme_result = 0
                    elif indicators_operator in MUL_BASED_COMBINATIONS:
                        theme_result = 1

                    # iterate all indicators of a theme
                    for indicator in indicators:
                        if feat[indicator["field"]] == QPyNullVariant(float):
                            discard_feat = True
                            discarded_feats_ids.append(feat_id)
                            break
                        # For "Average (equal weights)" it's equivalent to use
                        # equal weights, or to sum the indicators
                        # (all weights 1)
                        # and divide by the number of indicators (we use
                        # the latter solution)
                        if indicators_operator in (
                            "Sum (simple)",
                            "Average (equal weights)",
                            "Multiplication (simple)",
                        ):
                            indicator_weighted = feat[indicator["field"]]
                        else:
                            indicator_weighted = feat[indicator["field"]] * indicator["weight"]

                        if indicators_operator in SUM_BASED_COMBINATIONS:
                            theme_result += indicator_weighted
                        elif indicators_operator in MUL_BASED_COMBINATIONS:
                            theme_result *= indicator_weighted
                        else:
                            error_message = "invalid indicators_operator: %s" % indicators_operator
                            raise RuntimeError(error_message)
                    if discard_feat:
                        break
                    if indicators_operator == "Average (equal weights)":
                        theme_result /= len(indicators)

                    # combine the indicators of each theme
                    # For "Average (equal weights)" it's equivalent to use
                    # equal weights, or to sum the themes (all weights 1)
                    # and divide by the number of themes (we use
                    # the latter solution)
                    if themes_operator in ("Sum (simple)", "Average (equal weights)", "Multiplication (simple)"):
                        theme_weighted = theme_result
                    else:
                        theme_weighted = theme_result * theme["weight"]

                    if themes_operator in SUM_BASED_COMBINATIONS:
                        svi_value += theme_weighted
                    elif themes_operator in MUL_BASED_COMBINATIONS:
                        svi_value *= theme_weighted
                if discard_feat:
                    svi_value = QPyNullVariant(float)
                else:
                    if themes_operator == "Average (equal weights)":
                        svi_value /= len(themes)

                current_layer.changeAttributeValue(feat_id, svi_attr_id, svi_value)
        msg = (
            "The SVI has been calculated for fields containing "
            "non-NULL values and it was added to the layer as "
            "a new attribute called %s"
        ) % svi_attr_name
        iface.messageBar().pushMessage(tr("Info"), tr(msg), level=QgsMessageBar.INFO)
        if discarded_feats_ids:
            widget = toggle_select_features_widget(
                tr("Warning"),
                tr("Invalid indicators were found in some features while " "calculating SVI"),
                tr("Select invalid features"),
                current_layer,
                discarded_feats_ids,
                current_layer.selectedFeaturesIds(),
            )
            iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)

        project_definition["indicators_operator"] = indicators_operator
        project_definition["themes_operator"] = themes_operator
        project_definition["svi_field"] = svi_attr_name
        return svi_attr_id, discarded_feats_ids

    except TypeError as e:
        current_layer.dataProvider().deleteAttributes([svi_attr_id])
        msg = "Could not calculate SVI due to data problems: %s" % e
        iface.messageBar().pushMessage(tr("Error"), tr(msg), level=QgsMessageBar.CRITICAL)
Example #13
0
def calculate_iri(
    iface, current_layer, project_definition, svi_attr_id, aal_field_name, discarded_feats_ids, iri_operator=None
):
    """
    Copy the AAL and calculate an IRI attribute to the current layer
    """

    # set default
    if iri_operator is None:
        iri_operator = DEFAULT_COMBINATION

    aal_weight = project_definition["children"][0]["weight"]
    svi_weight = project_definition["children"][1]["weight"]

    iri_attr_name = "IRI"
    iri_field = QgsField(iri_attr_name, QVariant.Double)
    iri_field.setTypeName(DOUBLE_FIELD_TYPE_NAME)

    attr_names = ProcessLayer(current_layer).add_attributes([iri_field])

    # get the id of the new attributes
    iri_attr_id = ProcessLayer(current_layer).find_attribute_id(attr_names[iri_attr_name])

    discarded_aal_feats_ids = []

    try:
        with LayerEditingManager(current_layer, "Add IRI", DEBUG):
            for feat in current_layer.getFeatures():
                feat_id = feat.id()
                svi_value = feat.attributes()[svi_attr_id]
                aal_value = feat[aal_field_name]
                if aal_value == QPyNullVariant(float) or feat_id in discarded_feats_ids:
                    iri_value = QPyNullVariant(float)
                    discarded_aal_feats_ids.append(feat_id)
                elif iri_operator == "Sum (simple)":
                    iri_value = svi_value + aal_value
                elif iri_operator == "Multiplication (simple)":
                    iri_value = svi_value * aal_value
                elif iri_operator == "Sum (weighted)":
                    iri_value = svi_value * svi_weight + aal_value * aal_weight
                elif iri_operator == "Multiplication (weighted)":
                    iri_value = svi_value * svi_weight * aal_value * aal_weight
                elif iri_operator == "Average (equal weights)":
                    # For "Average (equal weights)" it's equivalent to use
                    # equal weights, or to sum the indices (all weights 1)
                    # and divide by the number of indices (we use
                    # the latter solution)
                    iri_value = (svi_value + aal_value) / 2.0
                # store IRI
                current_layer.changeAttributeValue(feat_id, iri_attr_id, iri_value)
        project_definition["iri_operator"] = iri_operator
        # set the field name for the copied AAL layer
        project_definition["aal_field"] = aal_field_name
        project_definition["iri_field"] = attr_names[iri_attr_name]
        msg = (
            "The IRI has been calculated for fields containing "
            "non-NULL values and it was added to the layer as "
            "a new attribute called %s"
        ) % attr_names[iri_attr_name]
        iface.messageBar().pushMessage(tr("Info"), tr(msg), level=QgsMessageBar.INFO)
        widget = toggle_select_features_widget(
            tr("Warning"),
            tr("Invalid values were found in some features while calculating " "IRI"),
            tr("Select invalid features"),
            current_layer,
            discarded_aal_feats_ids,
            current_layer.selectedFeaturesIds(),
        )
        iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)
        return iri_attr_id

    except TypeError as e:
        current_layer.dataProvider().deleteAttributes([iri_attr_id])
        msg = "Could not calculate IRI due to data problems: %s" % e

        iface.messageBar().pushMessage(tr("Error"), tr(msg), level=QgsMessageBar.CRITICAL)