2

I am studying PyQGIS to analyze gis data. I want to set output path to PostgreSQL in QGIS 3.8 but I have no idea...

According to processing.algorithmHelp('qgis:simplifygeometries'), only show OUTPUT: ex> 'postgres:...'

it is my code

mycrs=QgsCoordinateReferenceSystem(3857)
uri=QgsDataSourceUri()
uri.setConnection("localhost","5432","TSDLINK","postgres","password")
geometrycol="geom"
tb_1="tsd_level"
uri.setDataSource("public",tb_1,geometrycol)
layer_6=QgsVectorLayer(uri .uri(False),tb_1,"postgres")
layer_6.setCrs(mycrs,True)
mycontext=QgsProcessingContext()
param_simply={'INPUT':layer_6 ,"METHOD": 0,'TOLERANCE': 3000,'OUTPUT':'??????'}
processing.run('qgis:simplifygeometries',param_simply,feedback=None,context=mycontext)

'output':?????????

How to set path?

3

To save the result in a postgres table you must create the connection string.

For example:

str_output_db = "postgis:dbname=\'" + self.dbname + "\' host=" + self.host + " port=" + self.port + " user=" + self.user + " password=" + self.password + " table=\"public\".\"" + table_name + "\" (the_geom) sql="
processing.run("native:saveselectedfeatures", {'INPUT': vlayer, 'OUTPUT': str_output_db})

Where vlayer is your vector layer and obviously you have to put your connection data, host, port etc.

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.