Beispiel #1
0
        protected void TryGetSizeOfMultiDs()
        {
            var subDsDic      = ds.GetSubDatasets();
            var subDsPathList = ds.GetSubDatasets().Values.ToList();
            RasterSourceTypeSingleInfo info = mRasterSourceManager.GetInstance().GetInputfileRasterSourceInfo(fileName);

            if (subDsDic.Count > 0)
            {
                if (info != null)
                {
                    string name = info.defaultDisplayDataset;
                    string key  = subDsDic.Keys.FirstOrDefault(t => t.Contains(name));
                    if (!string.IsNullOrEmpty(key))
                    {
                        var subDs = Gdal.OpenShared(subDsDic[key], Access.GA_ReadOnly);
                        Width  = subDs.RasterXSize;
                        Height = subDs.RasterYSize;
                        subDs.Dispose();
                    }

                    return;
                }
                else
                {
                    for (int i = 0; i < subDsDic.Count - 1; i++)
                    {
                        using (var curDs = Gdal.Open(subDsPathList[i], Access.GA_ReadOnly))
                            using (var nextDs = Gdal.Open(subDsPathList[i + 1], Access.GA_ReadOnly))
                            {
                                if (curDs != null && nextDs != null)
                                {
                                    if (curDs.RasterXSize != nextDs.RasterXSize ||
                                        (curDs.RasterYSize != nextDs.RasterYSize))
                                    {
                                        Width = Height = 0;
                                        return;
                                    }
                                }
                            }
                    }

                    using (var rDs = Gdal.Open(subDsPathList[0], Access.GA_ReadOnly))
                    {
                        Width  = rDs.RasterXSize;
                        Height = rDs.RasterYSize;
                        return;
                    }
                }
            }
        }
