[英]Translating PostGIS query to Django QuerySet (geoDjango postgis)
我試圖弄清楚如何將PostGIS查詢轉換為GeoDjango:
def my_view(request, tempounix):
conn.execute('SELECT ST_X(geom) as x, ST_Y(geom) AS y, accuracy, altitude, speed, tempounix '
'FROM authapp_posizione '
'ORDER BY tempounix DESC LIMIT 1;')
x, y, accuracy, altitude, speed, timestamp = c.fetchone()
data = {
"geometry": {
"type": "Point",
"coordinates": [x, y],
},
"type": "Feature",
"properties": {
"accuracy": accuracy,
"altitude": altitude,
"speed": speed,
"tempounix": tempounix,
},
}
return JsonResponse(data)
要使用Django的QuerySet和Django的ORM,您將需要定義模型並使用它們:
# models.py:
class Position(models.Model):
geom = models.PointField(...)
accuracy = models.IntegerField()
altitude = models.IntegerField()
speed = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
現在:
p = Position.objects.order_by("-created_at").first()
if p:
print(p.created_at, p.geom.coordinates, p.altitude)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.