在 Python 中存储纬度和经度值的理想数据类型是什么?
What is the ideal data type to store latitude and longitude values in Python?
在 Python 中存储纬度和经度值的理想数据类型是什么?以GTFS中的stops.txt
为例,
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
对应于What is the ideal data type to use when storing latitude / longitudes in a MySQL database?,高赞的答案是:
Use MySQL's spatial extensions with GIS.
我假设您需要 6 位小数。所以, float 会保存你的数据。或者精度设置为 6 的小数?
你要求存储纬度和经度,根据你要存储的数据量来判断,我会选择一个浮点元组列表:
data = [
(48.82694828196076,2.367038433387592),
(48.82694828196076,2.367038433387592)
]
当然,元组可以随时替换为列表。特别是因为它只存储 2 个浮点数,访问它的成本并不高,你根本不会真正注意到其中的区别。这将产生一个嵌套列表:
data = [
[48.82694828196076,2.367038433387592],
[48.82694828196076,2.367038433387592]
]
你可以定义FLOAT(N,D),默认是(10,2) 其中2是小数位数,10是包括小数在内的总位数,float的小数精度可以达到24位DOUBLE(N,D) 有 53 个位置。
我认为 namedtuple 是要走的路,通过名称轻松访问,并且像元组一样使用(为了精确使用 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)
我想在这里使用带有 (18,15) 的十进制字段就足够了,其中 18 是总长度,15 是 lat/lng 值的小数位数。 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)
在 Python 中存储纬度和经度值的理想数据类型是什么?以GTFS中的stops.txt
为例,
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
对应于What is the ideal data type to use when storing latitude / longitudes in a MySQL database?,高赞的答案是:
Use MySQL's spatial extensions with GIS.
我假设您需要 6 位小数。所以, float 会保存你的数据。或者精度设置为 6 的小数?
你要求存储纬度和经度,根据你要存储的数据量来判断,我会选择一个浮点元组列表:
data = [
(48.82694828196076,2.367038433387592),
(48.82694828196076,2.367038433387592)
]
当然,元组可以随时替换为列表。特别是因为它只存储 2 个浮点数,访问它的成本并不高,你根本不会真正注意到其中的区别。这将产生一个嵌套列表:
data = [
[48.82694828196076,2.367038433387592],
[48.82694828196076,2.367038433387592]
]
你可以定义FLOAT(N,D),默认是(10,2) 其中2是小数位数,10是包括小数在内的总位数,float的小数精度可以达到24位DOUBLE(N,D) 有 53 个位置。
我认为 namedtuple 是要走的路,通过名称轻松访问,并且像元组一样使用(为了精确使用 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)
我想在这里使用带有 (18,15) 的十进制字段就足够了,其中 18 是总长度,15 是 lat/lng 值的小数位数。 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)