I have a list of csv files "name.csv" that I want to use to make various analyses in QGIS. I would like to automatize the following steps for each file :

  1. create new project name_main.qgis;
  2. import text layer;
  3. import polygon layer;
  4. [... some analyses...]
  5. save project to a new directory

I know it is possible to do batch analyses with the Atlas plugin, e.g. to export various maps. I also looked at the processing plugin but it seems to work only within one project. But in this case the different files correspond to different projects, not maps.

Each dataset corresponds to 1 site within 1 region. I would like to be able to visualize each map, if possible. Hence various projects. But to be more precise, I need to calculate mean slope from polygons (so, raster interpolation, terrain analysis and zonal statistics). I also need to reproject all the maps (coordinates x,y,z in meters) with GPS coordinates I have for 4 points each time.

Maybe I should first reproject from gps points and then merge all the sites in one map to do batch analyses?

I got the first steps to work but I have an issue for importing shapefiles: I need to first create shapefiles from text files, then to re-import the shapefile.

Here is what I have :

for cname in glob.glob('*.txt'):
      uri2 = "file:///" + csv_path + cname + "?delimiter=%s&wktField=%s" % (";", "wkt")
      name = cname.replace('.txt', '')
      cont = QgsVectorLayer(uri2, name, "delimitedtext")
      shlayer =  QgsVectorFileWriter.writeAsVectorFormat(cont,csv_path + name +'.shp',"utf-8",None,"ESRI Shapefile")
# Add to QGIS canvas
iface.addVectorLayer(shlayer, name, "ogr")

The shlayer is not created, and obviously I get an error when I try to import it. Is there a problem with how I import the WKT layer ?

  • 1
    Why do you want to create so many projects? Do you really want the projects or some analysis data results? Please clarify which analysis tools you intend to use.
    – underdark
    Apr 10, 2016 at 20:06
  • You might be better off posting your other question as a new question to attract more attention :)
    – Joseph
    Apr 25, 2016 at 9:23
  • 1
    Thanks @Joseph =) Actually I just solved my problem, so the edited code above works for me
    – Nausi
    Apr 25, 2016 at 10:02

1 Answer 1


The following is just to give you an idea on how to automate some of the steps you mentioned, hopefully you can modify it to include some of the analytical tools you need:

from PyQt4.QtCore import QFileInfo
import glob, os, processing
proj = QgsProject.instance()

# STEP 1: Define Project name and paths for csv files and shapefiles

project_name = "name"
project_path = "C:\Users\You\Desktop\Test_folder\\"
csv_path = "C:\Users\You\Desktop\Test_folder\csv\\"
polygon_path = "C:\Users\You\Desktop\Test_folder\shapefiles\\"

# STEP 2: Import csv files

# Sets current directory to path of csv files
# Finds each .csv file and identify 'X', 'Y' fields
for fname in glob.glob("*.csv"):
    uri = "file:///" + csv_path + fname + "?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", "x", "y")
    # Replace '.csv' in name when loading into QGIS
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    # Add to QGIS canvas

# STEP 3: Import shapefiles

# Sets current directory to path of shapefiles
# Finds each shapefile
for layer in glob.glob('*.shp'):
    # Replace '.shp' in name when loading into QGIS
    name = layer.replace('.shp', '')
    # Add to QGIS canvas
    iface.addVectorLayer(layer, name, "ogr")

# STEP 4: Do some analysis
# Example - To use tools from the Processing Toolbox, 
# use the Python Console to know which parameters are required.
# E.g. To see the parameters:
# `processing.alghelp("qgis:mergevectorlayers")`
# To run the tool:
# `processing.runalg("qgis:mergevectorlayers", layer_1, layer_2, None)`

# STEP 5: Save Project to directory

proj.write(QFileInfo(project_path + name + "_main.qgs"))
  • 1
    Sorry for the late reply - this should get me on the way, thank you !
    – Nausi
    Apr 15, 2016 at 14:50
  • @Nausi - No need to apologise and your most welcome! Hope it will help =)
    – Joseph
    Apr 15, 2016 at 14:51

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.