Wednesday, May 20, 2009

Popular Places

The following ruby program is the one used to compute the most popular places in the world based on the number of photos for a given cell. The program generates the grid (based on latitude and longitude ranges and resolutions), queries panoramio and parses the result looking for the keyword "count". The results can be seen at my blog at Starlab.

The use of this program is the following: At the command line type:


PoPlaces minLong maxLong minLat maxLat longResolution latResolution



Note: The values have to be given in degrees.


#!/usr/bin/ruby
#-------------------------------------------------------------------------------
#
# PoPlaces.rb
#
# The purpose of this Ruby program is to retrieve the number of photographs
# for a set of cells defined by the user defined area (with a certain longitude
# and latitude resolution
#
# References:
# - Panoramio API : http://www.panoramio.com/api
#
#
# @ Miquel Garcia Fernandez, May 2009
#
# Contact me at: miquel.garciafernandez@gmail.com
#
#-------------------------------------------------------------------------------

# HTTP library is required
require 'net/http'

# Create default values for Longitude, latitude and resolution
dLongMin = -180.0
dLongMax = +180.0

dLatMin = -90.0
dLatMax = +90.0

dLongRes = 0.5
dLatRes = 0.25

# Parse the argument list if any
ARGV.each_with_index do |a, i_a|
dLongMin = a.to_f if i_a == 0
dLongMax = a.to_f if i_a == 1

dLatMin = a.to_f if i_a == 2
dLatMax = a.to_f if i_a == 3

dLongRes = a.to_f if i_a == 4
dLatRes = a.to_f if i_a == 5
end

# Compute the number of cells required in longitude and latitude
iNLongs = (dLongMax - dLongMin)/dLongRes ;
iNLats = (dLatMax - dLatMin)/dLatRes ;

# Loop over all latitudinal and longitudinal indices
(0...iNLongs).each do |iLong|

(0...iNLats).each do |iLat|

# Define the central point of the cell
dLong = dLongMin + dLongRes*(iLong + 1.0/2.0)
dLat = dLatMin + dLatRes*(iLat + 1.0/2.0)

# Define the vertices of the cell
dLongIntMax = dLong + dLongRes/2.0
dLongIntMin = dLong - dLongRes/2.0
dLatIntMax = dLat + dLatRes/2.0
dLatIntMin = dLat - dLatRes/2.0

# Retrieve the number of photos in Panoramio
url = "www.panoramio.com"
uri = "/map/get_panoramas.php?order=popularity&set=public&from=0&to=1&" +
"minx=#{dLongIntMin}&miny=#{dLatIntMin}&" +
"maxx=#{dLongIntMax}&maxy=#{dLatIntMax}&size=medium"
res = Net::HTTP.get(url, uri)

# Parse the Panoramio output in order to extract the photo count for the
# current cell
iNPhotos = 0
res.each \
{ |line| iNPhotos= /[0-9]+/.match(line)[0] if line=~/\{ \"count\":/ }

# Output the result to the standard output
print "%7.2f %7.2f %7.2f %7.2f %7d\n" %
[dLongIntMin, dLongIntMax, dLatIntMin, dLatIntMax, iNPhotos]

end # End of inner loop (latitudes)

end # End of outer loop (longitudes)

No comments:

Post a Comment