3

I use QGIS 3.16. I need to find a way to separate the columns of a layer and export each column in a new layer named like the column.

For example: I have a file.csv that I import with the geometry wkt. After import in QGIS I have 6 columns: id, A, B, C, D and E. Is it possible to find a way to export each column in a layer .shp:

  • A.shp: with 2 columns: id, A
  • B.shp: id, B
  • C.shp: id, C
  • D.shp: id, D
  • E.shp: id, E

sample of data

In this sample "id"= "MAILLE" and the columns following are A,B,C,D and E.

Do you know a way to achieve that in QGIS?

4
  • Can you provide a sample of your data ?
    – Taras
    Commented Dec 6, 2022 at 13:35
  • 1
    related post
    – JGH
    Commented Dec 6, 2022 at 13:45
  • @Taras I added a sample that can be downloaded by wetransfer, I didn't know how to join it to my post, I hope it's ok, let me know if I can do better Commented Dec 6, 2022 at 14:17
  • You can use Refactor fields and drop the columns you dont need
    – Bera
    Commented Dec 6, 2022 at 17:42

3 Answers 3

6

If its only a few and automating it is too much hassle, use Layer... Save As.. and choose a name and select the columns you want to save in the dialog:

enter image description here

5

The easiest way to do this all at once probably is using the algorithm from Menu Processing > Toolbox > Retain fields in batch mode:

  1. Select your Input Layer, add as many rows as you want to create layers, then use Autofill... > Fill Down to insert the same layer for each row.

  2. Go to the second column Fields to retain > Autofill... > Calculate by Expression and insert this expression (replace my_layer with the name of your input layer). It will automatically select the field id plus for each row another one of the remaining attribute fields:

     array( 
         array_remove_all(
             map_akeys (attributes(get_feature_by_id ('my_layer',1))),
             'id'
         )[@row_number] ,
         'id'
     )   
    
  3. Select a folder/filename for the output.

You're done - when running it, you'll get 5 layers.

enter image description here

2
  • 1
    I have qgis 3.16 on my virtual machine and this tool doesn't exist in it. I did it on my local machine on 3.22 and it works really well ! If you know how I can do it on 3.16 it will be great? If not it's already great thanks ! Commented Jan 9, 2023 at 15:02
  • Retain fields was introduced in QGIS 3.18, so this solution indeed is not possible in 3.16, unfortunately.
    – Babel
    Commented Jan 9, 2023 at 15:13
2

It is not QGIS solution

P.S. To me it sounds more like Pandas&GeoPandas task. Therefore try the following code:

import pandas as pd
import geopandas as gpd
from os.path import isfile, join, normpath

csv_file = 'imp2646.csv'
path_to_project = normpath('C:/Users/Taras/Downloads/project/')
path_to_csv_file = join(path_to_project, csv_file)

if isfile(path_to_csv_file):
    df = pd.read_csv(path_to_csv_file, sep=';')
    gs = gpd.GeoSeries.from_wkt(df['wkt_geom_2154'])
    gdf = gpd.GeoDataFrame(df, geometry=gs, crs="EPSG:2154")
    gdf = gdf.drop('wkt_geom_2154', axis=1)
    all_columns = gdf.columns.tolist()
    all_columns.remove('geometry')
    all_columns.remove('MAILLE')
    all_columns.sort()
    pairs = list(zip(["MAILLE"]*len(all_columns), all_columns))
    for pair in pairs:
        id, attr = pair[0], pair[1]
        attributes = list(pair)
        attributes.append('geometry')
        gdf_subset = gdf[attributes]
        gdf_subset.to_file(join(path_to_project, f"{attr}.shp"))

and get the output that will look like this:

result

Note: Resulting features of each layer overlap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.