~ubuntu-branches/ubuntu/quantal/marble/quantal

« back to all changes in this revision

Viewing changes to src/plugins/render/weather/bbc-xml-generator.rb

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-11 15:43:02 UTC
  • Revision ID: james.westby@ubuntu.com-20110711154302-lq69ftcx125g1jx5
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby
 
2
 
 
3
require 'net/http'
 
4
require 'rubygems'
 
5
require 'xmlsimple'
 
6
require 'pp'
 
7
 
 
8
 
 
9
class Station
 
10
    attr_reader :long, :lat, :priority, :station
 
11
    attr_writer :priority
 
12
    def initialize(number)
 
13
        @id = number.to_s
 
14
        @url = 'http://newsrss.bbc.co.uk/weather/forecast/' + @id + '/ObservationsRSS.xml'
 
15
    end
 
16
    
 
17
    def parse()
 
18
        xmlData = Net::HTTP.get_response(URI.parse(@url)).body
 
19
        begin
 
20
            data = XmlSimple.xml_in(xmlData, { 'KeyAttr' => 'name' })
 
21
        rescue REXML::ParseException
 
22
            return false
 
23
        end
 
24
        
 
25
        channel = (data["channel"])[0]
 
26
        
 
27
        name = channel["title"].to_s.sub( /BBC - Weather Centre - Latest Observations for /, "" )
 
28
        nameList = name.split( /, / )
 
29
        @station = nameList[0].to_s
 
30
        @country = nameList[1].to_s
 
31
        
 
32
        item = channel["item"][0]
 
33
        @lat = item['lat'].to_s.to_f
 
34
        @long = item['long'].to_s.to_f
 
35
        if @lat == "N/A" or @long == "N/A"
 
36
            return false
 
37
        end
 
38
        
 
39
        return true
 
40
    end
 
41
        
 
42
    def to_s()
 
43
        string = "Id: "
 
44
        string += @id.to_s
 
45
        string += "\n"
 
46
        string += "Station: "
 
47
        string += @station.to_s
 
48
        string += "\n"
 
49
        string += "Country: "
 
50
        string += @country.to_s
 
51
        string += "\n"
 
52
        string += "lat: "
 
53
        string += @lat.to_s
 
54
        string += "\n"
 
55
        string += "lon: "
 
56
        string += @long.to_s
 
57
        string += "\n"
 
58
        return string
 
59
    end
 
60
    
 
61
    def to_xml()
 
62
        string = "<Station>\n"
 
63
        string += "    <name>" + @station + "</name>\n"
 
64
        string += "    <Country>" + @country + "</Country>\n"
 
65
        string += "    <id>" + @id.to_s + "</id>\n"
 
66
        string += "    <priority>" + @priority.to_s + "</priority>\n"
 
67
        string += "    <Point>\n"
 
68
        string += "        <coordinates>" + @long.to_s + "," + @lat.to_s + "</coordinates>\n"
 
69
        string += "    </Point>\n"
 
70
        string += "</Station>\n"
 
71
        return string
 
72
    end
 
73
    
 
74
#    def priority=(newPriority)
 
75
#        @priority = newPriority
 
76
#    end
 
77
    
 
78
    def angelDistance(other)
 
79
        begin
 
80
            distance = Math.acos( Math.sin( @lat / 180 * Math::PI ) * Math.sin( other.lat / 180 * Math::PI ) + Math.cos( @lat / 180 * Math::PI ) * Math.cos( other.lat / 180 * Math::PI ) * Math.cos( other.long / 180 * Math::PI - @long / 180 * Math::PI ) ).abs
 
81
        rescue
 
82
            distance = 0
 
83
        end
 
84
        return distance
 
85
    end
 
86
end
 
87
    
 
88
puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 
89
puts "<StationList>"
 
90
stations = Array.new()
 
91
1.upto( 8000 ) do |i| # Should be about 8000
 
92
    station = Station.new( i )
 
93
    if station.parse
 
94
        minDist = 2.0 * Math::PI
 
95
        minDistStation = "none"
 
96
        stations.each do |other|
 
97
            distance = station.angelDistance( other )
 
98
            if distance < minDist
 
99
                minDist = distance
 
100
                minDistStation = other.station
 
101
            end
 
102
        end
 
103
        
 
104
        priority = 32
 
105
        min = Math::PI / 8.0
 
106
        while minDist < min and priority > 0
 
107
            priority -= 1
 
108
            min = min / 1.17
 
109
        end
 
110
        
 
111
        station.priority = priority
 
112
        
 
113
        puts station.to_xml
 
114
        
 
115
        stations = stations << station
 
116
    end
 
117
end
 
118
puts "</StationList>"