On Sun, Jul 13, 2008 at 11:20 PM, Christopher Barker
<[EMAIL PROTECTED]> wrote:
> Eric Lemoine wrote:
>> If the encoder converts to str/unicode, then we'll end up with strings
>> in the GeoJSON.
>
> Isn't JSON all text anyway?
I just meant that {"pi": "3.14"} is different than {"pi": 3.14}. So if
my converter converts Decimals into strings then I'll end up with
{"pi": "3.14"}, which I don't want.
> Indeed, the reason that a decimal.Decimal
> can not be automatically generated from a float is that it's unclear
> which decimal value you'd want:
>
> >> f = 0.1
> >> repr(f)
> '0.10000000000000001'
>
> that's the as-exact-as-it-needs-to-be-to-assure-the-same
> binary-value-in-memory answer:
>
> >> str(f)
> '0.1'
>
> that's the rounded-to-a-reasonable-amount value.
>
> In this case, it's pretty obvious, but remember that python is designed
> around the philosophy of "refusing the temptation to guess"
>
>> But I don't know whether the decimal to float conversion is lossless?
>> Definitely something I need to consider...
>
> No, it's not. There are decimal numbers that can not be exactly
> represented as floating point, and the decimal module allows you to set
> precision well higher than floats.
>
> That being said, for practical purposes, with geo-coordinates, I doubt
> there will be issues.
>
>>> Assuming it can be arranged such that decimals in input GeoJOSN get
>>> encoded correctly with geojson.dump(s),
>
> As the code must be going from floats to strings anyway, having a method
> to go from Decimal to string makes sense.
>
> How is geojson.dumps() doing the float->string conversion now?
It's simplejson's job. And here's what simplejson does (current trunk):
# Assume this produces an infinity on all machines (probably not guaranteed)
INFINITY = float('1e66666')
FLOAT_REPR = repr
def floatstr(o, allow_nan=True):
# Check for specials. Note that this type of test is processor- and/or
# platform-specific, so do tests which don't depend on the internals.
if o != o:
text = 'NaN'
elif o == INFINITY:
text = 'Infinity'
elif o == -INFINITY:
text = '-Infinity'
else:
return FLOAT_REPR(o)
if not allow_nan:
raise ValueError("Out of range float values are not JSON compliant: %r"
% (o,))
return text
> The
> string formating operations should work the same way with Decimals as
> floats.
What do you mean? What your decimalstr func would look like?
Thanks a lot Christopher,
--
Eric
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community