Opened 14 years ago

Closed 14 years ago

#341 closed enhancement (fixed)

WKTWriter option to reduce the number of decimals

Reported by: atrofast Owned by: warmerdam
Priority: major Milestone: 3.3.0
Component: C API Version: main
Severity: Feature Request Keywords:
Cc: mloskot, pramsey

Description

Currently the WKTWriter will often format using more zero's than necessary even though the double value might just be 1.0. It would be great to have an option in the C-API where we could change the actual formatter used and reduce the number of zeros/decimals. I would be happy to take this project on, with the hope that it will be included upstream in GEOS.

Attachments (1)

trim_round.diff (6.9 KB ) - added by atrofast 14 years ago.
Adds trim and rounding precision options to WKTWriter and exposes them in the C-API

Download all attachments as: .zip

Change History (7)

comment:1 by mloskot, 14 years ago

Cc: mloskot added

It is a good idea. I would suggest to update not only the C API but also the C++.

comment:2 by atrofast, 14 years ago

Tinkering with this I've been thinking about two approaches.

The first would be to just chop off zeroes and round superfluous nines which are there because of floating point inaccuracy reasons. If there are no more decimals left (as in the case of 1.0000000) the end number would be just 1. There are cases when due to the peculiarities of the double type the number might be something like 1.000000000000001 when in fact it is not, then we could chop off the last 1 and go on with trimming the rest of the zeroes.

Another approach I thought of was to let the user specify the precision of the sprintf function in WKTWriter::writeNumber so it'd use the specified precision instead of 16.

Which of these approaches do you feel is the most appropriate (or maybe both should be available)

Thank you for your input.

by atrofast, 14 years ago

Attachment: trim_round.diff added

Adds trim and rounding precision options to WKTWriter and exposes them in the C-API

comment:3 by mloskot, 14 years ago

Sounds fine to me. However, what about output precision derived from precision model in the second proposed solution? Precision of output is linked to precision of model being used, isn't it?

comment:4 by atrofast, 14 years ago

From what I can tell there are only three precision models: FLOATING (16 decimals), FLOATING_SINGLE (8 decimals) and FIXED (1 I think). Being able to set roundingPrecision gives you a bit more control over how it is displayed. I don't actually touch the underlying numbers, just the strings. The diff I posted overrides the precision model IF you have specified a rounding precision. Does this cause any issue that I might be unaware of?

Thank you for your input!

comment:5 by warmerdam, 14 years ago

Cc: pramsey added
Owner: changed from pramsey to warmerdam

I'm going to do a quick review and apply this as there doesn't seem to be any issues I'm aware of and it would be helpful to get this into trunk sooner rather than later.

comment:6 by warmerdam, 14 years ago

Resolution: fixed
Status: newclosed

The code seems fine. I have also added some very minimal unit testing of this functionality and committed in trunk (r2993).

Note: See TracTickets for help on using tickets.