Geographic Information Systems Asked on April 6, 2021
I am attempting to write a plugin to export a LayerVector (Points) to images.
I need to get the symbology of each point to export as images (PNG).
My intention here is to create a new file containing the attributes of the LayerVector. I want to add one column 'icon_url'
with the name of the image that will be used as symbology for the point.
The following code works for some symbologies, but some types the originalSymbolForFeature
return None
.
How to draw the visual representation used in QGIS and export it to a image?
import time;
layer = iface.mapCanvas().layers()[0]
clonedLayer = layer.clone()
layerRenderer = clonedLayer.renderer()
renderContext = QgsRenderContext()
renderContext.setUseAdvancedEffects(True)
renderContext.setFlags(QgsRenderContext.Flag.RenderBlocking | QgsRenderContext.Flag.Antialiasing)
imageList = list()
iconField = QgsField('icon_url', QVariant.String, 'text')
clonedLayer.startEditing()
res = clonedLayer.addAttribute(iconField)
clonedLayer.commitChanges()
clonedLayer.startEditing()
for feature in clonedLayer.getFeatures():
layerRenderer.startRender(renderContext, clonedLayer.fields())
symbol = layerRenderer.originalSymbolForFeature(feature, renderContext)
layerRenderer.stopRender(renderContext)
curImage = symbol.asImage(QSize(32, 32))
try:
imgIndex = imageList.index(curImage)
except Exception as e:
imageList.append(curImage)
imgIndex = len(imageList) - 1
feature.setAttribute("icon_url", './' + str(imgIndex) + ".png")
clonedLayer.updateFeature(feature)
clonedLayer.commitChanges()
wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326')
QgsVectorFileWriter.writeAsVectorFormat(clonedLayer, 'C:/Users/danilo/Downloads/symbol/myjson_danilo_.json',
'utf-8', wgs_crs, 'CSV', layerOptions=['GEOMETRY=AS_XY'])
In fact the function originalSymbolForFeature
only work for some symbology types (Strange behaviour yet nothing about it in documentation).
I've just changed to originalSymbolsForFeature
and everything worked well.
symbol = layerRenderer.originalSymbolsForFeature(feature, renderContext)
if len(symbol) <= 0:
continue
else:
symbol = symbol[0]
PS: Using QGIS 3.10.2
Answered by Asfixia on April 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP