0

I have a csv file with the following columns:

table   geometry    field   type    typeName    len   precision

and my goal is to create a number of shp files programatically with PyQGIS.

My script stops after creating the first shp file.

Here's my code.

import sys
import re 
import csv
from PyQt4.QtCore import *
from PyQt4.QtGui import * 

reload(sys)  
sys.setdefaultencoding('utf8')
final_list = []

with open(r"D:\Users\Ravi_narayanan\Documents\shpgen.csv", "rU") as s:  
    reader_tables = csv.DictReader(s,delimiter=';')
    liste_tables = list(reader_tables)
    l_tables = [i['table'] for i in liste_tables]
    l_tables = set(l_tables)
    for table in l_tables:
        l1 = [line for line in liste_tables if line['table'] == table] 
        geometry = l1[1]['geometry']
        layer_mem = QgsVectorLayer(geometry+"?crs=epsg:2154",table,"memory")
        QgsVectorFileWriter.writeAsVectorFormat(layer_mem,r"D:\setupCAPFT2.00\{}.shp".format(table),"utf-8",None,"ESRI Shapefile",False)
        layer_path = "D:\{}.shp".format(table)
        shp = QgsVectorLayer(layer_path,table,'ogr')
        pr = shp.dataProvider()
        shp.startEditing()
        for field in l1:
            if field['type'] != 'Double':
                pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']))])
            else:
                pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']),int(field['precision']))])
        shp.updateFields()
        shp.commitChanges()
        shp.stopEditing()
        del layer_mem,shp,pr   
1
  • Hello BERA, I forgot to add my problem, i just edited the post. My problem is that the script creates one shp file and it stops. Commented Dec 18, 2017 at 11:29

2 Answers 2

1

It was the

shp.stopEditing()

line that stop the script

0

I think you shouldn't use both dataProvider and an editing session at the same. It's either one or the other one.

The same script without the dataprovider, working only in an editing session:

shp = QgsVectorLayer(layer_path,table,'ogr')
shp.startEditing()
for field in l1:
    if field['type'] != 'Double':
        shp.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']))])
    else:
        shp.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']),int(field['precision']))])
shp.commitChanges()

or with the dataProvider without an editing session

shp = QgsVectorLayer(layer_path,table,'ogr')
pr = shp.dataProvider()
for field in l1:
    if field['type'] != 'Double':
        pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']))])
    else:
        pr.addAttributes([QgsField(field['field'],getattr(QVariant,field['type']),field['typeName'],int(field['len']),int(field['precision']))])
shp.updateFields()

I would personally go for the first one.

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.