Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hampsterx opened this issue May 7, 2018 · 1 comment
Open

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

hampsterx opened this issue May 7, 2018 · 1 comment

Comments

@hampsterx
Copy link

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
Copy link
Collaborator

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants