I've been using PyQGIS for a few of weeks. I'd write a script which shows all earthshakes responding to given parameters, so I wrote this code:
from PyQt5.QtGui import *
from PyQt5.QtCore import QVariant
import processing
layerTerremoti = QgsVectorLayer("LayerGIS/totaleepicentriterremotiinunareadi200kmdatoscanaliguria.gpkg", "Terremoti", "ogr")
terrSelezionati = QgsVectorLayer("LayerGIS/terrSelezionati.shp")
with edit(terrSelezionati):
for feat in terrSelezionati.getFeatures():
terrSelezionati.deleteFeature(feat.id())
layer = qgis.utils.iface.activeLayer()
widgetGetBuff = iface.messageBar().createMessage("Buffer Layer", "Show Me")
widgetGetIntervalloInizio = iface.messageBar().createMessage("IntervalloInizio", "Show me")
widgetGetIntervalloFine = iface.messageBar().createMessage("IntervalloFine", "Show me")
elementi_selezionati = layer.selectedFeatures()
buffDist, ok = QInputDialog().getInt(widgetGetBuff, "Inserimento parametro buffer", "inserisci distanza buffer in Km", 0, 10, 200, QLineEdit.Normal)
if ok and buffDist:
rangeInizio, ok = QInputDialog().getInt(widgetGetIntervalloInizio, "Inserimento
parametro intervallo", "Inserisci prima data del range temporale", 1000, 1000, 2010,
QLineEdit.Normal)
rangeFine, ok = QInputDialog().getInt(widgetGetIntervalloFine, "Inserimento parametro
intervallo", "Inserisci seconda data del range temporale", 1001, 1100, 2020,
QLineEdit.Normal)
aRispettoLayer = QgsVectorLayer('Polygon?crs=EPSG:3003', 'areaRispetto', 'memory')
vpr = terrSelezionati.dataProvider()
pr = aRispettoLayer.dataProvider()
count = 0
for i in elementi_selezionati:
buff = i.geometry().buffer(buffDist*1000,18)
b = QgsFeature()
b.setGeometry(buff)
pr.addFeatures([b])
for ftre in layerTerremoti.getFeatures():
if (ftre['Anno'] >= rangeInizio) and (ftre['Anno'] <= rangeFine):
count = count +1
QgsFeature(ftre)
vpr.addFeatures([ftre])
terrSelezionati.updateExtents()
outres = processing.run("gdal:clipvectorbypolygon", {
'INPUT': terrSelezionati,
'MASK' : aRispettoLayer,
'OUTPUT': 'LayerGIS/terremotiRitag.shp'
})
terremotiRitagliati =
QgsVectorLayer('LayerGIS/terremotiRitag.shp',"TerrRitagliati","ogr")
tr = terremotiRitagliati.dataProvider()
tr.addAttributes([QgsField("Anno", QVariant.Int)])
terremotiRitagliati.updateFields()
indCampo = terremotiRitagliati.fields().indexOf("Anno")
conto = 0
confronto = 0
for f in terremotiRitagliati.getFeatures():
conto = conto + 1
tr.addFeatures([f])
terremotiRitagliati.updateFeature(f)
print("conto")
print(conto)
for el in layerTerremoti.getFeatures():
confronto = el['N15']
terremotiRitagliati.startEditing()
for g in terremotiRitagliati.getFeatures():
if confronto == g['id']:
terremotiRitagliati.changeAttributeValue(g.id(), indCampo , el['Anno'])
terremotiRitagliati.updateFeature(g)
print(g["Anno"])
terremotiRitagliati.commitChanges()
countMsg = QMessageBox()
if conto> 0:
countMsg.setText("Sono stati trovati " + str(conto) + " terremoti tra il " +
str(rangeInizio) + "e il " + str(rangeFine))
else:
countMsg.setText("Non sono stati trovati terremoti avvenuti tra il " +
str(rangeInizio) + " e il " + str(rangeFine))
countMsg.show()
QgsProject.instance().addMapLayers([terremotiRitagliati])
QgsProject.instance().addMapLayers([aRispettoLayer])
iface.mapCanvas().zoomToSelected()
The last code rows are salient. The problem is attribute "Anno" doesn't update, so the attributes table of the layer "terremotiRitagliati" shows all values of column "Anno" as NULL. Why happens that?
.shp
and.gpkg
? Otherwise, check the spacing, I do not like that everything is outside thewith edit(terrSelezionati):