First I downloaded http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/ogr2ogr.py and I am importing this. How can I get ogr2ogr to run in my script?

Nothing seems to be working and I'm not sure how to continue problem solving.

#!/usr/bin/env python    
import ogr2ogr
import sys

if __name__=="__main__":
ogr2ogr.main('-f', "Mapinfo File", '/home/firstuser/birmingham/Wood.tab', "/home/firstuser/birmingham/Wood.shp")

With this I get main() takes at most 3 arguments (4 given)

and when I do:

if __name__=="__main__":
ogr2ogr.main('-f', "Mapinfo File", '/home/firstuser/birmingham/Wood.tab')

I get:

Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]
           [-select field_list] [-where restricted_where]
           [-progress] [-sql <sql statement>]
           [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]
           [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]
           [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]
           [-simplify tolerance]
           [-fieldTypeToString All|(type1[,type2]*)] [-explodecollections]
           dst_datasource_name src_datasource_name
           [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3] [layer [layer ...]]

 -f format_name: output file format name, possible values are:
 -f "ESRI Shapefile"
 -f "MapInfo File"
 -f "TIGER"
 -f "S57"
 -f "DGN"
 -f "Memory"
 -f "BNA"
 -f "CSV"
 -f "GML"
 -f "GPX"
 -f "KML"
 -f "GeoJSON"
 -f "Interlis 1"
 -f "Interlis 2"
 -f "GMT"
 -f "SQLite"
 -f "ODBC"
 -f "MSSQLSpatial"
 -f "PostgreSQL"
 -f "MySQL"
 -f "PCIDSK"
 -f "DXF"
 -f "Geoconcept"
 -f "GeoRSS"
 -f "GPSTrackMaker"
 -f "PGDump"
 -f "GPSBabel"
 -append: Append to existing layer instead of creating new if it exists
  -overwrite: delete the output layer and recreate it empty
  -update: Open existing output datasource in update mode
  -progress: Display progress on terminal. Only works if input layers have the "fast      feature count" capability
  -select field_list: Comma-delimited list of fields from input layer to
                 copy to the new layer (defaults to all)
  -where restricted_where: Attribute query (like SQL WHERE)
  -sql statement: Execute given SQL statement and save result.
  -skipfailures: skip features or layers that fail to convert
  -gt n: group n features per transaction (default 200)
  -spat xmin ymin xmax ymax: spatial query extents
  -simplify tolerance: distance tolerance for simplification.
  -dsco NAME=VALUE: Dataset creation option (format specific)
  -lco  NAME=VALUE: Layer creation option (format specific)
  -nln name: Assign an alternate name to the new layer
  -nlt type: Force a geometry type for new layer.  One of NONE, GEOMETRY,
  MULTIPOLYGON, or MULTILINESTRING.  Add "25D" for 3D layers.
  Default is type of source layer.
  -dim dimension: Force the coordinate dimension to the specified value.
  -fieldTypeToString type1,...: Converts fields of specified types to
  fields of type string in the new layer. Valid types are :
  Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,
  StringList. Special value All can be used to convert all fields to strings.
  -a_srs srs_def: Assign an output SRS
  -t_srs srs_def: Reproject/transform to this SRS on output
  -s_srs srs_def: Override source SRS

  Srs_def can be a full WKT definition (hard to escape properly),
  or a well known definition (ie. EPSG:4326) or a file with a WKT
  • check this, stackoverflow.com/questions/450285/…
    – Antony
    Jul 10, 2013 at 16:22
  • also for a few examples, bostongis.com/?content_name=ogr_cheatsheet
    – Antony
    Jul 10, 2013 at 16:24
  • @Antony This would work if i was in windows and say running FWtoos, but i am in linux and need to call upon the ogr2ogr.py module/script. correct?
    – Moggy
    Jul 10, 2013 at 16:40
  • @Antony Got it working, installed FWtools on linux and used os.system to use the command. Thanks!!
    – Moggy
    Jul 10, 2013 at 17:08
  • Could you post the solution as an answer below so that the next person looking for the solution can easily find it?
    – om_henners
    Jul 11, 2013 at 6:01

2 Answers 2


I believe the solution to OP's question is to wrap the arguments as a list, and offset the list by one empty string (It's expecting the first argument to be the script name, so you can just ignore that argument:

ogr2ogr.main(['', '-f', "Mapinfo File", '/home/firstuser/birmingham/Wood.tab'])

if you use bash shell in linux, you can use $locate ogr2ogr.py to locate the ogr2ogr's path, and then you can use the following code

$python PATH/ogr2ogr.py -f PARAMETERS PATH/YourToFileName PATH/YourFromFileName

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.