1

I am building a QGIS plugin, and want to add some base maps from the shapefiles which were stored in the PostgreSQL database.

My question is:

Is there a way to import shapefiles stored in the database into the QGIS plugin without displaying in the map canvas?


Updated on 13/06/2019:

Sorry I didn't explain the question clearly. I will try to describe my question in more detail.

Currently, I managed to store shapefiles in the database (Figure 1) and use "Add PostGIS Layer" (Figure 2) to visualise the shapefile in QGIS (Figure 3).

In the plugin I am going to build, I would like to choose one of these shapefiles (stored in the database) as my environmental layer (something like a base map).

However, my problem is that I can only choose the layers from the map canvas (shapefiles which have already been added into QGIS). My plugin looks something like figure 4, when users click the red circle, a list of shapefiles stored in the database is supposed to be displayed instead of maps on QGIS canvas.

(Figure 1:) Shapefile stored in the database

(Figure 2:) Connect my database to QGIS

(Figure 3:) Shapefile visualised in QGIS

(Figure 4:) Draft of the plugin I am going to build

  • Can you please clarify (maybe with a screenshot) on whether the 'names' of the shapefiles are what is stored in PostgreSQL, or are the actual shapefiles uploaded into PostgreSQL? – JasonInVegas Jun 12 at 20:54
  • 2
    Shapefile is a file-based storage format. It is not possible to store a shapefile in a database (when stored, it becomes a table with rows). You will have better luck searching for documentation on whether you can access PostGIS geometry in PostgreSQL from QGIS without display. – Vince Jun 12 at 21:30
  • I'm a bit confused about what you're trying to do.. can you include some code or pseudocode to show what you're trying to achieve here; please mask any personal/sensitive information like usernames and passwords (something like XXXXX would do). – Michael Stimson Jun 13 at 0:09
  • Thank you JasonInVegas. I have added some screenshot to make my question more clear. For your question: 1. The examples of the names of the shapefiles are "areas benefiting from flood defences", "flood alert areas", etc. 2. Yes, I have stored actual shapefiles into PostgreSQL (and I can visualise them in QGIS by connecting to PostGIS) – rui jiang Jun 13 at 11:49
  • Hi Vince. You are right. After I stored my shapefiles in the database it became a table with rows but it can still be visualised via other software. Thanks a lot for the keyword you mentioned, I will do some research on "access PostGIS geometry in PostgreSQL from QGIS without display". – rui jiang Jun 13 at 11:54
2

Unfortunately, there is no simple way how to list available table/layers in PostGIS directly from QGIS API. The easiest way is to use connector class which is part of the db_manager plugin (github link). Look at the last part of this answer how to use that class.

After that, you have a list of all vector tables in the database. Then you can instantiate them (as QgsVectorLayer) and add them to canvas, (which makes them available in standard QGIS widgets) or You can keep only descriptors in memory, fill standard QCombobox with names (and other useful stuff) and then instantiate them as needed. The second approach is a bit more work, but definitely a lot more efficient.

  • That is very useful. Thanks a lot. – rui jiang Jun 28 at 22:11

Your Answer

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

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