jazzband / geojson

Python bindings and utilities for GeoJSON
https://pypi.python.org/pypi/geojson/
BSD 3-Clause "New" or "Revised" License
898 stars 120 forks source link

How to disable rounding decimal coordinates #202

Open NoharaMasato opened 1 year ago

NoharaMasato commented 1 year ago

Issues

By default DEFAULT_PRECISION = 6 and instantiating Point class rounds decimal number like below.

>>> import geojson
>>> geojson.Point((1e-9, 0))
{"coordinates": [0.0, 0], "type": "Point"}

Proposal

I think decimal numbers should be kept as it is by default , and we can optionally specify precision. I wonder which kind of use cases people want to round decimal numbers when they use geojson library.

trcmohitmandokhot commented 10 months ago

I've been thinking about this question, which is a good one. I think there are a couple of parts to your question, so I am attempting to break them down the best I can. Please clarify as needed:
1) When you create a co-ordinate with a value such as Point((1e-9,0)) the 1e-9 is a "Real" number, stored as an object of type "float". Python's native round() function applies on that object and provides a representation of that fraction as best as it can following the rules and limitations of Floating Point Arithmetic. 2) The geojson instance functions offer a way to change the precision of round function used in python. This will allow you to preserve the decimal coordinates you need. Example...

>>> p = geojson.Point((1e-9,0),precision=9)  
{"coordinates": [1e-09, 0], "type": "Point"}

3) With regards to why DEFAULT_PRECISION = 6, I believe this has to do with interoperability considerations in the GeoJSON RFC 7946 Sec 11.2. A precision of 6 lies offers a representation of ~10cm in Lat/Lon co-ordinates. This is deemed good enough for major surveying activities, and adequate precision to exchange information in. A very good article was posted on GIS Stackexchange about the impact of precision in GPS coordinates, which someone who is interested can read here