7

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.

3
  • 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
    Commented Apr 8, 2015 at 21:45
  • I opened an enhancement request to make this idea an ogr feature: trac.osgeo.org/gdal/ticket/5917 Commented Apr 9, 2015 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. Commented Apr 10, 2015 at 16:43

3 Answers 3

7

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.

3
  • 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). Commented Apr 9, 2015 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.'
    – user2856
    Commented Apr 9, 2015 at 20:07
  • Nominated for inclusion in the GDAL/OGR Cookbook: github.com/pcjericks/py-gdalogr-cookbook/pull/155. Thanks Luke! Commented Apr 9, 2015 at 21:26
7

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.

1

For certain file extensions (e.g. .MDB), there may be multiple compatible drivers (PGeo, ODBC, MDB). In this case, to choose a specific driver (e.g. MDB) out of the three, it is necessary to set the OGR_SKIP environment variable for the GDAL command line tools (e.g. ogrinfo, ogr2ogr etc)

As of latest GDAL (3.x), this worked for me on Ubuntu:

export OGR_SKIP="ODBC,PGeo" # "ODBC PGeo" does not work

ogrinfo GEONAMES.mdb

Output:

INFO: Open of `GEONAMES.mdb'
      using driver `MDB' successful.
1: GEONAMES_ANNO_HYPSO (Polygon)
2: GEONAMES_ANNO_HYDRO (Polygon)
3: GEONAMES_ANNO_PLACES (Polygon)

Note: Geonames data (referenced here) was taken from https://www.mass.gov/info-details/massgis-data-geographic-place-names, as download location has changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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