/// <summary> /// Loads a KML file /// </summary> private void LoadKMLFile() { if (KMLPath == null || m_KMLIcons == null) { return; } Cleanup(); m_parser.Cleanup(); WaitMessage waitMessage = new WaitMessage(); m_KMLIcons.Add(waitMessage); // Create a reader to read the file try { string kml = m_loader.LoadKML(KMLPath); KMLPath = m_loader.RealKMLPath; if (kml != null) { try { // Load the actual kml data m_parser.ReadKML(kml, m_KMLIcons, KMLPath); } catch (Exception ex) { Log.Write(Log.Levels.Error, "KMLImporter: " + ex.ToString()); MessageBox.Show( String.Format(CultureInfo.InvariantCulture, "Error loading KML file '{0}':\n\n{1}", KMLPath, ex.ToString()), "KMLImporter error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, true ? MessageBoxOptions.RtlReading : MessageBoxOptions.ServiceNotification); } } } catch (Exception ex) // Catch error if stream reader failed { Log.Write(Log.Levels.Error, "KMLImporter: " + ex.ToString()); MessageBox.Show( String.Format(CultureInfo.InvariantCulture, "Error opening KML file '{0}':\n\n{1}", KMLPath, ex.ToString()), "KMLImporter error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, true ? MessageBoxOptions.RtlReading : MessageBoxOptions.ServiceNotification); } // Cleanup m_KMLIcons.Remove(waitMessage); KMLPath = null; }
/// <summary> /// Downloads a KML/KMZ file from the given URL /// </summary> private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (bUpdating) { return; } bUpdating = true; try { if (!m_firedStartup || (layer != null && layer.IsOn)) { string fullurl = url; if (sender == viewTimer) { if (!bViewStopped) { if (DrawArgs.Camera.ViewMatrix != lastView) { lastView = DrawArgs.Camera.ViewMatrix; bUpdating = false; return; } bViewStopped = true; } else { if (DrawArgs.Camera.ViewMatrix != lastView) { lastView = DrawArgs.Camera.ViewMatrix; bViewStopped = false; } bUpdating = false; return; } fullurl += (fullurl.IndexOf('?') == -1 ? "?" : "&") + GetBBox(); } string saveFile = Path.GetFileName(Uri.EscapeDataString(url)); if (saveFile == null || saveFile.Length == 0) { saveFile = "temp.kml"; } saveFile = Path.Combine(KMLParser.KmlDirectory + "\\temp\\", saveFile); FileInfo saveFileInfo = new FileInfo(saveFile); if (!saveFileInfo.Directory.Exists) { saveFileInfo.Directory.Create(); } // Offline check if (World.Settings.WorkOffline) { throw new Exception("Offline mode active."); } WebDownload myClient = new WebDownload(fullurl); myClient.DownloadFile(saveFile); // Extract the file if it is a kmz file string kmlFile = saveFile; // Create a reader to read the file KMLLoader loader = new KMLLoader(); if (Path.GetExtension(saveFile) == ".kmz") { bool bError = false; kmlFile = loader.ExtractKMZ(saveFile, out bError); if (bError) { return; } } // Read all data from the reader string kml = loader.LoadKML(kmlFile); if (kml != null) { try { // Load the actual kml data owner.ReadKML(kml, layer, kmlFile); } catch (Exception ex) { Log.Write(Log.Levels.Error, "KMLParser: " + ex.ToString()); MessageBox.Show( String.Format(CultureInfo.InvariantCulture, "Error loading KML file '{0}':\n\n{1}", kmlFile, ex.ToString()), "KMLParser error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); } } m_firedStartup = true; } } catch (Exception ex) { Log.Write(Log.Levels.Error, "KMLParser: " + ex.ToString()); } bUpdating = false; }