For my own data analysis pipeline, I am running a split screen setup with iPython notebooks on one side, generating variables from source data, pushing them to a CSV file which QGIS then reads/joins to an appropriate geography, instantly visible on the other side of the screen.

I would like to push the control of the running instance of QGIS to the ipython notebook, in effect allowing a super quick calculation/visualization cycle. Right now I can do this in the QGIS python console with something like this:

canvas = qgis.utils.iface.mapCanvas()
cLayer = canvas.currentLayer()

but I would like to:

  1. connect to an existing instance of qgis from an external python environment
  2. at a minimum refresh the map, after each re-write to the csv from ipython notebook, but preferably
  3. re-calculate breaks (using quantiles, natural breaks) based on the newly joined values

Any pointers appreciated!

  • 3
    This is going to be a lot tricker than you might think. First of all, process security will ensure that a process that doesn't want to be controlled can't be controlled by another process. QGIS would have to provide a well-defined IPC interface, or use something like D-Bus to allow other processes to interact with it. The next question is, how would that tie into the QGIS user interface? If you're in edit mode for a layer in QGIS, and trigger an update from your notebook, what happens in QGIS? – Lukas Graf Nov 17 '13 at 20:00
  • 3
    What I imagine could work is writing a QGIS plugin that reads the CSV, and does all the refreshing and recalculating you need it to do, and bind that to a shortcut like F5. On the other side in the IPython notebook, write a function that saves your data to the CSV and make sure that function will always get imported into your global namespace, by using the PYTHONSTARTUP environment variable for example. – Lukas Graf Nov 17 '13 at 20:05
  • 1
    No, my suggestion was to not make the update fully automatic. Bind reload to a quick shortcut like F5 in QGIS, and save to a simple function in notebook. Still two actions, but should nevertheless speed up your cycle. A one button solution would be possible I guess, but considerably more complex, and you'd need to be willing to invest quite some time into it. – Lukas Graf Nov 17 '13 at 21:36
  • 1
    Turns out, IPython provides some very useful features for IPC: Communication between an IPython Kernel and an interactive interpreter like the notebook follows a decoupled two-process model. Also, messaging between kernel and client is well specified and documented, and uses ZeroMQ. So you could probably leverage that, and talk to the IPython kernel from a QGIS plugin (via ZMQ). Still a lot of work though. – Lukas Graf Nov 17 '13 at 21:47
  • 2
    Throw away your CSV and use instead PostgreSQL/PostGIS. There is a mecanism to notify change in the database to QGIS that could help synchronize content on both side. See this blog post oslandia.com/en/2017/10/07/refresh-your-maps-from-postgresql for QGIS. On the notebook side (pure Python, you may look at tapoueh.org/blog/2018/07/postgresql-listen-notify) – ThomasG77 Jan 30 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.