"The explosive growth of the GeoWeb and geographic information has made GIS powerful media for the general public to communicate, but perhaps more importantly, GIS have also become media for constructive dialogs and interactions about social issues." - Sui & Goodchild
This is an old revision of the document!
Objectif : créer un plugin permettant d'ajouter des entités géographiques formalisées avec le format WKT dans la couche sélectionnée dans la table des couches.
Initialiser un dossier de configuration d'un nouveau plugin avec le Plugin Builder préalablement installé et selon le Chapitre 2.3.3 Marche à suivre – structure du plugin du tutoriel INSIT.
Dans le Chapitre 2.3.4 Marche à suivre – interface graphique du plugin du tutoriel INSIT est indiqué que l'UI a été initialisée par le builder dans le fichier avec l'extension .ui. Aussi que ce fichier peut être modifié avec les outils Qt (QtDesigner ou QtCreator) pour ajouter des éléments d'interface.
<widget class="QPlainTextEdit" name="plainTextEdit"> <property name="geometry"> <rect> <x>30</x> <y>50</y> <width>341</width> <height>181</height> </rect> </property> </widget>
D'après le chapitre 2.3.5 Marche à suivre – code Python du plugin du tutoriel INSIT, tout se passe dans la fonction run (tout en bas du script) :
print('Button OK of the plugin is successfully pushed!')
La méthode toPlainText renvoie le contenu du champ texte de la boîte de dialogue
mywkt = self.dlg.plainTextEdit.toPlainText() print('Content: ' + mywkt)
Supposant que ce contenu est une représentation WKT d'une géométrie, il est possible d'ajouter à la couche sélectionnée une entité géographique portant cette géométrie comme suit :
layer = iface.activeLayer() # load the current selected layer mywkt = self.dlg.plainTextEdit.toPlainText() # get the WKT content in the textfield layer.startEditing() # set the layer in edit mode geom = QgsGeometry() # create an empty geometry geom = QgsGeometry.fromWkt(mywkt) # parse the WKT content to initialize the geometry feat = QgsFeature() # create an empty feature feat.setGeometry(geom) # attach the geometry to the feature layer.dataProvider().addFeatures([feat]) # add the new feature to the layer layer.commitChanges() # save changes