b1_
b1_

Reputation: 2126

Django: How do i store a geo point in database

Needed correct datatype for geo points.

I will get and display it with google map API so format like

Usecase:

So, no distances beetween points and etc hacks.

DB: postgres 8

Django: 1.4

Upvotes: 11

Views: 11482

Answers (5)

Sarath Ak
Sarath Ak

Reputation: 8649

I'm using this in model

latitude = models.DecimalField(max_digits=11, decimal_places=7,null=True,blank=True)
longitude = models.DecimalField(max_digits=11, decimal_places=7,null=True,blank=True)

Upvotes: 0

andilabs
andilabs

Reputation: 23281

From django documentation about DecimalField:

DecimalField.max_digits

The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places.

DecimalField.decimal_places

The number of decimal places to store with the number.

which is refering to the Python Decimal

To make good choice about accurate data type and precission you should consider:

  1. what is minimum possible value (latitude can be from 0 (down)up to (-)90 degrees) _ _.
  2. what is maximum possible value (longitude can range from 0 (down)up to (-)180 degrees) _ _ _.
  3. what is accuracy (decimal_places), you wish. Pleas notice that it has impact on zoom level on Google Maps.

By the way, for better understanding, it is good to know how the calculation is done (Python code):

def deg_to_dms(deg):
    d = int(deg)
    md = abs(deg - d) * 60
    m = int(md)
    sd = (md - m) * 60
    return [d, m, sd]

def decimal(deg,min,sec): 
    if deg < 0:
       dec= -1.0 * deg + 1.0 * min/60.0 + 1.0 * sec/3600.0
       return -1.0 * dec
    else:
        dec=1.0 * deg + 1.0 * min/60.0 + 1.0 * sec/3600.0;

    return dec

Upvotes: 3

Mevin Babu
Mevin Babu

Reputation: 2475

Check out GeoDjango and see if it helps you. If your stack is configured to run GeoDjango, you can do this.

Your models will looks like this

from django.contrib.gis.db import models
class LocationPoint(models.Model):
    point = models.PointField(srid=4326,dim=3)
    accuracy = models.FloatField(default=0.0)
    objects = models.GeoManager()

To save the point to the database all you will have to do is

from django.contrib.gis.geos import Point
point = Point(x=12.734534, y=77.2342, z=0, srid=4326)

location = LocationPoint()
location.point = point
location.save()

GeoDjango gives you extended abilities to do geospatial queries which you might be interested in the near future, like finding the distance between points, finding the nearest locations around a point etc.

Here's the link to GeoDjango

Upvotes: 11

Christopher H
Christopher H

Reputation: 2194

I use longitude and latitude in my django setup.

My model includes:

long_position   = models.DecimalField (max_digits=8, decimal_places=3)
lat_position   = models.DecimalField (max_digits=8, decimal_places=3)

For more precision you may want the decimal_places to be more.

When you want to display it in the Google Map API method you would reference your model and write a python code to output like this:

output = some_long_position + "," + some_lati_position

Upvotes: 1

girasquid
girasquid

Reputation: 15526

It looks like you're going to be storing a latitude and a longitude. I would go with a DecimalField for this, and store each number separately.

Upvotes: 3

Related Questions