Esempio n. 1
0
        /// <summary>
        /// Creates a KmlProvider from a Kml stream
        /// </summary>
        /// <param name="stream">The stream to read from</param>
        /// <exception cref="ArgumentNullException"></exception>
        public static KmlProvider FromKml(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var kml = new KmlProvider(KmlFile.Load(stream));

            //kml.InitIds();
            return(kml);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a KmlProvider from a Kmz stream
        /// </summary>
        /// <param name="stream">The stream to read from</param>
        /// <param name="internalFile">The internal file to read</param>
        /// <exception cref="ArgumentNullException"></exception>
        public static KmlProvider FromKmz(Stream stream, string internalFile = null)
        {
            var kmz = KmzFile.Open(stream);

            if (string.IsNullOrEmpty(internalFile))
            {
                var kml = new KmlProvider(kmz.GetDefaultKmlFile());
                kml.kmzFile = kmz;
                //kml.InitIds();
                return(kml);
            }

            //NOTE:DON'T KNOW IF THIS IS CORRECT!
            using (var ms = new MemoryStream(kmz.ReadFile(internalFile)))
            {
                var kml = new KmlProvider(KmlFile.Load(ms));
                //kml.InitIds();
                return(kml);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Attempt to append all styles from an external kml/kmz resource  <param name="url"></param> to this KmlProvider's styles
        /// </summary>
        /// <param name="url"></param>
        private void LoadExternalStyles(Uri url)
        {
            try
            {
                var request = WebRequest.Create(url);
                using (var response = request.GetResponse())
                {
                    // not as efficient as reading the styles directly, but simplifies dealing with KML or KMZ.
                    // Also, external style files typically contain styles only (ie no  geometries).
                    KmlProvider prov = null;
                    if (url.ToString().ToLower().EndsWith(".kml"))
                    {
                        prov = FromKml(response.GetResponseStream(), true);
                    }
                    else if (url.ToString().ToLower().EndsWith(".kmz"))
                    {
                        prov = FromKmz(response.GetResponseStream(), null, true);
                    }

                    foreach (var key in prov._kmlStyles.Keys)
                    {
                        if (!_kmlStyles.ContainsKey(key))
                        {
                            _kmlStyles.Add(key, prov._kmlStyles[key]);
                        }
                    }

                    foreach (var key in prov._styleMaps.Keys)
                    {
                        if (!_styleMaps.ContainsKey(key))
                        {
                            _styleMaps.Add(key, prov._styleMaps[key]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }