Exemplo n.º 1
0
        /// <summary>
        /// Runs the operation
        /// </summary>
        private void btnOk_Click(object sender, EventArgs e)
        {
            CoordinateSystem cs = ProjectionTreeView1.SelectedCoordinateSystem;

            if (cs == null)
            {
                MessageService.Current.Info("No projection is selected.");
                return;
            }

            if (!LayersControl1.Filenames.Any())
            {
                MessageService.Current.Info("No files are selected.");
                return;
            }

            var projection = new SpatialReference();

            if (!projection.ImportFromEpsg(cs.Code))
            {
                MessageService.Current.Info("Failed to initialize the selected projection.");
                return;
            }

            var report = new TesterReportForm();
            int count  = 0; // number of successfully processed files

            foreach (string name in LayersControl1.Filenames)
            {
                var layer = GeoSource.Open(name) as ILayerSource;
                if (layer == null)
                {
                    continue;
                }

                string projName = layer.Projection != null ? layer.Projection.Name : "";
                if (layer.LayerType != LayerType.Invalid && layer.Projection != null)
                {
                    layer.Projection.CopyFrom(projection);
                    count++;
                }
                else
                {
                    report.AddFile(name, projName, ProjectionOperation.Skipped, "");
                }
            }

            if (count > 0)
            {
                MessageService.Current.Info(string.Format("The projection was successfully assigned to the files: {0}",
                                                          count));
            }

            if (report.MismatchedCount > 0)
            {
                report.ShowReport(projection, "The following files were not processed:", ReportType.Assignment);
            }

            LayersControl1.UpdateProjections();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Does the reprojection work
        /// </summary>
        private void DoReprojection(IEnumerable <string> filenames, ISpatialReference projection, bool inPlace)
        {
            var report = new TesterReportForm();

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

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

            foreach (string filename in filenames)
            {
                var layer = GeoSource.Open(filename) as ILayerSource;
                if (layer == null)
                {
                    continue;
                }

                ILayerSource layerNew = null;

                if (projection.IsSame(layer.Projection))
                {
                    report.AddFile(layer.Filename, projection.Name, ProjectionOperaion.SameProjection, "");
                    files.Add(layer.Filename);
                }
                else
                {
                    TestingResult result = _reprojectingService.Reproject(layer, out layerNew, projection, report);
                    if (result == TestingResult.Ok || result == TestingResult.Substituted)
                    {
                        var 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
                    {
                        var 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 = _context.Layers.Select(l => l.Filename).ToList();

            names = files.Except(names);

            if (count == 0)
            {
                MessageService.Current.Info("No files to add to the map.");
                return;
            }

            if (!projection.IsSame(_context.Map.Projection))
            {
                MessageService.Current.Info(
                    "Chosen projection is different from the project one. The layers can't be added to map.");
                return;
            }

            if (!names.Any())
            {
                MessageService.Current.Info("No files to add to the map.");
                return;
            }

            if (MessageService.Current.Ask("Do you want to add layers to the project?"))
            {
                //_context.Layers.StartAddingSession();

                foreach (string filename in names)
                {
                    var ds    = GeoSource.Open(filename);
                    var layer = LayerSourceHelper.ConvertToLayer(ds);
                    _context.Layers.Add(layer);
                }

                //_context.Layers.StopAddingSession();
            }
        }