Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:01:45+00:00 2026-05-25T21:01:45+00:00

I am using Python and Sqlalchemy to store latitude and longitude values in a

  • 0

I am using Python and Sqlalchemy to store latitude and longitude values in a Sqlite database. I have created a hybrid method for my Location object,

@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

All the values like cos_rad_lat and sin_rad_lat are values I pre-calculated to optimize the calculation. Anyhow, when I run the following query,

pq = Session.query(model.Location).filter(model.Location.great_circle_distance(loc) < 10)

I get the following error,

line 809, in great_circle_distance
    * math.cos(self.rad_lng - other.rad_lng)
TypeError: a float is required

When I print the values for self.rad_lng and other.rad_lng I get, for example,

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

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:01:45+00:00Added an answer on May 25, 2026 at 9:01 pm

    You can’t really use the math module that way:

    >>> 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
    

    You’ll have combine sqalchemy.func.* in place of math.* in a @great_circle_distance.expression method for all of that kind of cleverness. Unfortunately, you can’t do that with sqlite, either; it doesn’t provide trig functions
    You could use PostgreSQL, which does, or you can try to add these functions to sqlite yourself:

    EDIT It’s actually not to hard to add functions to sqlite: This is NOT tested.

    Have to add the math functions to sqlite:

    engine = sqlalchemy.create_engine("sqlite:///:memory:/")
    raw_con = engine.raw_connection()
    raw_con.create_function("cos", 1, math.cos)
    raw_con.create_function("acos", 1, math.acos)
    
    class Location(...):
        ...
        @hybrid_method
        def great_circle_distance(self, 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(self.rad_lng - other.rad_lng)
                             + self.sin_rad_lat
                             * other.sin_rad_lat
                             ) * 3959
    
        @great_circle_distance.expression
        def great_circle_distance(cls, other):
            return sqlalchemy.func.acos(  cls.cos_rad_lat 
                             * other.cos_rad_lat 
                             * sqlalchemy.func.cos(cls.rad_lng - other.rad_lng)
                             + cls.sin_rad_lat
                             * other.sin_rad_lat
                             ) * 3959
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I'm using SQLalchemy for a Python project, and I want to have a tidy
I've created a python application which uses elixir/sqlalchemy to store data. The second release
I am using SqlAlchemy, a python ORM library. And I used to access database
I'm building an app in python. I'm using sqlalchemy-migrate to track my database schema.
I am developing a Python web app using sqlalchemy to communicate with mysql database.
I've created a simple DB using SQLAlchemy in python and there is a column
I've been using Python for most of my web projects lately, and have come
How do you unit test your python DAL that is using postgresql. In sqlite
I'm implementing a Python ontology class that uses a database backend to store and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.