home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 119 / cdrom119.iso / internet / wwind / World_Wind_1.3.5_Full.exe / Plugins / Earthquake2 / Earthquake_2.0.2.1.cs < prev    next >
Encoding:
Text File  |  2006-05-08  |  7.1 KB  |  239 lines

  1. //----------------------------------------------------------------------------
  2. // NAME: RSS Earthquake
  3. // DESCRIPTION: Earthquake 2.0.2 -- This version uses the USGS 7day Earthquake RSS feed.
  4. // DEVELOPER: Chad Zimmerman
  5. // WEBSITE: http://www.alteviltech.com/
  6. //----------------------------------------------------------------------------
  7. using System;
  8. using System.Globalization;
  9. using System.IO;
  10. using WorldWind;
  11. using WorldWind.Renderable;
  12. using WorldWind.Net;
  13. using System.Windows.Forms;
  14. using System.Xml;
  15. using System.Xml.XPath;
  16.  
  17. namespace XMLEarthquake.Plugin
  18. {
  19.     /// <summary>
  20.     /// Tutorial1
  21.     /// </summary>
  22.     public class Earthquake : WorldWind.PluginEngine.Plugin
  23.     {
  24.         System.Windows.Forms.MenuItem menuItem;
  25.         //System.Windows.Forms.MenuItem menuItem2;
  26.         
  27.         static string EarthquakeXmlUri = "http://earthquake.usgs.gov/eqcenter/recenteqsww/catalogs/eqs7day-M2.5.xml";
  28.         Icons EQIcons = new Icons("EarthQuake Icons");
  29.  
  30.         public override void Load()
  31.         {
  32.             //menuItem = new System.Windows.Forms.MenuItem();
  33.             //menuItem.Text = "Refresh Earthquake 7 Day List";
  34.             //menuItem.Click += new System.EventHandler(menuItem_Click);
  35.             //ParentApplication.PluginsMenu.MenuItems.Add(menuItem);  
  36.  
  37.             //m_Application.WorldWindow.CurrentWorld.RenderableObjects.Add(EQIcons);
  38.  
  39.             //base.Load();
  40.             
  41.             if (ParentApplication.WorldWindow.CurrentWorld != null && ParentApplication.WorldWindow.CurrentWorld.Name.IndexOf("Earth") >= 0)
  42.             {
  43.                 menuItem = new System.Windows.Forms.MenuItem();
  44.                 menuItem.Text = "Refresh Earthquake 7 Day List";
  45.                 menuItem.Click += new System.EventHandler(menuItem_Click);
  46.                 ParentApplication.PluginsMenu.MenuItems.Add(menuItem);
  47.  
  48.                 EQIcons.IsOn = false;
  49.                 m_Application.WorldWindow.CurrentWorld.RenderableObjects.Add(EQIcons);
  50.  
  51.                 base.Load();
  52.             }
  53.         }
  54.  
  55.         public override void Unload()
  56.         {
  57.             // Clean up, remove menu item
  58.             ParentApplication.PluginsMenu.MenuItems.Remove(menuItem);
  59.  
  60.             m_Application.WorldWindow.CurrentWorld.RenderableObjects.Remove(EQIcons);
  61.  
  62.             base.Unload ();
  63.         }
  64.  
  65.         void menuItem_Click(object sender, EventArgs e)
  66.         {
  67.             EQIcons.IsOn = true;
  68.             
  69.             CultureInfo icy = CultureInfo.InvariantCulture;
  70.             
  71.             EarthquakeEntry[] earthquakeList = GetEarthquakeList(EarthquakeXmlUri);
  72.  
  73.             foreach(EarthquakeEntry entry in earthquakeList)
  74.             {
  75.                 float magnitude = float.Parse(entry.subjects[0], icy); // Earthquake magnitude
  76.                 string quakedepth = entry.subjects[2]; //depth
  77.                 DateTime QuakeDate = System.DateTime.Parse(entry.description, icy); // Show the entry date / time
  78.                 
  79.                 
  80.                 DateTime CurrentDate = DateTime.Now.AddDays(-1); //DateTime.Now;
  81.                 DateTime PastDay = DateTime.Now.AddDays(-1);
  82.                 DateTime PastWeek = DateTime.Now.AddDays(-3);
  83.                 
  84.                 //MessageBox.Show("CurrentDate: "+CurrentDate);
  85.                 
  86.                 string bitmapSize = "small"; // small, medium, big, large
  87.                 string bitmapRange = "CD";  // CD, PD, PW
  88.                 
  89.                 // Set the icon color    
  90.                 if(QuakeDate >= CurrentDate)
  91.                     bitmapRange = "CD";
  92.                 else if((QuakeDate == PastDay) || (QuakeDate >= PastWeek))
  93.                     bitmapRange = "PD";
  94.                 else
  95.                     bitmapRange = "PW";
  96.                 
  97.                 // Set the icon size
  98.                 if(magnitude  <= 3) // small
  99.                     bitmapSize = "small";
  100.                 else if((magnitude == 4) || (magnitude <= 5)) // medium
  101.                     bitmapSize = "medium";
  102.                 else if((magnitude == 6) || (magnitude <= 7)) // large
  103.                     bitmapSize = "big";
  104.                 else
  105.                     bitmapSize = "large";
  106.  
  107.                 
  108.                 string bitmapFileName = String.Format("{0}_{1}.png", bitmapRange, bitmapSize);
  109.                 string bitmapPath = Path.Combine(PluginDirectory, bitmapFileName);
  110.                 
  111.                 
  112.                 CreateIcon(entry.title +"\nDepth: "+ quakedepth +"\n"+ entry.description, "Click the icon to visit related page", (float)entry.latitude, (float)entry.longitude, 0, entry.link, bitmapPath);
  113.             }
  114.         }
  115.  
  116.         public static EarthquakeEntry[] GetEarthquakeList(string uri)
  117.         {
  118.             XPathDocument docNav = new XPathDocument(uri);
  119.             XPathNavigator nav = docNav.CreateNavigator();
  120.             XPathNodeIterator itemIter = nav.Select("/rss/channel/item");
  121.  
  122.             XmlNamespaceManager context = new XmlNamespaceManager(nav.NameTable);
  123.             context.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
  124.             context.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
  125.             context.AddNamespace("feedburner", "http://rssnamespace.org/feedburner/ext/1.0");
  126.  
  127.             System.Collections.ArrayList itemList = new System.Collections.ArrayList();
  128.             
  129.             CultureInfo icy = CultureInfo.InvariantCulture;
  130.  
  131.             while(itemIter.MoveNext())
  132.             {
  133.                 try
  134.                 {
  135.                     EarthquakeEntry curEntry = new EarthquakeEntry();
  136.  
  137.                     System.Collections.ArrayList subjectList = new System.Collections.ArrayList();
  138.  
  139.                     XPathNodeIterator iter = itemIter.Current.Select("*");
  140.  
  141.                     while(iter.MoveNext())
  142.                     {
  143.                         switch(iter.Current.Name)
  144.                         {
  145.                             case "title":
  146.                                 curEntry.title = iter.Current.Value;
  147.                                 break;
  148.                             case "description":
  149.                                 curEntry.description = iter.Current.Value;
  150.                                 break;
  151.                             case "link":
  152.                                 curEntry.link = iter.Current.Value;
  153.                                 break;
  154.                             case "geo:lat":
  155.                                 curEntry.latitude = double.Parse(iter.Current.Value, icy);
  156.                                 break;
  157.                             case "geo:long":
  158.                                 curEntry.longitude = double.Parse(iter.Current.Value, icy);
  159.                                 break;
  160.                             case "dc:subject":
  161.                                 subjectList.Add(iter.Current.Value);
  162.                                 break;
  163.                             case "feedburner:origLink":
  164.                                 curEntry.origLink = iter.Current.Value;
  165.                                 break;
  166.  
  167.                         }
  168.                     }
  169.  
  170.                     curEntry.subjects = (string[])subjectList.ToArray(typeof(string));
  171.                     itemList.Add(curEntry);
  172.                 }
  173.                 
  174.                 catch(Exception)
  175.                 {
  176.                     //ignore bad data for now, keep processing rest of items
  177.                 }
  178.             }
  179.  
  180.             return (EarthquakeEntry[])itemList.ToArray(typeof(EarthquakeEntry));
  181.         }
  182.  
  183.         static string getInnerTextFromFirstChild(XPathNodeIterator iter)
  184.         {
  185.             if(iter.Count == 0)
  186.             {
  187.                 return null;
  188.             }
  189.             else
  190.             {
  191.                 iter.MoveNext();
  192.                 return iter.Current.Value;
  193.             }
  194.         }
  195.  
  196.         /// <summary>
  197.         /// Creates an icon on the globe
  198.         /// </summary>
  199.         /// <param name="Name">The name of the item</param>
  200.         /// <param name="Desc">The description</param>
  201.         /// <param name="Lat">The latitude for the icon</param>
  202.         /// <param name="Lon">The longitude for the icon</param>
  203.         /// <param name="URL">The URL to open when the icon is clicked</param>
  204.         /// <param name="bitmapPath">The path to the bitmap to show</param>
  205.         private void CreateIcon(string Name, string Desc, float Lat, float Lon, int Alt, string URL, string bitmapPath)
  206.         {
  207.             int bitmapSize = 64;
  208.  
  209.             // Create the icon and set initial settings
  210.             Icon ic = new Icon(
  211.                 Name,                                        // name
  212.                 Lat,                                        // latitude
  213.                 Lon,                                        // longitude
  214.                 (float)Alt);                                // altitude
  215.  
  216.             // Set optional icon settings
  217.             ic.Description = Desc;
  218.             ic.ClickableActionURL = URL;
  219.             ic.TextureFileName = bitmapPath;
  220.             ic.Height = ic.Width = bitmapSize;
  221.             ic.RenderPriority = RenderPriority.Icons;
  222.  
  223.             // Add the icon to the layer
  224.             EQIcons.AddIcon(ic);
  225.         }
  226.  
  227.         public struct EarthquakeEntry
  228.         {
  229.             public string title;
  230.             public string description;
  231.             public string link;
  232.             public double latitude;
  233.             public double longitude;
  234.             public string[] subjects;
  235.             public string origLink;
  236.         }
  237.     }
  238. }
  239.