public void GdalDocument_CalculateExtents()
        {
            var doc = new GdalConfigurationDocument();

            Assert.Null(doc.CalculateExtent());
            var loc = new GdalRasterLocationItem {
                Location = "C:\\temp\\location_1"
            };

            loc.AddItem(new GdalRasterItem {
                FileName = "1_1.tif", MinX = 1, MinY = 1, MaxX = 2, MaxY = 2
            });
            doc.AddLocation(loc);
            var ext = doc.CalculateExtent();

            Assert.Equal(1, ext.MinX);
            Assert.Equal(1, ext.MinY);
            Assert.Equal(2, ext.MaxX);
            Assert.Equal(2, ext.MaxY);
            var loc2 = new GdalRasterLocationItem {
                Location = "C:\\temp\\location_2"
            };

            loc2.AddItem(new GdalRasterItem {
                FileName = "2_1.tif", MinX = -1, MinY = -1, MaxX = 2, MaxY = 2
            });
            doc.AddLocation(loc2);
            ext = doc.CalculateExtent();
            Assert.Equal(-1, ext.MinX);
            Assert.Equal(-1, ext.MinY);
            Assert.Equal(2, ext.MaxX);
            Assert.Equal(2, ext.MaxY);
            loc2.AddItem(new GdalRasterItem {
                FileName = "1_2.tif", MinX = 2, MinY = 1, MaxX = 3, MaxY = 3
            });
            ext = doc.CalculateExtent();
            Assert.Equal(-1, ext.MinX);
            Assert.Equal(-1, ext.MinY);
            Assert.Equal(3, ext.MaxX);
            Assert.Equal(3, ext.MaxY);
        }
Exemplo n.º 2
0
        private object UpdateConfigurationDocument(BackgroundWorker worker, DoWorkEventArgs e, params object[] args)
        {
            GdalConfigurationDocument conf = (GdalConfigurationDocument)args[0];

            IServerConnection conn = (IServerConnection)args[1];

            string[] toAdd    = args[2] as string[];
            string[] toRemove = args[3] as string[];
            bool     isAlias  = (bool)args[4];

            worker.ReportProgress(0, Strings.UpdatingConfiguration);

            int total    = toAdd.Length + toRemove.Length;
            int unit     = (total / 100);
            int progress = 0;

            var result = new UpdateConfigResult()
            {
                Added = new List <string>(), Removed = new List <string>()
            };

            //Remove first
            foreach (var remove in toRemove)
            {
                string dir = null;
                if (isAlias)
                {
                    dir = remove.Substring(0, remove.LastIndexOf("\\")); //NOXLATE
                }
                else
                {
                    dir = Path.GetDirectoryName(remove);
                }

                var loc = FindLocation(conf, dir);
                if (null != loc)
                {
                    string f = isAlias ? remove.Substring(remove.LastIndexOf("\\") + 1) : Path.GetFileName(remove); //NOXLATE
                    loc.RemoveItem(f);
                    result.Removed.Add(remove);
                    if (loc.Items.Length == 0)
                    {
                        conf.RemoveLocation(loc);
                    }
                }
                progress += unit;
                worker.ReportProgress(progress, string.Format(Strings.ProcessedItem, remove));
            }

            //Then add
            foreach (var add in toAdd)
            {
                string dir = null;
                if (isAlias)
                {
                    int idx = add.LastIndexOf("/"); //NOXLATE
                    if (idx >= 0)
                    {
                        dir = add.Substring(0, idx);
                    }
                    else
                    {
                        dir = add.Substring(0, add.LastIndexOf("%") + 1); //NOXLATE
                    }
                }
                else
                {
                    dir = Path.GetDirectoryName(add);
                }
                var loc = conf.AddLocation(dir);

                //Create a temp feature source to attempt interrogation of extents
                var values = new NameValueCollection();
                values["DefaultRasterFileLocation"] = add;                                                                  //NOXLATE
                var fs = ObjectFactory.CreateFeatureSource("OSGeo.Gdal", values);                                           //NOXLATE

                var resId = new ResourceIdentifier("Session:" + conn.SessionID + "//" + Guid.NewGuid() + ".FeatureSource"); //NOXLATE
                fs.ResourceID = resId.ToString();
                conn.ResourceService.SaveResource(fs);

                var scList = conn.FeatureService.GetSpatialContextInfo(fs.ResourceID, false);

                var raster = new GdalRasterItem();

                if (isAlias)
                {
                    int idx = add.LastIndexOf("/"); //NOXLATE
                    if (idx >= 0)
                    {
                        raster.FileName = add.Substring(add.LastIndexOf("/") + 1); //NOXLATE
                    }
                    else
                    {
                        raster.FileName = add.Substring(add.LastIndexOf("%") + 1); //NOXLATE
                    }
                }
                else
                {
                    raster.FileName = Path.GetFileName(add);
                }

                if (scList.SpatialContext.Count > 0)
                {
                    raster.MinX = Convert.ToDouble(scList.SpatialContext[0].Extent.LowerLeftCoordinate.X, CultureInfo.InvariantCulture);
                    raster.MinY = Convert.ToDouble(scList.SpatialContext[0].Extent.LowerLeftCoordinate.Y, CultureInfo.InvariantCulture);
                    raster.MaxX = Convert.ToDouble(scList.SpatialContext[0].Extent.UpperRightCoordinate.X, CultureInfo.InvariantCulture);
                    raster.MaxY = Convert.ToDouble(scList.SpatialContext[0].Extent.UpperRightCoordinate.Y, CultureInfo.InvariantCulture);
                }
                else
                {
                    raster.MinX = -10000000;
                    raster.MinY = -10000000;
                    raster.MaxX = 10000000;
                    raster.MaxY = 10000000;
                }

                loc.AddItem(raster);

                result.Added.Add(Path.Combine(dir, raster.FileName));

                progress += unit;
                worker.ReportProgress(progress, string.Format(Strings.ProcessedItem, add));
            }

            //Re-calculate combined extent for spatial context
            var env = conf.CalculateExtent();

            if (env != null)
            {
                conf.SpatialContexts[0].Extent = env;
            }

            return(result);
        }