def create_pdf(self, params: Dict[str, str], response: QgsServerResponse, project: QgsProject) -> None:
        """ Create a PDF from cadastral data
        """
        # Load ressources based on passed params
        res = self.get_ressources(params, project)

        # Get compte communal
        comptecommunal = cadastre_common.getCompteCommunalFromParcelleId(
            res.geo_parcelle,
            res.connectionParams,
            res.connector
        )
        pmulti = 1
        all_cities = params.get('ALLCITIES', 'f').lower() in ['t', 'true', '1']
        if res.type == 'proprietaire' and pmulti == 1:
            comptecommunal = cadastre_common.getProprietaireComptesCommunaux(
                comptecommunal,
                res.connectionParams,
                res.connector,
                all_cities
            )

        if self.debugMode:
            QgsMessageLog.logMessage("Comptecommunal = %s" % comptecommunal, 'cadastre', Qgis.Debug)

        # Export PDF
        qex = cadastreExport(project, res.layer, res.type, comptecommunal, res.geo_parcelle)

        paths = qex.exportAsPDF()

        if not paths:
            raise CadastreError(424, 'An error occured while generating the PDF')

        if self.debugMode:
            QgsMessageLog.logMessage("exportAsPDF(), paths: %s" % paths, 'cadastre', Qgis.Debug)

        tokens = []
        for path in map(Path, paths):
            uid = uuid4()

            if self.debugMode:
                QgsMessageLog.logMessage("Item path: %s" % path)

            newpath = self.cachedir / ('%s.pdf' % uid.hex)
            path.rename(newpath)
            tokens.append(uid.hex)

        write_json_response({
            'status': 'success',
            'message': 'PDF generated',
            'data': {
                'url': {
                    'request': 'getPdf',
                    'service': 'cadastre',
                    'token': None
                },
                'tokens': tokens
            }
        }, response)
    def create_pdf(self, params:Dict[str,str], response: QgsServerResponse, project: QgsProject) -> None:
        """ Create a PDF from cadastral data
        """
        # Load ressources based on passed params
        res = self.get_ressources(params, project)

        # Get compte communal
        comptecommunal = cadastre_common.getCompteCommunalFromParcelleId(
            res.geo_parcelle,
            res.connectionParams,
            res.connector
        )
        pmulti = 1
        if res.type == 'proprietaire' and pmulti == 1:
            comptecommunal = cadastre_common.getProprietaireComptesCommunaux(
                comptecommunal,
                res.connectionParams,
                res.connector
            )

        if self.debugMode:
            QgsMessageLog.logMessage( "Comptecommunal = %s" % comptecommunal,'cadastre',Qgis.Debug )

        # Export PDF
        qex = cadastreExport(project, res.layer,res.type,comptecommunal,res.geo_parcelle)

        paths = qex.exportAsPDF()

        if not paths:
            raise CadastreError(424,'An error occured while generating the PDF')

        if self.debugMode:
            QgsMessageLog.logMessage( "exportAsPDF(), paths: %s" % paths,'cadastre', Qgis.Debug )

        tokens = []
        for path in map(Path,paths):
            uid = uuid4()

            if self.debugMode:
                QgsMessageLog.logMessage( "Item path: %s" % path )

            newpath = self.cachedir / ('%s.pdf' % uid.hex)
            path.rename(newpath)
            tokens.append( uid.hex )

        write_json_response({
            'status': 'success',
            'message': 'PDF generated',
            'data': {
                'url': {
                    'request': 'getPdf',
                    'service': 'cadastre',
                    'token': None
                },
                'tokens': tokens
            }
        },response)
Example #3
0
)

pmulti = 1
if export_type == 'proprietaire' and pmulti == 1:
    comptecommunal = CadastreCommon.getProprietaireComptesCommunaux(
        comptecommunal,
        connectionParams,
        connector
    )

# Export PDF
print(target_dir)
qex = cadastreExport(
    p,
    layer,
    export_type,
    comptecommunal,
    feat['geo_parcelle'],
    target_dir
)
paths = qex.exportAsPDF()
print(paths)

# Add path into file
with open(output_log, 'w') as f:
    for path in paths:
        f.write(path)

# Exit
QgsApplication.exitQgis()
    def createPdf(self):
        '''
        Create a PDF from cadastre data
        '''
        params = self.request.parameterMap()

        # Check if needed params are set
        if 'LAYER' not in params or 'PARCELLE' not in params or 'TYPE' not in params or 'MAP' not in params:
            body = {
                'status':
                'fail',
                'message':
                'Missing parameters: MAP, LAYER, PARCELLE, TYPE are required '
            }
            self.setJsonResponse('200', body)
            return

        # Get layer and expression
        pmap = params['MAP']
        player = params['LAYER']
        pparcelle = params['PARCELLE']
        ptype = params['TYPE']

        # Check type
        if ptype.lower() not in ('parcelle', 'proprietaire'):
            QgsMessageLog.logMessage(
                "Cadastre - Parameter TYPE must be parcelle or proprietaire")
            body = {
                'status': 'fail',
                'message': 'Parameter TYPE must be parcelle or proprietaire'
            }
            self.setJsonResponse('200', body)
            return

        # Open project
        self.projectPath = pmap
        pfile = QFileInfo(pmap)
        p = QgsProject.instance()
        p.read(pfile)

        # Find layer
        lr = QgsMapLayerRegistry.instance()
        layerList = [
            layer for lname, layer in lr.mapLayers().items()
            if layer.name() == player
        ]
        if len(layerList) != 1:
            QgsMessageLog.logMessage("Cadastre - layer %s not in project" %
                                     player)
            body = {
                'status': 'fail',
                'message': 'The source layer cannot be found in the project'
            }
            self.setJsonResponse('200', body)
            return
        layer = layerList[0]

        # Get feature
        import re
        pattern = re.compile("^([A-Z0-9]+)*$")
        if not pattern.match(pparcelle):
            QgsMessageLog.logMessage(
                "Cadastre - PARCELLE has not the correct format")
            body = {
                'status': 'fail',
                'message': 'PARCELLE has not the correct format'
            }
            self.setJsonResponse('200', body)
            return

        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - layer = %s  - geo_parcelle = %s" %
                (layer.name(), pparcelle))

        req = QgsFeatureRequest()
        req.setFilterExpression(' "geo_parcelle" = \'%s\' ' % pparcelle)

        it = layer.getFeatures(req)
        feat = None
        for f in it:
            feat = f
            break
        if not feat:
            QgsMessageLog.logMessage(
                "CADASTRE - No feature found for layer %s and parcelle %s" %
                (player, pparcelle))
            body = {
                'status':
                'fail',
                'message':
                'No feature found for layer %s and parcelle %s' %
                (player, pparcelle)
            }
            self.setJsonResponse('200', body)
            return

        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - feature geo_parcelle = %s" %
                feat['geo_parcelle'])

        # Get layer connection parameters
        self.connectionParams = cadastre_common.getConnectionParameterFromDbLayer(
            layer)
        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - connection params = %s" %
                self.connectionParams)

        self.connector = cadastre_common.getConnectorFromUri(
            self.connectionParams)
        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - after getting connector")

        # Get compte communal
        comptecommunal = cadastre_common.getCompteCommunalFromParcelleId(
            feat['geo_parcelle'], self.connectionParams, self.connector)
        pmulti = 1
        if ptype == 'proprietaire' and pmulti == 1:
            comptecommunal = cadastre_common.getProprietaireComptesCommunaux(
                comptecommunal, self.connectionParams, self.connector)
        if self.debugMode:
            QgsMessageLog.logMessage("cadastre debug - comptecommunal = %s" %
                                     comptecommunal)

        # Export PDF
        qex = cadastreExport(layer, ptype, comptecommunal,
                             feat['geo_parcelle'])
        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - after instanciating cadastreExport")

        paths = qex.exportAsPDF()
        if self.debugMode:
            QgsMessageLog.logMessage(
                "cadastre debug - after exportAsPDF(), path: %s" % paths)

        if paths:
            tokens = []
            for path in paths:
                uid = uuid4()

                if self.debugMode:
                    QgsMessageLog.logMessage("cadastre debug - item path: %s" %
                                             path)
                newpath = os.path.join(
                    # '/srv/qgis/temp/',
                    tempfile.gettempdir(),
                    '%s.pdf' % uid)
                if self.debugMode:
                    QgsMessageLog.logMessage(
                        "cadastre debug - item newpath: %s" % newpath)

                # shutil.copy(path,newpath)
                os.rename(path, newpath)
                tokens.append(str(uid))

            body = {
                'status': 'success',
                'message': 'PDF generated',
                'data': {
                    'url': {
                        'request': 'getPdf',
                        'service': 'cadastre',
                        'token': None
                    },
                    'tokens': tokens
                }
            }

            self.setJsonResponse('200', body)
            return
        else:
            QgsMessageLog.logMessage(
                "CADASTRE - Error during export: no PDF output")
            body = {
                'status': 'fail',
                'message': 'An error occured while generating the PDF'
            }
            self.setJsonResponse('200', body)
            return
