QGIS 2.14 & 2.18
Step 1 : I do an extraction with QgsDataSourceURI()
#get geometries
uri = QgsDataSourceURI()
uri.setConnection("svdbocsva003.test.blavla.lan", "1521", "tata", "toto", "pwd")
uri.setSrid ('31370');
uri.setWkbType(QGis.WKBMutiPolygon)
uri.setDataSource("OSIRIS", "OSI_SITES" , "SHAPE",b1query ,"OBJECTID")
uri.uri()
sdoLayer = QgsVectorLayer(uri.uri(), "requete", 'oracle')
sdoLayer.setCrs(QgsCoordinateReferenceSystem(31370))
QgsMapLayerRegistry.instance().addMapLayer(sdoLayer)
sdoFeatures = sdoLayer.getFeatures()
All works fine until this step. The layer is added to the project correctly. Features are displayed.
Step 2 : I try to write this layer to hard disk
error = QgsVectorFileWriter.writeAsVectorFormat(sdoLayer, "C:\\user\\test\\test0.shp", "test",None, "ESRI Shapefile")
if error == QgsVectorFileWriter.NoError:
print "success!"
else :
print "error"
del error
I get an error message. The file is written but empty as seen as an error has occurred.
Step 3 : I try to create new vector layer with joined data from oracle attributes table and the geom layer with a common field
writer = QgsVectorFileWriter("C:\\user\\test\\test1.shp","test", fields, QGis.WKBMultiPolygon, QgsCoordinateReferenceSystem(31370), "ESRI Shapefile")
# add a feature
for attr in attributes :
print(attr[0])
exp = QgsExpression('ID_WS = {}' .format(attr[0]))
request = QgsFeatureRequest(exp)
request.setLimit(1)
feat = QgsFeature()
feat.setGeometry(sdoLayer.getFeatures(request).next().geometry())
#feat.setAttributes([1, "text"])
writer.addFeature(feat)
# delete the writer to flush features to disk
del writer
QGIS is crashing randomly when i iterate on attributes. OGR Logs gives me Source de données invalide () (invalid data source) in the OGR logs. It looks like the problem. Sometimes it does not crash so i can note the file is empty.
EDIT1 : With a workaround by activating the debugger as explained in this post, QGIS is crashing less frequently.When it's not crashing, i get a feature without geometry with a field FID. By the way, i had defined an output layer processing variable. It was generating error in the log. I deleted it as it was not used. So now the only error message i get is :
Traitement(1): Cannot find variable: svdbocsva003
Traitement(1): Cannot find variable: svdbocsva003
Where svdbocsva003 is the beginning of the host address. I don't understandwhat what is wrong.
EDIT2 : STEP2 solved
- The crash occurs when i want to write a file for a second time. So the overwrite is the cause. No need to be loaded in a project for crashing
- The right usage of QgsVectorFileWriter.writeAsVectorFormat is with the overrideGeometryType as QgsWKBTypes.MultiPolygon in my case and forceMulti as True. With that it's generating well the vector layer
Step 3 solved in the selected answer, my answer
for
loop supposed to be indented like you have shown? Also, you have a space in the line'ID_WS = {}' .format(attr[0])
but I would assume this would pop up an error. And maybe takefeat = QgsFeature()
outside thefor
loop.