/// <summary>
        /// Reprojects a grid
        /// </summary>
        /// <param name="grid">Grid object to reproject</param>
        /// <param name="inPlace">Whether reprojected file should replace the initial one</param>
        /// <returns>True on success and false otherwise</returns>
        public static TestingResult Reproject(MapWinGIS.Grid grid, out MapWinGIS.Grid gridNew, MapWinGIS.GeoProjection projection, frmTesterReport report)
        {
            string sourcePrj    = grid.Header.GeoProjection.ExportToProj4();
            string targetPrj    = projection.ExportToProj4();
            string origFilename = grid.Filename;

            MapWinGIS.ICallback callback = grid.GlobalCallback;
            gridNew = null;

            LayerSource obj    = new LayerSource(grid);
            LayerSource objNew = null;

            if (CoordinateTransformation.SeekSubstituteFile(obj, projection, out objNew))
            {
                gridNew = objNew.Grid;
                return(TestingResult.Substituted);
            }

            string newFilename = CoordinateTransformation.FilenameWithProjectionSuffix(origFilename, grid.Header.GeoProjection, projection);

            newFilename = CoordinateTransformation.GetSafeNewName(newFilename);

            // setting callback
            if (report != null)
            {
                if (!report.Visible)
                {
                    report.InitProgress(projection);
                }

                report.ShowFilename(grid.Filename);
            }

            bool result = MapWinGIS.GeoProcess.SpatialReference.ProjectGrid(ref sourcePrj, ref targetPrj, ref origFilename, ref newFilename, true, report);

            if (report != null)
            {
                report.ClearFilename();
            }

            if (!result)
            {
                return(TestingResult.Error);
            }

            // TODO: no need to open it if only a name is supposed to be returned
            gridNew = new MapWinGIS.Grid();
            gridNew.Open(newFilename, MapWinGIS.GridDataType.UnknownDataType, false, MapWinGIS.GridFileType.UseExtension, callback);
            gridNew.AssignNewProjection(projection.ExportToProj4());

            return(TestingResult.Ok);
        }
Example #2
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);
        }