31

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

7
  • It depends what operating system. Basically make a "for" loop, see gis.stackexchange.com/questions/7803/…
    – Mike T
    Commented May 14, 2012 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
    Commented May 14, 2012 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. Commented May 14, 2012 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
    Commented May 14, 2012 at 14:10
  • Have you tried OGR library and it's command-line tools? Commented May 16, 2012 at 8:58

8 Answers 8

39

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.

5
  • 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
    Commented May 14, 2012 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
    Commented May 14, 2012 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
    Commented May 14, 2012 at 12:22
  • 1
    Note that Windows command line and batch file command differ: You have to double the percent %f to %%f. See ss64.com/nt/for.html for details.
    – AndreJ
    Commented Aug 23, 2016 at 15:53
  • This a great, is the a similar approach on a MAC Commented Oct 2, 2016 at 16:00
13

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.


2
  • This will cause an error if the filename contains spaces. Commented Mar 14, 2018 at 15:21
  • I believe this answer above works with spaces. most likely @JasonRDalton edited the old version :)
    – Faris
    Commented Feb 11, 2021 at 5:35
9

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

3
  • 2
    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
    Commented May 16, 2012 at 11:26
  • NB This reply came from a closely related question that referred to different file formats.
    – whuber
    Commented May 16, 2012 at 14:07
  • Hi, when I try this approach in Ubuntu e.g. ogr2ogr -f "KML" {outfolder} {infolder} I get the error: 'ERROR 4: Failed to create KML file'. Any idea what might be going wrong here? I have built GDAL with KML support
    – marty_c
    Commented Feb 23, 2015 at 12:10
8

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

2
  • for a in *.shp: is a syntax error; you are thinking from glob import glob; for a in glob('*.shp'):
    – Mike T
    Commented May 14, 2012 at 10:15
  • oh sorry, i have written it in wrong style. i have updated. @ Mike Toews thanks.
    – urcm
    Commented May 14, 2012 at 10:27
4

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

2
  • 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
    Commented May 14, 2012 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
    Commented May 15, 2012 at 9:54
2

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

0

Universal Translator in Mapinfo can handle multiple files. Go to the destination folder, select all shapefiles, define CRS for input and output. It should be done.

0

This is refinement of Aragon's answer above. It will only run ogr2ogr on .TAB or .tab files, and ignore other file types in the folder.

#!/usr/bin/env python3

import os 

for a in os.listdir(os.getcwd()):
  fileName, ext = os.path.splitext(a)
  if ext.lower() == '.tab':
    print('ogr2ogr -f "ESRI Shapefile" %s %s' % (a.replace(ext,
                           '.shp'), fileName + ext))
    os.system('ogr2ogr -f "ESRI Shapefile" %s %s' % (a.replace(ext,
                           '.shp'), fileName + ext))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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