Here's some quick-and-dirty Python code that works on a simple shapefile layer:
# Get the currently selected layer
inLayer = iface.activeLayer()
# Get its list of fields
inFields = inLayer.dataProvider().fields()
# Convert its geometry type enum to a string we can pass to
# QgsVectorLayer's constructor
inLayerGeometryType = ['Point','Line','Polygon'][inLayer.geometryType()]
# Convert its CRS to a string we can pass to QgsVectorLayer's constructor
inLayerCRS = inLayer.crs().authid()
# Make the output layer
outLayer = QgsVectorLayer(inLayerGeometryType + '?crs='+inLayerCRS, \
inLayer.name() + u'_copy', \
'memory')
# Copy the fields from the old layer into the new layer
outLayer.startEditing()
outLayer.dataProvider().addAttributes(inFields.toList())
outLayer.commitChanges()
# Add it to the map
QgsMapLayerRegistry.instance().addMapLayer(outLayer)
I load up a simple shape file, select that layer in the Layers box, paste the above code into the Python console, and a new layer appears with the same attributes.
The key parts here (I'm guessing from your question) are the lines that get the list of fields from the input layer (inLayer.dataProvider().fields()
, documented here) and add those same fields to the output layer (outLayer.dataProvider().addAttributes(inFields.toList())
, documented here)
Effectively, I see that you have set the string "aa"
as output. If you want to use your result for further steps, you have to set the last algorithm parameter as None
.
Firstly, you need to change this:
target_ds= processing.runalg("gdalogr:rasterize",
{"INPUT": ellipse_shp,
"FIELD": "id1",
"DIMENSIONS": 0,
"WIDTH": x_count,
"HEIGHT": y_count,
"RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
"TFW": 1,
"RTYPE": 4,
"NO_DATA": 1,
"COMPRESS": 0,
"JPEGCOMPRESSION": 1,
"ZLEVEL": 1,
"PREDICTOR": 1,
"TILED": False,
"BIGTIFF": 2,
"EXTRA": '',
"OUTPUT": "aa"})
to this:
target_ds= processing.runalg("gdalogr:rasterize",
{"INPUT": ellipse_shp,
"FIELD": "id1",
"DIMENSIONS": 0,
"WIDTH": x_count,
"HEIGHT": y_count,
"RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
"TFW": 1,
"RTYPE": 4,
"NO_DATA": 1,
"COMPRESS": 0,
"JPEGCOMPRESSION": 1,
"ZLEVEL": 1,
"PREDICTOR": 1,
"TILED": False,
"BIGTIFF": 2,
"EXTRA": '',
"OUTPUT": None})
and then you may handle your result in this way:
# You may use any name instead of 'result'
result = processing.getObject(target_ds["OUTPUT"])
Now it works?
Best Answer
I modified your code to consider a polygon vector layer. However, if you get the features of the original layer, you only need a little portion of your original code for working adequately (without any editing session if you use the
QgsDataProvider
class).This is my code:
When above code was running at the Python Console (Ctrl+Alt+P) of QGIS, fields of original layer were correctly copied at the memory layer named "result"; as can be observed at the next image. It worked for me.
On the other hand, your approach of set attributes to a list of fields instead a list of values is correct because if I comment these code lines:
it doesn't populate the fields with data; as it also observed at the next image: