简体   繁体   English

GeoDjango + PostGIS计算错误的距离

[英]GeoDjango + PostGIS calculates wrong Distances

I just installed PostGIS with GeoDjango. 我刚刚用GeoDjango安装了PostGIS。 Everything worked fine, but now I have a problem and cant find out the reason for this. 一切都很好,但现在我有一个问题,无法找到原因。

I have model like this: 我有这样的模型:

from django.contrib.gis.db import models

class Shop(models.Model):
    name = models.CharField(max_length=80)
    point = models.PointField(null=True, blank=True)
    objects = models.GeoManager()

And I set its point to this position (49.794254,9.927489). 我指出了这个立场(49.794254,9.927489)。 Then i create a point like this: 然后我创建一个像这样的点:

pnt = fromstr('POINT(50.084068 8.238381)')

The distance between this points should be about ~ 125 km, but when i do this: 这些点之间的距离应该约为125公里,但是当我这样做时:

results = Shop.objects.distance(pnt)
print results[0].distance.km

I'm getting always about 60 km too much in my result, so it returns 190 km! 我的结果总是超过60公里,所以返回190公里! My SRIDs of both points are 4326... probably something wrong with that? 我的两个积分的SRID都是4326 ......可能有些不对劲?

And maybe another interesting fact, when i do this: 也许另一个有趣的事实,当我这样做:

pnt.distance(shop.point)

it returns 1.713790... as a result. 它返回1.713790 ...结果。

What am I doing wrong? 我究竟做错了什么? Any alternatives for me to use with python + django? 我可以选择使用python + django吗? If there is a better solution I would not need to use PostGIS. 如果有更好的解决方案,我就不需要使用PostGIS。

Hope you can help me! 希望你能帮我!

Chris 克里斯

I just ran this query in postgis : 我刚刚在postgis中运行了这个查询:

select round(CAST(ST_Distance_Sphere(ST_GeomFromText('POINT(49.794254 9.927489)',4326), ST_GeomFromText('POINT(50.084068 8.238381)',4326)) As numeric)/1000.0,2) as distance_km;
 distance_km 
-------------
      190.50

the result is in fact 190.50, so it seems there's nothing wrong with your 190 km result 结果实际上是190.50,所以你的190公里的结果似乎没有错

same result with this awesome page , there is a brief explanation of how to calculate this distances. 与这个令人敬畏的页面相同的结果,有一个如何计算这个距离的简要说明。

the 1.713790... result seems to be in the same units of the srid, or in other words that number is not in meters. 1.713790 ......结果似乎与srid的单位相同,换句话说,这个数字不是以米为单位。

EDIT Ooohh I just saw your problem you misplaced the lat and lon, in the WKT format, Longitude comes first so the real query should be: 编辑 Ooohh我刚刚看到你的问题,你错误地放置了lat和lon,在WKT格式中,Longitude首先出现,所以真正的查询应该是:

select round(CAST(ST_Distance_Sphere(ST_GeomFromText('POINT(9.927489 49.794254)',4326), ST_GeomFromText('POINT(8.238381 50.084068)',4326)) As numeric)/1000.0,2) as distance_km;
 distance_km 
-------------
      125.10

so the points should be created like this 所以应该像这样创建积分

POINT(9.927489 49.794254)
POINT(8.238381 50.084068)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM