예제 #1
0
        /// <summary>
        /// Runs the operation
        /// </summary>
        private void btnOk_Click(object sender, EventArgs e)
        {
            CoordinateSystem cs = this.ProjectionTreeView1.SelectedCoordinateSystem;

            if (cs == null)
            {
                MessageBox.Show("No projection is selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (this.LayersControl1.Filenames.Count() == 0)
                {
                    MessageBox.Show("No files are selected", m_mapWin.ApplicationInfo.ApplicationName,
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MapWinGIS.GeoProjection projection = new MapWinGIS.GeoProjection();
                    if (projection.ImportFromEPSG(cs.Code))
                    {
                        frmTesterReport report = new frmTesterReport();
                        int             count  = 0; // number of successfully processed files

                        foreach (string name in this.LayersControl1.Filenames)
                        {
                            LayerSource layer    = new LayerSource(name);
                            string      projName = layer.Projection != null ? layer.Projection.Name : "";
                            if (layer.Type != LayerSourceType.Undefined)
                            {
                                layer.Projection = projection;
                                count++;
                            }
                            else
                            {
                                report.AddFile(name, projName, ProjectionOperaion.Skipped, "");
                            }
                        }

                        if (count > 0)
                        {
                            MessageBox.Show(string.Format("The projection was successfully assigned to the files: {0}", count), m_mapWin.ApplicationInfo.ApplicationName,
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        if (report.MismatchedCount > 0)
                        {
                            report.ShowReport(projection, "The following files were not processed:", ReportType.Assignment);
                        }
                        this.LayersControl1.UpdateProjections();
                    }
                    else
                    {
                        MessageBox.Show("Failed to initialize the selected projection", m_mapWin.ApplicationInfo.ApplicationName,
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Does the reprojection work
        /// </summary>
        private void DoReprojection(IEnumerable <string> filenames, MapWinGIS.GeoProjection projection, bool inPlace)
        {
            frmTesterReport report = new frmTesterReport();

            report.InitProgress(projection);
            List <string> files = new List <string>();

            int count = 0;  // number of successfully reprojected shapefiles

            foreach (string filename in filenames)
            {
                LayerSource layer    = new LayerSource(filename);
                LayerSource layerNew = null;

                if (projection.get_IsSame(layer.Projection))
                {
                    report.AddFile(layer.Filename, projection.Name, ProjectionOperaion.SameProjection, "");
                    files.Add(layer.Filename);
                }
                else
                {
                    TestingResult result = CoordinateTransformation.ReprojectLayer(layer, out layerNew, projection, report);
                    if (result == TestingResult.Ok || result == TestingResult.Substituted)
                    {
                        ProjectionOperaion oper    = result == TestingResult.Ok ? ProjectionOperaion.Reprojected : ProjectionOperaion.Substituted;
                        string             newName = layerNew == null ? "" : layerNew.Filename;
                        report.AddFile(layer.Filename, layer.Projection.Name, oper, newName);
                        files.Add(newName == "" ? layer.Filename : newName);
                        count++;
                    }
                    else
                    {
                        ProjectionOperaion operation = result == TestingResult.Error ? ProjectionOperaion.FailedToReproject : ProjectionOperaion.Skipped;
                        report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, "");
                    }
                }

                layer.Close();
                if (layerNew != null)
                {
                    layerNew.Close();
                }
            }
            report.ShowReport(projection, "Reprojection results:", ReportType.Loading);

            IEnumerable <string> names = m_mapWin.Layers.Select(l => l.FileName);

            names = files.Except(names);

            if (count > 0)
            {
                if (projection.get_IsSame(m_mapWin.Project.GeoProjection))
                {
                    if (names.Count() > 0)
                    {
                        if (MessageBox.Show("Do you want to add layers to the project?", m_mapWin.ApplicationInfo.ApplicationName,
                                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            m_mapWin.Layers.StartAddingSession();

                            foreach (string filename in names)
                            {
                                m_mapWin.Layers.Add(filename);
                            }
                            m_mapWin.Layers.StopAddingSession();
                        }
                    }
                    else
                    {
                        MessageBox.Show("No files to add to the map.",
                                        m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Chosen projection is different from the project one. The layers can't be added to map.",
                                    m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("No files to add to the map.",
                                m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Reprojects layer of undefined type
        /// </summary>
        /// <param name="filename">Filename of layer to reproject. A changed name will be returned when new file was created</param>
        /// <param name="projection">New projection</param>
        /// <param name="inPlace">Whether new files should be written</param>
        /// <param name="report">A reference to report form</param>
        /// <returns>True on success and false otherwise</returns>
        public static TestingResult ReprojectLayer(LayerSource layer, out LayerSource newLayer, MapWinGIS.GeoProjection projection, frmTesterReport report)
        {
            newLayer = null;
            TestingResult result = TestingResult.Error;

            switch (layer.Type)
            {
            case LayerSourceType.Shapefile:
                MapWinGIS.Shapefile sfNew = null;
                result = CoordinateTransformation.Reproject(layer.Shapefile, out sfNew, projection, report);
                if (sfNew != null)
                {
                    newLayer = new LayerSource(sfNew);
                }
                break;

            case LayerSourceType.Grid:
                MapWinGIS.Grid gridNew = null;
                result = CoordinateTransformation.Reproject(layer.Grid, out gridNew, projection, report);
                if (gridNew != null)
                {
                    newLayer = new LayerSource(gridNew);
                }
                break;

            case LayerSourceType.Image:
                MapWinGIS.Image imageNew = null;
                result = CoordinateTransformation.Reproject(layer.Image, out imageNew, projection, report);
                if (imageNew != null)
                {
                    newLayer = new LayerSource(imageNew);
                }
                break;

            default:
                System.Diagnostics.Debug.Print("Coordinate transformation: unsupported interface");
                break;
            }
            return(result);
        }
        /// <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 layer of undefined type
        /// </summary>
        /// <param name="filename">Filename of layer to reproject. A changed name will be returned when new file was created</param>
        /// <param name="projection">New projection</param>
        /// <param name="inPlace">Whether new files should be written</param>
        /// <param name="report">A reference to report form</param>
        /// <returns>True on success and false otherwise</returns>
        public static TestingResult ReprojectLayer(string filename, out string newFilename, MapWinGIS.GeoProjection projection, frmTesterReport callback)
        {
            newFilename = "";
            LayerSource source = new LayerSource(filename, callback);

            if (source.Type != LayerSourceType.Undefined)
            {
                LayerSource   sourceNew = null;
                TestingResult result    = CoordinateTransformation.ReprojectLayer(source, out sourceNew, projection, callback);
                if (sourceNew != null)
                {
                    newFilename = sourceNew.Filename;
                }

                return(result);
            }
            else
            {
                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);
        }
        /// <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);
        }