0

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 qgis.utils
import os 
import sys
from qgis.core import *
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.

Rose
  • 205
  • 3
  • 12
  • 1
    When you assign `polygon`, `$pathdir` will not be expanded because it's in single-quotes. There are also a bunch of variable references that aren't properly double-quoted (run your script through [shellcheck.net](https://www.shellcheck.net)). Also, the first use of `echo .. | cut` should be replaced by the `dirname` command, and I'd use `basebinary=${b%.*}` instead of the second. – Gordon Davisson Jul 31 '18 at 07:01
  • Hi @GordonDavisson I've put all the variables that are expanded in double quotes and have the same error message when executing the bash script – Rose Jul 31 '18 at 20:42
  • 1
    Including removing the single-quotes in the assignment to `polygon`? If so, the next step is to add `set -x` to the beginning of the script, so that it prints commands as it runs them. Note that it may use unexpected quoting/escaping when it prints commands -- it prints *something equivalent to* what's being executed, and there are often many equivalent ways to quote/escape a command, and it may print a different one from what you expected. But if it prints something not equivalent to what you expected, then that's an indication of where the problem is. – Gordon Davisson Jul 31 '18 at 22:26

0 Answers0