5

I'm writing a qgis plugin to create an image in the print composer of the current map. I can get the map rendered in the proper place with a frame of the right width, and I can get grid annotations rendered in the proper places and orientation, but I can't get the actual grid lines to render. Here is the code I'm using:

def createImage(self):
    # Initialize the composition object.
    mapRenderer = self.iface.mapCanvas().mapRenderer()
    composition = QgsComposition(mapRenderer)
    composition.setPlotStyle(QgsComposition.Print)
    composition.setPaperSize(483, 330)
    composition.setPrintResolution(300)

    # Initialize the map object.  Allow a top margin of 10mm, a bottom
    # margin of 50mm, a left margin of 10mm, and a right margin of 10mm.
    mapOriginXMm = 10
    mapOriginYMm = 10
    mapWidthMm = 483 - 10 -10
    mapHeightMm = 330 - 10 - 50
    composerMap = QgsComposerMap(composition, \
                                     mapOriginXMm, \
                                     mapOriginYMm, \
                                     mapWidthMm, \
                                     mapHeightMm)
    composition.addItem(composerMap)

    # Create the frame around the map.
    composerMap.setFrameEnabled(1)
    pen = QPen()
    pen.setWidth(0.2)
    pen.setJoinStyle(Qt.MiterJoin)
    composerMap.setPen(pen)

    # Initialize the image object.
    dpi = 300
    dpmm = dpi / 25.4
    paperWidthPixels = int(dpmm * composition.paperWidth())
    paperHeightPixels = int(dpmm * composition.paperHeight())
    image = QImage(QSize(paperWidthPixels, paperHeightPixels), \
                       QImage.Format_ARGB32)
    image.setDotsPerMeterX(dpmm * 1000)
    image.setDotsPerMeterY(dpmm * 1000)
    image.fill(0)

    # Create the map grid.
    composerMap.setGridEnabled(True)
    composerMap.setGridIntervalX(500)
    composerMap.setGridIntervalY(500)
    composerMap.setShowGridAnnotation(True)
    composerMap.setGridAnnotationPrecision(0)
    composerMap.setGridStyle(QgsComposerMap.Solid)

    # Top
    composerMap.setGridAnnotationPosition(QgsComposerMap.OutsideMapFrame, \
                                              QgsComposerMap.Top)
    composerMap.setGridAnnotationDirection(QgsComposerMap.Horizontal, \
                                               QgsComposerMap.Top)
    # Bottom
    composerMap.setGridAnnotationPosition(QgsComposerMap.OutsideMapFrame, \
                                              QgsComposerMap.Bottom)
    composerMap.setGridAnnotationDirection(QgsComposerMap.Horizontal, \
                                               QgsComposerMap.Bottom)

    # Left
    composerMap.setGridAnnotationPosition(QgsComposerMap.OutsideMapFrame, \
                                              QgsComposerMap.Left)
    composerMap.setGridAnnotationDirection(QgsComposerMap.Vertical, \
                                               QgsComposerMap.Left)

    # Right
    composerMap.setGridAnnotationPosition(QgsComposerMap.OutsideMapFrame, \
                                              QgsComposerMap.Right)
    composerMap.setGridAnnotationDirection(QgsComposerMap.Vertical, \
                                               QgsComposerMap.Right)

    composerMap.setAnnotationFrameDistance(1)
    composerMap.setGridPenWidth(5.0)
    composerMap.setGridPenColor(QColor(0, 0, 0))
    composerMap.setAnnotationFontColor(QColor(0, 0, 0))
    composerMap.setGridBlendMode(QPainter.CompositionMode_DestinationOver)

    # Do the rendering.
    imagePainter = QPainter(image)
    paperRectMm = QRectF(0, \
                             0, \
                             composition.paperWidth(), \
                             composition.paperHeight())
    paperRectPixels = QRectF(0, 0, paperWidthPixels, paperHeightPixels)
    composition.render(imagePainter, paperRectPixels, paperRectMm)
    imagePainter.end()

    image.save("/opt/tmpPhotos/test.tif", "tif")

Can anyone tell me what I'm missing?

2

I stumbled upon the solution. Instead of

composition.addItem(composerMap)

the code should read

composition.addComposerMap(composerMap)
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.