12

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?

3
  • Look for OGR2OGR
    – CARTOS
    Commented Jan 21, 2015 at 7:48
  • I thought that was build into QGIS? Maybe I'm mistaken.
    – Frida
    Commented Jan 21, 2015 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. Commented Jan 21, 2015 at 15:50

4 Answers 4

27

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 (Ctrl+Alt+P).

  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.

3
  • 1
    @gcarrillo, you should! This is very useful and saves a lot of bother.
    – Joseph
    Commented Jan 22, 2015 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
    Commented Jan 23, 2015 at 13:19
  • how to enter all of the subfolders and export from there or to define specific folder? Commented May 24, 2023 at 10:46
10
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 the QgsRasterFileWriter

P.S.: You can find a more detailed example in the PyQGIS Developer Cookbook.

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

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:

import os
from qgis.core import *

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.

0

is this also possible with .gpkg-Layers to .xlsx?

I tried:

for vLayer in iface.mapCanvas().layers():
QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
    myDir + vLayer.name() + ".xlsx", "utf-8", 
    vLayer.crs())

But I receive this error message:

'Creation of data source failed (OGR error: sqlite3_open(P:\x01_Projektordner\x819\x819128_IEK_Augsburg\x05_Daten\\GIS\\Shape\\FNP_bestand_nach Gemeinden_Tabellengemeinde_Adelsried.xlsx.gpkg) failed: unable to open database file)')

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