修改 Django 模型有多难?
我正在做地理定位,Django 没有 PointField。所以,我被迫用 RAW SQL 编写。 GeoDjango,Django 库,不支持以下对 MYSQL 数据库的查询(有人可以帮我验证一下吗?)
cursor.execute("SELECT id FROM l_tag WHERE\
(GLength(LineStringFromWKB(LineString(asbinary(utm),asbinary(PointFromWKB(point(%s, %s)))))) < %s + accuracy + %s)\
我不知道为什么 GeoDjango 库不能在 MYSQL 数据库中执行此操作。我讨厌编写 RAW SQL 来计算两点之间的距离。有没有办法为 Django 创建自己的库来处理这个问题?如果是这样,有多难?
I am doing geolocation, and Django does not have a PointField. So, I am forced to writing in RAW SQL. GeoDjango, the Django library, does not support the following query for MYSQL databases (can someone verify that for me?)
cursor.execute("SELECT id FROM l_tag WHERE\
(GLength(LineStringFromWKB(LineString(asbinary(utm),asbinary(PointFromWKB(point(%s, %s)))))) < %s + accuracy + %s)\
I don't nkow why GeoDjango library cannot do this in MYSQL database. I hate writing RAW SQL for calculating distances between two points. Is there a way I can create my own library for Django that can handle this? If so, how hard is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不扩展已经存在的类呢?只是好奇并希望我能具体帮助你。
Why don't you just extend a class that's already there? Just curious and wish I could help you specifically.
GeoDjango 确实有一个 PointField。
看起来您正在尝试进行内部字段查找,这在 MySQL 上不起作用(截至 2010 年 4 月),但在 Postgres 上起作用:
class Tag(Model):
point = PointField()
Tag.objects.filter(point__dwithin=(point,D(mi=4)))
小心这种查询,因为它需要表扫描。如果您可以容忍选择矩形区域内的所有点,则可以使用包含边界框的查询:
Tag.objects.filter(point__contained=geom)
,其中geom 是多边形。
GeoDjango does have a PointField.
It looks like you're trying to do a dwithin field lookup, which does not work on MySQL (as of April 2010), but does in Postgres:
class Tag(Model):
point = PointField()
Tag.objects.filter(point__dwithin=(point,D(mi=4)))
Careful with this kind of query, as it requires a table scan. If you can tolerate selecting all points within a rectangular region, you could use the bounding box contained query:
Tag.objects.filter(point__contained=geom)
where geom is a polygon.