7

I am trying to convert MapINFO tabs to ESRI Shapefiles using FW Tools. I downloaded the tools and can run the FW Tools Shell and convert no problem:

ogr2ogr -f "ESRI Shapefile" E:\CABWorking\MapINFO2SHP\SHP\LincsBoundary.shp E:\CABWorking\MapINFO2SHP\MapINFOData\Lincolnshire.tab

In the shell you can type python and use the shell as a python interpreter. Awesome, but I need this to work as I want to convert a ton of MapINFO files at once.

My problem is: I can't figure out how to get the following code to work in a python environment. I've been searching all morning and just have no clue. Any help would be awesome!

Thanks!

sgrieve
  • 3,726
  • 1
  • 19
  • 36
Cody Brown
  • 4,387
  • 5
  • 43
  • 78

2 Answers2

8

In Python the os module provides a quick and dirty way to run a command line argument such as the one you posted as part of a larger script as outlined in the Python documentation

import os
command = "ogr2ogr -f \"ESRI Shapefile\" E:\\CABWorking\\MapINFO2SHP\\SHP\\LincsBoundary.shp E:\\CABWorking\\MapINFO2SHP\\MapINFOData\\Lincolnshire.tab"
os.system(command)

Note the use of backslashes to escape characters such as " and \ in the command string.

As I said, this is quick and dirty, the Pythonic way to do this is to use the Subprocess library which provides the same functionality but in a more stable framework, and with some more features:

import os
command = "ogr2ogr -f \"ESRI Shapefile\" E:\\CABWorking\\MapINFO2SHP\\SHP\\LincsBoundary.shp E:\\CABWorking\\MapINFO2SHP\\MapINFOData\\Lincolnshire.tab"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000) 
#the third command suppresses the opening of a new window for each process
#and can be removed if this is not the desired outcome
process.wait() #waits for one process to complete before launching another one

I used this answer to solve my issue. Thanks a ton @sgrieve. The final code ended up being:

import os, glob
inputDir = raw_input("==> ") # Had to have this code compatible with 2.3.4
outputDir = raw_input("==> ") # So that meant no easy file dialog boxes
TabFiles = glob.glob(str(inputDir) + '/*.tab')
for TabFile in TabFiles:
    TabFileName = TabFile[(TabFile.rfind("\\"))+1:(TabFile.rfind("."))]
    command = 'ogr2ogr -f "ESRI Shapefile" ' + outputDir + "/" + TabFileName + '.shp ' + TabFile
    os.system(command)

I'd then open the FWTools Shell, enter python and call the script with:

execfile("script")
Cody Brown
  • 4,387
  • 5
  • 43
  • 78
sgrieve
  • 3,726
  • 1
  • 19
  • 36
  • 1
    I really like this solution, but when run it doesn't work. I think it's because python is trying to run the command in CMD as oppose to the FW Tools Shell. Maybe I need to bring the shell into CMD as well? – Cody Brown Sep 17 '12 at 15:04
  • Yes, you will need to import the FW tools shell into the command prompt. The process is outlined here http://pythoncentral.org/how-to-install-python-2-7-on-windows-7-python-is-not-recognized-as-an-internal-or-external-command/ for importing python to the command shell, but the process should be the same for FW Tools. – sgrieve Sep 17 '12 at 15:55
  • Thanks a ton. Took me a little bit to get it going, but it now runs pretty smoothly thanks to your help! I'll paste the code in your answer. – Cody Brown Sep 18 '12 at 14:10
  • It is an old post but escaping the double quote allowed me to execute ogr2ogr from python or php. After setting up the command, from python I used subprocess.Popen(command, stdout=subprocess.PIPE, ,) and from php exec($command);, I used the ogr2ogr from http://www.gisinternals.com. – Ralph Dell Jun 01 '17 at 13:27
2

use this code with entering its folder with your command line:

for %f in (*.tab) do ogr2ogr -f "ESRI Shapefile" %~new.shp %f

i hope it helps you...

urcm
  • 22,533
  • 4
  • 57
  • 109