AnswerBun.com

Create new date for temporal layer in QGIS

I have a bunch of features (e.g. ‘sampling point 1’) and am storing a bunch of environmental data for each feature (e.g. 'pH', 'EC', etc.) plus a date. The new time manager for QGIS makes exploring this data great.

However, what I can’t figure out is some simple way of inputting new data. Specifically I would like to recreate each feature for each new date so that the attribute table that originally looked something like this:

fid | name    | pH  | date
--- | ---     | --- | ---
1   | point 1 | 6   | 1990-01-01
2   | point 2 | 5   | 1990-01-01
3   | point 1 | 7   | 2020-05-01 
4   | point 2 | 7   | 2020-05-01

Could be updated to something like this:

fid | name    | pH   | date
--- | ---     | ---  | ---
1   | point 1 | 6    | 1990-01-01
2   | point 2 | 5    | 1990-01-01
3   | point 1 | 7    | 2020-05-01 
4   | point 2 | 7    | 2020-05-01
5   | point 1 | NULL | 2020-06-01
6   | point 2 | NULL | 2020-06-01

I can filter by the latest date, select the filtered features, copy the selected features, paste as a new temporary layer, change the date and attributes and then copy these features and paste them back into the original layer. But that seems very convoluted.

Is there a simpler way to create a new date for the features in a layer? Or am I just approaching the problem wrong way?

Geographic Information Systems Asked on December 8, 2021

2 Answers

2 Answers

Another probably a more simpler solution can be achieved with the following workflow

Step 1. Select all features in that layer

Step 2. Start editing the layer

Step 3. Copy and paste selected features in the same layer

Step 4. Modify values in "pH" field with '' using the Field Calculator. Do not forget to click "Update selected"

Step 5. Update the "id" for new feature with maximum("id") + @row_number using the Field Calculator. Do not forget to click "Update selected"

Step 6. Change the date with to_date('2020-06-' || format_date(to_date("date"), 'dd'))

Answered by Taras on December 8, 2021

I hope the Question was interpreted correctly.

In QGIS I can suggest using a Virtual Layer through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is one point layer "test" with its corresponding attribute table, see image below.

input

With the following query, it is possible to achieve the result, i.e. to duplicate all points in the same layer while modifying some values as well generating new ids.

SELECT *
FROM (
    -- number series
    WITH RECURSIVE generate_series(category) AS (
        SELECT conf.start
        FROM conf
        UNION ALL
        SELECT category + conf.step
        FROM generate_series, conf
        WHERE category + conf.step <= conf.stop
    ),

    -- configuration
    conf AS (
        SELECT MAX(CAST("id"  AS INTEGER)) + 1 AS start,
               2*COUNT() AS stop,
               1 AS step
        FROM test
    )
    
    -- creating new table with the corresponding order of values
    SELECT t."osm_id",
           NULL AS "lastchange",
           gs.category AS id,
           t."lat",
           t."lon",
           t."Name",
           t.geometry 
    FROM generate_series AS gs, conf AS c
    JOIN test AS t ON t."id" = gs.category - c.stop/2
)

UNION ALL

SELECT *
FROM test

The output Virtual Layer will look like as following.

result

where

  • new values where set to NULL for the Attribute "lastchange"
  • added an "id"s for new values

Note that each location contains double geometries


References:

Answered by Taras on December 8, 2021

Add your own answers!

Related Questions

traslate Google Satellite XYZ tiles map inside QGIS?

0  Asked on March 1, 2021 by ocelot73

 

What is the second argument in QgsProcessingUtils.mapLayerFromString()?

1  Asked on February 28, 2021 by francisco-bonnefoy

       

Fix “zigzags” in TIN surface (Civil 3D)

0  Asked on February 28, 2021

   

Can QGIS open the file type .pitemx or .pkinfo?

1  Asked on February 28, 2021 by sir-socks

   

How to calculate elevation angle between two GPS coordinates?

1  Asked on February 28, 2021 by hesham-k

   

Invalid geometry type after clipping

0  Asked on February 28, 2021 by david-koller

     

Deleting layout pages in QGIS 3

1  Asked on February 27, 2021 by darryl

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.