def test_get_layer_attribute_names(self): """Test we can get the correct attributes back""" layer = make_polygon_layer() # with good attribute name attributes, position = layer_attribute_names( layer, [QVariant.Int, QVariant.String], 'TEST_STRIN') # Not a typo... expected_attributes = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] expected_position = 2 message = 'expected_attributes, got %s, expected %s' % ( attributes, expected_attributes) assert (attributes == expected_attributes), message message = 'expected_position, got %s, expected %s' % ( position, expected_position) assert (position == expected_position), message # with non existing attribute name attributes, position = layer_attribute_names( layer, [QVariant.Int, QVariant.String], 'MISSING_ATTR') expected_attributes = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] expected_position = None message = 'expected_attributes, got %s, expected %s' % ( attributes, expected_attributes) assert (attributes == expected_attributes), message message = 'expected_position, got %s, expected %s' % ( position, expected_position) assert (position == expected_position), message # with raster layer layer = make_padang_layer() attributes, position = layer_attribute_names(layer, [], '') message = 'Should return None, None for raster layer, got %s, %s' % ( attributes, position) assert (attributes is None and position is None), message
def show_female_ratio_attribute(self, visible_flag): """Hide or show the female ratio attribute in the dialog. :param visible_flag: Flag indicating if the female ratio attribute should be hidden or shown. :type visible_flag: bool """ theBox = self.cboFemaleRatioAttribute theBox.blockSignals(True) theBox.clear() theBox.blockSignals(False) if visible_flag: currentKeyword = self.get_value_for_key(self.defaults["FEM_RATIO_ATTR_KEY"]) fields, attributePosition = layer_attribute_names(self.layer, [QtCore.QVariant.Double], currentKeyword) fields.insert(0, self.tr("Use default")) fields.insert(1, self.tr("Don't use")) theBox.addItems(fields) if currentKeyword == self.tr("Use default"): theBox.setCurrentIndex(0) elif currentKeyword == self.tr("Don't use"): theBox.setCurrentIndex(1) elif attributePosition is None: # currentKeyword was not found in the attribute table. # Use default theBox.setCurrentIndex(0) else: # + 2 is because we add use defaults and don't use theBox.setCurrentIndex(attributePosition + 2) theBox.setVisible(visible_flag) self.lblFemaleRatioAttribute.setVisible(visible_flag)
def show_elderly_ratio_attribute(self): """Show the elderly ratio attribute in the dialog. """ box = self.cboElderlyRatioAttribute box.blockSignals(True) box.clear() box.blockSignals(False) current_keyword = self.get_value_for_key( self.defaults['ELDERLY_RATIO_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Double], current_keyword) box.addItem(self.global_default_string, self.global_default_data) box.addItem(self.do_not_use_string, self.do_not_use_data) for field in fields: box.addItem(field, field) if current_keyword == self.global_default_data: box.setCurrentIndex(0) elif current_keyword == self.do_not_use_data: box.setCurrentIndex(1) elif attribute_position is None: # current_keyword was not found in the attribute table. # Use default box.setCurrentIndex(0) else: # + 2 is because we add use defaults and don't use box.setCurrentIndex(attribute_position + 2)
def show_aggregation_attribute(self, visible_flag): """Hide or show the aggregation attribute in the keyword editor dialog. :param visible_flag: Flag indicating if the aggregation attribute should be hidden or shown. :type visible_flag: bool """ box = self.cboAggregationAttribute box.blockSignals(True) box.clear() box.blockSignals(False) if visible_flag: current_keyword = self.get_value_for_key( self.defaults['AGGR_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Int, QtCore.QVariant.String], current_keyword) box.addItems(fields) if attribute_position is None: box.setCurrentIndex(0) else: box.setCurrentIndex(attribute_position) box.setVisible(visible_flag) self.lblAggregationAttribute.setVisible(visible_flag)
def show_female_ratio_attribute(self, visible_flag): """Hide or show the female ratio attribute in the dialog. :param visible_flag: Flag indicating if the female ratio attribute should be hidden or shown. :type visible_flag: bool """ box = self.cboFemaleRatioAttribute box.blockSignals(True) box.clear() box.blockSignals(False) if visible_flag: current_keyword = self.get_value_for_key( self.defaults['FEM_RATIO_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Double], current_keyword) fields.insert(0, self.tr('Use default')) fields.insert(1, self.tr('Don\'t use')) box.addItems(fields) if current_keyword == self.tr('Use default'): box.setCurrentIndex(0) elif current_keyword == self.tr('Don\'t use'): box.setCurrentIndex(1) elif attribute_position is None: # current_keyword was not found in the attribute table. # Use default box.setCurrentIndex(0) else: # + 2 is because we add use defaults and don't use box.setCurrentIndex(attribute_position + 2) box.setVisible(visible_flag) self.lblFemaleRatioAttribute.setVisible(visible_flag)
def show_adult_ratio_attribute(self): """Hide or show the adult ratio attribute in the dialog. """ box = self.cboAdultRatioAttribute box.blockSignals(True) box.clear() box.blockSignals(False) current_keyword = self.get_value_for_key( self.defaults['ADULT_RATIO_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Double], current_keyword) fields.insert(0, self.global_default_string) fields.insert(1, self.do_not_use_string) box.addItems(fields) if current_keyword == self.global_default_string: box.setCurrentIndex(0) elif current_keyword == self.do_not_use_string: box.setCurrentIndex(1) elif attribute_position is None: # current_keyword was not found in the attribute table. # Use default box.setCurrentIndex(0) else: # + 2 is because we add use defaults and don't use box.setCurrentIndex(attribute_position + 2)
def show_elderly_ratio_attribute(self, visible_flag): """Hide or show the elderly ratio attribute in the dialog. :param visible_flag: Flag indicating if the elderly ratio attribute should be hidden or shown. :type visible_flag: bool """ box = self.cboElderlyRatioAttribute box.blockSignals(True) box.clear() box.blockSignals(False) if visible_flag: current_keyword = self.get_value_for_key( self.defaults['ELDERLY_RATIO_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Double], current_keyword) fields.insert(0, self.tr('Use default')) fields.insert(1, self.tr('Don\'t use')) box.addItems(fields) if current_keyword == self.tr('Use default'): box.setCurrentIndex(0) elif current_keyword == self.tr('Don\'t use'): box.setCurrentIndex(1) elif attribute_position is None: # current_keyword was not found in the attribute table. # Use default box.setCurrentIndex(0) else: # + 2 is because we add use defaults and don't use box.setCurrentIndex(attribute_position + 2) box.setVisible(visible_flag) self.lblElderlyRatioAttribute.setVisible(visible_flag)
def test_getLayerAttributeNames(self): """Test we can get the correct attributes back""" myLayer = makePolygonLayer() #with good attribute name myAttrs, myPos = layer_attribute_names(myLayer, [ QVariant.Int, QVariant.String], 'TEST_STRIN') # Not a typo... myExpectedAttrs = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] myExpectedPos = 2 myMessage = 'myExpectedAttrs, got %s, expected %s' % ( myAttrs, myExpectedAttrs) assert (myAttrs == myExpectedAttrs), myMessage myMessage = 'myExpectedPos, got %s, expected %s' % ( myPos, myExpectedPos) assert (myPos == myExpectedPos), myMessage #with inexistent attribute name myAttrs, myPos = layer_attribute_names( myLayer, [QVariant.Int, QVariant.String], 'MISSING_ATTR') myExpectedAttrs = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] myExpectedPos = None myMessage = 'myExpectedAttrs, got %s, expected %s' % ( myAttrs, myExpectedAttrs) assert (myAttrs == myExpectedAttrs), myMessage myMessage = 'myExpectedPos, got %s, expected %s' % ( myPos, myExpectedPos) assert (myPos == myExpectedPos), myMessage #with raster layer myLayer = makePadangLayer() myAttrs, myPos = layer_attribute_names(myLayer, [], '') myMessage = 'Should return None, None for raster layer, got %s, %s' % ( myAttrs, myPos) assert (myAttrs is None and myPos is None), myMessage
def test_getLayerAttributeNames(self): """Test we can get the correct attributes back""" layer = make_polygon_layer() #with good attribute name myAttrs, myPos = layer_attribute_names(layer, [ QVariant.Int, QVariant.String], 'TEST_STRIN') # Not a typo... myExpectedAttrs = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] myExpectedPos = 2 message = 'myExpectedAttrs, got %s, expected %s' % ( myAttrs, myExpectedAttrs) assert (myAttrs == myExpectedAttrs), message message = 'myExpectedPos, got %s, expected %s' % ( myPos, myExpectedPos) assert (myPos == myExpectedPos), message #with inexistent attribute name myAttrs, myPos = layer_attribute_names( layer, [QVariant.Int, QVariant.String], 'MISSING_ATTR') myExpectedAttrs = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] myExpectedPos = None message = 'myExpectedAttrs, got %s, expected %s' % ( myAttrs, myExpectedAttrs) assert (myAttrs == myExpectedAttrs), message message = 'myExpectedPos, got %s, expected %s' % ( myPos, myExpectedPos) assert (myPos == myExpectedPos), message #with raster layer layer = make_padang_layer() myAttrs, myPos = layer_attribute_names(layer, [], '') message = 'Should return None, None for raster layer, got %s, %s' % ( myAttrs, myPos) assert (myAttrs is None and myPos is None), message
def test_get_layer_attribute_names(self): """Test we can get the correct attributes back""" layer = make_polygon_layer() #with good attribute name attributes, position = layer_attribute_names(layer, [ QVariant.Int, QVariant.String], 'TEST_STRIN') # Not a typo... expected_attributes = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] expected_position = 2 message = 'expected_attributes, got %s, expected %s' % ( attributes, expected_attributes) assert (attributes == expected_attributes), message message = 'expected_position, got %s, expected %s' % ( position, expected_position) assert (position == expected_position), message #with non existing attribute name attributes, position = layer_attribute_names( layer, [QVariant.Int, QVariant.String], 'MISSING_ATTR') expected_attributes = ['KAB_NAME', 'TEST_INT', 'TEST_STRIN'] expected_position = None message = 'expected_attributes, got %s, expected %s' % ( attributes, expected_attributes) assert (attributes == expected_attributes), message message = 'expected_position, got %s, expected %s' % ( position, expected_position) assert (position == expected_position), message #with raster layer layer = make_padang_layer() attributes, position = layer_attribute_names(layer, [], '') message = 'Should return None, None for raster layer, got %s, %s' % ( attributes, position) assert (attributes is None and position is None), message
def show_aggregation_attribute(self): """Hide or show the aggregation attribute in the keyword editor dialog. """ box = self.cboAggregationAttribute box.blockSignals(True) box.clear() box.blockSignals(False) current_keyword = self.get_value_for_key( self.defaults['AGGR_ATTR_KEY']) fields, attribute_position = layer_attribute_names( self.layer, [QtCore.QVariant.Int, QtCore.QVariant.String], current_keyword) box.addItems(fields) if attribute_position is None: box.setCurrentIndex(0) else: box.setCurrentIndex(attribute_position)
def validateKeywords(self): """Check if the postprocessing layer has all needed attribute keywords. This is only applicable in the case where were are not using the AOI (in other words self.aoiMode is False). When self.aoiMode is True then we always use just the defaults and dont allow the user to create custom aggregation field mappings. This method is called on instance creation and should always be called if you change any state of the aggregator class. On completion of this method the self.isValid flag is set. If this flag is not True, then no aggregation or postprocessing work will be carried out (these methods will raise an InvalidAggregatorError). Args: None Returns: None Raises: Errors are propogated """ # Otherwise get the attributes for the aggregation layer. # noinspection PyBroadException try: myKeywords = self.keywordIO.read_keywords(self.layer) #discussed with Tim,in this case its ok to be generic except Exception: # pylint: disable=W0703 myKeywords = {} if self.aoiMode: myKeywords[self.defaults['FEM_RATIO_ATTR_KEY']] = self.tr( 'Use default') self.keywordIO.update_keywords(self.layer, myKeywords) self.isValid = True return else: myMessage = m.Message( m.Heading( self.tr('Select attribute'), **PROGRESS_UPDATE_STYLE), m.Paragraph(self.tr( 'Please select which attribute you want to use as ID for ' 'the aggregated results'))) self._sendMessage(myMessage) #myKeywords are already complete category = myKeywords['category'] aggregationAttribute = self.defaults['AGGR_ATTR_KEY'] femaleRatio = self.defaults['FEM_RATIO_ATTR_KEY'] femaleRatioKey = self.defaults['FEM_RATIO_KEY'] if ('category' in myKeywords and category == 'postprocessing' and aggregationAttribute in myKeywords and femaleRatio in myKeywords and (femaleRatio != self.tr('Use default') or femaleRatioKey in myKeywords)): self.isValid = True #some keywords are needed else: #set the default values by writing to the myKeywords myKeywords['category'] = 'postprocessing' myAttributes, _ = layer_attribute_names( self.layer, [QtCore.QVariant.Int, QtCore.QVariant.String]) if self.defaults['AGGR_ATTR_KEY'] not in myKeywords: myKeywords[self.defaults['AGGR_ATTR_KEY']] = \ myAttributes[0] if self.defaults['FEM_RATIO_ATTR_KEY'] not in myKeywords: myKeywords[self.defaults['FEM_RATIO_ATTR_KEY']] = self.tr( 'Use default') if self.defaults['FEM_RATIO_KEY'] not in myKeywords: myKeywords[self.defaults['FEM_RATIO_KEY']] = \ self.defaults['FEM_RATIO'] self.keywordIO.update_keywords(self.layer, myKeywords) self.isValid = False