I am working in Ubuntu 16.04 LTS
This is my bash script;
# Binarize raster for later polygon creation
path=/home/rose/Desktop/test/DSM_BM24_2017_1000_4735.tif
pathdir=$(echo $path | cut -d "/" -f 1,2,3,4,5)
gdal_calc.py -A $path --outfile=$pathdir/binary.tif --NoDataValue=-9999 --calc "1 * (A != -9999)"
# Polygonize raster
binary=$pathdir/binary.tif
b=$(basename $binary)
basebinary=$(echo $b | cut -d "." -f 1)
gdal_polygonize.py $binary $pathdir/polygon$basebinary.shp polygon$basebinary
# Make EPSG:2193 projection file for polygon
ogr2ogr -a_srs EPSG:2193 -f "ESRI Shapefile" /home/rose/Desktop/test/finalpolygonbinary.shp /home/rose/Desktop/test/polygonbinary.shp
# Define variable to import to py and postgis script 'pypostgis'
polygon='$pathdir/finalpolygon$basebinary.shp'
# Call python script
python pypostgis.py $polygon
This is my python script that is being called in my bash script, which grabs the polygon shapefile and imports it into a table in PgAdmin III;
import psycopg2
import os
import sys
from qgis.core import QgsVectorLayer, QgsVectorLayerImport
from qgis.core import QgsDataSourceURI, QgsVectorLayer
# import layer into database shp2pgs function
def shp2pgs(layer):
conn = psycopg2.connect("dbname='rosespace' host='localhost' port = '5432' user='rose' password='postgres' ")
cursor = conn.cursor()
uri = "dbname='rosespace' host='localhost' port=5432 user='rose' password='postgres' table=\"public\".\"%s\" (geom) sql=" %layer.name().lower()
importvector = QgsVectorLayerImport.importLayer(layer, uri, "postgres", layer.crs(), False, False)
print importvector
# Import and use $polygon variable from bash script for layer, which will eventually be called by the function above
shapefile = sys.argv[1]
print shapefile
basename_shp = (os.path.splitext(os.path.basename(shapefile))[0]).lower()
layer = QgsVectorLayer(shapefile, basename_shp, 'ogr')
#Call function with shapefile variable - layer, originally from above bash script
shp2pgs(layer)
When I ran the bash script, this was the output of print importvector;
print importvector
(9, u'Unable to load postgres provider')
Would any of you happen to know what this error is pointing to / means and if there is a solution?
I'm guessing it's something to do with my imported variable from bash - sys.argv[1] but I've checked it, and it is a string type. I have also put quotes around the variable and it still didn't work.
I have also checked the python script substituting sys.argv[1] with a normal string filepath and it worked perfectly within python console in QGIS 2.18.
Update In my bash script I have also put double quotes around each variable expansion, ran the script and had the same error message come through.
sudo apt install libpq-dev
I still get the same error message