I am trying to set a feature in a PyQGIS processing script
Its fields are defined like so:
interp_fields=QgsFields()
interp_fields.append(QgsField('X_int', type=QVariant.Double))
interp_fields.append(QgsField('Y_int', type=QVariant.Double))
interp_fields.append(QgsField('Z_int', type=QVariant.Double))
The sink I am pushing the feature into looks like this:
(interp_sink, dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT_INTERP,
context,
interp_fields,
source.wkbType(),
source.sourceCrs()
My value I am trying to set is from a pandas dataframe "new" and I am iterating over an array that has the same count of rows
for counter, interp_z in enumerate(interp_sampled_values):
#just to see if we actually got a value
feedback.pushInfo(str(new["X_int"][counter]))
if feedback.isCanceled():
break
#make new feature
new_feat = QgsFeature()
#set its fields
new_feat.setFields(interp_fields)
#create geometry from point
interp_geometry=QgsGeometry.fromPointXY(QgsPointXY(new["X_int"][counter],new["Y_int"][counter]))
new_feat.setGeometry(interp_geometry)
# Add a feature in the sink
new_feat.setAttribute('X_int',new["X_int"][counter])
#feedback.pushInfo(new_feat.attributes)
interp_sink.addFeature(new_feat, QgsFeatureSink.FastInsert)
I KNOW that new["X_int"][counter]
is delivering a float64 value. Geometry is just fine.
I am getting the error message
Could not store attribute "X_int": Could not convert value "" to target type "double"
I know however, that it should work formally, as I can give it a constant like
new_feat.setAttribute('X_int',1)
And it works just fine.
What am I doing wrong?
It seems to be some type conversion issue.
new_feat.setAttribute('X_int',int(new["X_int"][counter]))
works. But there I am losing precision. I need double. However, it doesn't accept floats for the double typeint
in afloat
to fail -float64
and Pythonfloat
are not the same, which is why casting one to the other works.