/// <summary>
        /// Reprojects layer source, including shapefiles, images and grids.
        /// </summary>
        public TestingResult Reproject(ILayerSource layer, out ILayerSource newLayer, ISpatialReference projection, TesterReportForm report)
        {
            if (layer.SeekSubstituteFile(projection, out newLayer))
            {
                return(TestingResult.Substituted);
            }

            string newFilename = ProjectionHelper.FilenameWithProjectionSuffix(layer.Filename, layer.Projection, projection);

            newFilename = GetSafeNewName(newFilename);

            switch (layer.LayerType)
            {
            case LayerType.Shapefile:
                newLayer = Reproject(layer as IFeatureSet, projection, newFilename);
                break;

            case LayerType.Grid:
                newLayer = Reproject(layer as GridSource, projection, newFilename);
                break;

            case LayerType.Image:
                newLayer = Reproject(layer as BitmapSource, projection, newFilename);
                break;
            }
            return(newLayer != null ? TestingResult.Ok : TestingResult.Error);
        }
Exemple #2
0
        /// <summary>
        /// Tests if datasources projection matches map projection. Performs reprojection is needed or
        /// substitues original datasources with reprojected version created earlier.
        /// </summary>
        private ILayerSource TestProjectionMismatch(ILayerSource layer, out bool abort)
        {
            abort = false;

            ILayerSource newLayer;
            var          result = _mismatchTester.TestLayer(layer, out newLayer);

            switch (result)
            {
            case TestingResult.Ok:
                newLayer = layer;
                break;

            case TestingResult.Substituted:
                // do nothing; user new layer
                break;

            case TestingResult.SkipFile:
            case TestingResult.Error:
                MessageService.Current.Info("Unable to reproject layer: " + layer.Name);
                return(null);

            case TestingResult.CancelOperation:
                abort = true;
                return(null);
            }

            return(newLayer);
        }
Exemple #3
0
        /// <summary>
        /// Tests projection of a single layer
        /// </summary>
        public TestingResult TestLayer(ILayerSource layer, out ILayerSource newLayer)
        {
            if (layer == null)
            {
                throw new ArgumentException("Empty layer reference was passed");
            }

            var mapProj   = _context.Map.Projection;
            var layerProj = layer.Projection;

            var result = CheckIsSame(layer, mapProj, layerProj, out newLayer);

            if (result)
            {
                return(newLayer != null ? TestingResult.Substituted : TestingResult.Ok);
            }

            if (!layer.Projection.IsEmpty)
            {
                if (mapProj.IsEmpty)
                {
                    return(HandleEmptyMapProjection(layerProj));
                }

                return(HandleProjectionMismatch(layer, mapProj, out newLayer));
            }

            if (!mapProj.IsEmpty)
            {
                return(HandleEmptyLayerProjection(layer, mapProj));
            }

            return(TestingResult.Ok);
        }
Exemple #4
0
        /// <summary>
        /// Checks if layer projection is the same as map projection, including search for dialects and substitutes.
        /// </summary>
        private bool CheckIsSame(
            ILayerSource layer,
            ISpatialReference mapProj,
            ISpatialReference layerProj,
            out ILayerSource newLayer)
        {
            newLayer = null;

            bool isSame = mapProj.IsSameExt(layerProj, layer.Envelope, 10);

            if (!isSame)
            {
                isSame = _context.Projections.IsDialectOf(mapProj, layerProj);
            }

            if (!isSame)
            {
                if (layer.SeekSubstituteFile(mapProj, out newLayer))
                {
                    _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.Substituted,
                                    newLayer.Filename);
                    return(true);
                }
            }

            return(isSame);
        }
 public DatasourceInput(ILayerSource source)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     Datasource = source;
 }
