Python OGR – How to Suppress Warnings in Python/OGR

ogrpython

I have a process in Python using OGR via the API and the command line. I am tired of my logs being filled with various warnings (so many that it no longer prints after the first 1000). These happen in Python when I call ogr2ogr and when I use OGR's layer.SetFeature. I expect these warnings each time, they do not actually interfere with anything, and I do not need to see them in my logs.

I can't find anything in the documentation or online for how to suppress these warnings when they occur from an ogr2ogr call and/or with the OGR API in Python. The closest answer I found applied to GDAL, not OGR.

UPDATE: I am getting two different types of warnings that are clogging up my logs:

Warning 6: Normalized/laundered field name: 'Shape_Length' to 'Shape_Leng'

Warning 1: Value 3.40282346638999984e+23 of field can_open of feature 0 not successfully written. Possibly due to too larger number with respect to field width

The former only occurs once and is not much of a problem, the second one occurs for each feature in my feature class where that value is in the cell, which can be thousands of features * up to 20 columns (of course it stops printing after 1000 warnings issued). These warnings don't actually cause problems in my output and I would like to hide them.

Here is the sample code for the ogr2ogr command (I am just clipping an input gdb to an extent) that is causing these error messages, but the same also occurs when working in the OGR API, with shapefiles that have these values.

ogr2ogr -clipsrc -102.38428516 54.5384116051 -102.031403056 54.7529375968 -spat -102.38428516 54.5384116051 -102.031403056 54.7529375968 -f "ESRI Shapefile" <output.shp> <input.gdb>

Best Answer

If you set the CPL_LOG environment variable, you can direct the GDAL/OGR errors to a file. If you specify the file as /dev/null on Linux/Mac and NUL on Windows then they will be suppressed completely.

i.e.

Linux

$ ogr2ogr  -f "ESRI Shapefile" test.shp test.gpkg test
Warning 6: Normalized/laundered field name: 'verylongfieldname' to 'verylongfi'
Warning 6: Normalized/laundered field name: 'anotherlongfieldname' to 'anotherlon'
Warning 1: Value 3.40282299999999979e+23 of field anotherlon of feature 0 not successfully written. Possibly due to too larger number with respect to field width
$ export CPL_LOG=/dev/null
ogr2ogr  -f "ESRI Shapefile" test.shp test.gpkg test
$

Windows

> ogr2ogr  -f "ESRI Shapefile" test.shp test.gpkg test
Warning 6: Normalized/laundered field name: 'verylongfieldname' to 'verylongfi'
Warning 6: Normalized/laundered field name: 'anotherlongfieldname' to 'anotherlon'
Warning 1: Value 3.40282299999999979e+23 of field anotherlon of feature 0 not successfully written. Possibly due to too larger number with respect to field width
> set CPL_LOG=NUL
ogr2ogr  -f "ESRI Shapefile" test.shp test.gpkg test
>

Note the following is untested:

The python API should respect this environment variable, but you could also try gdal.SetConfigOption('CPL_LOG', '/dev/null') (replace '/dev/null' with 'NUL' on Windows).