Skip to main content
added 157 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

This solution only changes the order of columns in the attribute table, and not in the original file

This solution only changes the order of columns in the attribute table, and not in the original file.

There is also aThis PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can achieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    Parameters:
    ==========
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result


References:

This solution only changes the order of columns in the attribute table, and not in the original file

There is also a PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can achieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result

This solution only changes the order of columns in the attribute table, and not in the original file.

This PyQGIS solution utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can achieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    Parameters:
    ==========
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result


References:

added 9 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

This solution only changes the order of columns in the attribute table, and not in the original file

There is also a PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can tryachieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result

This solution only changes the order of columns in the attribute table, and not in the original file

There is also a PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can try the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result

This solution only changes the order of columns in the attribute table, and not in the original file

There is also a PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can achieve with the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result

Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

This solution only changes the order of columns in the attribute table, and not in the original file

There is also a PyQGIS solution available. It utilizes the setColumns() and setAttributeTableConfig() methods.

Let's assume there is a layer called 'gis_osm_buildings', see the image below.

input

To get the original order of fields:

layer = iface.activeLayer()
print([layer.attributeDisplayName(index) for index in layer.attributeList()])

that gives:

['osm_id', 'lastchange', 'code', 'fclass', 'geomtype', 'name', 'type', 'height', 'levels']

To set a new order of fields one can try the following function:

from qgis.core import QgsProject

def setting_new_columns_order(layer_name: str, new_order: list) -> None:
    """
    Changes order of all columns in the layer
    :param layer_name: name of the layer
    :param new_order: a list with a new order of columns
    """

    layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    layer_attr_table_config = layer.attributeTableConfig()
    columns_config = layer_attr_table_config.columns()

    columns_new_order_dict = {column : new_order.index(column.name) for column in columns_config if
                              column.type == 0 and column.name in new_order}
    columns_new_order_list = sorted(columns_new_order_dict, key=columns_new_order_dict.get, reverse=False)

    layer_attr_table_config.setColumns(columns_new_order_list)
    layer.setAttributeTableConfig(layer_attr_table_config)

    return

setting_new_columns_order('gis_osm_buildings', ['name', 'type', 'osm_id', 'code', 'fclass', 'height', 'levels', 'lastchange', 'geomtype'])

result