计算两个 PointField 之间的距离 - 为什么我的结果不正确?
Calculating distance between two PointField (s) - Why is my result incorrect?
我正在尝试计算两个位置之间的距离(以英里为单位),但是我得到的结果不正确。
我认为它不正确的原因是因为我在这个 website 上放置了位置(纬度和经度)并且我得到以英里为单位的距离 0.055
。这是我的代码中的详细信息
PointField A : (-122.1772784, 47.7001663)
PointField B : (-122.1761632, 47.700408)
Distance : 0.001141091551967795
不过根据网站的说法,距离应该是
Distance: 0.055 miles
这是我计算距离的方法。
这是我的模型
class modelEmp(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
location = models.PointField(srid=4326,max_length=40, blank=True, null=True)
objects = GeoManager()
这就是我计算距离的方式
result = modelEmpInstance.location.distance(PointFieldBLocation)
where result = 0.001141091551967795
关于我在这里可能做错了什么以及为什么我的结果与网站不同的任何建议?
你的计算没有错,但结果是EPSG:4326
的单位,即degrees
。为了计算所需单位的距离,我们需要执行以下操作:
将点转换为具有 meter
个单位的 EPSG。
- 如果你不太在意计算的准确性,你可以使用
EPSG:3857
(但结果会是0.08104046068988752mi
)。
- 如果您确实关心计算的准确性,则您需要找到一个 EPSG,其计量单位适合您所在的位置。由于您的点位于西雅图地区附近,因此适当的 EPSG 为
32148
.
创建一个 Distance
对象,以米为单位计算距离
最后转换为miles
:
from django.contrib.gis.measure import Distance
result = Distance(
m = modelEmpInstance.location.transform(
32148, clone=True
).distance(PointFieldBLocation.transform(32148, clone=True)
)
print(
'Raw calculation: {}\nRounded calculation: {}'
.format(result.mi, round(result.mi, 2)
)
这将打印:
Raw calculation: 0.0546237743898667
Rounded calculation: 0.055
我正在尝试计算两个位置之间的距离(以英里为单位),但是我得到的结果不正确。
我认为它不正确的原因是因为我在这个 website 上放置了位置(纬度和经度)并且我得到以英里为单位的距离 0.055
。这是我的代码中的详细信息
PointField A : (-122.1772784, 47.7001663)
PointField B : (-122.1761632, 47.700408)
Distance : 0.001141091551967795
不过根据网站的说法,距离应该是
Distance: 0.055 miles
这是我计算距离的方法。
这是我的模型
class modelEmp(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
location = models.PointField(srid=4326,max_length=40, blank=True, null=True)
objects = GeoManager()
这就是我计算距离的方式
result = modelEmpInstance.location.distance(PointFieldBLocation)
where result = 0.001141091551967795
关于我在这里可能做错了什么以及为什么我的结果与网站不同的任何建议?
你的计算没有错,但结果是EPSG:4326
的单位,即degrees
。为了计算所需单位的距离,我们需要执行以下操作:
将点转换为具有
meter
个单位的 EPSG。- 如果你不太在意计算的准确性,你可以使用
EPSG:3857
(但结果会是0.08104046068988752mi
)。
- 如果您确实关心计算的准确性,则您需要找到一个 EPSG,其计量单位适合您所在的位置。由于您的点位于西雅图地区附近,因此适当的 EPSG 为
32148
.
- 如果你不太在意计算的准确性,你可以使用
创建一个
Distance
对象,以米为单位计算距离最后转换为
miles
:from django.contrib.gis.measure import Distance result = Distance( m = modelEmpInstance.location.transform( 32148, clone=True ).distance(PointFieldBLocation.transform(32148, clone=True) ) print( 'Raw calculation: {}\nRounded calculation: {}' .format(result.mi, round(result.mi, 2) )
这将打印:
Raw calculation: 0.0546237743898667 Rounded calculation: 0.055