Ejemplo n.º 1
0
 // Load saved items from the Registry.
 private void LoadFiles()
 {
     // Reload items from the registry.
     for (int i = 0; i < _NumFiles; i++)
     {
         string file_name = (string)RegistryTools.GetSetting(
             _ApplicationName, "FilePath" + i.ToString(), "");
         if (file_name != "")
         {
             FileInfos.Add(new FileInfo(file_name));
         }
     }
 }
Ejemplo n.º 2
0
        // Save the current items in the Registry.
        private void SaveFiles()
        {
            // Delete the saved entries.
            for (int i = 0; i < _NumFiles; i++)
            {
                RegistryTools.DeleteSetting(_ApplicationName, "FilePath" + i.ToString());
            }

            // Save the current entries.
            int index = 0;

            foreach (FileInfo file_info in FileInfos)
            {
                RegistryTools.SaveSetting(_ApplicationName,
                                          "FilePath" + index.ToString(), file_info.FullName);
                index++;
            }
        }
Ejemplo n.º 3
0
        public DialogResult OpenFileDialog()
        {
            string         filename = "";
            OpenFileDialog ofd      = new OpenFileDialog
            {
                Title            = "Open a GIS layer",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            };
            var initialDirectory = RegistryTools.GetSetting("FAD3", "LastOpenedLayerDirectory", "");

            if (initialDirectory.ToString().Length > 0)
            {
                ofd.InitialDirectory = initialDirectory.ToString();
            }

            ofd.Filter = "ESRI Shapefile (shp)|*.shp|" +
                         "KML files (kml)|*.kml|" +
                         "Georeferenced raster files (jpg, tiff,bmp)|*.jpg;*.tif;*.tiff;*.bmp|" +
                         "Other files |*.*)";
            ofd.FilterIndex = 1;

            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK && ofd.FileName.Length > 0)
            {
                filename = ofd.FileName;

                var(success, errMsg) = _mapLayersHandler.FileOpenHandler(filename);
                if (!success)
                {
                    MessageBox.Show(errMsg, "Error in opening file", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dr = DialogResult.Cancel;
                }
            }
            return(dr);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the opening of map layer files from a file open dialog
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public (bool success, string errMsg) FileOpenHandler(string fileName, string layerName = "", bool reproject = false)
        {
            var success = false;
            var errMsg  = "";
            var fm      = new FileManager();

            if (!fm.get_IsSupported(fileName))
            {
                errMsg = "Datasource isn't supported by MapWinGIS";
            }
            else
            {
                var obj = fm.Open(fileName, tkFileOpenStrategy.fosAutoDetect, null);
                if (fm.LastOpenIsSuccess)
                {
                    if (fm.LastOpenStrategy == tkFileOpenStrategy.fosVectorLayer)
                    {
                        var shapefile = obj as Shapefile;
                        success = shapefile != null;
                        if (success)
                        {
                            if (reproject)
                            {
                                int reprojectCount = 0;
                                var sf             = shapefile.Reproject(MapControl.GeoProjection, reprojectCount);
                                if (reprojectCount > 0 || sf.NumShapes > 0)
                                {
                                    shapefile = sf;
                                }
                            }
                            if (AddLayer(shapefile, layerName) < 0)
                            {
                                success = false;
                                errMsg  = "Failed to add layer to map";
                            }
                        }
                    }
                    else if (fm.LastOpenStrategy == tkFileOpenStrategy.fosRgbImage)
                    {
                        var folderPath = Path.GetDirectoryName(fileName);
                        var file       = Path.GetFileNameWithoutExtension(fileName);
                        var ext        = Path.GetExtension(fileName);
                        var prjFile    = $@"{folderPath}\{file}.prj";
                        var worldFile  = $@"{folderPath}\{file}.{WorldfileExtension(ext)}";

                        if (File.Exists(prjFile) || File.Exists(worldFile))
                        {
                            var image = obj as MapWinGIS.Image;
                            success = image != null;
                            if (success)
                            {
                                if (AddLayer(image) < 0)
                                {
                                    success = false;
                                    errMsg  = "Failed to add layer to map";
                                }
                            }
                        }
                        else
                        {
                            errMsg = $"{fileName} does not have a projection or world file";
                        }
                    }
                    else if (fm.LastOpenStrategy == tkFileOpenStrategy.fosDirectGrid ||
                             fm.LastOpenStrategy == tkFileOpenStrategy.fosProxyForGrid)
                    {
                        var grid = new MapWinGIS.Grid();
                        success = grid.Open(fileName, GridDataType.DoubleDataType, false, GridFileType.UseExtension, null);
                        if (success)
                        {
                            AddLayer(grid, Path.GetFileName(fileName), true, true);
                        }
                    }
                }
                else
                {
                    errMsg = "Failed to open datasource: " + fm.get_ErrorMsg(fm.LastErrorCode);
                }
            }
            if (success)
            {
                //save directory to the registry
                RegistryTools.SaveSetting("FAD3", "LastOpenedLayerDirectory", Path.GetDirectoryName(fileName));
            }
            return(success, errMsg);
        }