2

I have a geoJSON file with 4,500 rows. I need to edit this file as follow:

  • if plot_num starts with 2 -> rep = 2
  • first 11 rows of line = 1
  • second 11 rows of line = 2

I have been updating the fields manually, but it is taking me for ever and I have 8 files that need the same. How can I use the Expression Dialog tool in QGIS to do so? I wish it was like Excel where I can just copy and paste or use the corner dropdown tool. Can you write a QGIS expression or a Python function to be used in QGIS?

Example (24 rows/4,500 rows):

line    rep     plot_num
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
1       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
2       2       256
1       3       356
1       3       356
3

2 Answers 2

3

I figure it out that you can use the tool "Select features using an expression" and then update the fields using "Open field calculator". On the "Select features using an expression" I used the following to find the empty "rep" fields where "plot_num" was 1.

CASE
WHEN left("plot_num",1) IS 1 THEN "rep" is NULL
END

Once those where selected, I updated their values using "Open field calculator" by selecting the field that needed to be updated and setting the desired value.

2

You can use one single expression to create each of the fields at once:

  1. To create the value of field rep as the first character of field plot_num, use this expression:

    left (plot_num , 1)

  2. To create the field line like: the first 11 features of each plot_num with value 1, the rest with value 2, use this expression:

     with_variable (
         'plot',
         plot_num,
         if (
             array_min (array_agg( $id, filter:=plot_num=@plot)) + 11 <= $id,
             2,1
         )
     )
    

enter image description here

6
  • Would you please review your code? It runned for about 10 mins on my MacBook Pro (32GB RAM) for about 10 mins and I couldn't get QGIS to respond. Should I keep waiting or try a PC with more RAM? Dec 22, 2021 at 21:07
  • How many features do you have? How large is your data set? I think that is the problem.
    – Babel
    Dec 22, 2021 at 21:08
  • I have about 20 features (columns) x 4500 rows. Dec 22, 2021 at 21:14
  • Columns are called fields, rows are features. In my screenshot, I have 4 fields (attributes) and over 33 features. So maybe 4500 feautures is just too much? So the problem is with the second expression only, right? The first one was OK?
    – Babel
    Dec 22, 2021 at 21:17
  • 1
    I used a PC with more memory and it worked as expected. Great answer BTW! Dec 22, 2021 at 23:59

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.