0

I was working on an example provided in the pyqgis developer cookbook and performed the code in the QGIS version 2.8.9 but i am getting the following error:

ImportError: No module named MapComposer

I had tried the link provided in the book but that is not working.

My code is as follows:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import MapComposer

lyr = QgsVectorLayer("/qgis_data/ms/mmississippi.shp",
"Mississippi", "ogr")
reg = QgsMapLayerRegistry.instance()
reg.addMapLayer(lyr)
mr = iface.mapCanvas().mapRenderer()
qc = MapComposer.MapComposer(qmlr=reg, qmr=mr)

setGridAnnoPos = qc.composerMap.setGridAnnotationPosition
setGridAnnoDir = qc.composerMap.setGridAnnotationDirection
qcm = QgsComposerMap

qc.composerMap.setGridEnabled(True)
qc.composerMap.setGridIntervalX(.75)
qc.composerMap.setGridIntervalY(.75)
qc.composerMap.setGridStyle(qcm.Solid)

qc.composerMap.setShowGridAnnotation(True)
qc.composerMap.setGridAnnotationPrecision(0)

setGridAnnoPos(qcm.OutsideMapFrame, qcm.Top)
setGridAnnoDir(qcm.Horizontal, qcm.Top)
setGridAnnoPos(qcm.OutsideMapFrame, qcm.Bottom)
setGridAnnoDir(qcm.Horizontal, qcm.Bottom)
setGridAnnoPos(qcm.OutsideMapFrame, qcm.Left)
setGridAnnoDir(qcm.Vertical, qcm.Left)
setGridAnnoPos(qcm.OutsideMapFrame, qcm.Right)
setGridAnnoDir(qcm.Vertical, qcm.Right)

qc.composerMap.setAnnotationFrameDistance(1)
qc.composerMap.setGridPenWidth(.2)
qc.composerMap.setGridPenColor(QColor(0, 0, 0))
qc.composerMap.setAnnotationFontColor(QColor(0, 0, 0))
qc.c.addComposerMap(qc.composerMap)

qc.output("/qgis_data/map.jpg", "jpg")
4
  • Have you checked this question: Creating new print composer programmatically? [closed
    – ahmadhanb
    Sep 3, 2020 at 5:51
  • There is nothing related to MapComposer library. Actually i am looking for this library. and that example is more over dedicated to set page size Sep 3, 2020 at 7:39
  • You may look at gis.stackexchange.com/questions/190165/… for a clue. In my opinion, you are trying to mix too many samples code inherited from multiple PyQGIS versions associated with corresponding QGIS versions. For specific doc for QGIS 2.8, look at the API docs e.g qgis.org/api/2.8/annotated.html Not sure why you need a so old version of QGIS (2.8 released in february 2015 FYI)
    – ThomasG77
    Sep 4, 2020 at 21:01
  • I am using this code because i am new to python don't know how to write codes on my own so i was using this code to make grid on the vector file which i had. I don,t know how to make grid using QGIS 3.10 if you know how to write a python code for making grid in a vector file then please help Sep 5, 2020 at 7:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.