2

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.

Community
  • 1
  • 1
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134

5 Answers5

3

I'm assuming you need 6 decimal places. So, float would hold your data. Or decimal with precision set to 6?

AGS
  • 401
  • 5
  • 17
3

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)
Netwave
  • 40,134
  • 6
  • 50
  • 93
1

you can define FLOAT(N,D) where default is (10,2) where 2 is the number of decimals and 10 is the total number of digits including decimals, decimal precisions can go up to 24 places for float and 53 places for DOUBLE(N,D).

arun.rajput
  • 127
  • 2
  • 3
0

You've asked to store latitude and longitude and jugdging by the amount of data you are going to store, I would go for a list of tuples of floats:

data = [
    (48.82694828196076,2.367038433387592),
    (48.82694828196076,2.367038433387592)
]

The tuple can - of course - be replaced by a list at any point. Especially since it only stores 2 floats, the access to it is not that costly, you won't really notice the difference at all. This would then yield to a nested list:

data = [
    [48.82694828196076,2.367038433387592],
    [48.82694828196076,2.367038433387592]
]
jhoepken
  • 1,842
  • 3
  • 17
  • 24
0

I guess it's enough to use decimal field here with (18,15), where 18 is total length and 15 is the decimal count of a lat/lng value. 15 + 3 = 18.

lat = models.DecimalField(max_digits=18, decimal_places=15, default=None, null=True, blank=True)
lon = models.DecimalField(max_digits=18, decimal_places=15, default=None, null=True, blank=True)
tolga
  • 2,462
  • 4
  • 31
  • 57