Beispiel #2
0
        public static WarpDataset Open(string filePath)
        {
            var ds = Gdal.OpenShared(filePath, Access.GA_ReadOnly);

            if (ds == null)
            {
                return new WarpDataset(null, string.Empty)
                       {
                           fileName = filePath
                       }
            }
            ;
            else
            {
                return(new WarpDataset(ds, filePath));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Query information about a dataset modeling terrain information.
        /// </summary>
        /// <param name="databaseRootPath">
        /// A <see cref="String"/> that specify the path of the directory containins the terrain information.
        /// </param>
        /// /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="databaseRootPath"/> is null.
        /// </exception>
        public static void Query(string databaseRootPath)
        {
            if (databaseRootPath == null)
            {
                throw new ArgumentNullException("databasePath");
            }

            string[] datasetFiles = Directory.GetFiles(databaseRootPath, "*.*", SearchOption.AllDirectories);

            foreach (string datasetPath in datasetFiles)
            {
                try
                {
                    using (Dataset dataset = Gdal.OpenShared(datasetPath, Access.GA_ReadOnly))
                    {
                        GeoElevationTerrainDataset geoElevationTerrainDataset;

                        // Determine terrain dataset information and collect it
                        switch (dataset.GetDriver().ShortName)
                        {
                        // Elevation datasets
                        case "EHdr":                                        // USGS DEM
                            if (datasetPath.ToLowerInvariant().EndsWith(".dem") == false)
                            {
                                continue;
                            }
                            geoElevationTerrainDataset = new GeoElevationTerrainDataset(datasetPath, dataset);
                            _GeoElevationTree.Insert(geoElevationTerrainDataset.Blocks);
                            break;

                        case "SRTMHGT":                                         // USGS SRTM
                            geoElevationTerrainDataset = new GeoElevationTerrainDataset(datasetPath, dataset);
                            _GeoElevationTree.Insert(geoElevationTerrainDataset.Blocks);
                            break;

                        default:
                            throw new NotSupportedException(String.Format("driver {0} is not supported"));
                        }
                    }
                }
                catch (Exception exception)
                {
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Reads the metadata from a file.  NOTE: this is a synchronous call.
        /// </summary>
        /// <param name="fullPath">the file to read the metadata from</param>
        /// <returns>the RasterInfo for that file</returns>
        public static RasterInfo CreateFromMapFile(string fullPath)
        {
            var rasterInfo = new RasterInfo(fullPath);

            using (var dataset = Gdal.OpenShared(fullPath, Access.GA_ReadOnly))
            {
                rasterInfo.ImageSize     = new Size(dataset.RasterXSize, dataset.RasterYSize);
                rasterInfo.NumberOfBands = dataset.RasterCount;
                rasterInfo.Projection    = dataset.GetProjection();
                rasterInfo.MapArea       = CalculateMapArea(dataset);
            }

            Preprocessor pre = new Preprocessor();

            foreach (int zoomLevel in Enumerable.Range(0, 5))
            {
                pre.ProjectAndTile(rasterInfo, zoomLevel, null);
            }

            return(rasterInfo);
        }
 private void DoSession(AbstractWarpDataset srcRaster, AbstractWarpDataset geoRaster, SpatialReference dstSpatialRef, FY3_VIRR_PrjSettings prjSettings, Action <int, string> progressCallback)
 {
     if (_curSession == null || _curSession != srcRaster || _isBeginSession)
     {
         if (progressCallback != null)
         {
             progressCallback(_readyProgress++, "读取及预处理经纬度数据集");
         }
         ReadyLocations(_geoDataProvider, SpatialReferenceFactory.CreateSpatialReference(4326), dstSpatialRef, out _srcLocationSize, out _xs, out _ys, out _maxPrjEnvelope, progressCallback);
         if (progressCallback != null)
         {
             progressCallback(_readyProgress++, "准备其他参数");
         }
         if (prjSettings.IsRadRef || prjSettings.IsRad)
         {
             ReadyRadiationArgs(srcRaster);
         }
         if (prjSettings.IsSolarZenith && prjSettings.IsRadRef)
         {
             _solarZenithCacheFilename = GetSolarZenithCacheFilename(geoRaster.fileName);    //太阳天顶角数据
             if (!File.Exists(_solarZenithCacheFilename))
             {
                 ReadySolarZenithArgsToFile(geoRaster);
             }
             else
             {
                 var ds = Gdal.OpenShared(_solarZenithCacheFilename, Access.GA_ReadOnly);
                 _solarZenithCacheRaster = new WarpDataset(ds, _solarZenithCacheFilename);
             }
             if (prjSettings.IsSensorZenith)
             {
                 ReadySensorZenith(geoRaster);
             }
         }
         _rasterDataBands = TryCreateRasterDataBands(srcRaster, prjSettings, progressCallback);
         _isBeginSession  = false;
     }
 }
Beispiel #6
0
        public void ProjectAndTile(RasterInfo originFile, int zoomLevel, IProgress <Status> progress)
        {
            var status = new Status
            {
                Total   = SupportedProjections.Length,
                Current = 0
            };

            foreach (var projection in SupportedProjections)
            {
                Size fullMapSize = projection.FullMapSizeFor(zoomLevel);
                var  tilesWide   = fullMapSize.Width / projection.TileSize.Width;
                var  tilesTall   = fullMapSize.Height / projection.TileSize.Height;

                status.Current++;
                status.Total  += tilesWide * tilesTall;
                status.Message = $"Projecting {projection.Name} at Zoom Level {zoomLevel}";
                progress?.Report(status);

                string baseDir = Path.Combine(projection.GetType().Name, $"{zoomLevel}");
                if (!Directory.Exists(baseDir))
                {
                    Directory.CreateDirectory(baseDir);
                }

                using (var source = Gdal.OpenShared(originFile.FullPath, Access.GA_ReadOnly))
                    using (var destination = Gdal.AutoCreateWarpedVRT(source, source.GetProjection(), projection.Wkt,
                                                                      ResampleAlg.GRA_NearestNeighbour, .0125))
                    {
                        var sourceInfo = DetermineRenderInformation(destination);

                        var ratioX = destination.RasterXSize / fullMapSize.Width;
                        var ratioY = destination.RasterYSize / fullMapSize.Height;

                        var destWidth    = (int)projection.TileSize.Width;
                        var destHeight   = (int)projection.TileSize.Height;
                        var sourceWidth  = (int)(destWidth * ratioX);
                        var sourceHeight = (int)(destHeight * ratioY);

                        var buffer = new byte[destWidth * destHeight * sourceInfo.ChannelCount];
                        var stride = destWidth * sourceInfo.ChannelCount;

                        for (int row = 0; row < tilesWide; row++)
                        {
                            for (int column = 0; column < tilesTall; column++)
                            {
                                int x = column * sourceWidth;
                                int y = row * sourceHeight;
                                destination.ReadRaster(x, y, sourceWidth, sourceHeight, buffer, destWidth,
                                                       destHeight, sourceInfo.ChannelCount, sourceInfo.BandMap, sourceInfo.PixelSpace, stride, 1);

                                var bitmap = BitmapSource.Create(destWidth, destHeight, 96, 96, sourceInfo.PixelFormat,
                                                                 sourceInfo.Colors, buffer, stride);

                                using (var stream = File.Create(Path.Combine(baseDir, $"{row}-{column}.png")))
                                {
                                    var encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
                                    encoder.Save(stream);
                                }

                                status.Current++;
                                progress?.Report(status);
                            }
                        }
                    }
            }

            status.Current = status.Total;
            status.Message = string.Empty;
            progress?.Report(status);
        }
            public DefaultTerrainElevationFactory(string databaseRoot, double lat, double lon, uint maxLod)
            {
                if (databaseRoot == null)
                {
                    throw new ArgumentNullException("databaseRoot");
                }

                // Open the dataset.
                try {
                    _DatabaseDataset = Gdal.OpenShared(databaseRoot, Access.GA_ReadOnly);

                    if (_DatabaseDataset.RasterCount != 1)
                    {
                        throw new NotSupportedException();
                    }

                    DatabaseRoot = databaseRoot;

                    using (Band band = _DatabaseDataset.GetRasterBand(1)) {
                        switch (band.GetColorInterpretation())
                        {
                        case ColorInterp.GCI_Undefined:
                            switch (_DatabaseDataset.GetDriver().ShortName)
                            {
                            case "SRTMHGT":
                            case "VRT":
                                break;

                            default:
                                throw new NotSupportedException("unknown GDAL driver");
                            }
                            break;

                        default:
                            throw new NotSupportedException("unknown color interpretation");
                        }
                    }

                    // Determine current position
                    double[] datasetTransform = new double[6], datasetInvTransform = new double[6];
                    _DatabaseDataset.GetGeoTransform(datasetTransform);
                    Gdal.InvGeoTransform(datasetTransform, datasetInvTransform);

                    double xCurrentPosition, yCurrentPosition;
                    Gdal.ApplyGeoTransform(datasetInvTransform, lon, lat, out xCurrentPosition, out yCurrentPosition);

                    // Let the higher levels to be pixel aligned
                    int x = (int)Math.Floor(xCurrentPosition), y = (int)Math.Floor(yCurrentPosition);
                    int maxLodStride = (int)Math.Pow(2.0, maxLod);

                    x -= x % maxLodStride;
                    y -= y % maxLodStride;

                    x = Math.Max(x, 0);
                    y = Math.Max(y, 0);

                    Gdal.ApplyGeoTransform(datasetTransform, x, y, out lon, out lat);

                    Latitude  = lat;
                    Longitude = lon;
                } catch {
                    // Ensure GDAL object disposition
                    if (_DatabaseDataset != null)
                    {
                        _DatabaseDataset.Dispose();
                    }
                    // Exception
                    throw;
                }
            }