Exemple #6
0
        /// <summary>
        /// Handles the projection mismatch by implementing the selected mismatch behavior.
        /// </summary>
        private TestingResult HandleProjectionMismatch(
            ILayerSource layer,
            ISpatialReference mapProj,
            out ILayerSource newLayer)
        {
            newLayer = null;

            if (layer.LayerType == LayerType.WmsLayer)
            {
                return(TestingResult.Ok);
            }

            // user should be prompted
            if (!_usePreviousAnswerMismatch && _context.Config.ShowProjectionMismatchDialog)
            {
                var model = new ProjectionMismatchModel(layer, true, mapProj);
                if (!_context.Container.Run <ProjectionMismatchPresenter, ProjectionMismatchModel>(model))
                {
                    return(TestingResult.CancelOperation);
                }

                _usePreviousAnswerMismatch                   = model.UseAnswerLater;
                _context.Config.ProjectionMismatch           = model.MismatchBehavior;
                _context.Config.ShowProjectionMismatchDialog = !model.DontShow;
            }

            var behavior = _context.Config.ProjectionMismatch;

            switch (behavior)
            {
            case ProjectionMismatch.Reproject:
                var result = _reprojectingService.Reproject(layer, out newLayer, mapProj, _report);

                if (result == TestingResult.Ok || result == TestingResult.Substituted)
                {
                    var oper = result == TestingResult.Ok
                                       ? ProjectionOperation.Reprojected
                                       : ProjectionOperation.Substituted;
                    string newName = newLayer == null ? "" : newLayer.Filename;
                    _report.AddFile(layer.Filename, layer.Projection.Name, oper, newName);
                    return(newName == layer.Filename ? TestingResult.Ok : TestingResult.Substituted);
                }

                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.FailedToReproject, "");
                return(TestingResult.Error);

            case ProjectionMismatch.IgnoreMismatch:
                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.MismatchIgnored, "");
                return(TestingResult.Ok);

            case ProjectionMismatch.SkipFile:
                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.Skipped, "");
                return(TestingResult.SkipFile);
            }

            throw new ApplicationException("Invalid ProjectionMismatch setting: " + behavior);
        }
        public ProjectionMismatchModel(ILayerSource layerSource, bool isMismatch, ISpatialReference projectProj)
        {
            if (layerSource == null)
            {
                throw new ArgumentNullException("layerSource");
            }

            IsMismatch        = isMismatch;
            ProjectProjection = projectProj;
            LayerSource       = layerSource;
        }
Exemple #8
0
        public static int GetIcon(ILayerSource source)
        {
            if (source == null)
            {
                return(GetIcon());
            }

            var gt = LayerSourceHelper.GetGeometryType(source);

            return(GetIcon(source.LayerType, gt));
        }
        /// <summary>
        /// Adds a layer to the topmost Group
        /// </summary>
        /// <param name="layerSource"> object to be added as new layer </param>
        /// <param name="visible"> Should this layer to be visible in the map? </param>
        /// <param name="legendVisible"> Should this layer be visible in the legend? </param>
        /// <param name="targetGroupHandle"> layerHandle of the group into which this layer should be added </param>
        /// <param name="positionInGroup"> Position in group new layer should be inserted at </param>
        /// <returns> layerHandle to the Layer on success, -1 on failure </returns>
        public int Add(ILayerSource layerSource, bool visible, bool legendVisible, int targetGroupHandle = -1, int positionInGroup = -1)
        {
            var newLayer = layerSource.InternalObject;

            var mapLayerHandle = _axMap.AddLayer(newLayer, visible);

            if (mapLayerHandle < 0)
            {
                return(mapLayerHandle);
            }

            _axMap.LockWindow(tkLockMode.lmLock);

            var legendGroups = _legend.Groups as LegendGroups;

            if (legendGroups != null)
            {
                var grp = legendGroups.FindOrCreateGroup(targetGroupHandle) as LegendGroup;

                if (grp != null)
                {
                    var lyr = _legend.CreateLayer(mapLayerHandle, newLayer);
                    lyr.HideFromLegend = !legendVisible;
                    lyr.Expanded       = _mapControl.ExpandLayersOnAdding;

                    if (positionInGroup == -1)
                    {
                        grp.AddLayer(lyr);
                    }
                    else
                    {
                        grp.InsertLayer(positionInGroup, lyr);
                    }
                }
            }

            if (legendVisible)
            {
                _legend.SelectedLayerHandle = mapLayerHandle;
            }

            _axMap.LockWindow(tkLockMode.lmUnlock);

            _legend.Redraw();
            _legend.FireLayerAdded(mapLayerHandle);

            return(mapLayerHandle);
        }
Exemple #10
0
        /// <summary>
        /// Handles the empty layer projection by implementing selected projection absence behavior.
        /// </summary>
        private TestingResult HandleEmptyLayerProjection(ILayerSource layer, ISpatialReference mapProj)
        {
            bool projectProjectionExists = !mapProj.IsEmpty;

            // user should be prompted
            if (!_usePreviousAnswerAbsence && _context.Config.ShowProjectionAbsenceDialog)
            {
                var model = new ProjectionMismatchModel(layer, false, mapProj);
                if (!_context.Container.Run <ProjectionMismatchPresenter, ProjectionMismatchModel>(model))
                {
                    return(TestingResult.CancelOperation);
                }

                _usePreviousAnswerAbsence                   = model.UseAnswerLater;
                _context.Config.ProjectionAbsence           = model.AbsenceBehavior;
                _context.Config.ShowProjectionAbsenceDialog = !model.DontShow;
            }

            // when there is no projection in project, it can't be assign for layer
            var behavior = _context.Config.ProjectionAbsence;

            if (!projectProjectionExists && _context.Config.ProjectionAbsence == ProjectionAbsence.AssignFromProject)
            {
                behavior = ProjectionAbsence.IgnoreAbsence;
            }

            switch (behavior)
            {
            case ProjectionAbsence.AssignFromProject:
                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.Assigned, "");
                layer.AssignProjection(mapProj);
                return(TestingResult.Ok);

            case ProjectionAbsence.IgnoreAbsence:
                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.AbsenceIgnored, "");
                return(TestingResult.Ok);

            case ProjectionAbsence.SkipFile:
                _report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperation.Skipped, "");
                return(TestingResult.SkipFile);
            }

            return(TestingResult.Ok);
        }
Exemple #11
0
        /// <summary>
        /// Projection name is written as suffix to the filename in case of reprojection.
        /// Let's try to find file with the same name, but correct suffix for projection.
        /// It's assumed here that projection for the layer passed doesn't match the project one.
        /// </summary>
        public static bool SeekSubstituteFile(this ILayerSource layer, ISpatialReference targetProjection, out ILayerSource newLayer)
        {
            newLayer = null;

            if (layer.LayerType == LayerType.VectorLayer)
            {
                // no substibutes for OGR layers
                return(false);
            }

            string testName = FilenameWithProjectionSuffix(layer.Filename, layer.Projection, targetProjection);

            if (!File.Exists(testName))
            {
                return(false);
            }

            var layerTest = GeoSource.Open(testName) as ILayerSource;

            if (layerTest == null)
            {
                return(false);
            }

            var f1 = new FileInfo(layer.Filename);
            var f2 = new FileInfo(testName);

            // the size of .shp files must be exactly the same
            bool equalSize = !(layerTest.LayerType == LayerType.Shapefile && f1.Length != f2.Length);

            if (layerTest.Projection.IsSameExt(targetProjection, layerTest.Envelope, 10) && equalSize)
            {
                newLayer = layerTest;
                return(true);
            }

            return(false);
        }
Exemple #12
0
        /// <summary>
        /// Gets the geometry type for vector datasource. GeometryType.None will be returned for raster datasource.
        /// </summary>
        public static GeometryType GetGeometryType(ILayerSource source)
        {
            if (!source.IsVector)
            {
                return(GeometryType.None);
            }

            var vector = source as IVectorLayer;

            if (vector != null)
            {
                return(vector.GeometryType);
            }

            var fs = source as IFeatureSet;

            if (fs != null)
            {
                return(fs.GeometryType);
            }

            return(GeometryType.None);
        }
Exemple #13
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();
            }
        }
Exemple #14
0
 /// <summary>
 /// Adds a layer to the topmost Group
 /// </summary>
 /// <param name="newLayer"> object to be added as new layer </param>
 /// <param name="visible"> Should this layer to be visible? </param>
 /// <param name="targetGroupHandle"> layerHandle of the group into which this layer should be added </param>
 /// <returns> layerHandle to the Layer on success, -1 on failure </returns>
 public int Add(ILayerSource newLayer, bool visible, int targetGroupHandle)
 {
     return(Add(newLayer, visible, true, targetGroupHandle));
 }
 public virtual int Add(ILayerSource layerSource, bool visible = true)
 {
     return(_axMap.AddLayer(layerSource.InternalObject, visible));
 }
 public DatasourceCancelEventArgs(ILayerSource datasource)
 {
     Datasource = datasource;
 }
Exemple #17
0
 public bool AddToMap(ILayerSource source)
 {
     return(_layerService.AddDatasource(source));
 }
Exemple #18
0
 /// <summary>
 /// Adds a layer to the topmost Group
 /// </summary>
 /// <param name="newLayer"> object to be added as new layer </param>
 /// <param name="visible"> Should this layer to be visible? </param>
 /// <returns> layerHandle to the Layer on success, -1 on failure </returns>
 public override int Add(ILayerSource newLayer, bool visible = true)
 {
     return(Add(newLayer, visible, true, -1));
 }