Search code examples
pythontypeslatitude-longitude

What is the ideal data type to store latitude and longitude values in Python?


What is the ideal data type to store latitude and longitude values in Python? Take a stops.txt in GTFS as an example,

stop_lat,stop_lon
48.82694828196076,2.367038433387592
48.82694828196076,2.367038433387592
48.829830695271106,2.376120009344837
48.829830695271106,2.376120009344837
48.83331371557845,2.387299704383512
48.83331371557845,2.387299704383512
48.840542782965464,2.3794094631230687
48.840542782965464,2.3794094631230687
48.844652150982945,2.37310814754528

Corresponding to What is the ideal data type to use when storing latitude / longitudes in a MySQL database? and the highly upvoted answer is:

Use MySQL's spatial extensions with GIS.


Solution

  • I think namedtuple is the way to go, easy accessing by name, and used like tuple(for the precision use float):

    In[4]: from collections import namedtuple
    In[5]: coords = namedtuple("Coords", ['x', 'y'])
    In[6]: coords
    Out[6]: __main__.Coords
    In[7]: coords(1,2)
    Out[7]: Coords(x=1, y=2)
    In[8]: coords(1.235445,2.2345345)
    Out[8]: Coords(x=1.235445, y=2.2345345)
    In[9]: coords(1.2354451241234214,2.234534512412414134)
    Out[9]: Coords(x=1.2354451241234214, y=2.2345345124124143)