コード例 #1
0
    def __save__(self, qgis_project_file=None):
        """
        Save the currect project. Because the normal project.write
         loses

        :param qgis_project_file: str
        :return: None

        """
        f, fn = os.path.split(self.qprjf)
        temp_prj_n = fn + '.tmp'
        out_filet = os.path.join(f, temp_prj_n)
        out_file = qgis_project_file or self.qprjf
        outf_info = QFileInfo(out_filet)
        if os.path.exists(out_filet):
            os.remove(out_filet)

        logger.debug('Saving project to %s' % out_file)

        assert self.qprj.write(
            outf_info), 'failed to save QGIS project to file %s  ' % out_filet

        doc = QDomDocument()
        doc.setContent(xml_jano.etree.tostring(
            xml_jano.etree.parse(out_filet)))

        qgisNode = doc.documentElement()

        os.remove(out_filet)

        # canvas bridge for layer order in new style (after 2.4 version)
        self.canvas_bridge.writeProject(doc)

        # and it seesm there is old style legend order  (before 2.4 version ) so I add this element as well for compatibility
        if self.canvas_bridge.hasCustomLayerOrder():
            logger.debug(
                '...creating legend element for old style (<2.4 version) projects to specifiy layer order '
            )
            clorder = self.canvas_bridge.customLayerOrder()
            lelem = QgsLayerTreeUtils.writeOldLegend(
                doc, self.qprj.layerTreeRoot(),
                self.canvas_bridge.hasCustomLayerOrder(), clorder)
            qgisNode.appendChild(lelem)

        # now the composers
        for composer_name in self.composers:
            logger.debug('...creating composer %s' % composer_name)
            lxml_composer_el = self.qpath_extract_element(
                qpath='Composer[@title="%s"]' % composer_name)
            title = lxml_composer_el.get('title')
            visible = lxml_composer_el.get('visible')
            qt_composer_doc = QDomDocument()
            qt_composer_doc.setContent(
                '<Composer title="%s" visible="%s"></Composer>' %
                (title, visible))

            composer_el = qt_composer_doc.documentElement()
            # print qt_composer_doc.toString()
            composition = self.get_composition_object(
                composer_name=composer_name)

            assert composition.writeXML(
                composer_el,
                qt_composer_doc), 'Failed to write composer %s' % composer_name
            qgisNode.appendChild(qt_composer_doc.documentElement())

        # and finally save it
        fh = QFile(out_file)
        qts = QTextStream(fh)
        if not fh.open(QIODevice.WriteOnly):
            raise IOError, unicode(fh.errorString())
        qts.setCodec('UTF-8')
        doc.save(qts, 2)
