Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Many thanks

share|improve this question
check this, stackoverflow.com/questions/450285/… –  Antony Jul 10 '13 at 16:22
also for a few examples, bostongis.com/?content_name=ogr_cheatsheet –  Antony Jul 10 '13 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 '13 at 16:40
@Antony Got it working, installed FWtools on linux and used os.system to use the command. Thanks!! –  Moggy Jul 10 '13 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 '13 at 6:01
add comment

1 Answer

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'])
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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