0

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.

6
  • Is your DB-server running? Is gdal compiled with the postgres driver? Commented Jul 31, 2018 at 8:42
  • Hi @AndreasMüller, my DB-server is running and gdal is complied with the postgres driver, used sudo apt install libpq-dev I still get the same error message
    – Rose
    Commented Jul 31, 2018 at 20:31
  • Your solution uses a standalone pyqgis application without initializing qgis correctly. I think you also could use the ogr2ogr to write shapes into a postgres table. Commented Jul 31, 2018 at 21:05
  • Could you possibly explain to me how I'm not initializing qgis correctly? I am running this software on Ubuntu 16.04LTS
    – Rose
    Commented Jul 31, 2018 at 21:09
  • 1
    You may read the section in the pyqgis cookbook (docs.qgis.org/2.18/en/docs/pyqgis_developer_cookbook/…). But it would be easier to write the data with ogr2ogr which you already use.(bostongis.com/PrinterFriendly.aspx?content_name=ogr_cheatsheet) Commented Jul 31, 2018 at 21:28

1 Answer 1

1

Thanks to Andreas Muller's explanation above, the answer is that the QGIS modules in the python script needs to be initialized if the script is standalone. These are instructions to do this in Ubuntu 16.04 LTS;

# supply path to qgis install location
QgsApplication.setPrefixPath("/usr/bin/qgis", True)

# create a reference to the QgsApplication, setting the
# second argument to False disables the GUI
qgs = QgsApplication([], False)

# load providers
qgs.initQgis()

# Write your code here to load some layers, use processing
# algorithms, etc.

# When your script is complete, call exitQgis() to remove the
# provider and layer registries from memory

qgs.exitQgis()

Sourced from PyQGIS Cookbook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.