script_create_cadastre.py

Babacar FASSA, 2019-03-22 06:03 PM

Download (6.6 KB)

 
1
from qgis.PyQt.QtCore import QCoreApplication
2
from qgis.core import (QgsProcessing,QgsFeatureSink,QgsProcessingException,QgsProcessingAlgorithm,#QgsRasterLayer,QgsMapLayerRegistry,QgsProcessingAlgorithm,
3
QgsProcessingParameterFeatureSource,QgsProcessingParameterFeatureSink,QgsFeatureRequest,QgsFeature,QgsProcessingParameterField,QgsProcessingParameterString,QgsRasterLayer,QgsProject,QgsProcessingParameterRasterDestination)
4
from qgis.utils import iface,QgsMessageBar
5
import processing
6

    
7

    
8
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
9

    
10
        #Declaration de nos shapes comme des variables text
11
        Vector_layer_of_communes = 'Vector_layer_of_communes'
12
        OUTPUT_raster = 'OUTPUT_raster'
13
        Commune_name='Commune_name'
14
        INSEE_code='INSEE_code'
15
        EPSG_code='EPSG_code'
16

    
17
        def tr(self, string):
18
                """
19
                Returns a translatable string with the self.tr() function.
20
                """
21
                return QCoreApplication.translate('Processing', string)
22

    
23
        def createInstance(self):
24
                return ExampleProcessingAlgorithm()
25

    
26
        def name(self):
27
                return 'Cadastre FR - WMS - Add a cadastral map'
28

    
29
        def displayName(self):
30
                return self.tr('Cadastre FR - WMS - Add a cadastral map')
31
                
32
        #Creation de group pour organiser laffichage des scripts
33
        def group(self):
34
                return self.tr(self.groupId())
35

    
36
        def groupId(self):
37
                return 'Circet'
38

    
39
        #Creation de description du script
40
        def shortHelpString(self):
41
                #Partie pour afficher la description du script
42
                return self.tr("Cadastre FR - WMS - Add a cadastral mapt")
43

    
44
        #Partie diniatilisation du script
45
        def initAlgorithm(self, config=None):
46
                
47
                #Initialisation de nos variables shapes dentree comme des shapes
48
                self.addParameter(
49
                        QgsProcessingParameterFeatureSource(
50
                                self.Vector_layer_of_communes,
51
                                self.tr('Input Vector_layer_of_communes'),
52
                                [QgsProcessing.TypeVectorAnyGeometry]
53
                        )
54
                )
55
        
56
        
57
        #Initialisation de nos variables shapes de sorti comme des shapes
58
        
59
                self.addParameter(QgsProcessingParameterRasterDestination(
60
                        self.OUTPUT_raster,
61
                        self.tr("OUTPUT_raster"),
62
                        None, False))
63
                
64
                
65
                #Initialisation de nos variables dattribut des shapes dentree
66
                self.addParameter(
67
        QgsProcessingParameterField(
68
            self.Commune_name,#"SpeciesAttribute",
69
            description=self.Commune_name,#"Species attribute",
70
            parentLayerParameterName=self.Vector_layer_of_communes,#"SpeciesRangePolygons",INSEE_code
71
            type=QgsProcessingParameterField.Any#String
72
        )
73
    )
74
        
75
        
76
                self.addParameter(
77
                        QgsProcessingParameterField(
78
                                self.INSEE_code,#"SpeciesAttribute",
79
                description=self.INSEE_code,#"Species attribute",
80
                parentLayerParameterName=self.Vector_layer_of_communes,#"SpeciesRangePolygons",INSEE_code
81
                                type=QgsProcessingParameterField.Any
82
                        )
83
                )
84
                
85
                self.addParameter(
86
                        QgsProcessingParameterString(
87
                                self.EPSG_code,#name="SpeciesAttribute",
88
                description='Code du projection, exemple: 2154'#self.EPSG_code
89
                                #self.EPSG_code,#description="Species attribute",
90
                                #self.Vector_layer_of_communes,#parentLayerParameterName="SpeciesRangePolygons",
91
                                #type=QgsProcessingParameterField.Any
92
                        )
93
                )
94

    
95
        #Partie dimplentation du code python qui fait le traitement
96
        def processAlgorithm(self, parameters, context, feedback):
97
        
98
                #Parametrage des shapes dentree
99
                shape_commune = self.parameterAsSource(parameters, self.Vector_layer_of_communes, context)
