DenisCarriere / geocoder

:earth_asia: Python Geocoder
http://geocoder.readthedocs.org
MIT License
1.63k stars 288 forks source link

Geocoding issue due to float->string scientific notation #349

Open hampsterx opened 6 years ago

hampsterx commented 6 years ago

Eg lat=51.1701954, lng=0.0000992

ends up as

https://maps.googleapis.com/maps/api/geocode/json?key=MY_KEY&sensor=false&latlng=51.1701954%2C+9.92e-05&language=

I have monkey patched temporarily with

class Location(DefaultLocation):

    @staticmethod
    def format_float(value, precision=10):
        # If value is actually a string already, return unchanged
        if isinstance(value, (str,)):
            return value

        # https://stackoverflow.com/questions/6416474/how-to-avoid-printing-scientific-notation-in-python-without-adding-extra-digits
        if precision < 0:
            f = "%f" % value
        else:
            f = "%.*f" % (precision, value)

        p = f.partition(".")

        s = "".join((p[0], p[1], p[2][0], p[2][1:].rstrip("0")))

        return s

    def __str__(self):
        if self.ok:
            return u'{0}, {1}'.format(self.format_float(self.lat), self.format_float(self.lng))
        return u''

def _location_init(self, location, **kwargs):
    return {
        'latlng': str(Location(location)),
        'sensor': 'false',
    }

GoogleReverse._location_init = _location_init

Would do a PR but that Location class is used by every reverse geocoder, should be ok tho. Happy to do PR if this seems like a sensible solution.

thomas-lab commented 6 years ago

Hey there!

Thanks for the input. It's quite strange that the unit tests didn't find this issue. Will look into that later. You should definitely do a PR :)