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

  • SEARCH
  • Home
  • 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 4024262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:42:00+00:00 2026-05-20T10:42:00+00:00

I’m using sqlAlchemy ORM and would like to like to calculate and return the

  • 0

I’m using sqlAlchemy ORM and would like to like to calculate and return the distance from a given point and the stored points.

class Event(Base):

    __tablename__ = 'events'
    # Schema
    id = Column(Integer, primary_key=True)
    title = Column(String(150))
    description = Column(Text)
    url = Column(String(800))
    lat = Column(Float)
    lng = Column(Float)

….and my query:

    nearest = """SELECT *, ((lat - '-41.288889') * (lat - '-41.288889')
 + (lng  - 174.777222) * (lng  - 174.777222)) AS distance FROM events 
ORDER BY distance ASC """

e = Event.query.from_statement(nearest)

This of seems to return the objects in the correct order, but I have no access to the distance attribute. How do I access this value – or what is the best-practice way of accomplishing this?

  • 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-20T10:42:01+00:00Added an answer on May 20, 2026 at 10:42 am

    Problem 1: Latitude degrees and longitude degrees have different lengths … same at the equator (assuming earth is a sphere) but at the poles a longitude degree has zero length.

    Update To give an example of the severity of your problem:

    Travelling north or south by one degree of latitude will cover about 111.2 km (assuming that the earth is a sphere). Travelling east or west by one degree of longitude will cover about 111.2 km at the equator. At a latitude of -41.3 degrees, it will cover only 83.5 km.

    From (-41.3, 174.8) due east to (-41.3, 174.92) is 10.0 km. Your calculation treats it as though it is 13.3 km away — an error of 33%.

    You can get to within a 4 metre error by a fairly simple approximation:

    from math import pi, sqrt, radians, cos
    Earth_radius_km = 6371.009
    km_per_deg_lat = 2 * pi * Earth_radius_km / 360.0
    
    # what your SQL query is in effect doing
    def approx_dist_1(lat1, lon1, lat2, lon2):
        return km_per_deg_lat * sqrt((lat1 - lat2) ** 2 + (lon1 - lon2) ** 2)
    
    # better version    
    def approx_dist_2(lat1, lon1, lat2, lon2):
        # calculate km_per_deg_lon for your central station in Python and 
        # embed it in your query
        km_per_deg_lon = km_per_deg_lat * cos(radians(lat1))
        return sqrt((km_per_deg_lat *(lat1 - lat2)) ** 2 + (km_per_deg_lon * (lon1 - lon2)) ** 2)
    

    This approximate distance is good enough for “find the nearest pizza shop” applications, and has the advantage that it can be used in bare-bones environments like SQLite which don’t support sin/cos/tan and their inverses out of the box.

    Problem 2: SQLite is tolerant, but you shouldn’t make a habit of using quotes as in (lat - '-41.288889')

    Problem 3: I can’t reproduce your problem at the SQLlite level:

    sqlite> create table foo (lat float, lon float);
    sqlite> insert into foo values(99.9, -170.1);
    sqlite> select * from foo;
    99.9|-170.1
    sqlite> SELECT *, ((lat - '-41.288889') * (lat - '-41.288889')
       ...>  + (lon  - 174.777222) * (lon  - 174.777222)) AS distance from foo;
    99.9|-170.1|138874.600631492
    sqlite>
    

    Perhaps you should elaborate on “This of seems to return the objects in the correct order, but I have no access to the distance attribute” … what does introspecting e tell you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build

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.