8

I have a lot of .TAB files open in QGIS and I want to save them to .SHP so I can work with them in ArcMap.

Is there a way to save/export all these files at the same time or do I really have to "save as" separately for every single file?

  • Look for OGR2OGR – CARTOS Jan 21 '15 at 7:48
  • I thought that was build into QGIS? Maybe I'm mistaken. – Frida Jan 21 '15 at 7:50
  • Maybe use ogr2ogr as a text tool instead of having QGIS use it. This opens up possibilities for converting without opening QGIS - and with the ability to run a sequence of such instructions, converting a set of files one after another. This will work well if you have an easy way to get a list of the filenames (like if they are some kind of sequence). If we're looking at five or six layers then it'll be as easy to do one by one using QGIS - but if we're talking about many more layers/files it might be worth the effort. Check out bat files on Windows for saving a sequence of text commands. – Rostranimin Jan 21 '15 at 15:50
21

Try this Frida:

  1. Create a folder to store your Shapefiles there (e.g., I've created the folder /tmp/data/, I use GNU/Linux).

  2. In QGIS, open the QGIS Python console.

  3. Write the following line, editing the right hand side to match the full path to your folder (make sure you include the trailing slash/backslash):

    myDir = '/tmp/data/'

  4. Press Enter.

  5. Copy the following lines to the QGIS Python console:

    for vLayer in iface.mapCanvas().layers():
        QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
            myDir + vLayer.name() + ".shp", "utf-8", 
            vLayer.crs(), "ESRI Shapefile" )
    
  6. Press Enter a couple of times.

You should now have your Shapefiles inside the folder you created in step 1.

If you face troubles, let me know your OS and the full path to your folder.

  • 1
    @gcarrillo, you should! This is very useful and saves a lot of bother. – Joseph Jan 22 '15 at 13:57
  • A plugin seems like a good idea! A similar script for rasters would be useful as well. I can only seem to "save" the spatial locations of rasters meant for MapInfo by opening them in QGIS and saving them as .tif (GeoTIFF) files. It seems like ArcMap cannot access the spatial information of the rasters unless I do this. – Frida Jan 23 '15 at 13:19
8
from qgis.core import *

suffix = "_foo"
pathToFile = "/path/to/wherever/"

layers = iface.legendInterface().layers()
for layer in layers:
    newName = layer.name() + suffix + ".shp"
    ret = QgsVectorFileWriter.writeAsVectorFormat(layer, pathToFile + newName, "utf-8", None, "ESRI Shapefile")
    if ret == QgsVectorFileWriter.NoError:
        print newName + " saved to " + pathToFile + "!"

For Raster Layer use QgsRasterFileWriter

€: You can find a more detailed example in the Cookbook.

  • is there really no way to do this through the gui? – chrismarx Feb 4 '16 at 21:35
  • @chrismarx "save as"? afraid not (or at least - not that i'm aware of). – ymirsson Feb 5 '16 at 18:33
2

Just a little addition: If you would like to change the CRS of the exported file as well and add some more prefix and suffix you can alter the script like so:

from qgis.core import *
import os
pathToFile = "S:\\pathway\\"
trs = QgsCoordinateReferenceSystem()
trs.createFromId(31370)
suffix = "_Lambert1972_Versie2016-01-04"
prefix = "Transect_Vuursalamander_"
layers = iface.legendInterface().layers()
for layer in layers:
    newName = prefix + layer.name() + suffix + ".shp"
    ret = QgsVectorFileWriter.writeAsVectorFormat(layer,pathToFile + newName,'utf-8',trs,'ESRI Shapefile')
    if ret == QgsVectorFileWriter.NoError:
        print newName + " saved to " + pathToFile + "!"

The 31370 can be replaced by the EPSG of the CRS you would like the exported file to be in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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