/// <summary>
        /// Reprojects image
        /// </summary>
        public static TestingResult Reproject(MapWinGIS.Image image, out MapWinGIS.Image imageNew, MapWinGIS.GeoProjection projection, frmTesterReport report)
        {
            MapWinGIS.GeoProjection projImage = new MapWinGIS.GeoProjection();
            projImage.ImportFromProj4(image.GetProjection());

            string sourcePrj    = image.GetProjection();
            string targetPrj    = projection.ExportToProj4();
            string origFilename = image.Filename;

            MapWinGIS.ICallback callback = image.GlobalCallback;
            imageNew = null;

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

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

            string newFilename = CoordinateTransformation.FilenameWithProjectionSuffix(origFilename, projImage, projection);

            newFilename = CoordinateTransformation.GetSafeNewName(newFilename);

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

                report.ShowFilename(image.Filename);
            }

            MapWinGIS.GeoProcess.SpatialReference.ProjectImage(sourcePrj, targetPrj, origFilename, newFilename, report);

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

            imageNew = new MapWinGIS.Image();
            if (imageNew.Open(newFilename, MapWinGIS.ImageType.USE_FILE_EXTENSION, false, callback))
            {
                return(TestingResult.Ok);
            }
            else
            {
                imageNew = null;
                return(TestingResult.Error);
            }
        }
        /// <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);
        }
