Last active
March 23, 2023 14:35
-
-
Save thbaumann/234ada8deda059b4dd88d91e829ebb3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spaltenname = 'name' | |
anzahl_spaltenzeichen = 8 # wieviele zeichen soll fuer die spaltenueberschrift verwendet werden. bsp 8 | |
datentyp = QVariant.Double # datentyp der zu erzeugenden Spalten | |
vectorLayer = iface.activeLayer() # aktiver Layer | |
idx = vectorLayer.fields().indexOf(spaltenname) | |
values = vectorLayer.uniqueValues(idx) | |
#print(values) | |
provider = vectorLayer.dataProvider() | |
# Abfrage, ob Spalten wirklich angelegt werden sollen | |
reply = QMessageBox.question(iface.mainWindow(), 'Continue?', | |
'Sollen diese Felder angelegt werden? : \n'+'\n'.join(values), QMessageBox.Yes, QMessageBox.No) | |
if reply == QMessageBox.Yes: | |
#wenn user ja klickt | |
for fieldname in values: | |
field = QgsField(fieldname[0:anzahl_spaltenzeichen], datentyp) | |
provider.addAttributes([field]) | |
vectorLayer.updateFields() | |
request = QgsFeatureRequest() | |
with edit(vectorLayer): | |
for feature in vectorLayer.getFeatures(request): | |
relevantes_feld= feature[spaltenname] #aktueller inhalt der spalte mit eindeutigen werten bsp gruenflaeche | |
feature[relevantes_feld[0:anzahl_spaltenzeichen]] =feature.geometry().area() # bsp: spalte gruenflaeche fuellen mit ermittelter flaeche | |
vectorLayer.updateFeature(feature) | |
else: | |
#wenn user nein klickt | |
print("dann nicht") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment