Weather application
It is an application which gives you the details of the present weather such as temperature conditions, wind speed, humidity etc. It also provides a brief forecast for the weather ahead.
How to proceed
Prerequisite :
Basics for making form based application in C# .
Step 1 :
Get through any API, which provides weather details either in JSON or XML format. (for ex Yahoo Weather API ,OpenWeatherAPI etc).
This application uses the yahoo weather API. Here we are retrieving an XML sheet from yahoo demanding the weather and conditions . XML sheet of Yahoo weather can be accessed though the following link
http://weather.yahooapis.com/forecastrss?w=2294864
where 2294864 is woeid number of the city assigned by Yahoo. You can find out woeid of your own city from the woeid lookup . In this project I am using the same link as give above.
Step 2 :
Write program to read the required details from JSON or XML file and store the values for the further use in your application. In this application we are using XMLReader class of C# for collecting the information from the XML sheet.
Step 3 :
Retrive the stored data and display it on your GUI(Graphical User Interface) by formatting.
Project and Code
Set Up GUI
You may prefer developing your own GUI. Given below is a demo GUI layout which is being used by my program. If you want to copy the code then you should develop the same GUI as given in the layout.
Items in GUI
1. comboBox1 - for selecting city from a list or to enter woeid.
2. button1 - for refreshing the screen.
3. button2 - for closing the application.
4. label2, label20 - for displaying name of city and country.
5. pictureBox1 - displaying icon for present weather condition.
6. label1, label10, label9 - displaying present temperature.
7. label3 to label8 - displaying various parameters of weather
8. pictureBox1, pictureBox3 to pictureBox5 - displaying icons for weather forecast of consecutive days
9. label12 to label27 - displaying various parameters for weather condition of consecutive days
10. label11 - displaying the last updated time.
Extra Include Files
Icons
You may download any set of icons which you want to display in your GUI. In this project I am using VClouds Weather Icons set form Deviantart. You may also download the same. Note the name of the icons must be in accordance to the weather codes (Alternative Link).
Weather codes are different weather conditions assigned as a number which is given by Yahoo in the XML sheet according to the weather condition , such as 32 for sunny day 36 for hot day. Like this there are 47 weather condition codes and 3200 is returned when data is not available and you should have 47 icons for each weather condition + 1 icon for not available :P.
Final Code
//In the given program ../Pics/ is the folder where weather icons are stored
Demo Screen Shot of the completed Application. You can customize GUI according to your own choice.
For queries feel free to comment or mail me.
It is an application which gives you the details of the present weather such as temperature conditions, wind speed, humidity etc. It also provides a brief forecast for the weather ahead.
How to proceed
Prerequisite :
Basics for making form based application in C# .
Step 1 :
Get through any API, which provides weather details either in JSON or XML format. (for ex Yahoo Weather API ,OpenWeatherAPI etc).
This application uses the yahoo weather API. Here we are retrieving an XML sheet from yahoo demanding the weather and conditions . XML sheet of Yahoo weather can be accessed though the following link
http://weather.yahooapis.com/forecastrss?w=2294864
where 2294864 is woeid number of the city assigned by Yahoo. You can find out woeid of your own city from the woeid lookup . In this project I am using the same link as give above.
Step 2 :
Write program to read the required details from JSON or XML file and store the values for the further use in your application. In this application we are using XMLReader class of C# for collecting the information from the XML sheet.
String st = String.Format(@"http://weather.yahooapis.com/forecastrss?w=2294864"); //URL string. In this string 2294864 is woeid of city.
XmlDocument wData = new XmlDocument(); //creating new XML document
wData.Load(st); //laoding XML file from URL
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable); // loading namespace manager
manager.AddNamespace("yweather", @"http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); //selecting a particular node
string temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition",manager).Attributes["temp"].Value; //selecting an Attribue
Step 3 :
Retrive the stored data and display it on your GUI(Graphical User Interface) by formatting.
Project and Code
Set Up GUI
You may prefer developing your own GUI. Given below is a demo GUI layout which is being used by my program. If you want to copy the code then you should develop the same GUI as given in the layout.
Items in GUI
1. comboBox1 - for selecting city from a list or to enter woeid.
2. button1 - for refreshing the screen.
3. button2 - for closing the application.
4. label2, label20 - for displaying name of city and country.
5. pictureBox1 - displaying icon for present weather condition.
6. label1, label10, label9 - displaying present temperature.
7. label3 to label8 - displaying various parameters of weather
8. pictureBox1, pictureBox3 to pictureBox5 - displaying icons for weather forecast of consecutive days
9. label12 to label27 - displaying various parameters for weather condition of consecutive days
10. label11 - displaying the last updated time.
Extra Include Files
using System.Web; //for connecting to web using System.Xml.Linq; //for XML using System.Xml;
Icons
You may download any set of icons which you want to display in your GUI. In this project I am using VClouds Weather Icons set form Deviantart. You may also download the same. Note the name of the icons must be in accordance to the weather codes (Alternative Link).
Weather codes are different weather conditions assigned as a number which is given by Yahoo in the XML sheet according to the weather condition , such as 32 for sunny day 36 for hot day. Like this there are 47 weather condition codes and 3200 is returned when data is not available and you should have 47 icons for each weather condition + 1 icon for not available :P.
Final Code
//In the given program ../Pics/ is the folder where weather icons are stored
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Web; using System.Xml.Linq; using System.Xml; namespace WeatherApp { public partial class Form1 : Form { string Temperature; string Condition; string Humidity; string Windspeed; string Town; string Region; string Country; string Code; string Local; string woeid; string LastUpdate; string[] next_day=new string[10]; string[] next_cond = new string[10]; string[] next_condt = new string[10]; string[] next_high = new string[10]; string[] next_low = new string[10]; public Form1() { InitializeComponent(); woeid = "2294864"; getWeather(); label1.Text = Temperature; label2.Text = Town+", "+Region; label6.Text = Condition; label7.Text = Humidity; label8.Text = Windspeed; label10.Text = string.Format("\u00B0")+ "F"; label12.Text = next_day[1]; label15.Text = next_day[2]; label17.Text = next_day[3]; label19.Text = next_day[4]; label20.Text = Country; label13.Text = next_condt[1]; label14.Text = next_condt[2]; label16.Text = next_condt[3]; label18.Text = next_condt[4]; label21.Text = next_high[1] + string.Format("\u00B0") + "F"; label24.Text = next_high[2] + string.Format("\u00B0") + "F"; label26.Text = next_high[3] + string.Format("\u00B0") + "F"; label28.Text = next_high[4] + string.Format("\u00B0") + "F"; label22.Text = next_low[1] + string.Format("\u00B0") + "F"; label23.Text = next_low[2] + string.Format("\u00B0") + "F"; label25.Text = next_low[3] + string.Format("\u00B0") + "F"; label27.Text = next_low[4] + string.Format("\u00B0") + "F"; setIcons(); setIcon(); comboBox1.Items.Add("Berhampur"); comboBox1.Items.Add("Bhubaneshwar"); comboBox1.Items.Add("Delhi"); comboBox1.Items.Add("Kolkata"); comboBox1.Items.Add("Mumbai"); comboBox1.Items.Add("Chennai"); comboBox1.Items.Add("Rourkela"); comboBox1.Items.Add("New York"); comboBox1.Items.Add("Los Angeles"); comboBox1.Items.Add("London"); comboBox1.Items.Add("Paris"); } private void setIcons() { pictureBox1.Image = Image.FromFile(getString(next_cond[1])); pictureBox3.Image = Image.FromFile(getString(next_cond[2])); pictureBox4.Image = Image.FromFile(getString(next_cond[3])); pictureBox5.Image = Image.FromFile(getString(next_cond[4])); } private string getString(string code) { if(code.Equals("3200")) { return "../Pics/na.png"; } else { return "../Pics/" + code + ".png"; } } private void setIcon() { if(Code.Equals("3200")) { pictureBox2.Image = Image.FromFile("../Pics/na.png"); } else { string st = "../Pics/"+Code+".png"; pictureBox2.Image = Image.FromFile(st); } } private void getWeather() { String st = String.Format(@"http://weather.yahooapis.com/forecastrss?w="); st = st + woeid; XmlDocument wData = new XmlDocument(); wData.Load(st); XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable); manager.AddNamespace("yweather", @"http://xml.weather.yahoo.com/ns/rss/1.0"); XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition",manager).Attributes["temp"].Value; Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition",manager).Attributes["text"].Value; Code = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["code"].Value; Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value; Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value; Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value; Region = channel.SelectSingleNode("yweather:location", manager).Attributes["region"].Value; Country = channel.SelectSingleNode("yweather:location", manager).Attributes["country"].Value; Local = channel.SelectSingleNode("item").SelectSingleNode("title", manager).InnerXml; LastUpdate = channel.SelectSingleNode("item").SelectSingleNode("pubDate", manager).InnerXml; XmlNodeList forecast = channel.SelectSingleNode("item").SelectNodes("yweather:forecast", manager); for(int i=0;i<forecast.Count;i++) { next_day[i] = forecast[i].Attributes["day"].Value; next_cond[i] = forecast[i].Attributes["code"].Value; next_condt[i] = forecast[i].Attributes["text"].Value; next_high[i] = forecast[i].Attributes["high"].Value; next_low[i] = forecast[i].Attributes["low"].Value; } label11.Text = "Last Updated on : " + LastUpdate ; double x = (Double.Parse(Temperature)-32) * 5.0/9.0; x = (int)x; label9.Text = x.ToString() +" "+ string.Format("\u00B0") + "C"; } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } private void button1_Click(object sender, EventArgs e) { switch(comboBox1.Text) { case "Berhampur": woeid = "2295220"; break; case "Bhubaneshwar": woeid = "2294941"; break; case "Delhi": woeid = "2295019"; break; case "Chennai": woeid = "2295424"; break; case "Kolkata": woeid = "2295386"; break; case "Rourkela": woeid = "2294864"; break; case "Los Angeles": woeid = "2442047"; break; case "London": woeid = "44418"; break; case "Paris": woeid = "615702"; break; case "New York": woeid = "2459115"; break; default: woeid = comboBox1.Text; break; } getWeather(); label1.Text = Temperature; label2.Text = Town + ", " + Region; label6.Text = Condition; label7.Text = Humidity; label8.Text = Windspeed; label10.Text = string.Format("\u00B0") + "F"; label12.Text = next_day[1]; label15.Text = next_day[2]; label17.Text = next_day[3]; label19.Text = next_day[4]; label20.Text = Country; label13.Text = next_condt[1]; label14.Text = next_condt[2]; label16.Text = next_condt[3]; label18.Text = next_condt[4]; setIcons(); setIcon(); } } }
Demo Screen Shot of the completed Application. You can customize GUI according to your own choice.
For queries feel free to comment or mail me.



