[Qgis-user] Rounding off QgsPointXY
Ujaval Gandhi
ujaval at spatialthoughts.com
Sun Feb 20 20:42:17 PST 2022
Another approach would be to use 'Snap points to Grid' algorithm. This
allows you to snap the points to a regular grid. Choose 0.01 as the grid
spacing and you'll get points rounded off to 2 decimal points (As David
mentioned, floating-point precision means the printed values won't be exact
2 decimals). Sample PyQGIS code below
layer = iface.activeLayer()
output = processing.run("native:snappointstogrid",
{'INPUT':layer,
'HSPACING':0.01,
'VSPACING':0.01,
'OUTPUT': 'TEMPORARY_OUTPUT'})
for f in output['OUTPUT'].getFeatures():
geom = f.geometry().asPoint()
print(geom)
[image: Logo] <https://spatialthoughts.com/>
Ujaval Gandhi
Spatial Thoughts
mobile: +91-8095684687
email: ujaval at spatialthoughts.com
[image: LinkedIn icon] <https://www.linkedin.com/in/spatialthoughts/> [image:
Twitter icon] <https://twitter.com/spatialthoughts>
On Sun, Feb 20, 2022 at 11:03 PM David Strip <qgis-user at stripfamily.net>
wrote:
> 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.1149100000002363*2* 0), 0.25+0.25j] and a point like
> QgsPointXY(6500.1149100000002363*1* 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 <qgis-user at stripfamily.net>
> 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
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220221/eae3379f/attachment.html>
More information about the Qgis-user
mailing list