Author

Topic: [10 BTC] GeoDjango code [CLOSED] (Read 1831 times)

sr. member
Activity: 364
Merit: 250
May 14, 2011, 02:43:11 PM
#6
Sorry, I got a solution from jarly, forgot to lock the thread.
full member
Activity: 126
Merit: 100
May 14, 2011, 02:28:57 AM
#5
Ok, so upon further research (I'm sorry for not doing a more initially), it seems that your best bet is to write a raw SQL query.

http://docs.djangoproject.com/en/1.2/topics/db/sql/#performing-raw-sql-queries

I can't write the exact code and query without knowing more details about the database you're using, the tables and such, but assuming you're using PostGIS, it should go something like this:

Code:
Orders.objects.raw('SELECT * FROM orders WHERE ST_Distance( point, %s ) < maxdistance', [original_point]);

Fiddle around with it and let me know if it works.
kgo
hero member
Activity: 548
Merit: 500
May 13, 2011, 11:08:14 PM
#4
Given source point item, point1, only get point items, point2, where:

 (point2.x > (point1.x - max_range)) and (point2.x < (point1.x + max_range)) and (point2.y > (point1.y - max_range)) and (point2.y < (point1.y + max_range)).

You can hopefully do this with raw SQL.  This will filter out anything that's blatantly outside the range.  After that, use the normal:

distance = sqrt( (point2.x - point1.x) ^ 2) + (point2.y - point1.y) ^ 2)

With the remaining objects to filter the list to the correct result set.

EDIT:  Also make sure the X and Y columns are indexed in the database for quick lookups.
sr. member
Activity: 364
Merit: 250
May 13, 2011, 10:53:48 PM
#3
I'm raising the bounty to 10 BTC.
full member
Activity: 126
Merit: 100
May 13, 2011, 01:28:08 AM
#2
Because I don't know much about GeoDjango, and I don't exactly how the database that you're using handles it on the back-end, I can't say for sure that it's more efficient than looping through all of the models, but the distance_lte function seems to be the sort of function that you're looking for. I'd imagine that the database would handle it as efficiently as reasonably possible.

http://docs.djangoproject.com/en/1.3/ref/contrib/gis/geoquerysets/#distance-lte

Of course, the fact that every order has its own maximum distance makes it difficult... So, what I would do is enforce a "maximum" maximum distance, then use the function to limit your loop to those within that "maximum" distance.

So, for example, if the choice for maximum distance was <30 mi, then you take your point, find all the orders within 30 mi of the point using the function, and then loop to filter out the ones that are too far away. There might be an easier way to do this, but that's the best way I can find at the moment.
sr. member
Activity: 364
Merit: 250
May 13, 2011, 12:25:22 AM
#1
So I have a list of models of Orders. They have a maximum distance, they also have a Point object.
I need a function that will take a point, and find all the orders the point's within the maximum distance of.

I'll pay 10 BTC for a solution that doesn't loop over all the orders.
Jump to: