I'd start by building the array of values using just the rounded x, y, and complex value, not storing a QgsPointXY. Something like

triplets = []
for point in points:
    # point consists of a QgsPointXY and a complex value - [QgsPointXy, complex_value]
    triplets +=  [(round(point[0].x(), 3), round(point[0].y(), 3), point[1])]

#sort points by y value
triplets.sort(key = lambda point: point[1])
# sort points by x value. Sort is stable, so now we have sorted by x as primary, y as secondary

#walk the list of points looking for points with same rounded value, summing complex value, outputting a new point when x or y doesn't match
complex_sum = 0
last_x = triples[0][0]
last_y  = triples[0][1]
output = []
for t in triplets:
    if ((t[0] == last_x AND t[1] == last_y)):
        complex_sum += t[2]
    else:
        output += [(last_x, last_y, complex_sum)]
        last_x = t[0]
        last_y = t[1]
        complex_sum = t[2]


at the end of the loop the var output is a list of triplets (x, y, complex) with the third element equal to the sum of the the complex value for all points with the same rounded coordinates.
I haven't actually tested, this so there could be errors. But the idea is there.
There's probably a more pythonic way to do this, but possibly harder to read.


On 2/20/2022 7:07 AM, Asim al-sofi wrote:
Thank you for your reply
The issue I have is  that I have an array of values. each value consists of xy coordinates of a point and a complex value.
What I want is that the points that have the same xy coordinates to be added together(i.e,, their complex values need to be summed up)
Because of the high number of decimals of each point, sometimes a point like QgsPointXY(6500.11491000000023632 0), 0.25+0.25j] and a point like QgsPointXY(6500.11491000000023631 0), 0.25+0.25j] would be considered as two different points.
Thanks in advance
Asim

On Sun, Feb 20, 2022 at 5:21 AM David Strip <[email protected]> wrote:
On 2/19/2022 6:03 PM, Asim al-sofi wrote:
Hi everyone
I have a problem rounding off the QgsPointXY to say 3 decimals? How can I do that?
If I use the numpy.round(point,decimals) then I get an np.array back as a type and not a QgsPointXY.
Can someone help?
Kind regards
Asim

what are you trying to achieve? Keep in mind that in general decimal fractions do not have exact representations in floating point, so if you round a coordinate to 3 decimals, store it somewhere, then print it, there will almost certainly be more than three numbers past the decimal point. If it's the printed representation of the number that matters, deal with it in the formatting of the printed representation.

That said, you set() method of QgsPointXY to set the values to their rounded values. If my_pt is a QgsPointXY, then

    my_pt.set(round(my_pt.x(), 3), round(my_pt.y(), 3))



_______________________________________________
Qgis-user mailing list
[email protected]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to