0

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.

6
  • Try adding a specific type function around it?
    – Ian Turton
    Commented Apr 18, 2023 at 14:53
  • I tried that. "converting" it with 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 type Commented Apr 18, 2023 at 15:03
  • 2
    Just using float() did the job. Which is weird, because the pandas dtypte of the values is float64 as well Commented Apr 18, 2023 at 15:08
  • I'd expect storing an int in a float to fail -
    – Ian Turton
    Commented Apr 18, 2023 at 15:24
  • 2
    Pandas float64 and Python float are not the same, which is why casting one to the other works.
    – Matt
    Commented Apr 18, 2023 at 20:09

1 Answer 1

0

Casting the pandas float to a standard python float was the solution

new_feat.setAttribute('X_int',float(new["X_int"][counter]))

Even though their dtype is the same, they are not.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.