I found a couple of good answers like [here][1] for example, but I do not manage to adapt them and make them work...
I would like now to adapt this [answer][1], but all I get is a bunch of errors. I tried to get rid off Processings since it does not seem to work for me (QGIS 2.18.2). I am not understanding pretty well the implication of the processes, I do not know if it is the good way to do it and what are the problems.
Here is the code for creating the new shapefile and new field:
##testes=group
##Ratio =name
##layer=vector
##output_file=output vector
from PyQt4.QtCore import QVariant, QFileInfo
from qgis.core import QGis, QgsFeature, QgsField, QgsFields, QgsProject
import processing
layer= iface.activeLayer().dataProvider().dataSourceUri()
layer = path[:path.rfind('|')]
mylayer = QgsVectorLayer(layer, "inventaris_points_in_roi_single_new", "ogr")
if not mylayer.isValid():
print("Layer failed to load!")
vectorLayer = processing.getObject(mylayer)
provider = vectorLayer.dataProvider()
New_layer= processing.getObject(layer)
fields = QgsFields()
fields = provider.fields()
fields.append(QgsField('AREA', QVariant.Double,'',20,3))
fields.append(QgsField('PERIMETER', QVariant.Double,'',20,3))
fields.append(QgsField('RATIO', QVariant.Double,'',20,3))
writer = processing.VectorWriter(output_file, None, fields.toList(),
QGis.WKBMultiPolygon, New_layer.crs())
features = processing.features(vectorLayer)
for feat in features:
writer.addFeature(feat)
del writer
And the error :
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "c:/users/hocquetfp/appdata/local/temp/tmp_oajpe.py", line 18, in <module>
vectorLayer = processing.getObject(mylayer)
File "C:/Program Files/QGIS 2.18/apps/qgis/./python/plugins\processing\tools\dataobjects.py", line 216, in getObject
ret = getObjectFromUri(uriorname)
File "C:/Program Files/QGIS 2.18/apps/qgis/./python/plugins\processing\tools\dataobjects.py", line 244, in getObjectFromUri
if normalizeLayerSource(layer.source()) == normalizeLayerSource(uri):
File "C:/Program Files/QGIS 2.18/apps/qgis/./python/plugins\processing\tools\dataobjects.py", line 222, in normalizeLayerSource
source = source.replace('\\', '/')
AttributeError: 'QgsVectorLayer' object has no attribute 'replace'
And of course, make the link between the two codes.
[1]: Creating new layer from another one and adding new fields with Qgis processing python scripts?