forked from 3liz/QgisProcessingScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Create_vector_layer_from_Postgis_table.py
47 lines (38 loc) · 1.14 KB
/
Create_vector_layer_from_Postgis_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
##[3liz - Database]=group
##Create vector layer from postgis table=name
##Host=string localhost
##Port=number 5432
##Database=string
##User=string
##Password=string
##Schema=string public
##Table=string
##Geometry_column=string geom
##Where_clause=string
##Unique_id_field_name=string id
##output=output vector
from qgis.core import *
from processing.core.VectorWriter import VectorWriter
# Create uri from database connection options
uri = QgsDataSourceURI()
uri.setConnection(Host, str(Port), Database, User, Password)
uri.setDataSource(Schema, Table, Geometry_column, Where_clause, Unique_id_field_name)
# Create the vector layer
layer = QgsVectorLayer(uri.uri(), 'vlayer', 'postgres')
# Output the vector layer
if layer.isValid():
# Create writer
writer = VectorWriter(
output,
None,
layer.dataProvider().fields(),
layer.dataProvider().geometryType(),
layer.crs()
)
# Export features
features = layer.getFeatures()
for feat in features:
writer.addFeature(feat)
del writer
else:
progress.setText('<b>## The layer is invalid - Please check the connection parameters.</b>')