def test_init(self): """Test FieldMappingTab initialization.""" layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid_complex.geojson', with_keywords=False, clone_to_memory=True) layer.keywords = {} field_mapping = FieldMappingTab(age_ratio_group, PARENT, IFACE) field_mapping.set_layer(layer) # Empty keywords should give empty for all aspect parameter_values = field_mapping.get_parameter_value() self.assertEqual(parameter_values['fields'], {}) for v in parameter_values['values'].values(): self.assertIsNone(v) # Make sure all keys exist fields_keys = parameter_values['fields'].keys() values_keys = parameter_values['values'].keys() age_ratio_fields_keys = [field['key'] for field in age_ratio_group[ 'fields']] for key in fields_keys + values_keys: self.assertIn(key, age_ratio_fields_keys) # Check field list fields = [] for index in xrange(field_mapping.field_list.count()): fields.append(field_mapping.field_list.item(index)) labels = [i.text() for i in fields] for field in layer.dataProvider().fields(): if field.type() not in qvariant_numbers: continue self.assertIn(field.name(), labels)
def test_init_gender_ratio(self): """Test FieldMappingTab initialization for gender ratio.""" layer = load_test_vector_layer('gisv4', 'aggregation', 'small_grid_complex.geojson', with_keywords=False, clone_to_memory=True) layer.keywords = { 'inasafe_default_values': { female_ratio_field['key']: 0.7 } } field_mapping = FieldMappingTab(gender_ratio_group, PARENT, IFACE) field_mapping.set_layer(layer) parameter_values = field_mapping.get_parameter_value() self.assertEqual(parameter_values['fields'], {}) for k, v in parameter_values['values'].items(): if k == female_ratio_field['key']: self.assertEqual(0.7, v) else: message = 'Key {key} gives not None, but {value}'.format( key=k, value=v) self.assertIsNone(v, message) # Make sure all keys exist fields_keys = parameter_values['fields'].keys() values_keys = parameter_values['values'].keys() gender_ratio_fields_keys = [ field['key'] for field in gender_ratio_group['fields'] ] for key in fields_keys + values_keys: self.assertIn(key, gender_ratio_fields_keys) # Check field list fields = [] for index in xrange(field_mapping.field_list.count()): fields.append(field_mapping.field_list.item(index)) labels = [i.text() for i in fields] for field in layer.dataProvider().fields(): if field.type() not in qvariant_numbers: continue self.assertIn(field.name(), labels)
def main(): """Main function to run the example.""" layer = load_test_vector_layer( 'aggregation', 'district_osm_jakarta.geojson', clone=True) app = QApplication([]) field_mapping = FieldMappingTab(age_ratio_group, PARENT, IFACE) field_mapping.set_layer(layer) widget = QWidget() layout = QGridLayout() layout.addWidget(field_mapping) widget.setLayout(layout) widget.show() sys.exit(app.exec_())
def populate_tabs(self): """Populating tabs based on layer metadata.""" self.delete_tabs() layer_purpose = self.metadata.get('layer_purpose') if not layer_purpose: message = tr( 'Key layer_purpose is not found in the layer {layer_name}' ).format(layer_name=self.layer.name()) raise KeywordNotFoundError(message) if layer_purpose == layer_purpose_exposure['key']: layer_subcategory = self.metadata.get('exposure') elif layer_purpose == layer_purpose_hazard['key']: layer_subcategory = self.metadata.get('hazard') else: layer_subcategory = None field_groups = get_field_groups(layer_purpose, layer_subcategory) for field_group in field_groups: tab = FieldMappingTab(field_group, self, self.iface) tab.set_layer(self.layer, self.metadata) self.addTab(tab, field_group['name']) self.tabs.append(tab)