Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-SqlAlchemy:按大圆距离过滤查询?_Python_Database_Sqlite_Sqlalchemy_Pylons - Fatal编程技术网

Python-SqlAlchemy:按大圆距离过滤查询?

Python-SqlAlchemy:按大圆距离过滤查询?,python,database,sqlite,sqlalchemy,pylons,Python,Database,Sqlite,Sqlalchemy,Pylons,我正在使用Python和Sqlalchemy在Sqlite数据库中存储纬度和经度值。我已经为我的位置对象创建了一个 @hybrid_method def great_circle_distance(self, other): """ Tries to calculate the great circle distance between the two locations If it succeeds, it will return the great-circle d

我正在使用Python和Sqlalchemy在Sqlite数据库中存储纬度和经度值。我已经为我的位置对象创建了一个

@hybrid_method
def great_circle_distance(self, other):
    """
    Tries to calculate the great circle distance between the two locations

    If it succeeds, it will return the great-circle distance
    multiplied by 3959, which calculates the distance in miles.

    If it cannot, it will return None.

    """
    return math.acos(  self.cos_rad_lat 
                     * other.cos_rad_lat 
                     * math.cos(self.rad_lng - other.rad_lng)
                     + self.sin_rad_lat
                     * other.sin_rad_lat
                     ) * 3959
所有值,如
cos_rad_lat
sin_rad_lat
都是我预先计算的值,以优化计算。无论如何,当我运行以下查询时

pq = Session.query(model.Location).filter(model.Location.great_circle_distance(loc) < 10)
例如,当我打印
self.rad_lng
other.rad_lng
的值时

self.rad_lng: Location.rad_lng 
other.rad_lng: -1.29154947064

我做错了什么?

显然,你不能从这个字符串中得到一个浮点数

这是因为您使用的是“self”,它作为调用的第一个参数,表示该方法是对象的一部分,而不是您可能传递的某个var

你应该试试这个:

def great_circle_distance(self, first, other):
    """
    Tries to calculate the great circle distance between 
    the two locations by using the Haversine formula.

    If it succeeds, it will return the Haversine formula
    multiplied by 3959, which calculates the distance in miles.

    If it cannot, it will return None.

    """
    return math.acos(  self.cos_rad_lat 
                     * other.cos_rad_lat 
                     * math.cos(first.rad_lng - other.rad_lng)
                     + self.sin_rad_lat
                     * other.sin_rad_lat
                     ) * 3959
我假设上面的全局变量“self.cos_rad_lat”和“self.sin_rad_lat”
使用程序中其他地方的正确值启动,可能在同一对象的“init”部分。

显然,无法从该字符串获取浮点值

这是因为您使用的是“self”,它作为调用的第一个参数,表示该方法是对象的一部分,而不是您可能传递的某个var

你应该试试这个:

def great_circle_distance(self, first, other):
    """
    Tries to calculate the great circle distance between 
    the two locations by using the Haversine formula.

    If it succeeds, it will return the Haversine formula
    multiplied by 3959, which calculates the distance in miles.

    If it cannot, it will return None.

    """
    return math.acos(  self.cos_rad_lat 
                     * other.cos_rad_lat 
                     * math.cos(first.rad_lng - other.rad_lng)
                     + self.sin_rad_lat
                     * other.sin_rad_lat
                     ) * 3959
我假设上面的全局变量“self.cos_rad_lat”和“self.sin_rad_lat”
在程序中的其他地方使用正确的值启动,可能在同一对象的“init”部分。

看起来您已经正确地完成了所有操作,但不知何故,该方法实际上没有得到“混合”。你可能做了一些愚蠢的事情,比如没有在源代码中加入decorator?

看起来你做的每件事都是正确的,但不知何故,这个方法实际上并没有得到“杂交”。你可能做了一些愚蠢的事情,比如没有在源代码中实际使用decorator?

你不能这样使用
math
模块:

>>> c = toyschema.Contact()
>>> c.lat = 10
>>> c.lat
10
>>> import math
>>> math.cos(c.lat)
-0.83907152907645244
>>> math.cos(toyschema.Contact.lat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

您不能这样使用
数学
模块:

>>> c = toyschema.Contact()
>>> c.lat = 10
>>> c.lat
10
>>> import math
>>> math.cos(c.lat)
-0.83907152907645244
>>> math.cos(toyschema.Contact.lat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

这没有考虑SQLAlchemy“hybrid”装饰器的工作原理。这没有考虑SQLAlchemy“hybrid”装饰器的工作原理。这不是哈弗森公式,而是余弦公式的球面定律。查看(例如)我们是否可以查看您的
位置
类的更完整示例?特别是,这些属性是如何形成的。它们是类属性吗?哦,你是对的。不是哈弗森公式,只是大圆距离。这不是哈弗森公式,这是余弦公式的球面定律。查看(例如)我们是否可以查看您的
位置
类的更完整示例?特别是,这些属性是如何形成的。它们是类属性吗?哦,你是对的。不是haversine,只是很长的圆距离。我用这个导入,“从sqlalchemy.ext.hybrid导入hybrid_属性,hybrid_方法”我用这个导入,“从sqlalchemy.ext.hybrid导入hybrid_属性,hybrid_方法”谢谢!我有一种感觉,这与此有关。今晚晚些时候我会试试,让你知道!还有一件事。当sqlite数据库在内存中时,我就可以使用它了,但是对于已经创建的数据库,我似乎无法按照您描述的方式添加函数。如何将函数添加到已经创建的sqlite数据库中?问题不在于数据库已经创建,而在于每个连接都有函数;sqlite为磁盘上的数据库创建了一个全新的连接,以避免线程安全问题;最佳解决方案可能取决于您的线程需求;因此,您应该查看sqlalchemyGreat中的选项。谢谢你的提示。最后,我将一个事件侦听器附加到为每个连接创建自定义trig函数的池中:谢谢!我有一种感觉,这与此有关。今晚晚些时候我会试试,让你知道!还有一件事。当sqlite数据库在内存中时,我就可以使用它了,但是对于已经创建的数据库,我似乎无法按照您描述的方式添加函数。如何将函数添加到已经创建的sqlite数据库中?问题不在于数据库已经创建,而在于每个连接都有函数;sqlite为磁盘上的数据库创建了一个全新的连接,以避免线程安全问题;最佳解决方案可能取决于您的线程需求;因此,您应该查看sqlalchemyGreat中的选项。谢谢你的提示。最后,我将一个事件侦听器附加到为每个连接创建自定义trig函数的池: