How could one force to use a specific format to read input files?

Out of the box ogr2ogr automatically chooses from it's long list of formats, which most of the time is great. However today I'm troubleshooting data files which won't convert, but should, and I'm trying to get to the bottom of why. Knowing with confidence that ogr2ogr is using the correct driver and not picking the wrong driver(s) would be a great aid in that process.

A conversation on Freenode indicated OGR_SKIP config option might be one way (the only?), but I've not been able to concoct a working incantation yet.

Both command line ogr2ogr and python solutions are welcome.

  • Try something like ogr2ogr -f [outputformat] output.file input.file --config OGR_SKIP GML KML for skipping two drivers. Test with ogrinfo which lists what driver it used. – user30184 Apr 8 '15 at 21:45
  • I opened an enhancement request to make this idea an ogr feature: trac.osgeo.org/gdal/ticket/5917 – matt wilkie Apr 9 '15 at 20:32
  • @user30184, please add that as an answer. Ogr2ogr example was asked for as well as python and I'd like to give credit where due. – matt wilkie Apr 10 '15 at 16:43
up vote 4 down vote accepted

A python solution is fairly simple using ogr.GetDriverByName(in_format).Open(in_file).

import sys
from osgeo import ogr
def main(in_file, in_format, out_file, out_format):
    in_ds = ogr.GetDriverByName(in_format).Open(in_file)
    out_ds  = ogr.GetDriverByName(out_format).CopyDataSource(in_ds, out_file)

if __name__ == '__main__':
    main(*sys.argv[1:])

To take care of the CSV special case (see comments) without changing the input filename format:

def main(in_file, in_format, out_file, out_format):
    if in_format == 'CSV' and in_file[-3:].lower() != 'csv':
        in_file = 'CSV:' + in_file
    in_ds = ogr.GetDriverByName(in_format).Open(in_file)
    out_ds  = ogr.GetDriverByName(out_format).CopyDataSource(in_ds, out_file)

If you want to stick with the command line ogr2ogr, it's useful to know that the order that drivers are listed in ogrinfo --formats is the order in which they are tried, so you can limit the number of drivers you have to specifiy with OGR_SKIP.

  • this looks beautiful, but doesn't work for me. Script: gist.github.com/maphew/d9ca85f5080221475788, data: gist.github.com/maphew/3f1198ac2de718d1c1b6, command: python ogr-convert.py resid.txt CSV xtest3 "ESRI Shapefile", result: ERROR 10: Pointer 'hSrcDS' is NULL in 'OGR_Dr_CopyDataSource'. If data file is renamed to *.csv, then it works. So it looks GetDriverByName is overridden by something else. (This isn't the actual data I'm trying to convert, it's just one I know works, used for testing). – matt wilkie Apr 9 '15 at 17:17
  • 2
    @Matt, in general the code works. In this particular case the CSV driver was used, but it didn't attempt to open the .txt file so Open(in_file) returned None. According to the documentation - 'OGR only supports CSV files ending with the extention ".csv".'. However... you can force the driver to open the .txt - 'Starting with GDAL 1.8.0, for files structured as CSV, but not ending with .CSV extension, the 'CSV:' prefix can be added before the filename to force loading by the CSV driver.' – Luke Apr 9 '15 at 20:07
  • Nominated for inclusion in the GDAL/OGR Cookbook: github.com/pcjericks/py-gdalogr-cookbook/pull/155. Thanks Luke! – matt wilkie Apr 9 '15 at 21:26

As of GDAL-2.3.0 (commit), you can give a hint for a few of the JSON drivers by prefixing a special string to the filename. The strings to prefix filename are:

  • ESRIJSON:
  • GEOJSON:
  • TOPOJSON:

For example:

ogrinfo -ro -so ESRIJSON:esri.json
ogrinfo -ro -so GEOJSON:geo.json
ogrinfo -ro -so TOPOJSON:topo.json

will tell OGR to try the given driver when reading the corresponding json file.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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