Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Model Field to use in Django to store longitude and latitude values?

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.

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.

The official documentation uses a 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

long = models.DecimalField(max_digits=8, decimal_places=3) lat = models.DecimalField(max_digits=8, decimal_places=3) 

So what should I be using?

like image 557
Prometheus Avatar asked Jun 08 '15 10:06

Prometheus


People also ask

How do you plot latitude and longitude in Python?

fig = plt. figure(figsize=(8, 8)) m = Basemap(projection='lcc', resolution=None, width=8E6, height=8E6, lat_0=45, lon_0=-100,) m. etopo(scale=0.5, alpha=0.5) # Map (long, lat) to (x, y) for plotting x, y = m(-122.3, 47.6) plt. plot(x, y, 'ok', markersize=5) plt.

What are the field type in Django?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField. Similarly, for other fields.


1 Answers

Float is generally an approximation, see here for 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.

like image 116
mccc Avatar answered Sep 21 '22 14:09

mccc