コード例 #2
0
    def save(self, qgis_project_file=None):
        """

        :param qgis_project_file:
        :return:
        """

        out_file = qgis_project_file or self.qprjf
        logger.info('Saving %s to %s  ' % (self.qprjf, out_file))
        logger.debug('...creating main "qgis" element')
        #root with attrs
        doc = QDomDocument()
        qgisNode = doc.createElement('qgis')
        qgisNode.setAttribute('projectname', self.qprj.title())
        qgisNode.setAttribute('version', QGis.QGIS_VERSION)
        doc.appendChild(qgisNode)

        #title
        title_node = doc.createElement('title')
        titleText = doc.createTextNode(self.qprj.title())
        title_node.appendChild(titleText)
        qgisNode.appendChild(title_node)
        logger.debug('...creating transaction element')

        #transaction, added in 2.16

        transactionNode = doc.createElement('autotransaction')
        try:
            transactionNode.setAttribute('active',
                                         int(self.qprj.autoTransaction()))
        except AttributeError:  # qgis version < 2.16
            transactionNode.setAttribute('active', 0)
        qgisNode.appendChild(transactionNode)

        logger.debug('...creating evaluateDefaultValues element')
        #evaluate defaults
        evaluateDefaultValuesNode = doc.createElement('evaluateDefaultValues')
        try:
            evaluateDefaultValuesNode.setAttribute(
                'active', int(self.qprj.evaluateDefaultValues()))
        except AttributeError:
            evaluateDefaultValuesNode.setAttribute('active', 0)

        qgisNode.appendChild(evaluateDefaultValuesNode)

        #layer tree element
        logger.debug('...creating layer tree element')
        util.introspect(self.canvas, namefilter='legend')
        self.qprj.layerTreeRoot().writeXML(qgisNode)

        #relations,
        logger.debug('...creating relations element')
        relations_el = doc.createElement('relations')
        for rn, r in self.qprj.relationManager().relations().items():
            r.writeXML(relations_el, doc)
        qgisNode.appendChild(relations_el)
        logger.debug('...creating map canvas/map settings element')
        #now map canvas
        self.canvas.writeProject(doc)

        logger.debug(
            '...creating layer order element for new (>2.4 version) projects ')
        #canvas bridge for layer order in new style (after 2.4 version)
        self.canvas_bridge.writeProject(doc)

        #and it seesm there is old style legend order  (before 2.4 version ) so I add this element as well for compatibility
        if self.canvas_bridge.hasCustomLayerOrder():
            logger.debug(
                '...creating legend element for old style (<2.4 version) projects to specifiy layer order '
            )
            clorder = self.canvas_bridge.defaultLayerOrder()
            lelem = QgsLayerTreeUtils.writeOldLegend(
                doc, self.qprj.layerTreeRoot(),
                self.canvas_bridge.hasCustomLayerOrder(), clorder)
            qgisNode.appendChild(lelem)

        # now the composers
        for composer_name in self.composers:
            logger.debug('...creating composer %s' % composer_name)
            lxml_composer_el = self.qpath_extract_element(
                qpath='Composer[@title="%s"]' % composer_name)
            title = lxml_composer_el.get('title')
            visible = lxml_composer_el.get('visible')
            qt_composer_doc = QDomDocument()
            qt_composer_doc.setContent(
                '<Composer title="%s" visible="%s"></Composer>' %
                (title, visible))

            composer_el = qt_composer_doc.documentElement()
            #print qt_composer_doc.toString()
            composition = self.get_composition_object(
                composer_name=composer_name)

            assert composition.writeXML(
                composer_el,
                qt_composer_doc), 'Failed to write composer %s' % composer_name
            qgisNode.appendChild(qt_composer_doc.documentElement())

        #util.introspect(self.qprj.layerTreeRoot(), namefilter='layer')
        projectLayersNode = doc.createElement('projectlayers')
        logger.debug('...creating project layers element')
        for ml in self.canvas.layers():
            maplayerElem = doc.createElement('maplayer')
            ml.writeLayerXML(maplayerElem, doc)
            self.qprj.emit(QtCore.SIGNAL('writeMapLayer'), ml, maplayerElem,
                           doc)
            projectLayersNode.appendChild(maplayerElem)

        qgisNode.appendChild(projectLayersNode)
        #TODO find out how to read all the properties
        logger.debug('...creating project properties')
        prop_content = self.__fetch_properties__()
        prop_doc = QDomDocument()
        prop_doc.setContent(prop_content)

        qgisNode.appendChild(prop_doc.documentElement())

        # util.introspect(self.qprj )
        # print xml.etree.tostring(self.qpath_extract_element('/properties'))
        # print self.qprj.entryList('Digitizing', '')
        # print self.qprj.entryList('/', '')
        # print self.qprj.readEntry('Digitizing', 'SnappingMode')
        # print self.qprj.property('Digitizing')

        self.qprj.visibilityPresetCollection().writeXML(doc)

        # and finally save it
        fh = QFile(out_file)
        qts = QTextStream(fh)
        if not fh.open(QIODevice.WriteOnly):
            raise IOError, unicode(fh.errorString())
        qts.setCodec('UTF-8')
        doc.save(qts, 2)