本文介绍了NE,SW框内的GeoDjango的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Maps创建一个地理应用程序,并且收到了边框作为2个坐标:

I'm creating a geo app with Google Maps and I receive bounding box as 2 coordinates:

  • 东北
  • 西南

我有一个PointField模型.

I have a model with PointField.

from django.contrib.gis.db import models

class Place(models.Model):
    name            = models.CharField(max_length=200)
    address         = models.CharField(max_length=200)

    location        = models.PointField()

如何执行查询以获取边界框中的所有位置?

How could I perform a query to get all places within bounding box?

推荐答案

假定"2坐标"是x,y元组,例如:

Assuming that the "2 coordinates" are x,y tuples, for example:

ne = (50.0, -90)
sw = (45.5, -95)

您可以提取坐标并创建边界框元组:

You can extract the coordinates and create a bounding box tuple:

xmin = sw[0]
ymin = ne[1]
xmax = sw[1]
ymax = ne[0]
bbox = (xmin, ymin, xmax, ymax)

使用边界框几何图形,使用空间查找:

Using the bounding box geometry, query your Place records using a spatial lookup:

from django.contrib.gis.geos import Polygon

geom = Polygon.from_bbox(bbox)
queryset = Place.objects.filter(poly__contained=geom)

这篇关于NE,SW框内的GeoDjango的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 22:34