如何根据经度和纬度过滤geodjango [英] How to filter geodjango based on longitude and latitude

查看:132
本文介绍了如何根据经度和纬度过滤geodjango的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经存储了经度和纬度的应用程序,现在我想将其与geodjango集成,该应用程序看起来像这样。

I have an application that has been storing longitude and latitude now i want to integrate it with geodjango the application looks like this.

class Location(models.Model):
    #other fields here
    lat = models.CharField(blank=True, max_length=100)
    lng = models.CharField(blank=True, max_length=100)

现在我想根据距离过滤位置,例如过滤所有1公里以外的位置使用django.contrib.gis.measure从pk = 1的位置导入D和GEOSGeometry,或者我应该重构模型以具有点而不是经度和纬度,所以我可以做这样的事情:

now I will like to filter locations based on a distance say filter all location 1km away from location with pk = 1 using django.contrib.gis.measure import D and GEOSGeometry or should i refactor the model to have point rather than longitude and latitude so i can do something like this :

Location.objects.filter(point__dwithin=(D(km=5)))

任何建议和建议都可以。

any advice and recommendations will do.

推荐答案

向南使用... 3次迁移以解决此问题。 (链接:如何安装南部]

Using south... it would take 3 migrations to address this. (Link: How to install South]

1)添加名为 point PointField

from django.contrib.gis.db import models  <-- NEW

# ...

class Location(models.Model):
    #other fields here
    lat = models.CharField(blank=True, max_length=100)
    lng = models.CharField(blank=True, max_length=100)

    point = models.PointField(null=True)  <-- NEW






2)自动执行 schemamigration

patrick@localhost:~$ python manage.py schemamigration my_app_name --auto






3)执行自定义的数据迁移,您将在现有的 CharField中的每个元素上创建一个点属性

patrick@localhost:~$ python manage.py datamigration my_app_name latlng_to_point

编辑由 datamigration创建的文件管理命令:

# my_app_name/migrations/0034__latlng_to_point.py

from django.contrib.gis.geos import Point

# ...

class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
        for location in orm['my_app_name.Location'].objects.all():
            location.point = Point(float(location.lng), float(location.lat), srid=4326)
            location.save()

    def backwards(self, orm):
        "Write your backwards methods here."
        for location in orm['data.Location'].objects.all():
            location.point = None
            location.save()






4)从您的计算机中删除 CharField 建模并删除 null = True ,如果您愿意:


4) Delete the CharField from your model and remove null=True if you wish:

class Location(models.Model):
    # other fields here
    # DELETED lat = models.CharField(blank=True, max_length=100)
    # DELETED lng = models.CharField(blank=True, max_length=100)

    point = models.PointField(null=False)  <-- NEW: null=False






5)自动创建另一个模式迁移


5) Create another schemamigration automatically

patrick@localhost:~$ python manage.py schemamigration my_app_name --auto






6)成功运行迁移!这是将更改写入您的数据库等的时间。


6) Run the migrations successfully! This is when changes are written to your DB, etc.

patrick@localhost:~$ python manage.py migrate my_app_name

这篇关于如何根据经度和纬度过滤geodjango的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