开发者

django - add a POINT in a PostgreSQL database using GeoDjango from decimal coordinates

I'm using PostGIS with django. I know how to开发者_高级运维 add a "POINT" on PostgreSQL from decimal coordinates but how do I add it using GeoDjango?

This is how I do it in PostgreSQL:

UPDATE "my_table" SET coordinates=GeometryFromText('POINT(-93.7505 31.3059)',4269) WHERE id=101;

How do I do the same thing from django?


Here's what I think you're asking for:

GeoDjango uses an object relational mapper. In your models.py, you'll have to define a model that includes a point, e.g:

class my_points(models.Model):
  name = models.CharField(max_length = 100)
  coords = models.PointField()

Then, in one of your views, you'll need to instantiate a my_points object:

a = my_point(name="example", coords=x,y)

I imagine this isn't syntactically perfect, but the GeoDjango model api: http://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/ and the regular geodjango models guide: http://docs.djangoproject.com/en/dev/topics/db/models/ will probably get you where you need to go. Hope that helps.

Edit: Okay, I didn't understand your post. "add a POINT" indicated to me that you wanted to add one. Your SQL statement is for updating a value, though.

To update values in a database, you'll still need a model. I'm going to proceed on the assumption that you have a model set up called "my_points".

from django.contrib.gis.geos import GEOSGeometry
# queries the database for the object with an id=101
a = my_points.objects.get(id=101)
# defines your point
pnt = GEOSGeometry('POINT(5 23)')
# updates the object in memory
a.coords = pnt
# saves the changes to the database
a.save()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