Skip to content

Commit

Permalink
Improve picture distribution for Issue johnmckerrell#14
Browse files Browse the repository at this point in the history
Only apply group if amount of amount of returned pictures would be
bigger than FIND_IN_AREA_LIMIT

Signed-off-by: Radek Dostal <rd@radekdostal.com>
  • Loading branch information
dasty committed Jan 21, 2013
1 parent 8a22958 commit b029de5
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,29 @@ def self.find_in_area(bbox)
return
end
bits.map! { |b| b.to_f }
lat_step = (bits[1] - bits[3]) / Math.sqrt(FIND_IN_AREA_LIMIT)
lon_step = (bits[0] - bits[2]) / Math.sqrt(FIND_IN_AREA_LIMIT)
Photo.find( :all,

# try to obtain all pictures from bbox
@result = Photo.find( :all,
:conditions => [
"lat IS NOT NULL AND lon IS NOT NULL AND lat >= ? AND lat <= ? AND lon >= ? AND lon <= ? AND status = 'available'",
bits[1], bits[3], bits[0], bits[2] ],
:group => [ "floor(lat/", lat_step, "), floor(lon/", lon_step, ")" ],
:order => 'created_at DESC',
:limit => FIND_IN_AREA_LIMIT)

if (@result.length >= FIND_IN_AREA_LIMIT)
# we hit FIND_IN_AREA_LIMIT - apply group to distribute pictures around whole screen

lat_step = (bits[1] - bits[3]) / Math.sqrt(FIND_IN_AREA_LIMIT)
lon_step = (bits[0] - bits[2]) / Math.sqrt(FIND_IN_AREA_LIMIT)

@result = Photo.find( :all,
:conditions => [
"lat IS NOT NULL AND lon IS NOT NULL AND lat >= ? AND lat <= ? AND lon >= ? AND lon <= ? AND status = 'available'",
bits[1], bits[3], bits[0], bits[2] ],
:group => [ "floor(lat/", lat_step, "), floor(lon/", lon_step, ")" ],
:order => 'created_at DESC',
:limit => FIND_IN_AREA_LIMIT)
end
@result
end

def self.count_for_status( status, user = nil )
Expand Down

0 comments on commit b029de5

Please sign in to comment.