コード例 #1
0
        /// <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);
        }