ARTICLE

Doppler Radar

Posted by Bill Farley Articles | Internet & Web September 14, 2001
This is an app that will display the current doppler radar picture for any given zip code.
Reader Level:
Download Files:
 

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

Login to add your contents and source code to this article
post comment
     

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.

Posted by Shaun Walker Apr 12, 2011

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.

Posted by Sam Hobbs Apr 01, 2011

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) { }

}


Posted by Mike Dec 18, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts