Bitcoin Forum
December 09, 2016, 01:38:08 PM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: [10 BTC] GeoDjango code [CLOSED]  (Read 1551 times)
cuddlefish
Full Member
***
Offline Offline

Activity: 126



View Profile
May 13, 2011, 05: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.

Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481290688
Hero Member
*
Offline Offline

Posts: 1481290688

View Profile Personal Message (Offline)

Ignore
1481290688
Reply with quote  #2

1481290688
Report to moderator
1481290688
Hero Member
*
Offline Offline

Posts: 1481290688

View Profile Personal Message (Offline)

Ignore
1481290688
Reply with quote  #2

1481290688
Report to moderator
1481290688
Hero Member
*
Offline Offline

Posts: 1481290688

View Profile Personal Message (Offline)

Ignore
1481290688
Reply with quote  #2

1481290688
Report to moderator
unfinishe
Full Member
***
Offline Offline

Activity: 126



View Profile
May 13, 2011, 06: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.

Check out the results from my Bitcoin Survey Project!
https://bitcointalk.org/index.php?topic=88927.0
cuddlefish
Full Member
***
Offline Offline

Activity: 126



View Profile
May 14, 2011, 03:53:48 AM
 #3

I'm raising the bounty to 10 BTC.

kgo
Hero Member
*****
Offline Offline

Activity: 548


View Profile
May 14, 2011, 04:08:14 AM
 #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.
unfinishe
Full Member
***
Offline Offline

Activity: 126



View Profile
May 14, 2011, 07: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.

Check out the results from my Bitcoin Survey Project!
https://bitcointalk.org/index.php?topic=88927.0
cuddlefish
Full Member
***
Offline Offline

Activity: 126



View Profile
May 14, 2011, 07:43:11 PM
 #6

Sorry, I got a solution from jarly, forgot to lock the thread.

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!