Python Forum
Decoding lat/long in file name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decoding lat/long in file name
#4
This may be more "pythonic". Added some comments now that I understand what is going on.
def decodejs(sc):
    codes = "-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~"
    x = y = z = i = 0
    for ch in sc:
        # Convert asci letter to int using codes.  '-' signals early end of bytes
        if (digit := codes.index(ch)-1) < 0:
            break

        # Each digit contains 3 bits of latitude (y) and 3 bits of longitude (x)
        # interleaved like this y2x2y1x1y0x0.  Extract y2y1y0 and x2x1x0
        # and add to the latitude and longitude.
        x <<= 3
        y <<= 3
        for xbit, ybit, bit in zip((2, 8, 32), (1, 4, 16), (1, 2, 4)):
            x |= bit if digit & xbit else 0
            y |= bit if digit & ybit else 0
        z += 3
        i += 1

    # This scales latitude (x) from 0...0xFFFFFF to -180...180 and
    # the latitude (y) from from 0...0xFFFFFF to -90...90.
    x = x * 2**(2 - 3 * i) * 90 - 180
    y = y * 2**(2 - 3 * i) * 45 - 90

    if i < len(sc) and sc[i] == "-":
        z -= 2
        if i + 1 < len(sc) and sc[i + 1] == "-":
            z += 1

    return z-8, y, x


print(decodejs('0GAjIv8h'))
johnmcd likes this post
Reply


Messages In This Thread
Decoding lat/long in file name - by johnmcd - Mar-21-2024, 03:29 PM
RE: Decoding lat/long in file name - by deanhystad - Mar-21-2024, 07:33 PM
RE: Decoding lat/long in file name - by johnmcd - Mar-21-2024, 10:31 PM
RE: Decoding lat/long in file name - by deanhystad - Mar-22-2024, 03:02 AM
RE: Decoding lat/long in file name - by johnmcd - Mar-22-2024, 11:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Enigma Decoding Problem krisarmstrong 4 900 Dec-14-2023, 10:42 AM
Last Post: Larz60+
  json decoding error deneme2 10 4,081 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  flask app decoding problem mesbah 0 2,441 Aug-01-2021, 08:32 PM
Last Post: mesbah
  Decoding a serial stream AKGentile1963 7 8,992 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  xml decoding failure(bs4) roughstroke 1 2,343 May-09-2020, 04:37 PM
Last Post: snippsat
  python3 decoding problem but python2 OK mesbah 0 1,860 Nov-30-2019, 04:42 PM
Last Post: mesbah
  utf-8 decoding failed every time i try adnanahsan 21 11,263 Aug-27-2019, 04:25 PM
Last Post: adnanahsan
  hex decoding in Python 3 rdirksen 2 4,714 May-12-2019, 11:49 AM
Last Post: rdirksen
  Decoding log files in binary using an XML file. captainfantastic 1 2,508 Apr-04-2019, 02:24 AM
Last Post: captainfantastic
  decoding sub.process output with multiple \n? searching1 2 2,887 Feb-24-2019, 12:00 AM
Last Post: searching1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020