Example #5
0
)

pmulti = 1
if export_type == 'proprietaire' and pmulti == 1:
    comptecommunal = cadastre_common.getProprietaireComptesCommunaux(
        comptecommunal,
        connectionParams,
        connector
    )

# Export PDF
print(target_dir)
qex = cadastreExport(
    p,
    layer,
    export_type,
    comptecommunal,
    feat['geo_parcelle'],
    target_dir
)
paths = qex.exportAsPDF()
print(paths)

# Add path into file
with open(output_log, 'w') as f:
    for path in paths:
        f.write(path)

# Exit
QgsApplication.exitQgis()

Example #6
0
    def createPdf(self):
        '''
        Create a PDF from cadastre data
        '''
        # Load ressources based on passed params
        self.loadRessources()

        # Get compte communal
        comptecommunal = cadastre_common.getCompteCommunalFromParcelleId(
            self.geo_parcelle,
            self.connectionParams,
            self.connector
        )
        pmulti = 1
        if self.type == 'proprietaire' and pmulti == 1:
            comptecommunal = cadastre_common.getProprietaireComptesCommunaux(
                comptecommunal,
                self.connectionParams,
                self.connector
            )
        if self.debugMode:
            QgsMessageLog.logMessage( "cadastre debug - comptecommunal = %s" % comptecommunal )

        # Export PDF
        qex = cadastreExport(
            self.layer,
            self.type,
            comptecommunal,
            self.geo_parcelle
        )
        if self.debugMode:
            QgsMessageLog.logMessage( "cadastre debug - after instanciating cadastreExport" )

        paths = qex.exportAsPDF()
        if self.debugMode:
            QgsMessageLog.logMessage( "cadastre debug - after exportAsPDF(), path: %s" % paths )

        if paths:
            tokens = []
            for path in paths:
                uid = uuid4()

                if self.debugMode:
                    QgsMessageLog.logMessage( "cadastre debug - item path: %s" % path )
                newpath = os.path.join(
                    # '/srv/qgis/temp/',
                    tempfile.gettempdir(),
                    '%s.pdf' % uid
                )
                if self.debugMode:
                    QgsMessageLog.logMessage( "cadastre debug - item newpath: %s" % newpath )

                # shutil.copy(path,newpath)
                os.rename(path,newpath)
                tokens.append( str(uid) )

            body = {
                'status': 'success',
                'message': 'PDF generated',
                'data': {
                    'url': {
                        'request': 'getPdf',
                        'service': 'cadastre',
                        'token': None
                    },
                    'tokens': tokens
                }
            }

            self.setJsonResponse( '200', body)
            return
        else:
            QgsMessageLog.logMessage( "CADASTRE - Error during export: no PDF output")
            body = {
                'status': 'fail',
                'message': 'An error occured while generating the PDF'
            }
            self.setJsonResponse( '200', body)
            return