3

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>
2
  • I could provide code for you to test however I don't know how to get you the feature class I am working with. Any ideas how I can do that?
    – user20408
    Apr 15 '20 at 16:31
  • @user2856 i updated the question
    – user20408
    Apr 16 '20 at 22:54
5

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).

4
  • Thanks for this. A couple of questions, if you know... Should I be setting this in my python script or just doing so on the command line/in my .bashrc file? Will this also prevent actual errors from being logged, or just warnings? I'll give it a shot...thanks
    – user20408
    Apr 17 '20 at 20:20
  • It'll prevent errors being logged, but that's not an issue for python scripts,use gdal.UseExceptions() and ogr.UseExceptions()
    – user2856
    Apr 17 '20 at 23:44
  • @user2856 Is there a way to make this work in a Jupyter Notebook? ogr2ogr -f "ESRI Shapefile" test.shp test.gpkg test with ! set CPL_LOG=NUL does not work, nor does ! gdal.SetConfigOption('CPL_LOG', '/dev/NUL') (I want to receive other warnings in Notebook, just not these :) )
    – a11
    Jul 16 at 21:26
  • It's 'NUL' not '/dev/NUL' on Windows. Other than that I don't know, I don't use jupyter notebooks, but if I did (or in py scripts), I'd be using gdal.VectorTranslate('test.shp', 'test.gpkg', layers=['test']) not an external process. This should work with gdal.SetConfigOption('CPL_LOG', 'NUL') .
    – user2856
    Jul 16 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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