Description:
This is an app that will display the current doppler radar picture for any given zip code. The images come from the Weather Channel web site. The size of the image can be toggled between large and small versions.
The program has two methods which do all the work. The first is GetRadarId(). This method takes the requested zip code and finds the three letter identifier for the image used by the Weather Channel to name the jpg files for the doppler images. An HttpWebRequest/Response is used to get the raw html. Then a StreamReader reads the html one line at a time to find the line that contains the required info.
private void GetRadarId(string zip)
{
try{
string url = "http://www.weather.com/weather/local/"+zip;
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create
(url);
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
StreamReader sr = new StreamReader(str);
string line = " ";
while(line.IndexOf("mapURLPre") == -1){
line = sr.ReadLine();
}
string[] tokens = line.Split(new char[] {'_'});
radarid = tokens[2].Substring(0,3);
}
catch(Exception ex) {}
}
After the radar id is found, GetDoppler() downloads the appropriate jpg from the website. The picture box image is created using the static Image.FromStream() method which saves you from having to create a file.
public void GetDoppler()
{
try{
string url = "http://maps.weather.com/web/radar/us_"+radarid+"_closeradar_"+imagesize+"_usen.jpg";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
pictureBox1.Image = Image.FromStream(str);
byte[] inBuf = new byte[100000];
int bytesToRead = (int) inBuf.Length;
int bytesRead = 0;
while (bytesToRead > 0) {
int n = str.Read(inBuf, bytesRead,bytesToRead);
if (n==0)
break;
bytesRead += n;
bytesToRead -= n;
}
this.Text = "Doppler for: " + currentzip;
}
catch(Exception ex){Console.WriteLine(ex.ToString());}
}
This should be used for demonstration purposes only. The Terms of Use on the Weather Channel website are quite clear about reproduction and redistribution of their copyrighted images.
Requirement:
Requires Beta 2 .NET SDK
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Shaun WalkerPosted Apr 12, 2011, 1:52 PM
It breaks in the Zip portion of the project and refuses your dispose command (part of the new system). Once this is resolved the program will run but never displays a the new map despite updating the zip listed in the title. Also states that the variable 'ex' is declared but never used.
Sam HobbsPosted Apr 1, 2011, 6:22 PM
I think it would be much better to use the DOM to navigate the web page and find the image or whatever by finding the link; the "A" tag or whatever. I tried debugging this thing and it appears to be failing yet the catch block ignores the problem. Catch blocks such as in this sample should not be allowed in sample code. I don't have time to look at it anymore but it might be that it is reading past the end of the data.
MikePosted Dec 18, 2009, 5:16 PM
this code is somewhat out of date, but only to fetch the radarID. Replace your GetRadarId method with the one below.... private void GetRadarId(string zip) { try { string url = "http://www.weather.com/weather/local/" + zip; HttpWebRequest wr = (HttpWebRequest)WebRequest.Create (url); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); Stream str = ws.GetResponseStream(); StreamReader sr = new StreamReader(str); string line = " "; while (line.IndexOf("mw_currArray[0]") == -1) { line = sr.ReadLine(); } string[] tokens = line.Split(new char[] { '_' }); radarid = tokens[4]; } catch (Exception ex) { } }