Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign to a Django PointField model attribute?

Hi I have a Django Model as follows:

class Address(models.Model):
    geoCoords = models.PointField(null=True, blank=True,)

Now I create an instance of this model:

A = Address()

How can I set the coordinates of A's geoCoord field to (5.3, 6.2)? I can't find any example of where a point field is assigned to in this way. It's such a stupidly simple question. Actually, the coordinates I would like to assign to A.geoCord are from pygeocoder. It is a 2-item tuple of floats.

The documentation on Django PointFields is basically non-existant.

like image 744
Saqib Ali Avatar asked Jul 23 '14 21:07

Saqib Ali


People also ask

What is PointField in Django?

For the location, you are using the PointField , a GeoDjango-specific geometric field for storing a GEOS Point object that represents a pair of longitude and latitude coordinates. The other fields are normal Django fields of type CharField that can be used to store strings of small and large size.

What is srid in GeoDjango?

srid. Sets the SRID [2] (Spatial Reference System Identity) of the geometry field to the given value. Defaults to 4326 (also known as WGS84, units are in degrees of longitude and latitude).

What is GeoDjango?

GeoDjango is an included contrib module for Django that turns it into a world-class geographic web framework. GeoDjango strives to make it as simple as possible to create geographic web applications, like location-based services. Its features include: Django model fields for OGC geometries and raster data.

How do you name a Django model?

Naming Your Models The model definition is a class, so always use CapWords convention (no underscores). E.g. User , Permission , ContentType , etc. For the model's attributes use snake_case. E.g. first_name , last_name , etc.


2 Answers

In newer versions of Django you can:

from django.contrib.gis.geos import Point
pnt = Point(1, 1)

Plenty of examples available in https://docs.djangoproject.com/en/1.11/ref/contrib/gis/geos/

like image 68
Jordi Avatar answered Oct 21 '22 19:10

Jordi


You can use:

from django.contrib.gis.geos import GEOSGeometry
A.geoCoords = GEOSGeometry('POINT(LON LAT)', srid=4326) # 

where lat and lon mean latitude and longitude, respectively and srid is optional, indicating the Spatial Reference System Identifier.

You can see more on how to draw different geometries here: https://docs.djangoproject.com/en/dev/ref/contrib/gis/geos/#what-is-geos

like image 45
siolag161 Avatar answered Oct 21 '22 20:10

siolag161