TransWikia.com

Batch importing raster TIFFs into PostgreSQL using Python

Geographic Information Systems Asked on October 10, 2020

I have a fairly simple script where I aim to insert roughly 30 raster TIFFs into a PostgreSQL database.

To manually import one TIFF at a time I run this command

raster2pgsql.exe -s 2263 -d -C -I -M -l 2,4,8,16 rasterpath_001.tif -F -t 100x100 dem_001 | psql -d raster_test -U postgres -p 5434

This works great

Now to the Python script

folder=r'rasterpath'
for x in os.listdir(folder):
    if x.endswith('.tif'):
        fname='dem_'+x[-7:][:3]
        pg= "raster2pgsql.exe -s 2263 -d -C -I -M -l 2,4,8,16 {0} -F -t 100x100 {1} | psql -d raster_test -U postgres -p 5434 ".format(os.path.join(folder,x),fname)
        print(pg)
        subprocess.run(pg)

I get this error

ERROR: Unable to read raster file: dem_001
ERROR: Unable to read raster file: dem_002
...

But if I run the printed output from the Python script manually into the CMD it succesfully runs.

Kind of confused on what is going wrong here.

Using Python 3.7

One Answer

In response to comment, something like this would work in bash:

#!/bin/bash
PATH=/path/to/dest/
OPTIONS="-s 2263 -d -C -I -M -l 2,4,8,16 -F -t 100x100" 
CMD_PIPE="psql -d raster_test -U postgres -p 5434"
for fname in dem_001 dem_002;
do raster2pgsql $OPTIONS *.tif $fname | $CMD_PIPE;
done

:: windows (not tested)
:: FOR %%A IN (list) DO command parameters
%%OPTIONS="-s 2263 -d -C -I -M -l 2,4,8,16 -F -t 100x100"
%%TMP_FILE='tmpfile.sql'
FOR %%fname IN (dem_001 dem_002)
DO (raster2pgsql.exe %%OPTIONS *.tif %%fname >> %%TMP_FILE)

Answered by vinh on October 10, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP