コード例 #1
0
ファイル: datamanager.py プロジェクト: Ecocline/Qgis2threejs
  def _initRenderer(self):
    # set up a renderer
    labeling = QgsPalLabeling()
    renderer = QgsMapRenderer()
    renderer.setDestinationCrs(self.context.crs)
    renderer.setProjectionsEnabled(True)
    renderer.setLabelingEngine(labeling)

    # save renderer
    self._labeling = labeling
    self._renderer = renderer
コード例 #2
0
    def _initRenderer(self):
        # set up a renderer
        labeling = QgsPalLabeling()
        renderer = QgsMapRenderer()
        renderer.setDestinationCrs(self.exportSettings.crs)
        renderer.setProjectionsEnabled(True)
        renderer.setLabelingEngine(labeling)

        # save renderer
        self._labeling = labeling
        self._renderer = renderer
コード例 #3
0
ファイル: printer.py プロジェクト: candela-it/sunlumo
    def printToPdf(self, params):
        self.check_required_params(params)

        with change_directory(self.project_root):

            crs = QgsCoordinateReferenceSystem()
            crs.createFromSrid(params.get('srs'))

            mapRenderer = QgsMapRenderer()
            mapUnits = crs.mapUnits()
            mapRenderer.setMapUnits(mapUnits)

            mapExtent = QgsRectangle(*params.get('bbox'))
            mapRenderer.setExtent(mapExtent)

            le = QgsPalLabeling()
            mapRenderer.setLabelingEngine(le)

            layers = params.get('layers')
            self.setTransparencies(layers, params.get('transparencies'))
            mapRenderer.setLayerSet(layers)

            composer = (
                self.getLayoutbyName(params['layout'])
                .firstChildElement('Composition')
            )

            comp = QgsComposition(mapRenderer)
            comp.setPlotStyle(QgsComposition.Print)

            comp.readXML(composer, self.doc)
            # read composition elements
            comp.addItemsFromXML(composer, self.doc)

            comp.setPrintResolution(90)

            # set bbox for the first Map in the layout
            comp_map = comp.getComposerMapById(0)

            comp_map.setNewExtent(QgsRectangle(*params.get('bbox')))
            # comp_map.setNewScale(10000)

            # comp_map.setLayerSet(layers)
            # comp_map.setKeepLayerSet(True)

            # save the file
            comp.exportAsPDF(params['tmpFile'] + '.pdf')
コード例 #4
0
    def saveControlImage(self, tmpimg=''):
        # don't save control images for RenderVsOtherOutput (Vs) tests, since
        # those control images belong to a different test result
        if ('PAL_CONTROL_IMAGE' not in os.environ or 'Vs' in self._TestGroup):
            return
        imgpath = self.controlImagePath()
        # print "saveControlImage: {0}".format(imgpath)
        testdir = os.path.dirname(imgpath)
        if not os.path.exists(testdir):
            os.makedirs(testdir)
        imgbasepath = \
            os.path.join(testdir,
                         os.path.splitext(os.path.basename(imgpath))[0])
        for f in glob.glob(imgbasepath + '.*'):
            if os.path.exists(f):
                os.remove(f)
        if tmpimg and os.path.exists(tmpimg):
            shutil.copyfile(tmpimg, imgpath)
        else:
            print '\nsaveControlImage.render(): entered'
            print '{0}.{1}'.format(self._TestGroup, self._TestFunction)

            ms = self._MapSettings  # class settings
            """:type: QgsMapSettings"""
            if self._TestMapSettings is not None:
                ms = self._TestMapSettings  # per test settings
            print 'self._MapSettings...'
            print 'ms.layers(): {0}'.format(
                [self._MapRegistry.mapLayer(i).name() for i in ms.layers()])
            print 'ms.outputSize(): {0} x {1}'.format(ms.outputSize().width(),
                                                      ms.outputSize().height())
            print 'ms.outputDpi(): {0}'.format(ms.outputDpi())
            print 'ms.mapUnits(): {0}'.format(ms.mapUnits())
            print 'ms.extent(): {0}'.format(ms.extent().toString())
            print 'ms.hasCrsTransformEnabled(): {0}'.format(
                ms.hasCrsTransformEnabled())
            print 'ms.destinationCrs(): {0}'.format(
                ms.destinationCrs().authid())

            # pal = QgsPalLabeling()
            pal = self._Pal.clone()  # or custom settings are lost
            pal.init(ms)
            r = QgsMapRenderer()
            r.setLabelingEngine(pal)

            # this seems too redundant
            r.setOutputSize(ms.outputSize(), ms.outputDpi())
            r.setMapUnits(ms.mapUnits())
            r.setExtent(ms.extent())
            r.setProjectionsEnabled(ms.hasCrsTransformEnabled())
            r.setDestinationCrs(ms.destinationCrs())
            r.setLayerSet(ms.layers())

            ctx = r.rendererContext()
            ctx.setDrawEditingInformation(
                ms.testFlag(QgsMapSettings.DrawEditingInfo))
            ctx.setForceVectorOutput(
                ms.testFlag(QgsMapSettings.ForceVectorOutput))
            ctx.setUseAdvancedEffects(
                ms.testFlag(QgsMapSettings.UseAdvancedEffects))

            image = QImage(ms.outputSize(), QImage.Format_ARGB32)
            image.fill(ms.backgroundColor().rgb())
            image.setDotsPerMeterX(ms.outputDpi() / 25.4 * 1000)
            image.setDotsPerMeterY(ms.outputDpi() / 25.4 * 1000)

            p = QPainter(image)
            r.render(p)
            p.end()

            if not image.save(imgpath, 'png'):
                os.unlink(imgpath)

            # delete extraneous world file (always generated)
            # wrld_file = imgbasepath + '.PNGw'
            # if os.path.exists(wrld_file):
            #     os.remove(wrld_file)

        if not os.path.exists(imgpath):
            raise OSError('Control image not created: {0}'.format(imgpath))
コード例 #5
0
    def saveControlImage(self, tmpimg=''):
        # don't save control images for RenderVsOtherOutput (Vs) tests, since
        # those control images belong to a different test result
        if ('PAL_CONTROL_IMAGE' not in os.environ
                or 'Vs' in self._TestGroup):
            return
        imgpath = self.controlImagePath()
        # print "saveControlImage: {0}".format(imgpath)
        testdir = os.path.dirname(imgpath)
        if not os.path.exists(testdir):
            os.makedirs(testdir)
        imgbasepath = \
            os.path.join(testdir,
                         os.path.splitext(os.path.basename(imgpath))[0])
        for f in glob.glob(imgbasepath + '.*'):
            if os.path.exists(f):
                os.remove(f)
        if tmpimg and os.path.exists(tmpimg):
            shutil.copyfile(tmpimg, imgpath)
        else:
            print '\nsaveControlImage.render(): entered'
            print '{0}.{1}'.format(self._TestGroup, self._TestFunction)

            ms = self._MapSettings  # class settings
            """:type: QgsMapSettings"""
            if self._TestMapSettings is not None:
                ms = self._TestMapSettings  # per test settings
            print 'self._MapSettings...'
            print 'ms.layers(): {0}'.format(
                [self._MapRegistry.mapLayer(i).name() for i in ms.layers()]
            )
            print 'ms.outputSize(): {0} x {1}'.format(
                ms.outputSize().width(), ms.outputSize().height())
            print 'ms.outputDpi(): {0}'.format(ms.outputDpi())
            print 'ms.mapUnits(): {0}'.format(ms.mapUnits())
            print 'ms.extent(): {0}'.format(ms.extent().toString())
            print 'ms.hasCrsTransformEnabled(): {0}'.format(
                ms.hasCrsTransformEnabled())
            print 'ms.destinationCrs(): {0}'.format(
                ms.destinationCrs().authid())

            # pal = QgsPalLabeling()
            pal = self._Pal.clone()  # or custom settings are lost
            pal.init(ms)
            r = QgsMapRenderer()
            r.setLabelingEngine(pal)

            # this seems too redundant
            r.setOutputSize(ms.outputSize(), ms.outputDpi())
            r.setMapUnits(ms.mapUnits())
            r.setExtent(ms.extent())
            r.setProjectionsEnabled(ms.hasCrsTransformEnabled())
            r.setDestinationCrs(ms.destinationCrs())
            r.setLayerSet(ms.layers())

            ctx = r.rendererContext()
            ctx.setDrawEditingInformation(
                ms.testFlag(QgsMapSettings.DrawEditingInfo))
            ctx.setForceVectorOutput(
                ms.testFlag(QgsMapSettings.ForceVectorOutput))
            ctx.setUseAdvancedEffects(
                ms.testFlag(QgsMapSettings.UseAdvancedEffects))

            image = QImage(ms.outputSize(), QImage.Format_ARGB32)
            image.fill(ms.backgroundColor().rgb())
            image.setDotsPerMeterX(ms.outputDpi() / 25.4 * 1000)
            image.setDotsPerMeterY(ms.outputDpi() / 25.4 * 1000)

            p = QPainter(image)
            r.render(p)
            p.end()

            if not image.save(imgpath, 'png'):
                os.unlink(imgpath)

            # delete extraneous world file (always generated)
            # wrld_file = imgbasepath + '.PNGw'
            # if os.path.exists(wrld_file):
            #     os.remove(wrld_file)

        if not os.path.exists(imgpath):
            raise OSError('Control image not created: {0}'.format(imgpath))