14

I use QGIS to illustrate the results from my Matlab simulation. Matlab produces a CSV file which is connected to a shapefile within QGIS.

At the moment I have to close and reopen QGIS to show the new results from the Matlab simulation.

Is it possible to reload the Attribute table of the CSV file without closing and reopening QGIS?

1

5 Answers 5

4

I can suggest an alternative workflow that, while not necessarily easier than closing and opening QGIS each time, might make more sense:

  • Import your shapefile into spatialite
  • Import the CSV into spatialite (using the "Virtual TXT/CSV option)
  • Create a spatial view in spatialite by JOINing the spatial table (shapefile) to the virtual CSV table

Now, You can display the view in QGIS just like any spatial layer, and use the JOINed values from spatialite. But, each time the CSV is recreated, you will need to DROP the virtual CSV table, and re-link to it. Then a refresh in QGIS will show the new values. THis only takes a moment, and what's more, you can define many views, and keep the previous ones around if needed...

HTH

0
3

Right click on the csv layer, select Properties, select Rendering and tick the box that says Refresh Layer at Interval (seconds). I left the value at 0 seconds.

Each time the CSV changes, the map will update accordingly.

(works in QGIS V3.22.4)

1
  • I found that in QGIS V3.22.7 the tick mark is not saved when I leave interval at 0 seconds. When setting to 10 seconds, the tick mark is saved. Still the data is not refreshed automatically in my case. Commented Nov 30, 2023 at 10:57
2

For anyone still looking for an answer to this without using a spatialite format, I've found something that works.

If you have a csv and use a join to join it to a spatial layer, one of the options of the join is to cache the layer. You can get to this option if you go to the properties of your layer, then to the joined data and click edit join. If you disable the cache option, you can reload the data layer after the csv has changed and then trigger a repaint of your map to get a refreshed map view.

You can do the reloading and repainting in the Python Console. First, you have to define a name for the map canvas, the csv and for the layer:

mc = iface.mapCanvas()
layer=mc.currentLayer()
csv_layer=mc.currentLayer()

Be sure to have the layers active when you are naming them in the console when using the currentLayer method (it has to be the 'current layer').

Then, if you set the cache option as off for your join, you can alter the csv and then use the following two commands in the Python console to redraw your map:

csv_layer.reload()
layer.triggerRepaint()

This worked for me, but I only had 100 rows of data. It could be that operations will be slow without caching when you have a bigger dataset.

1

Go to the Attribute Table of the CSV layer and reload the table from there. Then a refresh will visualize the new data.

First step: Reload the table button in Attribute Table

Seconds step: Refresh the map view

I tested this in QGIS 3.22.7

0

Open the layer properties of the shapefile, and go to General--Coordinate reference system, click Create spatial index. Once it is finished, the shapefile is refreshed.

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.