Changes between Initial Version and Version 1 of UsersWikiQGIS


Ignore:
Timestamp:
Oct 29, 2013, 8:00:20 AM (11 years ago)
Author:
remic
Comment:

creating the page about using QGIS with PostGis

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiQGIS

    v1 v1  
     1
     2= Displaying PostGis geometries using QGIS =
     3
     4== Abstract==
     5This guide propose some tricks to use [http://www.qgis.org/ QGIS] to display and modify content of geometries tables in PostGIS
     6Please add precisions and new topics if you have ideas
     7== Summary ==
     8 * Why use QGIS with PostGIS
     9 * Workflow
     10 * QGIS limitations
     11 * 4 ways to display a postgis geometry in QGIS
     12 * Tricks
     13
     14== Why use QGIS with PostGIS ==
     15
     16 QGIS is an open source software with lot's of functionalities. PostGIS is natively integrated in it, and it works relatively well.
     17Also QGis as an extension repository and is fully scriptable with Python, which makes it a cornerstone for working in GIS with Open Source software.
     18When using PostGIS you always end up to have to display some geometry, so better use the right tool for it.
     19
     20== Workflow ==
     21
     22The workflow is always identical. The first time, you have to define a connection to PostGIS database ((default values are : host:localhost , port:5432 , Database:postgres)). Then you will load a postgis table containing a geometry columns as a new QGIS layer. Then this layer can be edited, and you have access to all the other columns (if the type is known by QGIS, else cast to ::text), for example to use it as a color categories, or as proportion field, etc.
     23 
     24== QGIS limitations ==
     25
     26Every loaded PostGis geometry layer must have a Primary key! That is, a way of uniquely identifying every row of the table. In PostGIS it is usually done adding an gid column in table definiton, like this : `CREATE TABLE mytable AS (idauto SERIAL int, other_columns...)`. If you have no other option , you can always use a trick to generate on the fly a fake uid (see Tricks).
     27
     28 PostGIS srid are not supported, so you have to manually choose the right projection and coordinate system when adding a PostGis geometry layer.
     29 Mixed geometry are not supported ( neither are GeometryCollections). So when using mixed geometries, you have to load 3 times the table each time selecting a different type of geometry . Us St_CollectionExtract or ST_Dump to break GeometryCollection appart.
     30 
     31 Loaded Postgis layers are intended for stable use. So when loading a layer from a postgis table, don't delete table or change table def, as it will crash QGIS. Reloading will discard bad postgis layer.
     32
     33 PostGis is made to display up to a few hundred thousand geometry with beautiful styling. More geometries than this is going to be slow, or will crash. You may want to clip you data to keep only a work set if working with massive data.
     34
     35 Funky type : QGIS works well with points, line , polygon. It will work to a certain extent with Multi, but will definitively not work with more subtle geometry type, like curve, mesh, tin, etc. Usually Postgis has function to convert to classical type (ST_CurveToLine for example)
     36
     37== 5 ways to display a postgis geometry in QGIS ==
     38 
     39 From easiest to most complicated and powerful
     40
     41 * Load the "QuickWKT" Extension. It should add a button "WKT". This open a text field where you can past WKT geometries. Warning : doesn't support curve or mixed type, but does support EWKT (you can specidy srid). This adds a layer to QGIS, you can add as many layers as you want. This should be used only for small tests.
     42
     43 * Select the "Add a PostGis Layer" in the "Layer" menu. This open a windows where you have to define a new connection to your postgis server (default values are : host:localhost , port:5432 , Database:postgres). You have several options you can try afterward by clicking on the edit button to edit the connection. When connected to a database, you can browse to your table and select it to display it in QGIS. You can select multiple table. A same time will appear X times if it has X geometry columns. If your table doesn't show, it is probably not registered in the public.geometry_columns PostGIS view. You have other option to display content.
     44
     45 * Select the DBManager in the menu Database. In the left panel, browse to your connection, then choose a table with geometry. You can right click on this table and chose "Add as a QGIS layer". This will load table even if geometry column is not listed in POstGIS view public.geometry_columns. This is useful if you create your table with queries like `CREATE TABLE my_table AS SELECT geom ...`.
     46
     47 * Select the DBManager in the DataBase menu, then browse to Postgis/your_connection. Now click on the "SQL WIndow" icon. This open a text editor where you can use any query you like. It is very useful when geometry are not directly accessible. For example, you have a table with id, X and Y columns, but no geometry column. You can use the following query `SELECT id, X, Y, ST_SetSRID(ST_MakePoint(X,Y), your_srid) AS geom FROM your_table`
     48You can test query with F5. There is auto completion. CTE are allowed, but you need to be strict on query formatting (no leading spaces or tabs, neither at the end). When you are satisfied, you can click on the "load as a new layer" box and choose an id and geom column.
     49Remember each line must have an unique id (see tricks), uni-type geometry, and proper formatting.
     50
     51 * If you prefer, there are various QGIS extensions allowing sql windows /and/or specific Postgis management.
     52
     53== Tricks ==
     54
     55
     56You can use materialized view (require Postgres >= 9.3), as a "buffer" for qgis.
     57Materialized view are like view with cache !
     58A view is just a stored query which is executed each time it is used. In a materialized view, the query is also stored, but so is the result of the query. Thus when using several time the materialized view, there is no need to do computing several times. The downside is you have to manually update materialized view when the content has changed.
     59See http://www.postgresql.org/docs/9.3/static/sql-creatematerializedview.html
     60
     61If you have a *very* recent QGIS version you can just add view like any other Postgis table. Else, you have to create a simple view querying you materilized view.
     62For example :
     63 `CREATE VIEW my_proxy_view AS SELECT * FROM my_materialized_view`
     64
     65
     66
     67 You can use QGis visualization settings to make the element appears dynamically.
     68This is better than just waiting with blank screen while objects are loading, at least when trying to define an interesting area.
     69For this, open Option/Rendering, then put something superior to 1000 in the appropriated field.
     70
     71
     72When you don't have a unique id for each row you want to display, you can create on on the fly using the SQL windows to load your geometries.
     73Simply add in the select :
     74`SELECT row_number() OVER() AS id_qgis` , and select id_qgis as the id column in the qgis list.