File indexing completed on 2024-05-12 15:31:19

0001 #!/usr/bin/ruby
0002 
0003 # SPDX-License-Identifier: LGPL-2.1-or-later
0004 #
0005 # SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0006 #
0007 
0008 require 'net/http'
0009 require 'rubygems'
0010 require 'xmlsimple'
0011 require 'pp'
0012 
0013 
0014 class Station
0015     attr_reader :long, :lat, :priority, :station
0016     attr_writer :priority
0017     def initialize(number)
0018         @id = number.to_s
0019         @url = 'http://newsrss.bbc.co.uk/weather/forecast/' + @id + '/ObservationsRSS.xml'
0020     end
0021     
0022     def parse()
0023         xmlData = Net::HTTP.get_response(URI.parse(@url)).body
0024         begin
0025             data = XmlSimple.xml_in(xmlData, { 'KeyAttr' => 'name' })
0026         rescue REXML::ParseException
0027             return false
0028         end
0029         
0030         channel = (data["channel"])[0]
0031         
0032         name = channel["title"].to_s.sub( /BBC - Weather Centre - Latest Observations for /, "" )
0033         nameList = name.split( /, / )
0034         @station = nameList[0].to_s
0035         @country = nameList[1].to_s
0036         
0037         item = channel["item"][0]
0038         @lat = item['lat'].to_s.to_f
0039         @long = item['long'].to_s.to_f
0040         if @lat == "N/A" or @long == "N/A"
0041             return false
0042         end
0043         
0044         return true
0045     end
0046         
0047     def to_s()
0048         string = "Id: "
0049         string += @id.to_s
0050         string += "\n"
0051         string += "Station: "
0052         string += @station.to_s
0053         string += "\n"
0054         string += "Country: "
0055         string += @country.to_s
0056         string += "\n"
0057         string += "lat: "
0058         string += @lat.to_s
0059         string += "\n"
0060         string += "lon: "
0061         string += @long.to_s
0062         string += "\n"
0063         return string
0064     end
0065     
0066     def to_xml()
0067         string = "<Station>\n"
0068         string += "    <name>" + @station + "</name>\n"
0069         string += "    <Country>" + @country + "</Country>\n"
0070         string += "    <id>" + @id.to_s + "</id>\n"
0071         string += "    <priority>" + @priority.to_s + "</priority>\n"
0072         string += "    <Point>\n"
0073         string += "        <coordinates>" + @long.to_s + "," + @lat.to_s + "</coordinates>\n"
0074         string += "    </Point>\n"
0075         string += "</Station>\n"
0076         return string
0077     end
0078     
0079 #    def priority=(newPriority)
0080 #        @priority = newPriority
0081 #    end
0082     
0083     def angelDistance(other)
0084         begin
0085             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
0086         rescue
0087             distance = 0
0088         end
0089         return distance
0090     end
0091 end
0092     
0093 puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
0094 puts "<StationList>"
0095 stations = Array.new()
0096 1.upto( 8000 ) do |i| # Should be about 8000
0097     station = Station.new( i )
0098     if station.parse
0099         minDist = 2.0 * Math::PI
0100         minDistStation = "none"
0101         stations.each do |other|
0102             distance = station.angelDistance( other )
0103             if distance < minDist
0104                 minDist = distance
0105                 minDistStation = other.station
0106             end
0107         end
0108         
0109         priority = 32
0110         min = Math::PI / 8.0
0111         while minDist < min and priority > 0
0112             priority -= 1
0113             min = min / 1.17
0114         end
0115         
0116         station.priority = priority
0117         
0118         puts station.to_xml
0119         
0120         stations = stations << station
0121     end
0122 end
0123 puts "</StationList>"