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.

I have a directory with several shapefiles. How can convert all these shapefiles to MapInfo with ogr2ogr. I know how I can convert one file. And I can make a batch script writing a line for each file. But isn't there an easier way to convert all the files in a directory (and subdirectory).

Hope someone can help.

share|improve this question
    
It depends what operating system. Basically make a "for" loop, see gis.stackexchange.com/questions/7803/… –  Mike T May 14 '12 at 9:06
    
OS is Windows 7. I try this in the command prompt: for %f in *.shp; do ogr2ogr -f MapInfo File $f.tab. But that doesn't work. I get the error: 'Unable to open datasour '$f.tab' with the following drivers..... –  Stefan May 14 '12 at 9:46
    
This is how to do SHP to TAB: ogr2ogr -f "MapInfo File" %1.tab %1.shp. But unless OGR is able to recognize a PRJ file correctly, you easily end up with TAB files with the wrong coordsys built-in. I recommend using the built-in Universal Translator (scaled down FME), which can also process all the files you select. It allows you to select the correct coordsys. –  Uffe Kousgaard May 14 '12 at 12:01
    
I do indeed have problems with the right projection in MapInfo File. I try to include the right projection, but this doesn't work. The projection in de mapinfo file is set to non-earth (meters). Is it possible to include the correct projection with the ogr2ogr tool. I would like to put the command lines in another script. So I rather not use the Universal Translator. –  Stefan May 14 '12 at 14:10
add comment

7 Answers 7

up vote 9 down vote accepted

On Windows, for the current and sub-directories under the current, try this command:

for /R %f in (*.shp) do ogr2ogr -f "MapInfo File" "%~dpnf.tab" "%f"

To briefly explain the trickery of what is going on here, %~dpnf.tab uses the variable %f, with which it adds the driver letter, path name (i.e., folder or directory), and extracts the file name (without the .shp file extension). Lastly, .tab is added immediately after the compound variable modifiers for the new extension.

So if you are in directory C:\MyData, and you have data in this directory, and sub-directories C:\MyData\Region1 and C:\MyData\Region1\City1, any Shapefile (with .shp extension) will be processed, and a similar named file with .tab will created in the same directory.

share|improve this answer
    
Cool! This works! Many thanks for the answer :) One more question: Is there also a way to refer to subdirectory and convert the files in another subdirectory. I have a containing various directories containting shapefiles. Such as: Netherlands\shape\provinceX Netherlands\shape\provinceY Netherlands\shape\provinceZ And would like to convert this into these folders: Netherlands\mapinfo\provinceX Netherlands\mapinfo\provinceY Netherlands\mapinfo\provinceZ Thanks :) –  Stefan May 14 '12 at 10:21
    
I've updated the above for a recursive search, however I don't know how to do the extra logic to change "shape" to "mapinfo"; maybe find/ask on superuser.com –  Mike T May 14 '12 at 11:06
1  
Thanks again for your quick response. This was very helpful. I only need to copy the files to the right directory. –  Stefan May 14 '12 at 12:22
add comment

As Alex Markov said, the best way would be for you to use OGR.

You will need to do a script, but a very simple one. Basicly if you are using windows, all you have to do is run the following code in the command line (inside de desired folder):

for %f in (*.tab) do ogr2ogr -f "KML" %~nf.kml %f

Check this site, all is explained here:

http://www.processamentodigital.com.br/?p=2511

share|improve this answer
1  
I think you can also do: ogr2ogr -f "KML" {outfolder} {infolder} Tip taken from underdark.wordpress.com/2012/05/09/batch-shapefile-clipping –  Nathan W May 16 '12 at 11:26
    
NB This reply came from a closely related question that referred to different file formats. –  whuber May 16 '12 at 14:07
add comment

Have you tried OGR library and it's command-line tools?

share|improve this answer
    
Is this not the same as FW Tools? Following your link, it tells me to download "the latest FWTools kit"... –  CatchingMonkey May 16 '12 at 9:01
1  
OGR is a part of FW Tools. You can run batch processing the way as @alexandre-neto has answered you. –  Alex Markov May 16 '12 at 9:41
add comment

with python:

import os 

for a in os.listdir(os.getcwd()):
   fileName, fileExtension = os.path.splitext(a)
   os.system('ogr2ogr -f "ESRI Shapefile" %s %s' % (a, fileName + '.tab'))

you can change os.getcwd() to your path where your files are located...

i hope it helps you

share|improve this answer
    
for a in *.shp: is a syntax error; you are thinking from glob import glob; for a in glob('*.shp'): –  Mike T May 14 '12 at 10:15
    
oh sorry, i have written it in wrong style. i have updated. @ Mike Toews thanks. –  Aragon May 14 '12 at 10:27
add comment

You don't need a loop, you can simply name a directory as source and destination and ogr2ogr will convert any files in the source directory. See here: Converting TAB to MIF / MIF to TAB

share|improve this answer
    
Thanks! Can I refer from one parent directory to all the subdirs and converting all the files to other subdirs. Ihave: Netherlands\shape\provinceX Netherlands\shape\provinceY Netherlands\shape\provinceZ and so on... And would like to convert this into these folders: Netherlands\mapinfo\provinceX Netherlands\mapinfo\provinceY Netherlands\mapinfo\provinceZ I can write a line for each province, but can I also do it with one command –  Stefan May 14 '12 at 12:24
    
I havent't tried this, but I don't think that this is possible. You would need a loop wich handles the respective input and output directories. In this case it is probably easier to follow a python approach. –  Torsten May 15 '12 at 9:54
add comment

sounds like you need a for loop - see http://www.robvanderwoude.com/for.php for an explanation of how they work.

share|improve this answer
add comment

For unix bash:

#!/bin/bash

for FILE in *.mif # cycles through all files in directory (case-sensitive!)
do
    echo "converting file: $FILE..."
    FILENEW=`echo $FILE | sed "s/.mif/_new.shp/"` # replaces old filename
    ogr2ogr \
    -f "ESRI Shapefile" \
    $FILENEW $FILE
done
exit

If you're working in a *nix-based OS (ie Linux or OS X), there are some batch shell scripts that clhenrik developed here (which the above is based on).

I'm working on a fork that makes some of the scripts a little more generic and provides a little more description on use.

share|improve this answer
add comment

Your Answer

 
discard

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.