Beispiel #1
0
        /// <summary>
        /// Attempts to call the open fileName method for any IDataProvider plugin
        /// that matches the extension on the string.
        /// </summary>
        /// <param name="fileName">A String fileName to attempt to open.</param>
        /// <param name="inRam">A boolean value that if true will attempt to force a load of the data into memory.  This value overrides the property on this DataManager.</param>
        /// <param name="progressHandler">Specifies the progressHandler to receive progress messages.  This value overrides the property on this DataManager.</param>
        public virtual IDataSet OpenFile(string fileName, bool inRam, IProgressHandler progressHandler)
        {
            // To Do: Add Customization that allows users to specify which plugins to use in priority order.

            // First check for the extension in the preferred plugins list

            //Fix by Jiri Kadlec - on Unix and Mac the path is case sensitive
            //on windows it is case insensitive. This check ensures that both
            //*.SHP and *.shp files can be opened on Windows.
            //PlatformID platform = Environment.OSVersion.Platform;
            //if (platform != PlatformID.MacOSX && platform != PlatformID.Unix)
            //{
            //    fileName = fileName.ToLower();
            //}
            // * Removed all "ToLower()" calculations here, since we only want
            // the extension comparison to be converted to lower, not the filename itself.
            string ext = Path.GetExtension(fileName);

            if (ext != null)
            {
                // Fix by Ted Dunsford, moved "ToLower" operation from the previous
                // location on the filename which would mess up the actual filename
                // identification, and only use the lower case for the extent testing.

                ext = ext.ToLower();
                IDataSet result;
                if (PreferredProviders.ContainsKey(ext))
                {
                    result = PreferredProviders[ext].Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                    // if we get here, we found the provider, but it did not succeed in opening the file.
                }

                // Then check the general list of developer specified providers... but not the directory providers

                foreach (IDataProvider dp in DataProviders)
                {
                    if (!GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                    {
                        continue;
                    }
                    // attempt to open with the fileName.
                    dp.ProgressHandler = ProgressHandler;

                    result = dp.Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }
Beispiel #2
0
        /// <summary>
        /// Opens the file making sure it can be returned as an IRaster.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="inRam">Indicates whether the file should be loaded into ram.</param>
        /// <param name="progressHandler">The progress handler.</param>
        /// <returns>The IRaster that was opened.</returns>
        public virtual IRaster OpenFileAsIRaster(string fileName, bool inRam, IProgressHandler progressHandler)
        {
            // CGX
            if (File.Exists(fileName))
            {
                // First check for the extension in the preferred plugins list
                string ext = Path.GetExtension(fileName);
                if (ext != null)
                {
                    ext = ext.ToLower();
                    IRaster result;
                    if (PreferredProviders.ContainsKey(ext))
                    {
                        result = PreferredProviders[ext].Open(fileName) as IRaster;
                        if (result != null)
                        {
                            return(result);
                        }

                        // if we get here, we found the provider, but it did not succeed in opening the file.
                    }

                    // Then check the general list of developer specified providers... but not the directory providers
                    foreach (IDataProvider dp in DataProviders)
                    {
                        if (!GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                        {
                            continue;
                        }

                        // attempt to open with the fileName.
                        dp.ProgressHandler = ProgressHandler;

                        result = dp.Open(fileName) as IRaster;
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
            }
            else
            {
                return(null);
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new image using an appropriate data provider
        /// </summary>
        /// <param name="fileName">The string fileName to open an image for</param>
        /// <param name="width">The integer width in pixels</param>
        /// <param name="height">The integer height in pixels</param>
        /// <param name="inRam">Boolean, true if the entire file should be created in memory</param>
        /// <param name="progHandler">A Progress handler</param>
        /// <param name="bandType">The band color type</param>
        /// <returns>An IImageData interface allowing access to image data</returns>
        public virtual IImageData CreateImage(string fileName, int width, int height, bool inRam,
                                              IProgressHandler progHandler, ImageBandType bandType)
        {
            // First check for the extension in the preferred plugins list
            fileName = fileName.ToLower();
            string ext = Path.GetExtension(fileName);

            if (ext != null)
            {
                IImageData result;
                if (PreferredProviders.ContainsKey(ext))
                {
                    IImageDataProvider rp = PreferredProviders[ext] as IImageDataProvider;
                    if (rp != null)
                    {
                        result = rp.Create(fileName, width, height, inRam, progHandler, bandType);
                        if (result != null)
                        {
                            return(result);
                        }
                    }

                    // if we get here, we found the provider, but it did not succeed in opening the file.
                }

                // Then check the general list of developer specified providers... but not the directory providers

                foreach (IDataProvider dp in DataProviders)
                {
                    if (GetSupportedExtensions(dp.DialogWriteFilter).Contains(ext))
                    {
                        IImageDataProvider rp = dp as IImageDataProvider;
                        if (rp != null)
                        {
                            // attempt to open with the fileName.
                            result = rp.Create(fileName, width, height, inRam, progHandler, bandType);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new raster using the specified raster provider and the Data Manager's Progress Handler,
        /// as well as its LoadInRam property.
        /// </summary>
        /// <param name="name">The fileName of the new file to create.</param>
        /// <param name="driverCode">The string code identifying the driver to use to create the raster.  If no code is specified
        /// the manager will attempt to match the extension with a code specified in the Dialog write filter.  </param>
        /// <param name="xSize">The number of columns in the raster</param>
        /// <param name="ySize">The number of rows in the raster</param>
        /// <param name="numBands">The number of bands in the raster</param>
        /// <param name="dataType">The data type for the raster</param>
        /// <param name="options">Any additional, driver specific options for creation</param>
        /// <returns>An IRaster representing the created raster.</returns>
        public virtual IRaster CreateRaster(string name, string driverCode, int xSize, int ySize, int numBands, Type dataType, string[] options)
        {
            // First check for the extension in the preferred plugins list
            string ext = Path.GetExtension(name).ToLower();

            if (ext != null)
            {
                IRaster result;
                if (PreferredProviders.ContainsKey(ext))
                {
                    IRasterProvider rp = PreferredProviders[ext] as IRasterProvider;
                    if (rp != null)
                    {
                        result = rp.Create(name, driverCode, xSize, ySize, numBands, dataType, options);
                        if (result != null)
                        {
                            return(result);
                        }
                    }

                    // if we get here, we found the provider, but it did not succeed in opening the file.
                }

                // Then check the general list of developer specified providers... but not the directory providers

                foreach (IDataProvider dp in DataProviders)
                {
                    if (GetSupportedExtensions(dp.DialogWriteFilter).Contains(ext))
                    {
                        IRasterProvider rp = dp as IRasterProvider;
                        if (rp != null)
                        {
                            // attempt to open with the fileName.
                            result = rp.Create(name, driverCode, xSize, ySize, numBands, dataType, options);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new class of vector that matches the given fileName.
        /// </summary>
        /// <param name="fileName">The string fileName from which to create a vector.</param>
        /// <param name="featureType">Specifies the type of feature for this vector file</param>
        /// <param name="progHandler">Overrides the default progress handler with the specified progress handler</param>
        /// <returns>An IFeatureSet that allows working with the dataset.</returns>
        /// <exception cref="ArgumentNullException">Raised when fileName is null.</exception>
        /// <exception cref="IOException">Raised when suitable DataProvider not found.</exception>
        public IFeatureSet CreateVector(string fileName, FeatureType featureType, IProgressHandler progHandler)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName", "fileName should be not null");
            }

            // To Do: Add Customization that allows users to specify which plugins to use in priority order.

            // First check for the extension in the preferred plugins list

            var           ext = Path.GetExtension(fileName);
            IDataProvider pdp;

            if (PreferredProviders.TryGetValue(ext, out pdp))
            {
                var vp = pdp as IVectorProvider;
                if (vp != null)
                {
                    var result = vp.CreateNew(fileName, featureType, true, progHandler);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                // if we get here, we found the provider, but it did not succeed in opening the file.
            }

            // Then check the general list of developer specified providers... but not the directory providers
            foreach (var dp in DataProviders.OfType <IVectorProvider>())
            {
                if (GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                {
                    // attempt to open with the fileName.
                    var result = dp.CreateNew(fileName, featureType, true, progHandler);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            throw new IOException(DataStrings.FileTypeNotSupported);
        }
Beispiel #6
0
        /// <summary>
        /// Attempts to call the open fileName method for any IDataProvider plugin that matches the extension on the string.
        /// </summary>
        /// <param name="fileName">A String fileName to attempt to open.</param>
        /// <param name="inRam">A boolean value that if true will attempt to force a load of the data into memory. This value overrides the property on this DataManager.</param>
        /// <param name="progressHandler">Specifies the progressHandler to receive progress messages. This value overrides the property on this DataManager.</param>
        /// <param name="providerName">Name of the provider that should be used for opening. If it is not set or the provider can't open the file, DS takes the first provider that can open the file.</param>
        public virtual IDataSet OpenFile(string fileName, bool inRam, IProgressHandler progressHandler, string providerName = "")
        {
            string ext = Path.GetExtension(fileName);

            if (ext != null)
            {
                // Fix by Ted Dunsford, moved "ToLower" operation from the previous location on the filename
                // which would mess up the actual filename identification, and only use the lower case for the extent testing.
                ext = ext.ToLower();
                IDataSet result;

                if (providerName != "") // if a provider name was given we try to find this provider and use it to open the file
                {
                    var provider = PreferredProviders.FirstOrDefault(kvp => kvp.Value.Name == providerName);
                    if (provider.Value != null)
                    {
                        if (GetSupportedExtensions(provider.Value.DialogReadFilter).Contains(ext))
                        {
                            result = provider.Value.Open(fileName);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                    var dp = DataProviders.FirstOrDefault(kvp => kvp.Name == providerName);
                    if (dp != null)
                    {
                        if (GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                        {
                            result = dp.Open(fileName);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }

                // Check for the extension in the preferred plugins list
                if (PreferredProviders.ContainsKey(ext))
                {
                    result = PreferredProviders[ext].Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                    // if we get here, we found the provider, but it did not succeed in opening the file.
                }

                // Check the general list of developer specified providers... but not the directory providers
                foreach (IDataProvider dp in DataProviders)
                {
                    if (!GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                    {
                        continue;
                    }
                    // attempt to open with the fileName.
                    dp.ProgressHandler = ProgressHandler;

                    result = dp.Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }
Beispiel #7
0
        private string GetFilter <T>(string description, Func <IDataProvider, string> dpFilter) where T : IDataProvider
        {
            var extensions = new List <string>();

            foreach (KeyValuePair <string, IDataProvider> item in PreferredProviders)
            {
                if (item.Value is T)
                {
                    // we don't have to check for uniqueness here because it is enforced by the HashTable
                    extensions.Add(item.Key);
                }
            }

            foreach (IDataProvider dp in DataProviders)
            {
                string[] formats = dpFilter(dp).Split('|');
                // We don't care about the description strings, just the extensions.
                for (int i = 1; i < formats.Length; i += 2)
                {
                    // Multiple extension types are separated by semicolons
                    string[] potentialExtensions = formats[i].Split(';');
                    foreach (string potentialExtension in potentialExtensions)
                    {
                        if (extensions.Contains(potentialExtension) == false)
                        {
                            if (dp is T)
                            {
                                extensions.Add(potentialExtension);
                            }
                        }
                    }
                }
            }
            var result = new StringBuilder();

            // We now have a list of all the file extensions supported
            if (extensions.Count > 0)
            {
                result.Append(String.Format("{0}|", description) + String.Join(";", extensions.ToArray()));
            }

            foreach (KeyValuePair <string, IDataProvider> item in PreferredProviders)
            {
                if (item.Value is T)
                {
                    // we don't have to check for uniqueness here because it is enforced by the HashTable
                    result.AppendFormat("| [{0}] - {1}| {0}", item.Key, item.Value.Name);
                }
            }
            // Now add each of the individual lines, prepended with the provider name
            foreach (IDataProvider dp in DataProviders)
            {
                string[] formats         = dpFilter(dp).Split('|');
                string   potentialFormat = null;
                for (int i = 0; i < formats.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        // For descriptions, prepend the name:
                        potentialFormat = "|" + dp.Name + " - " + formats[i];
                    }
                    else
                    {
                        // don't add this format if it was already added by a "preferred data provider"
                        if (PreferredProviders.ContainsKey(formats[i]) == false)
                        {
                            if (dp is T)
                            {
                                result.Append(potentialFormat);
                                result.Append("|" + formats[i]);
                            }
                        }
                    }
                }
            }
            result.Append("|All Files (*.*) |*.*");
            return(result.ToString());
        }