"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 : se familiariser avec l'API au travers de la console Python de QGIS
Objectif : découvrir les instructions pour extraire la représentation WKT des géométries d'une couche
Notez bien : certaines insctructions ci-dessous sont spécifiques à la couche ci-jointe : tutopoi.zip
print('QGIS speaking!')
import sys print(sys.version)
print('Projet pathname: ' + QgsProject.instance().fileName()) # pour un projet ouvert et sauvegardé
# pour une couche chargée et sélectionnée dans la table des couches layer = iface.activeLayer() print('Layer name: ' + layer.name()) print('Feature count: ' + str(layer.featureCount()))
layer = iface.activeLayer(); for feature in layer.getFeatures(): print(feature.id())
layer = iface.activeLayer() ft0 = layer.getFeature(0) # renvoie l'entité d'ID 0 print(ft0.fields().names()) # affiche les noms de attributs de l'entité print(ft0.attribute('NAME')) # affiche la valeur de l'attribut NAME de l'entité
layer = iface.activeLayer() ft0 = layer.getFeature(0) print(ft0.geometry().asWkt(7)) # 7 pour la précision décimale
Documentation : https://docs.python.org/3.6/tutorial/inputoutput.html#reading-and-writing-files
output = open('/home/olivier/tmp/wkt.txt','w')
output.write('Here is one WKT formatted feature geometry:') output.write('\n') layer = iface.activeLayer() ft0 = layer.getFeature(0) output.write(ft0.geometry().asWkt(7))
output.close()
Assemblez les diverses étapes précédentes pour obtenir un fichier qui liste au format WKT toutes les géométries de la couche sélectionnée dans la table des couches. On propose deux phases