def test_biuniquify_layer_name(self): layer1_name = 'asdhhkhlu18927309hgdkaghdzuz7817982_unique' layer2_name = 'asdhhkhlu18927309hgdkaghdzuz781712ziadgwz_unique' layer3_name = '' self.assertIsNone(layer_interaction.find_layer_by_name(layer1_name), 'Default layer name (\"{}\") is not unique!'.format(layer1_name)) self.assertIsNone(layer_interaction.find_layer_by_name(layer2_name), 'Default layer name (\"{}\") is not unique!'.format(layer2_name)) layer1 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer1_name, 'memory', False) layer2 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer2_name, 'memory', False) self.layer_list.extend([layer1_name, layer2_name]) reg = QgsMapLayerRegistry.instance() reg.addMapLayer(layer1) reg.addMapLayer(layer2) self.assertEqual('', layer_interaction.biuniquify_layer_name('')) self.assertEqual('', layer_interaction.biuniquify_layer_name(None)) # create a new unique name and add a layer with that name, to check the correctness of the functions while loop layer3_name = layer_interaction.biuniquify_layer_name(layer1_name) self.assertEqual(layer1_name + '0', layer3_name) layer3 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer3_name, 'memory', False) reg.addMapLayer(layer3) self.layer_list.extend([layer3_name]) self.assertEqual(layer1_name +'1', layer_interaction.biuniquify_layer_name(layer1_name)) self.assertEqual(layer1_name +'2', layer_interaction.biuniquify_layer_name(layer1_name + str(2))) self.assertEqual(layer2_name +'0', layer_interaction.biuniquify_layer_name(layer2_name))
def save_color_map(self): """ Save the currently open color-map as a qml-file into the project folder. :return: :rtype: """ from mole import extensions layer = iface.activeLayer() selected_layer = self.layers_dropdown.currentText() if layer.name() != selected_layer: layer = layer_interaction.find_layer_by_name(selected_layer) out_path = os.path.dirname(layer.publicSource()) out_path = os.path.join(out_path, layer.name() + '.qml') self.update_color_values() entry_written = self.color_entry_manager.write_color_map_as_qml( layer.name(), out_path) if entry_written: self.message_label.setStyleSheet(_fromUtf8("color: green;")) self.message_label.setText( 'Success - Legend was successfully written to \n\t"{}".'. format(out_path)) else: self.message_label.setStyleSheet(_fromUtf8("color: red;")) self.message_label.setText( 'Failure - Could not write legend to to \n\t"{}".'.format( out_path)) activeextensions = extensions.by_layername(layer.name()) try: activeextensions[0].colortable = out_path except: pass
def refresh_layer_list(self): from mole import extensions """ Update the color-pickers layer-dropdown with a list of the currently visible .tif-files :return: :rtype: """ legend.nodeExitSolo() dropdown = self.layers_dropdown dropdown.clear() raster_list = layer_interaction.get_raster_layer_list(iface, visibility='visible') layer = None for layer in raster_list: source = layer.publicSource() dropdown.addItem(layer.name()) self.color_entry_manager.add_layer(layer.name()) ext = extensions.by_layername(layer.name()) if ext == []: ltu_file = os.path.dirname(layer.publicSource()) ltu_file = os.path.join(ltu_file, layer.name() + '.qml') else: ext = ext[0] ltu_file = ext.colortable #print ext.colortable #print ltu_file if ltu_file != None: if os.path.isfile(os.path.join(ltu_file)): self.color_entry_manager.read_color_map_from_qml(os.path.join(ltu_file)) oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(self.layers_dropdown.currentText()) if oeq_global.QeQ_current_work_layer != None: #print config.open_layers_layer_name legend.nodeInitSolo([config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name])
def save_color_map(self): """ Save the currently open color-map as a qml-file into the project folder. :return: :rtype: """ from mole import extensions layer = iface.activeLayer() selected_layer = self.layers_dropdown.currentText() if layer.name() != selected_layer: layer = layer_interaction.find_layer_by_name(selected_layer) out_path = os.path.dirname(layer.publicSource()) out_path = os.path.join(out_path, layer.name() + '.qml') self.update_color_values() entry_written = self.color_entry_manager.write_color_map_as_qml(layer.name(), out_path) if entry_written: self.message_label.setStyleSheet(_fromUtf8("color: green;")) self.message_label.setText('Success - Legend was successfully written to \n\t"{}".'.format(out_path)) else: self.message_label.setStyleSheet(_fromUtf8("color: red;")) self.message_label.setText('Failure - Could not write legend to to \n\t"{}".'.format(out_path)) activeextensions = extensions.by_layername(layer.name()) try: activeextensions[0].colortable = out_path except: pass
def test_biuniquify_layer_name(self): layer1_name = 'asdhhkhlu18927309hgdkaghdzuz7817982_unique' layer2_name = 'asdhhkhlu18927309hgdkaghdzuz781712ziadgwz_unique' layer3_name = '' self.assertIsNone( layer_interaction.find_layer_by_name(layer1_name), 'Default layer name (\"{}\") is not unique!'.format(layer1_name)) self.assertIsNone( layer_interaction.find_layer_by_name(layer2_name), 'Default layer name (\"{}\") is not unique!'.format(layer2_name)) layer1 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer1_name, 'memory', False) layer2 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer2_name, 'memory', False) self.layer_list.extend([layer1_name, layer2_name]) reg = QgsMapLayerRegistry.instance() reg.addMapLayer(layer1) reg.addMapLayer(layer2) self.assertEqual('', layer_interaction.biuniquify_layer_name('')) self.assertEqual('', layer_interaction.biuniquify_layer_name(None)) # create a new unique name and add a layer with that name, to check the correctness of the functions while loop layer3_name = layer_interaction.biuniquify_layer_name(layer1_name) self.assertEqual(layer1_name + '0', layer3_name) layer3 = QgsVectorLayer('Polygon?crs=EPSG:3857', layer3_name, 'memory', False) reg.addMapLayer(layer3) self.layer_list.extend([layer3_name]) self.assertEqual(layer1_name + '1', layer_interaction.biuniquify_layer_name(layer1_name)) self.assertEqual( layer1_name + '2', layer_interaction.biuniquify_layer_name(layer1_name + str(2))) self.assertEqual(layer2_name + '0', layer_interaction.biuniquify_layer_name(layer2_name))
def setup_legend(self): legend.nodeExitSolo() oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name( self.layers_dropdown.currentText()) if oeq_global.QeQ_current_work_layer != None: #print config.open_layers_layer_name legend.nodeInitSolo([ config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name ])
def update_field_dropdowns(self, index): layer = layer_interaction.find_layer_by_name(self.input_layer.currentText()) provider = layer.dataProvider() fields = provider.fieldNameMap().keys() self.area.clear() self.perimeter.clear() self.height.clear() self.floors.clear() self.yoc.clear() for field in fields: self.area.addItem(field) self.perimeter.addItem(field) self.height.addItem(field) self.floors.addItem(field) self.yoc.addItem(field)
def update_field_dropdowns(self, index): layer = layer_interaction.find_layer_by_name( self.input_layer.currentText()) provider = layer.dataProvider() fields = provider.fieldNameMap().keys() self.area.clear() self.perimeter.clear() self.height.clear() self.floors.clear() self.yoc.clear() for field in fields: self.area.addItem(field) self.perimeter.addItem(field) self.height.addItem(field) self.floors.addItem(field) self.yoc.addItem(field)
def test_hide_or_remove_layer(self): iface = self.iface reg = QgsMapLayerRegistry.instance() v1_name = layer_interaction.biuniquify_layer_name('layer1') reg.addMapLayer(QgsVectorLayer('Polygon?crs=EPSG:3857', v1_name, 'memory', False)) self.layer_list.append(v1_name) self.assertEqual(reg.mapLayersByName(v1_name)[0].name(), v1_name, 'Layer was not added.') layer_interaction.unhide_or_remove_layer(v1_name, 'hide', iface) v1_layer = layer_interaction.find_layer_by_name(v1_name) self.assertFalse(iface.legendInterface().isLayerVisible(v1_layer)) layer_interaction.unhide_or_remove_layer(v1_name, 'unhide', iface) self.assertTrue(iface.legendInterface().isLayerVisible(v1_layer)) layer_interaction.unhide_or_remove_layer(v1_name, 'remove') self.assertEqual(reg.mapLayersByName(v1_name), [], 'Layer was not removed.')
def refresh_layer_list(self): from mole import extensions """ Update the color-pickers layer-dropdown with a list of the currently visible .tif-files :return: :rtype: """ legend.nodeExitSolo() dropdown = self.layers_dropdown dropdown.clear() raster_list = layer_interaction.get_raster_layer_list( iface, visibility='visible') layer = None for layer in raster_list: source = layer.publicSource() dropdown.addItem(layer.name()) self.color_entry_manager.add_layer(layer.name()) ext = extensions.by_layername(layer.name()) if ext == []: ltu_file = os.path.dirname(layer.publicSource()) ltu_file = os.path.join(ltu_file, layer.name() + '.qml') else: ext = ext[0] ltu_file = ext.colortable #print ext.colortable #print ltu_file if ltu_file != None: if os.path.isfile(os.path.join(ltu_file)): self.color_entry_manager.read_color_map_from_qml( os.path.join(ltu_file)) oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name( self.layers_dropdown.currentText()) if oeq_global.QeQ_current_work_layer != None: #print config.open_layers_layer_name legend.nodeInitSolo([ config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name ])
def test_hide_or_remove_layer(self): iface = self.iface reg = QgsMapLayerRegistry.instance() v1_name = layer_interaction.biuniquify_layer_name('layer1') reg.addMapLayer( QgsVectorLayer('Polygon?crs=EPSG:3857', v1_name, 'memory', False)) self.layer_list.append(v1_name) self.assertEqual( reg.mapLayersByName(v1_name)[0].name(), v1_name, 'Layer was not added.') layer_interaction.unhide_or_remove_layer(v1_name, 'hide', iface) v1_layer = layer_interaction.find_layer_by_name(v1_name) self.assertFalse(iface.legendInterface().isLayerVisible(v1_layer)) layer_interaction.unhide_or_remove_layer(v1_name, 'unhide', iface) self.assertTrue(iface.legendInterface().isLayerVisible(v1_layer)) layer_interaction.unhide_or_remove_layer(v1_name, 'remove') self.assertEqual(reg.mapLayersByName(v1_name), [], 'Layer was not removed.')
def test_find_layer_by_name(self): reg = QgsMapLayerRegistry.instance() v1_name = layer_interaction.biuniquify_layer_name('layer1') v2_name = layer_interaction.biuniquify_layer_name('layer2') v3_name = layer_interaction.biuniquify_layer_name('layer3') reg.addMapLayer(QgsVectorLayer('Polygon?crs=EPSG:3857', v1_name, 'memory', False)) self.layer_list.append(v1_name) reg.addMapLayer(QgsVectorLayer('Point?crs=EPSG:3857', v2_name, 'memory', False)) self.layer_list.append(v2_name) reg.addMapLayer(QgsVectorLayer('Point?crs=EPSG:3857', v3_name, 'memory', False)) self.layer_list.append(v3_name) layer = layer_interaction.find_layer_by_name(None) self.assertIsNone(layer, 'An error occured when trying to find a layer passing a none-type name.') layer = layer_interaction.find_layer_by_name('') self.assertIsNone(layer, 'An error occured when trying to find a layer passing an empty name.') layer = layer_interaction.find_layer_by_name('awdalwd') self.assertIsNone(layer, 'An error occured when trying to find a layer passing a non-existing name.') layer1 = layer_interaction.find_layer_by_name(v1_name) layer2 = layer_interaction.find_layer_by_name(v2_name) layer3 = layer_interaction.find_layer_by_name(v3_name) layer1_exists = layer2_exists = layer3_exists = False for key, layer_value in reg.mapLayers().iteritems(): if layer_value == layer1: layer1_exists = True elif layer_value == layer2: layer2_exists = True elif layer_value == layer3: layer3_exists = True self.assertTrue(layer1_exists, 'An error occured when trying to find the previously added layer ' + layer1.name() + '.') self.assertTrue(layer2_exists, 'An error occured when trying to find the previously added layer ' + layer2.name() + '.') self.assertTrue(layer3_exists, 'An error occured when trying to find the previously added layer ' + layer3.name() + '.')
def select_and_rename_files_for_sampling(self,sample_fields): """ Select all available layers for the point sampling and rename multiple occurrences of the same name. Prepend an index, to separate the layers and append the information, which color value is displayed. :return plugin: Return the plugin if it was found or None otherwise :rtype: plugin instance """ import mole.extensions as extensions sample_list = self.pst_dialog.inData table = self.pst_dialog.fieldsTable number_of_samples = len(sample_list) RGBa_appendices = ['R', 'G', 'B', 'a'] RGBa_index = 0 last_name = '' prefix = 0 replacement_map = {} for i in range(number_of_samples): # select all fields via the inData-view, # so the point sampling tool can manage its model accordingly/appropriately sample_list.setItemSelected(sample_list.item(i), True) # Get the source-name (as displayed in the field-table) and check if it was used already # (the name has to be split, since it is displayed in the form 'layer_name : Band x' to get the layer_name) table_index = table.rowCount()-1 table_text = table.item(table_index, 0).text().split(' : ') layer_name = table_text[0] band_name = table_text[1] layer = find_layer_by_name(layer_name) ext=extensions.by_layername(layer_name, 'Import') #if ext: # print "Test: "+ext[0].layer_name # Check if the layer was already used if last_name != layer_name: last_name = layer_name prefix += 1 RGBa_index = 0 if (layer.name() == config.building_outline_layer_name and (band_name.startswith('AREA') or band_name.startswith('PERIMETER') or band_name.startswith(config.building_id_key))): continue elif (layer.type() == QgsMapLayer.RasterLayer and layer.rasterType() == QgsRasterLayer.Multiband and layer.bandCount() == 4 ): # Truncate the name to a maximum of 6 characters, since QGIS limits the length of a feature's name to 10 # prepend prefix (with leading zero), truncated name and RGBa-appendix try: rgba = RGBa_appendices[RGBa_index] RGBa_index += 1 except IndexError as IError: RGBa_index = 0 print(self.__module__, 'IndexError when appending the RGBa-Appendix: {}'.format(IError)) if ext: export_name = ext[0].field_id + '_' + rgba else: export_name = '{:02d}{}_{}'.format(prefix, layer_name[0:6], rgba) replacement_map[layer_name] = export_name[:-2] # Change the text in the table, so the pst can manage its model accordingly/appropriately table.item(table_index, 1).setText(export_name) continue elif ext: # NEW fieldname ist nicht klar if ext[0].field_rename is not None: if band_name.startswith(tuple(ext[0].field_rename.keys())): if ext[0].field_rename[band_name]: table.item(table_index, 1).setText(ext[0].field_rename[band_name]) continue elif band_name.startswith(tuple(ext[0].par_in)): continue sample_list.setItemSelected(sample_list.item(i), False) return replacement_map
def select_and_rename_files_for_sampling(self, sample_fields): """ Select all available layers for the point sampling and rename multiple occurrences of the same name. Prepend an index, to separate the layers and append the information, which color value is displayed. :return plugin: Return the plugin if it was found or None otherwise :rtype: plugin instance """ import mole.extensions as extensions sample_list = self.pst_dialog.inData table = self.pst_dialog.fieldsTable number_of_samples = len(sample_list) RGBa_appendices = ['R', 'G', 'B', 'a'] RGBa_index = 0 last_name = '' prefix = 0 replacement_map = {} for i in range(number_of_samples): # select all fields via the inData-view, # so the point sampling tool can manage its model accordingly/appropriately sample_list.setItemSelected(sample_list.item(i), True) # Get the source-name (as displayed in the field-table) and check if it was used already # (the name has to be split, since it is displayed in the form 'layer_name : Band x' to get the layer_name) table_index = table.rowCount() - 1 table_text = table.item(table_index, 0).text().split(' : ') layer_name = table_text[0] band_name = table_text[1] layer = find_layer_by_name(layer_name) ext = extensions.by_layername(layer_name, 'Import') #if ext: # print "Test: "+ext[0].layer_name # Check if the layer was already used if last_name != layer_name: last_name = layer_name prefix += 1 RGBa_index = 0 if (layer.name() == config.building_outline_layer_name and (band_name.startswith('AREA') or band_name.startswith('PERIMETER') or band_name.startswith(config.building_id_key))): continue elif (layer.type() == QgsMapLayer.RasterLayer and layer.rasterType() == QgsRasterLayer.Multiband and layer.bandCount() == 4): # Truncate the name to a maximum of 6 characters, since QGIS limits the length of a feature's name to 10 # prepend prefix (with leading zero), truncated name and RGBa-appendix try: rgba = RGBa_appendices[RGBa_index] RGBa_index += 1 except IndexError as IError: RGBa_index = 0 print( self.__module__, 'IndexError when appending the RGBa-Appendix: {}'. format(IError)) if ext: export_name = ext[0].field_id + '_' + rgba else: export_name = '{:02d}{}_{}'.format(prefix, layer_name[0:6], rgba) replacement_map[layer_name] = export_name[:-2] # Change the text in the table, so the pst can manage its model accordingly/appropriately table.item(table_index, 1).setText(export_name) continue elif ext: # NEW fieldname ist nicht klar if ext[0].field_rename is not None: if band_name.startswith(tuple(ext[0].field_rename.keys())): if ext[0].field_rename[band_name]: table.item(table_index, 1).setText( ext[0].field_rename[band_name]) continue elif band_name.startswith(tuple(ext[0].par_in)): continue sample_list.setItemSelected(sample_list.item(i), False) return replacement_map
def test_find_layer_by_name(self): reg = QgsMapLayerRegistry.instance() v1_name = layer_interaction.biuniquify_layer_name('layer1') v2_name = layer_interaction.biuniquify_layer_name('layer2') v3_name = layer_interaction.biuniquify_layer_name('layer3') reg.addMapLayer( QgsVectorLayer('Polygon?crs=EPSG:3857', v1_name, 'memory', False)) self.layer_list.append(v1_name) reg.addMapLayer( QgsVectorLayer('Point?crs=EPSG:3857', v2_name, 'memory', False)) self.layer_list.append(v2_name) reg.addMapLayer( QgsVectorLayer('Point?crs=EPSG:3857', v3_name, 'memory', False)) self.layer_list.append(v3_name) layer = layer_interaction.find_layer_by_name(None) self.assertIsNone( layer, 'An error occured when trying to find a layer passing a none-type name.' ) layer = layer_interaction.find_layer_by_name('') self.assertIsNone( layer, 'An error occured when trying to find a layer passing an empty name.' ) layer = layer_interaction.find_layer_by_name('awdalwd') self.assertIsNone( layer, 'An error occured when trying to find a layer passing a non-existing name.' ) layer1 = layer_interaction.find_layer_by_name(v1_name) layer2 = layer_interaction.find_layer_by_name(v2_name) layer3 = layer_interaction.find_layer_by_name(v3_name) layer1_exists = layer2_exists = layer3_exists = False for key, layer_value in reg.mapLayers().iteritems(): if layer_value == layer1: layer1_exists = True elif layer_value == layer2: layer2_exists = True elif layer_value == layer3: layer3_exists = True self.assertTrue( layer1_exists, 'An error occured when trying to find the previously added layer ' + layer1.name() + '.') self.assertTrue( layer2_exists, 'An error occured when trying to find the previously added layer ' + layer2.name() + '.') self.assertTrue( layer3_exists, 'An error occured when trying to find the previously added layer ' + layer3.name() + '.')
def setup_legend(self): legend.nodeExitSolo() oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(self.layers_dropdown.currentText()) if oeq_global.QeQ_current_work_layer != None: #print config.open_layers_layer_name legend.nodeInitSolo([config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name])