Python 在 Django 中使用哪个模型字段来存储经度和纬度值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30706799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which Model Field to use in Django to store longitude and latitude values?
提问by Prometheus
I want to store my users location using longitude and latitude, at the moment this comes from Google Maps, but I will be using GeoDango and some point to work out distances between to points also.
我想使用经度和纬度存储我的用户位置,目前这来自谷歌地图,但我将使用 GeoDango 和一些点来计算点之间的距离。
However, my first confusion is which field in Django I should be using to store the longitude and latitude values? The information I'm getting is conflicting.
但是,我的第一个困惑是我应该使用 Django 中的哪个字段来存储经度和纬度值?我得到的信息是相互矛盾的。
The official documentation uses a FloatField
https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/#geographic-models
官方文档使用了FloatField
https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/#geographic-models
lon = models.FloatField()
lat = models.FloatField()
Where almost every answer on stackoverflow shows a DecimalField
stackoverflow 上的几乎每个答案都显示 DecimalField
long = models.DecimalField(max_digits=8, decimal_places=3)
lat = models.DecimalField(max_digits=8, decimal_places=3)
So what should I be using?
那我应该用什么?
采纳答案by mccc
Float is generally an approximation, see herefor some simple examples. You could get very nice results modifying your model to something like DecimalField(max_digits=9, decimal_places=6)
, since decimals are very important in coordinates but using more than 6 is basically meaningless.
Float 通常是一个近似值,请参阅此处以获取一些简单示例。将模型修改为类似DecimalField(max_digits=9, decimal_places=6)
,您可以获得非常好的结果,因为小数在坐标中非常重要,但使用超过 6 个基本上没有意义。
回答by Amulya Acharya
Use PointField to store lat long
使用 PointField 存储经纬度
p = Point(85.3240, 27.7172,srid=4326)
Django: 1.X:
姜戈:1.X:
https://docs.djangoproject.com/en/1.11/ref/contrib/gis/model-api/#pointfield
https://docs.djangoproject.com/en/1.11/ref/contrib/gis/model-api/#pointfield
Django: 2.X:
姜戈:2.X:
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/model-api/#pointfield
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/model-api/#pointfield
Django: 3.X: https://docs.djangoproject.com/en/3.0/ref/contrib/gis/model-api/#pointfield
Django: 3.X: https://docs.djangoproject.com/en/3.0/ref/contrib/gis/model-api/#pointfield
回答by shacker
The suggestion here to use DecimalField(max_digits=9, decimal_places=6)
resulted in errors for me when trying to save locations from Google Maps.
DecimalField(max_digits=9, decimal_places=6)
尝试从 Google 地图保存位置时,此处使用的建议导致我出错。
If we assume that the most common use case is retrieving point locations from the Maps API, and a typical URL from Google Maps when right-clicking and selecting "What's Here?" yields something like:
如果我们假设最常见的用例是从 Maps API 检索点位置,并在右键单击并选择“这里有什么?”时从 Google Maps 检索典型 URL。产生类似的东西:
https://www.google.com/maps/place/37°48'52.3"N+122°17'09.1"W/@37.814532,-122.2880467,17z
https://www.google.com/maps/place/37°48'52.3"N+122°17'09.1"W/@37.814532,-122.2880467,17z
(random place)
(随机地点)
So the longitude has 15 places before and after the decimal, which is why the accepted answer doesn't work. Since there's no reason to validate for "as short as possible" and db storage is cheap, I'm using:
所以经度在小数点前后各有 15 位,这就是接受的答案不起作用的原因。由于没有理由验证“尽可能短”并且数据库存储很便宜,我正在使用:
max_digits=22,
decimal_places=16)