﻿{"id":6234,"date":"2018-09-04T04:40:50","date_gmt":"2018-09-04T02:40:50","guid":{"rendered":"http:\/\/www.sigterritoires.fr\/?p=6234"},"modified":"2018-10-17T12:04:23","modified_gmt":"2018-10-17T10:04:23","slug":"python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality\/","title":{"rendered":"Python Methods for QGis: How to access vector data (postgis, spatiality)"},"content":{"rendered":"\r\n\r\n\r\n<p>In this series of articles we discuss how to access vector type data in Python and QGis. In the previous article ( <a href=\"https:\/\/translate.google.com\/translate?hl=en&amp;prev=_t&amp;sl=auto&amp;tl=en&amp;u=http:\/\/www.sigterritoires.fr\/index.php\/recettes-python-pour-qgis-acceder-aux-donnees-vecteurshapefile\/\">Python Recipes for QGis: access data<\/a> <a href=\"https:\/\/translate.google.com\/translate?hl=en&amp;prev=_t&amp;sl=auto&amp;tl=en&amp;u=http:\/\/www.sigterritoires.fr\/index.php\/recettes-python-pour-qgis-acceder-aux-donnees-vecteurshapefile\/\">vector (shapefile)<\/a> ) we have discussed \u00a0how to load a <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/shapefile-vs-geopackage\/\">shapefile<\/a> file . In this article we &lsquo;ll see how to load a Postgresql \/ <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/starting-with-postgres-postgis\/\">Postgis<\/a> table and a Spatialite table .<!--more--><\/p>\r\n\r\n\r\n\r\n<h2><strong>How to load<\/strong> <strong>a<\/strong> <strong>postgis<\/strong> <strong>layer<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>The other common data source for a QGis application is a vector layer contained in a Postgres \/ Postgis database. We will see how to load it using the Python code.<\/p>\r\n\r\n\r\n\r\n<p>The principle is the same as for a shapefile layer except that we will use another data provider and that, unlike to a shp file; the connection to the database must be created.<\/p>\r\n\r\n\r\n\r\n<p>In ScripRunner create a new script and name it <strong><em>charger_database.py.<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>Enter the following code:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6236\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality\/tuerca-7\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-3.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"tuerca\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-3.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6236\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-3.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><div class='stb-container stb-style-grey'><div class='stb-caption'><div class='stb-logo'><img class='stb-logo__image' src='data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERR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RuXvxuVrARDzeDymdWvXOg1anbDg27fR7e6gpqaG225bxNGjx5g0cSrPrF4V3bRpY6fsUeHzhtYnVX6u2Fj42NrfM8TlG2YAo4GvARNXrVo1PJVMalrb2jSTJk0Rurs7EUWJ7du2uHfu2nkI2AjsAtzSVX705wX5JLArX0tXgfl47hlkOS\/Nzc0d0eV2Z40dNz6voCA\/c\/v27b12uz3a2Nh4GtgHnAO8H7XQJL4okGsZSvkclyELhEnOsdgV8t9zZRH8vwpytZIgXCUU+cwgX\/7z4UuQL0H++UD+awDc\/MMFLMO8WgAAAABJRU5ErkJggg==' alt='img'\/><\/div><div class='stb-caption-content'><\/div><div class='stb-tool'><\/div><\/div><div class='stb-content'>\r\n<p># Customize this starter script by adding code <br \/># to the run_script function. See the Help for <br \/># complete information on how to create a script <br \/># and use Script Runner.<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00bb \u00a0\u00a0 \u00a0\u00bb \u00a0 \u00bbYour description of the script goes here\u00ab \u00a0\u00a0 \u00a0\u00bb \u00a0 \u00ab\u00a0<\/p>\r\n\r\n\r\n\r\n<p># Some commonly used imports<\/p>\r\n\r\n\r\n\r\n<p>from PyQt4.QtCore import * <br \/>from PyQt4.QtGui import * <br \/>from qgis.core import * <br \/>from qgis.gui import *<\/p>\r\n\r\n\r\n\r\n<p>def run_script ( iface ): <br \/>uri = QgsDataSourceURI () <br \/>uri.setConnection (\u00a0\u00bb \u00a0 localhost \u00a0 \u00bb,\u00ab \u00a0 5432 \u00a0 \u00bb,\u00ab \u00a0 postgres \u00a0 \u00a0\u00bb \u00a0 \u00ab\u00a0User1&Prime;, \u00a0\u00bb \u00a0 user1 \u00a0 \u00ab\u00a0) <br \/>uri.setDataSource (\u00a0\u00bb \u00a0 public \u00a0 \u00bb,\u00ab \u00a0 details \u00a0 \u00bb,\u00ab \u00a0 geom \u00a0 \u00a0\u00bb \u00a0 \u00ab\u00a0Version = 1\u00a0\u00bb) <br \/>layer = QgsVectorLayer ( uri.uri (), \u00a0\u00bb \u00a0 reefs \u00a0 \u00bb,\u00ab \u00a0 postgres \u00a0 \u00ab\u00a0) <br \/>if not layer.isValid (): <br \/>print \u00ab \u00a0 Layer% s did not load \u00a0 \u00bb% Layer.name () <br \/>QgsMapLayerRegistry.instance (). addMapLayers ([layer])<\/p>\r\n<\/div><\/div><\/figure>\r\n\r\n\r\n\r\n\r\n\r\n<p>The script must look like this: <\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2016\/08\/pv10.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"445\" data-attachment-id=\"6237\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality\/py21-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py21.png?fit=716%2C498&amp;ssl=1\" data-orig-size=\"716,498\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"py21\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py21.png?fit=640%2C445&amp;ssl=1\" class=\"wp-image-6237\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py21.png?resize=640%2C445&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py21.png?w=716&amp;ssl=1 716w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py21.png?resize=300%2C209&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>You will observe that the line of code that creates the layer<\/p>\r\n\r\n\r\n\r\n<p><strong><em>layer<\/em><\/strong> <strong><em>=<\/em><\/strong> <strong><em>QgsVectorLayer<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>uri.uri<\/em><\/strong> <strong><em>(), \u00ab\u00a0<\/em><\/strong> <strong><em>reefs<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>postgres<\/em><\/strong> <strong><em>\u00ab\u00a0)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>contains a provider <strong><em>postgres<\/em><\/strong> instead of <strong><em>ogr<\/em><\/strong> and that the file name is replaced by a <strong><em>uri<\/em><\/strong> object . It is this object that is in charge of the connection with the Postgis database.<\/p>\r\n\r\n\r\n\r\n<p><strong><em>uri<\/em><\/strong> <strong><em>=<\/em><\/strong> <strong><em>QgsDataSourceURI<\/em><\/strong> <strong><em>()<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>creates the object ,<\/p>\r\n\r\n\r\n\r\n<p><strong><em>uri.setConnection<\/em><\/strong> <strong><em>(\u00ab\u00a0<\/em><\/strong> <strong><em>localhost<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>5432<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>postgres<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>\u00ab\u00a0User1\u00a0\u00bb, \u00ab\u00a0<\/em><\/strong> <strong><em>user1<\/em><\/strong> <strong><em>\u00ab\u00a0)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>creates the connection string :<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>server address (here <em>localhost<\/em> )<\/li>\r\n<li>postgres listening port (here <em>5432<\/em> )<\/li>\r\n<li>name of the database ( here <em>postgres<\/em> )<\/li>\r\n<li>name of the user (here <em>user1<\/em> )<\/li>\r\n<li>user password (here <em>user1<\/em> )<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>If you want to run the script on your machine you must change these values for those of an existing database on your device ( if you keep localhost) or return a basic address accessible via the Internet.<\/p>\r\n\r\n\r\n\r\n<p><strong><em>uri.setDataSource<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>public<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>details<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>geom<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>\u00ab\u00a0Version = 1\u00a0\u00bb)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>This line defines which Postgis table to use in the connected database:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>source name ( here <em>public<\/em> )<\/li>\r\n<li>table name ( here <em>details<\/em> )<\/li>\r\n<li>\u00a0geometry field of the table name( here <em>geom<\/em> )<\/li>\r\n<li>eventually a SQL clause of type WHERE.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Right here <strong><em>version = 1<\/em><\/strong> corresponds to a clause<\/p>\r\n\r\n\r\n\r\n<p><em>SELECT * FROM details WHERE<\/em> <strong><em>version = 1<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>If you do not want to apply a filter to the table, simply return \u00a0\u00bb \u00a0\u00a0 \u00ab\u00a0.<\/p>\r\n\r\n\r\n\r\n<h2><strong>How to load<\/strong> <strong>a<\/strong> <strong>Spatialite layer<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>Very little used, therefore very much practical and powerful, the Spatialite bases can also be loaded simply with Python.<\/p>\r\n\r\n\r\n\r\n<p>Here is the code for a script comparable to the previous one but which loads a table from a Spatialite base:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6238\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality\/tuerca-8\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-4.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"tuerca\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-4.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6238\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/tuerca-4.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><div class='stb-container stb-style-grey'><div class='stb-caption'><div class='stb-logo'><img class='stb-logo__image' src='data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUh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p+3HSRdMa8Eisoe65Etrr5iauT3tJefRuXvxuVrARDzeDymdWvXOg1anbDg27fR7e6gpqaG225bxNGjx5g0cSrPrF4V3bRpY6fsUeHzhtYnVX6u2Fj42NrfM8TlG2YAo4GvARNXrVo1PJVMalrb2jSTJk0Rurs7EUWJ7du2uHfu2nkI2AjsAtzSVX705wX5JLArX0tXgfl47hlkOS\/Nzc0d0eV2Z40dNz6voCA\/c\/v27b12uz3a2Nh4GtgHnAO8H7XQJL4okGsZSvkclyELhEnOsdgV8t9zZRH8vwpytZIgXCUU+cwgX\/7z4UuQL0H++UD+awDc\/MMFLMO8WgAAAABJRU5ErkJggg==' alt='img'\/><\/div><div class='stb-caption-content'><\/div><div class='stb-tool'><\/div><\/div><div class='stb-content'>\r\n<p># Customize this starter script by adding code <br \/># to the run_script function. See the Help for <br \/># complete information on how to create a script <br \/># and use Script Runner.<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00bb \u00a0\u00a0 \u00a0\u00bb \u00a0 \u00bbYour description of the script goes here\u00ab \u00a0\u00a0 \u00a0\u00bb \u00a0 \u00ab\u00a0<\/p>\r\n\r\n\r\n\r\n<p># Some commonly used imports<\/p>\r\n\r\n\r\n\r\n<p>from PyQt4.QtCore import * <br \/>from PyQt4.QtGui import * <br \/>from qgis.core import * <br \/>from qgis.gui import *<\/p>\r\n\r\n\r\n\r\n<p>def run_script ( iface ): <br \/>uri = QgsDataSourceURI () <br \/>uri.setDatabase (\u00a0\u00bb \u00a0 H: \/repertoiresblog\/QGis\/validation.sqlite \u00a0 \u00ab\u00a0) <br \/>uri.setDataSource (\u00a0\u00bb \u00a0\u00a0 \u00bb,\u00ab \u00a0 com2011orig \u00a0 \u00bb,\u00ab \u00a0 geom \u00a0 \u00a0\u00bb \u00a0 \u00bb Cod_reg = 9&Prime;) <br \/>layer = QgsVectorLayer ( uri.uri (), \u00a0\u00bb \u00a0 Commons \u00a0 \u00bb,\u00ab \u00a0 spatialite \u00a0 \u00ab\u00a0) <br \/>if not layer.isValid (): <br \/>print \u00ab \u00a0 Layer% s did not load \u00a0 \u00bb% Layer.name () <br \/>QgsMapLayerRegistry.instance (). addMapLayers ([layer])<\/p>\r\n<\/div><\/div><\/figure>\r\n\r\n\r\n\r\n\r\n\r\n<p>Your script must look like this: <\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2016\/08\/pv11.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"449\" data-attachment-id=\"6239\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/python-methods-for-qgis-how-to-access-vector-data-postgis-spatiality\/py22-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py22.png?fit=711%2C499&amp;ssl=1\" data-orig-size=\"711,499\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"py22\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py22.png?fit=640%2C449&amp;ssl=1\" class=\"wp-image-6239\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py22.png?resize=640%2C449&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py22.png?w=711&amp;ssl=1 711w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/09\/py22.png?resize=300%2C211&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p><strong><em>uri.setDatabase<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>H: \/repertoiresblog\/QGis\/validation.sqlite<\/em><\/strong> <strong><em>\u00ab\u00a0)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>This line defines the path and name of the Spatialite base concerned .<\/p>\r\n\r\n\r\n\r\n<p><strong><em>\u00a0 uri.setDataSource<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>com2011orig<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>geom<\/em><\/strong> <strong><em>\u00ab\u00a0<\/em><\/strong> <strong><em>\u00bb<\/em><\/strong> <strong><em>Cod_reg<\/em><\/strong> <strong><em>= 9&Prime;)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>The first parameter, source, does not apply to Spatiality databases .<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>name of the table to load ( here <em>com2011orig<\/em> )<\/li>\r\n<li>name of the geometry field ( here <em>geom<\/em> )<\/li>\r\n<li>SQL filtering clause , optional ( here <em>cod_reg<\/em> <em>= 9<\/em> )<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p><strong><em>\u00a0<\/em><\/strong> <strong><em>layer<\/em><\/strong> <strong><em>=<\/em><\/strong> <strong><em>QgsVectorLayer<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>uri.uri<\/em><\/strong> <strong><em>(), \u00ab\u00a0<\/em><\/strong> <strong><em>Commons<\/em><\/strong> <strong><em>\u00bb,\u00ab<\/em><\/strong> <strong><em>spatialite<\/em><\/strong> <strong><em>\u00ab\u00a0)<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>We create the layer with the object uri we have just built and as data provider <strong><em>spatialite<\/em><\/strong> .<\/p>\r\n\r\n\r\n\r\n<h2><strong>Other<\/strong> <strong>data<\/strong> <strong>providers<\/strong> <strong>available<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>We have discussed three data providers for QgsVectorLayer: <strong><em>ogr,<\/em><\/strong> <strong><em>postmaster,<\/em><\/strong> and <strong><em>spatialite<\/em><\/strong> . The first is, in fact, a library that allows access to many formats.<\/p>\r\n\r\n\r\n\r\n<p>In addition to these three providers, you can use the following:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong><em>memory<\/em><\/strong> which loads the created layers in memory<\/li>\r\n<li><strong><em>mssql<\/em><\/strong> which allows to load a layer a Microsoft SQL database<\/li>\r\n<li><strong><em>wfs<\/em><\/strong> which allows to load a layer of a WFS stream<\/li>\r\n<li><strong><em>delimitedtext<\/em><\/strong> which allows to load a layer from a file text<\/li>\r\n<li><strong><em>gpx<\/em><\/strong> which loads routes and waypoints from a file gpx<\/li>\r\n<li><strong><em>grass<\/em><\/strong> which charges a GRASS layer<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>In the next article we will discuss how to consult the attributes and geometries of the vector layers loaded in QGis.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>In this series of articles we discuss how to access vector type data in Python and QGis. In the previous article ( Python Recipes for QGis: access data vector (shapefile) ) we have discussed \u00a0how to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"give_campaign_id":0,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1260],"tags":[],"class_list":["post-6234","post","type-post","status-publish","format-standard","hentry","category-non-classe-en"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-1Cy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/comments?post=6234"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6234\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=6234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=6234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=6234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}