100
                #Parametrage du resultat en fichier de sorti
101
                output_path_raster = self.parameterAsOutputLayer(parameters, self.OUTPUT_raster, context)
102
                attribut_INSEE_code = self.parameterAsString(parameters, self.INSEE_code, context)
103
                attribut_Commune_name = self.parameterAsString(parameters, self.Commune_name, context)
104
                attribut_EPSG_code = self.parameterAsString(parameters, self.EPSG_code, context)
105
        
106
                def recuperation_cadastre_commune(layer,INSEE_code,Commune_name,EPSG_code):
107

    
108
                        if EPSG_code == '2154' or EPSG_code == '3942' or EPSG_code == '3943' or EPSG_code == '3944' or EPSG_code == '3945' or EPSG_code == '3946' or EPSG_code == '3947' or EPSG_code == '3948' or EPSG_code == '3949' or EPSG_code == '3950' or EPSG_code == '32630' or EPSG_code == ' 32631' or EPSG_code == '32632' or EPSG_code == '3857' or EPSG_code == '4326' or EPSG_code == '4258' or EPSG_code == '32620' or EPSG_code == '2970' or EPSG_code == '2972' or EPSG_code == '2973' or EPSG_code == '2975' or EPSG_code == '32622' or EPSG_code == '32740' or EPSG_code == '32738' or EPSG_code == '4471' or EPSG_code == '32621' :
109
                                #progress.setText(u'EPSG code : ' + EPSG_code)
110
                                tab = []
111
                                for f in layer.getFeatures():
112

    
113
                                        col_select =str(f[INSEE_code]),f[Commune_name]
114
                                        tab.append(col_select)
115

    
116
                                        #Permet la suppression des doublons
117
                                        Lt= list(set(tab))
118
                                        Lt.sort()
119

    
120
                                for c_insee, n_couche in Lt  :
121

    
122
                    #AMORCES_CAD,LIEUDIT,CP.CadastralParcel,SUBFISCAL,CLOTURE,DETAIL_TOPO,HYDRO,VOIE_COMMUNICATION,BU.Building,BORNE_REPERE
123
                                        urlWithParams = "url=http://inspire.cadastre.gouv.fr/scpc/"+c_insee+".wms?contextualWMSLegend=0&crs=EPSG:"+EPSG_code+"&dpiMode=7&featureCount=10&format=image/png&layers=AMORCES_CAD&layers=LIEUDIT&layers=CP.CadastralParcel&layers=SUBFISCAL&layers=CLOTURE&layers=DETAIL_TOPO&layers=HYDRO&layers=VOIE_COMMUNICATION&layers=BU.Building&layers=BORNE_REPERE&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&maxHeight=1024&maxWidth=1280"
124

    
125
                                        rlayer = QgsRasterLayer(urlWithParams, 'Cadastre_'+n_couche+'_'+c_insee, 'wms')
126
                                        #My problems is to insert this result to the raster created "output_path_raster"
127
                                        output_path_raster=rlayer
128

    
129
                                        #progress.setText(u'Commune name : ' + n_couche+' - '+c_insee)
130
                                        #progress.setText(u'Validity of WMS : %s' % rlayer.isValid())
131

    
132
                                        #QgsProject.instance().addMapLayer(rlayer)
133

    
134
                                        '''if  rlayer.isValid() == True :
135
                                                iface.messageBar().pushMessage("Information :", "Ajout du cadastre dans Qgis_map : "+n_couche, Qgis.Info, duration=5)
136
                                                iface.mapCanvas().refresh()
137

138
                                        else :
139
                                                iface.messageBar().pushMessage("Warning :", "WMS du Cadastre est invalide : "+n_couche, Qgis.Warning, duration=10)
140

141
                        else :
142
                                iface.messageBar().pushMessage("Warning :", "Projection Inconnue ", Qgis.Warning, duration=10)
143
                                progress.setText(u'EPSG is unknown ')'''
144
        
145
                #Fonction de calcul du besoin
146
                recuperation_cadastre_commune(shape_commune,attribut_INSEE_code,attribut_Commune_name,attribut_EPSG_code)
147
                results = {}
148
                results[self.OUTPUT_raster] = output_path_raster
149
                #return {self.OUTPUT_PT: dest_id_boite_sans_PT, self.OUTPUT_CABLE: dest_id_boite_sans_cables}
150
                return results#{}