Esempio n. 3
0
        /// <summary>
        /// 测试单层投影
        /// </summary>
        /// <param name="layer">图层源(Shapefile或grid对象)</param>
        /// <param name="newLayer">输出新图层</param>
        /// <returns>返回测试结果</returns>
        public TestingResult TestLayer(LayerSource layer, out LayerSource newLayer)
        {
            if (layer == null)
            {
                throw new ArgumentException("空图层引用被通过");
            }

            newLayer = null;

            MapWinGIS.GeoProjection projectProj = m_mapWin.Project.GeoProjection;
            MapWinGIS.GeoProjection layerProj   = layer.Projection;
            bool isSame = projectProj.get_IsSameExt(layerProj, layer.Extents, 10);

            // 让我们看看我们是否有项目的投影的一种方言
            if (!isSame && !projectProj.IsEmpty)
            {
                ProjectionDatabase db = (ProjectionDatabase)m_mapWin.ProjectionDatabase;//投影数据源
                if (db != null)
                {
                    CoordinateSystem cs = db.GetCoordinateSystem(projectProj, ProjectionSearchType.Enhanced);//坐标系统
                    if (cs != null)
                    {
                        db.ReadDialects(cs);
                        foreach (string dialect in cs.Dialects)
                        {
                            MapWinGIS.GeoProjection projTemp = new MapWinGIS.GeoProjection();
                            if (!projTemp.ImportFromAutoDetect(dialect))
                            {
                                continue;
                            }

                            if (layerProj.get_IsSame(projTemp))
                            {
                                isSame = true;
                                break;
                            }
                        }
                    }
                }
            }

            // 投影中可以包含的文件的后缀名,让我们试着用正确的后缀搜索文件
            if (!isSame)
            {
                if (CoordinateTransformation.SeekSubstituteFile(layer, projectProj, out newLayer))
                {
                    m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Substituted, newLayer.Filename);
                    return(TestingResult.Substituted);
                }
            }

            if (!layer.Projection.IsEmpty)
            {
                if (projectProj.IsEmpty)
                {
                    // 层具有投影,项目没有;分配到投影,不提示用户

                    // 让我们找个众所周知的投影与EPSG编码
                    ProjectionDatabase db = m_mapWin.ProjectionDatabase as ProjectionDatabase;
                    if (db != null)
                    {
                        CoordinateSystem cs = db.GetCoordinateSystem(layerProj, ProjectionSearchType.UseDialects);
                        if (cs != null)
                        {
                            MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection();
                            if (proj.ImportFromEPSG(cs.Code))
                            {
                                layerProj = proj;
                            }
                        }
                    }

                    m_mapWin.Project.GeoProjection = layerProj;
                    return(TestingResult.Ok);
                }
                else if (isSame)
                {
                    // 相同投影
                    return(TestingResult.Ok);
                }
                else
                {
                    // 用户必须被提示
                    if (!m_usePreviousAnswerMismatch && !m_mapWin.ApplicationInfo.NeverShowProjectionDialog)
                    {
                        bool dontShow     = false;
                        bool useForOthers = false;

                        ArrayList list = new ArrayList();
                        list.Add("Ignore mismatch");
                        list.Add("Reproject file");
                        //list.Add("Skip file");
                        // PM 2013-05-03:
                        list.Add("Don't load the layer");

                        frmProjectionMismatch form = new frmProjectionMismatch((ProjectionDatabase)m_mapWin.ProjectionDatabase);

                        int choice = form.ShowProjectionMismatch(list, (int)m_mapWin.ApplicationInfo.ProjectionMismatchBehavior,
                                                                 projectProj, layer.Projection, out useForOthers, out dontShow);

                        form.Dispose();
                        if (choice == -1)
                        {
                            return(TestingResult.CancelOperation);
                        }

                        m_usePreviousAnswerMismatch = useForOthers;
                        m_mapWin.ApplicationInfo.ProjectionMismatchBehavior = (ProjectionMismatchBehavior)choice;
                        m_mapWin.ApplicationInfo.NeverShowProjectionDialog  = dontShow;
                    }

                    MapWinGIS.Interfaces.ProjectionMismatchBehavior behavior = m_mapWin.ApplicationInfo.ProjectionMismatchBehavior;

                    switch (behavior)
                    {
                    case ProjectionMismatchBehavior.Reproject:
                        TestingResult result = CoordinateTransformation.ReprojectLayer(layer, out newLayer, projectProj, m_report);
                        if (result == TestingResult.Ok || result == TestingResult.Substituted)
                        {
                            ProjectionOperaion oper    = result == TestingResult.Ok ? ProjectionOperaion.Reprojected : ProjectionOperaion.Substituted;
                            string             newName = newLayer == null ? "" : newLayer.Filename;
                            m_report.AddFile(layer.Filename, layer.Projection.Name, oper, newName);
                            return(newName == layer.Filename ? TestingResult.Ok : TestingResult.Substituted);
                        }
                        else
                        {
                            m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.FailedToReproject, "");
                            return(TestingResult.Error);
                        }

                    case ProjectionMismatchBehavior.IgnoreMismatch:
                        m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.MismatchIgnored, "");
                        return(TestingResult.Ok);

                    case ProjectionMismatchBehavior.SkipFile:
                        m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, "");
                        return(TestingResult.SkipFile);
                    }
                }
            }
            else if (!projectProj.IsEmpty)          // 图层投影是空的
            {
                bool projectProjectionExists = !projectProj.IsEmpty;

                // 用户必须被提示
                if (!m_usePreviousAnswerAbsence && !m_mapWin.ApplicationInfo.NeverShowProjectionDialog)
                {
                    bool dontShow     = false;
                    bool useForOthers = false;

                    ArrayList list = new ArrayList();

                    // 当在投影第一变体应排除
                    int val = projectProjectionExists ? 0 : 1;

                    if (projectProjectionExists)
                    {
                        // PM 2013-05-03:
                        //list.Add("Assign projection from project");
                        list.Add("Use the project's projection");
                    }
                    // list.Add("Ignore the absence");
                    // list.Add("Skip the file");
                    list.Add("Ignore the missing of projection file");
                    list.Add("Don't load the layer");

                    frmProjectionMismatch form = new frmProjectionMismatch((ProjectionDatabase)m_mapWin.ProjectionDatabase);
                    int choice = form.ShowProjectionAbsence(list, (int)m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior - val, projectProj, out useForOthers, out dontShow);
                    form.Dispose();

                    if (choice == -1)
                    {
                        return(TestingResult.CancelOperation);
                    }

                    choice += val;

                    m_usePreviousAnswerAbsence = useForOthers;
                    m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior = (ProjectionAbsenceBehavior)choice;
                    m_mapWin.ApplicationInfo.NeverShowProjectionDialog = dontShow;
                }

                // 当在项目没有投影,它不能分配层
                ProjectionAbsenceBehavior behavior = m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior;
                if (!projectProjectionExists && m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior == ProjectionAbsenceBehavior.AssignFromProject)
                {
                    behavior = ProjectionAbsenceBehavior.IgnoreAbsence;
                }

                switch (behavior)
                {
                case ProjectionAbsenceBehavior.AssignFromProject:
                    m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Assigned, "");
                    layer.Projection = projectProj;
                    return(TestingResult.Ok);

                case ProjectionAbsenceBehavior.IgnoreAbsence:
                    m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.AbsenceIgnored, "");
                    return(TestingResult.Ok);

                case ProjectionAbsenceBehavior.SkipFile:
                    m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, "");
                    return(TestingResult.SkipFile);
                }
            }
            else
            {
                // 层没有投影,项目也没有,不在这里
            }

            System.Diagnostics.Debug.Print("Invalid result in projection tester");
            return(TestingResult.Ok);
        }
        /// <summary>
        /// Reprojects a shapefile
        /// </summary>
        /// <param name="grid">Shapefile 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.Shapefile sfSource, out MapWinGIS.Shapefile sfNew, MapWinGIS.GeoProjection projection, frmTesterReport report)
        {
            sfNew = null;

            string origFilename = sfSource.Filename;

            MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
            int count = 0;

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

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

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

            newFilename = CoordinateTransformation.GetSafeNewName(newFilename);

            // settings callback
            MapWinGIS.ICallback callback = sfSource.GlobalCallback;
            if (report != null)
            {
                sfSource.GlobalCallback = report;

                if (!report.Visible)
                {
                    report.InitProgress(projection);
                }

                report.ShowFilename(sfSource.Filename);
            }

            // doing the job
            sf = sfSource.Reproject(projection, ref count);

            // restoring callback
            if (report != null)
            {
                sfSource.GlobalCallback = callback;
                report.ClearFilename();
            }

            if (sf != null && count == sfSource.NumShapes)
            {
                sf.GlobalCallback = sfSource.GlobalCallback;
                bool result = sf.SaveAs(newFilename, null);        // it doesn't close the editing mode
                if (!result)
                {
                    System.Diagnostics.Debug.Print("Error while saving reprojected shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
                }
                sfNew = sf;
                return(TestingResult.Ok);
            }

            //sf.Close();
            return(TestingResult.Error);
        }