0

I tried to create a script for computing layer statistics, create a style and save image but when I run it I get an error saying "invalid syntax (, line 32) See log for more details." How can I resolve this? Is the entire code even right? Will it enable me to generate layer statistics and save the image?

xxxx=group

x-x-x-x-x=name

Input_vector_layer=vector

Field_to_calculate_statistics_on=field Input_vector_layer

Statistics=output vector

Creating layer style

from qgis.core import * myVectorLayer = QgsVectorLayer(myVectorPath, myName, 'ogr') myTargetField = 'ELEV' myRangeList = [] myOpacity = 1

Creating the first symbol and range for airports with lower than the average elevation

myMin = 9.00 myMax = 789.00 myLabel = 'Group 1' myColour = QtGui.QColor('#ff0000') mySymbol1 = QgsSymbolV2.defaultSymbol(myVectorLayer.geometryType()) mySymbol1.setColor(myColour) mySymbol1.setAlpha(myOpacity) myRange1 = QgsRendererRangeV2(myMin, myMax, mySymbol1, myLabel) myRangeList.append(myRange1)

Creating the second symbol and range now make for airports with higher than the average elevation

myMin = 789.00 myMax = 1569.00 myLabel = 'Group 2' myColour = QtGui.QColor('#0000ff') mySymbol2 = QgsSymbolV2.defaultSymbol( myVectorLayer.geometryType()) mySymbol2.setColor(myColour) mySymbol2.setAlpha(myOpacity) myRange2 = QgsRendererRangeV2(myMin, myMax, mySymbol2 myLabel) myRangeList.append(myRange2)

creating the image, background colour, setting extent, size and saving the image

img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)

set image's background color

color = QColor(255, 255, 255) img.fill(color.rgb())

create painter

p = QPainter() p.begin(img) p.setRenderHint(QPainter.Antialiasing) render = QgsMapRenderer()

set layer set

lst = [layer.id()] # add ID of every layer render.setLayerSet(lst)

set extent

rect = QgsRectangle(render.fullExtent()) rect.scale(1.1) render.setExtent(rect)

set output size

render.setOutputSize(img.size(), img.logicalDpiX())

do the rendering

render.render(p) p.end()

save image

img.save("render.png","png")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.