Thursday, May 21, 2009

Example of vimrc

Let's face it,IDEs are fancy and cool, but I sometimes wonder to what extent these tools distract us with the real thing: programming. In this sense, the austerity of vi or vim may be helpful to really focus on the programming tasks. Besides, the fact that is console based makes us aware of the file structure of the application we are developing. I like NetBeans a lot, but frankly speaking, I get a bit lost on the file directory when developing Ruby on Rails applications. I really recommend vim even for this purpose. The learning curve may be daunting at first. But I think that it pays at the end (and not only for RoR, but also for Matlab and IDL programs).

Here I post a small snippet with the vimrc I am using.


" Syntax highlighting
syntax on

" Use spaces instead of tabs
set expandtab

" Set numbering
set numberwidth=5
set number

" Set autoindentation with 4 space indentation
set autoindent
set shiftwidth=4

" Set the ruler
set ruler

" Set the CUDA filetype
au BufNewFile,BufRead *.cu set ft=cu
"au BufNewFile,BufRead *.gnu set ft=gnuplot

" Set the searc highlight
set hlsearch

" Set syntax automatic folding
set foldmethod=syntax

" Set foldcolumns
set foldcolumn=3

" Set colorscheme to desert
colorscheme desert

" Instructions to print
" Courtesy of http://vim.wikia.com/wiki/Print_to_a_Postscript_file
let &printexpr="(v:cmdarg=='' ? ".
\"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice)".
\". ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error".
\" : system('mv '.v:fname_in.' '.v:cmdarg) + v:shell_error)"


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)