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 8258595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:35:45+00:00 2026-06-08T02:35:45+00:00

I am using geodjango and have a collection of points in my database. To

  • 0

I am using geodjango and have a collection of points in my database. To get a queryset of points within a certain area I use this:

queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))

My question is how can I return only one point(the point with the lowest distance)from the point I have passed it?

EDIT

I should mention that I am passing in coordinates and wanting to create a Point object with them. Then pass that point in as the origin and filter against that.
For example I have tried:

from spots.models import *
from django.contrib.gis.geos import *

origin = Point(28.011030, -26.029430)
distance_m = 1000

queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))
for q in queryset:
    print q.distance

This snippet of code gives me this error:

Traceback (most recent call last):
  File "<console>", line 2, in <module>
AttributeError: 'Spot' object has no attribute 'distance'

Interestingly enough if I do the following:

origin = Spot.objects.get(name='Montecasino').point
distance_m = 1000

for city in Spot.objects.distance(origin):
    print(city.name, city.distance)

(u'Design Quarter Parking', Distance(m=677.347841801))
(u'Montecasino', Distance(m=0.0))
(u'Fourways', Distance(m=1080.67723755))
  • 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-06-08T02:35:47+00:00Added an answer on June 8, 2026 at 2:35 am

    Finally a solution gathered from clues from GeoDjango distance filter with distance value stored within model – query which lead me to this post. From this information I was able to gather that you MUST SPECIFY the measure parameter in your distance query.
    You will see in the below snippet, I import measure as D. Then use it in the query.
    If you don’t specify it you will get this error:

    ValueError: Tuple required for `distance_lte` lookup type.
    

    To take just the point with the lowest distance I used order_by('distance')[:1][0]

    from spots.models import *
    from django.contrib.gis.geos import *
    from django.contrib.gis.measure import D
    
    distance_m = 20000
    origin = Point(28.011030, -26.029430)
    
    closest_spot = Spot.objects.filter(point__distance_lte=(origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using RestKit 0.10.1, I have objects served similar to this json format: {objects: [
I'm trying to compute clusters on a set of points in Python, using GeoDjango.
I am using geoDjango . I have installed following packages from source Gdal ,
I have implemented GeoDjango using postgis. Here is my model: ... geometria = models.PolygonField(srid=4326,
Using Java, assuming v1.6. I have a collection where the unique index is a
From GeoDjango Point Field, I get the following points: object1.point = POINT(-113.4741271000000040 53.4235217000000020) object2.point
Using d3, I have some simple markup like this: <div id=my-log></div> I have an
I am new to geodjango. I am using a geodjango model for my application.
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

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.