Using pyqgis in a QGIS 3 Plugin, I can get features from a layer and geometry from features. Is there a way to pass the geometry into psycopg2 so that it will work in PostGIS?
I did find the PPyGIS http://www.fabianowski.eu/projects/ppygis/ but could not get it to work with Python 3.7
# Create table
cursor.execute("CREATE TABLE structures (name VARCHAR(32) PRIMARY KEY, start VARCHAR(16), geom GEOMETRY);")
self.get_layers()
pipe_lyr = QgsProject.instance().mapLayersByName(self.pipe_layer)[0]
if not pipe_lyr.isValid():
self.popMsg('Layer is not valid')
return
pipes = pipe_lyr.getFeatures()
for pipe in pipes:
geom = pipe.geometry()
# Insert some data into table
cursor.execute("INSERT INTO structures (name, start, geom) VALUES (%s, %s, %s);", (str(pipe['name']), str(pipe['start']), geom))
print( pipe['name'])
It errors out with
TypeError: not all arguments converted during string formatting Traceback (most recent call last): File "C:/Users/Cary/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\invert_builder\invert_builder.py", line 288, in azure_db cursor.execute("INSERT INTO structures (name, start, geom) VALUES (%s, %s);", (str(pipe['name']), str(pipe['start']), geom)) TypeError: not all arguments converted during string formatting<
This is a plugin for QGIS 3.