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!
-
1The FW Tools binaries are several years old. If you are on Windows try OSGeo4W instead.Ragi Yaser Burhum– Ragi Yaser Burhum2012年09月17日 14:34:53 +00:00Commented Sep 17, 2012 at 14:34
-
possible duplicate of How to resample a batch of rasters using OGR/GDAL?Ian Turton– Ian Turton2012年09月17日 14:36:09 +00:00Commented Sep 17, 2012 at 14:36
2 Answers 2
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")
-
1I 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– Cody Brown2012年09月17日 15:04:22 +00:00Commented Sep 17, 2012 at 15:04
-
Yes, you will need to import the FW tools shell into the command prompt. The process is outlined here pythoncentral.org/… for importing python to the command shell, but the process should be the same for FW Tools.sgrieve– sgrieve2012年09月17日 15:55:58 +00:00Commented Sep 17, 2012 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– Cody Brown2012年09月18日 14:10:37 +00:00Commented Sep 18, 2012 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 gisinternals.com.Ralph Dell– Ralph Dell2017年06月01日 13:27:49 +00:00Commented Jun 1, 2017 at 13:27
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...
-
This seems great. Where about's would I specify the input and output directory?Cody Brown– Cody Brown2012年09月17日 15:05:00 +00:00Commented Sep 17, 2012 at 15:05