1

I have a processing tool that helps users to export data from PostGIS tables to GPKG. I am using ogr2ogr commands to achieve the tasks. I am calling the commands via subprocess. However, I cannot print out in the log window of the tool any error that may occur while executing the ogr2ogr. Is there any way to capture errors and display them in the log window? For now, the best I could do is a configuration option to create a log file at the end

-debug on --config CPL_LOG C:\Users\Export.log'

import subprocess
import asyncio
import time

outFile = r"C:\Users\INPUT_DATA.gpkg" bwk_views = [ ('view_1', 'output_1', 'POLYGON') ,('view_2', 'output_2', 'LINESTRING')]

commands = []

for view in bwk_views: layer_name = view[1] commands.append( f'ogr2ogr -update -append -f GPKG "{outFile}" PG:service=XX -sql "SELECT * FROM wlk.{view[0]}" -nln {layer_name} -nlt {view[2]} -a_srs EPSG:31287' )

async def export(delay, command): await asyncio.sleep(delay) subprocess.run(command, shell=True)

async def main():

# Wait tasks are completed one after another 
for i, command in enumerate(commands):
    await export(i, command)

asyncio.run(main())

Nil
  • 1,517
  • 8
  • 22

1 Answers1

2

I think your issue is related to the way you get the process.run output.

You may use

import subprocess

command = ["ogr2ogr", "input.shp", "output.shp"] result = subprocess.run(command, capture_output=True, text=True)

print(result.stdout) print(result.stderr)

Code borrowed from https://stackoverflow.com/questions/41171791/how-to-suppress-or-capture-the-output-of-subprocess-run

Personally, I would go the Python way using this recipe Issue to convert from PostgreSQL input to GPKG using Python GDAL API function gdal.VectorTranslate to get Python GDAL logs instead of mixing command line calls from Python and then trying to get command line logs within Python.

The important part for logs are lines

gdal.UseExceptions()
gdal.SetConfigOption('CPL_DEBUG', 'ON')

By default, errors are thrown to stdout

To redirect them to file, you may add the same option you mentioned but in Python context

gdal.SetConfigOption('CPL_LOG_ERRORS', 'C:\Users\Export.log')
ThomasG77
  • 30,725
  • 1
  • 53
  • 93