Ejemplo n.º 1
0
        public HeightMap GetHeightMap(BoundingBox bbox, string rasterFilePath, DEMFileDefinition format)
        {
            HeightMap heightMap = null;

            using (IRasterFile raster = _IRasterService.OpenFile(rasterFilePath, format.Type))
            {
                var metaData = raster.ParseMetaData(format);
                heightMap = raster.GetHeightMapInBBox(bbox, metaData, NO_DATA_OUT);
            }

            return(heightMap);
        }
Ejemplo n.º 2
0
        public FileMetadata ParseMetadata(string fileName, DEMFileDefinition fileFormat)
        {
            FileMetadata metadata = null;

            fileName = Path.GetFullPath(fileName);

            using (IRasterFile rasterFile = OpenFile(fileName, fileFormat.Type))
            {
                metadata = rasterFile.ParseMetaData(fileFormat);
            }

            Uri fullPath = new Uri(metadata.Filename, UriKind.Absolute);
            Uri relRoot  = new Uri(Path.GetFullPath(_localDirectory) + Path.DirectorySeparatorChar, UriKind.Absolute);

            metadata.Filename = Uri.UnescapeDataString(relRoot.MakeRelativeUri(fullPath).ToString());
            return(metadata);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a <see cref="FileMetadata"/> as JSON file containing raster file information.
        /// This metadata is used for fast indexing, preventing to open every raster file when performing spatial queries
        /// </summary>
        /// <param name="rasterFileName">Local file name</param>
        /// <param name="fileFormat">File format definition, see <see cref="DEMFileDefinition"/></param>
        /// <param name="force">If true, metadata will be replaced, if false the metadata will be generated only if the JSON file does not exists</param>
        public void GenerateFileMetadata(string rasterFileName, DEMFileDefinition fileFormat, bool force)
        {
            if (!File.Exists(rasterFileName))
            {
                throw new FileNotFoundException($"File {rasterFileName} does not exists !");
            }
            string outDirPath = GetManifestDirectory(rasterFileName);
            string bmpPath    = GetMetadataFileName(rasterFileName, outDirPath, ".bmp");
            string jsonPath   = GetMetadataFileName(rasterFileName, outDirPath, ".json");


            // Output directory "manifest"
            if (!Directory.Exists(outDirPath))
            {
                Directory.CreateDirectory(outDirPath);
            }

            if (force)
            {
                if (File.Exists(jsonPath))
                {
                    File.Delete(jsonPath);
                }
                if (File.Exists(bmpPath))
                {
                    File.Delete(bmpPath);
                }
            }

            // Json manifest
            if (File.Exists(jsonPath) == false)
            {
                Trace.TraceInformation($"Generating manifest for file {rasterFileName}.");

                FileMetadata metadata = this.ParseMetadata(rasterFileName, fileFormat);
                File.WriteAllText(jsonPath, JsonConvert.SerializeObject(metadata, Formatting.Indented));

                Trace.TraceInformation($"Manifest generated for file {rasterFileName}.");
            }
        }
Ejemplo n.º 4
0
 public FileMetadata ParseMetadata(IRasterFile rasterFile, DEMFileDefinition format)
 {
     return(rasterFile.ParseMetaData(format));
 }
Ejemplo n.º 5
0
        public FileMetadata ParseMetaData(DEMFileDefinition fileFormat)
        {
            // Data origin is lower left corner
            try
            {
                int[] shape = new int[1];
                shape[0] = 2;
                int ncols = _longVariable.Dimensions.First().Length;
                int nrows = _latVariable.Dimensions.First().Length;

                Array  longValues   = _longVariable.GetData(null, shape);
                Array  latValues    = _latVariable.GetData(null, shape);
                double xllcorner    = (double)longValues.GetValue(0);
                double yllcorner    = (double)latValues.GetValue(0);
                double cellsizex    = (double)longValues.GetValue(1) - (double)longValues.GetValue(0);
                double cellsizey    = (double)latValues.GetValue(1) - (double)latValues.GetValue(0);
                float  NODATA_value = -9999f;

                FileMetadata metadata = new FileMetadata(_filename, fileFormat)
                {
                    Height      = nrows,
                    Width       = ncols,
                    PixelScaleX = cellsizex,
                    PixelScaleY = cellsizey
                };
                metadata.pixelSizeX = metadata.PixelScaleX;
                metadata.pixelSizeY = metadata.PixelScaleY;

                if (fileFormat.Registration == DEMFileRegistrationMode.Grid)
                {
                    metadata.DataStartLat = yllcorner;
                    metadata.DataStartLon = xllcorner;
                    metadata.DataEndLat   = yllcorner + metadata.Height * metadata.pixelSizeY;
                    metadata.DataEndLon   = xllcorner + metadata.Width * metadata.pixelSizeX;

                    metadata.PhysicalStartLat = yllcorner;
                    metadata.PhysicalStartLon = xllcorner;
                    metadata.PhysicalEndLat   = metadata.DataEndLat;
                    metadata.PhysicalEndLon   = metadata.DataEndLon;
                }
                else
                {
                    metadata.DataStartLat = yllcorner;
                    metadata.DataStartLon = xllcorner;
                    metadata.DataEndLat   = yllcorner + (metadata.Height - 1) * metadata.pixelSizeY;
                    metadata.DataEndLon   = xllcorner + (metadata.Width - 1) * metadata.pixelSizeX;

                    metadata.PhysicalStartLat = yllcorner - (metadata.PixelScaleY / 2.0);
                    metadata.PhysicalStartLon = xllcorner - (metadata.PixelScaleX / 2.0);
                    metadata.PhysicalEndLat   = Math.Round(metadata.DataStartLat + metadata.Height * metadata.pixelSizeY - (metadata.PixelScaleY / 2.0), 10);
                    metadata.PhysicalEndLon   = Math.Round(metadata.DataStartLon + metadata.Width * metadata.pixelSizeX - (metadata.PixelScaleX / 2.0), 10);
                }

                metadata.SampleFormat = RasterSampleFormat.FLOATING_POINT;
                metadata.NoDataValue  = NODATA_value.ToString();
                return(metadata);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 6
0
        public FileMetadata ParseMetaData(DEMFileDefinition format)
        {
            FileMetadata metadata = new FileMetadata(FilePath, format);

            ///
            metadata.Height = TiffFile.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
            metadata.Width  = TiffFile.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();

            ///
            FieldValue[] modelPixelScaleTag = TiffFile.GetField(TiffTag.GEOTIFF_MODELPIXELSCALETAG);
            FieldValue[] modelTiepointTag   = TiffFile.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);

            byte[] modelPixelScale = modelPixelScaleTag[1].GetBytes();
            double pixelSizeX      = BitConverter.ToDouble(modelPixelScale, 0);
            double pixelSizeY      = BitConverter.ToDouble(modelPixelScale, 8) * -1;

            metadata.pixelSizeX  = pixelSizeX;
            metadata.pixelSizeY  = pixelSizeY;
            metadata.PixelScaleX = BitConverter.ToDouble(modelPixelScale, 0);
            metadata.PixelScaleY = BitConverter.ToDouble(modelPixelScale, 8);

            // Ignores first set of model points (3 bytes) and assumes they are 0's...
            byte[] modelTransformation = modelTiepointTag[1].GetBytes();
            metadata.DataStartLon = BitConverter.ToDouble(modelTransformation, 24);
            metadata.DataStartLat = BitConverter.ToDouble(modelTransformation, 32);
            metadata.DataEndLon   = metadata.DataStartLon + metadata.Width * pixelSizeX;
            metadata.DataEndLat   = metadata.DataStartLat + metadata.Height * pixelSizeY;

            if (metadata.DataStartLon > metadata.DataEndLon)
            {
                double temp = metadata.DataStartLon;
                metadata.DataStartLon = metadata.DataEndLon;
                metadata.DataEndLon   = temp;
            }
            if (metadata.DataStartLat > metadata.DataEndLat)
            {
                double temp = metadata.DataStartLat;
                metadata.DataStartLat = metadata.DataEndLat;
                metadata.DataEndLat   = temp;
            }

            if (format.Registration == DEMFileRegistrationMode.Grid)
            {
                metadata.PhysicalStartLat = metadata.DataStartLat;
                metadata.PhysicalStartLon = metadata.DataStartLon;
                metadata.PhysicalEndLat   = metadata.DataEndLat;
                metadata.PhysicalEndLon   = metadata.DataEndLon;
                metadata.DataStartLat     = Math.Round(metadata.DataStartLat + (metadata.PixelScaleY / 2.0), 10);
                metadata.DataStartLon     = Math.Round(metadata.DataStartLon + (metadata.PixelScaleX / 2.0), 10);
                metadata.DataEndLat       = Math.Round(metadata.DataEndLat - (metadata.PixelScaleY / 2.0), 10);
                metadata.DataEndLon       = Math.Round(metadata.DataEndLon - (metadata.PixelScaleX / 2.0), 10);
            }
            else
            {
                metadata.PhysicalStartLat = metadata.DataStartLat;
                metadata.PhysicalStartLon = metadata.DataStartLon;
                metadata.PhysicalEndLat   = metadata.DataEndLat;
                metadata.PhysicalEndLon   = metadata.DataEndLon;
            }
            var scanline = new byte[TiffFile.ScanlineSize()];

            metadata.ScanlineSize = TiffFile.ScanlineSize();

            // Grab some raster metadata
            metadata.BitsPerSample = TiffFile.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
            var sampleFormat = TiffFile.GetField(TiffTag.SAMPLEFORMAT);

            // Add other information about the data
            metadata.SampleFormat = sampleFormat[0].Value.ToString();
            // TODO: Read this from tiff metadata or determine after parsing
            metadata.NoDataValue = "-10000";

            metadata.WorldUnits = "meter";

            return(metadata);
        }
Ejemplo n.º 7
0
        public FileMetadata ParseMetaData(DEMFileDefinition fileFormat)
        {
            try
            {
                string ncolsLine        = _streamReader.ReadLine();
                string nrowsLine        = _streamReader.ReadLine();
                string xllcornerLine    = _streamReader.ReadLine();
                string yllcornerLine    = _streamReader.ReadLine();
                string cellsizeLine     = _streamReader.ReadLine();
                string NODATA_valueLine = _streamReader.ReadLine();

                DebugCheckRegistrationType(fileFormat.Registration, xllcornerLine, yllcornerLine);

                int    ncols        = int.Parse(ncolsLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1]);
                int    nrows        = int.Parse(nrowsLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1]);
                double xllcorner    = double.Parse(xllcornerLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);
                double yllcorner    = double.Parse(yllcornerLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);
                double cellsize     = double.Parse(cellsizeLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);
                float  NODATA_value = float.Parse(NODATA_valueLine.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);

                FileMetadata metadata = new FileMetadata(_filename, fileFormat);
                metadata.Height      = nrows;
                metadata.Width       = ncols;
                metadata.PixelScaleX = cellsize;
                metadata.PixelScaleY = cellsize;
                metadata.pixelSizeX  = cellsize;
                metadata.pixelSizeY  = -cellsize;

                if (fileFormat.Registration == DEMFileRegistrationMode.Grid)
                {
                    metadata.DataStartLat = yllcorner;
                    metadata.DataStartLon = xllcorner;
                    metadata.DataEndLat   = yllcorner + metadata.Height * metadata.pixelSizeY;
                    metadata.DataEndLon   = xllcorner + metadata.Width * metadata.pixelSizeX;

                    metadata.PhysicalStartLat = yllcorner;
                    metadata.PhysicalStartLon = xllcorner;
                    metadata.PhysicalEndLat   = metadata.DataEndLat;
                    metadata.PhysicalEndLon   = metadata.DataEndLon;
                }
                else
                {
                    metadata.DataStartLat = Math.Round(yllcorner + (metadata.pixelSizeY / 2.0), 10);
                    metadata.DataStartLon = Math.Round(xllcorner - (metadata.PixelScaleX / 2.0), 10);
                    metadata.DataEndLat   = yllcorner + metadata.Height * cellsize;
                    metadata.DataEndLon   = xllcorner + metadata.Width * cellsize;
                    metadata.DataEndLat   = Math.Round(metadata.DataEndLat - (metadata.PixelScaleY / 2.0), 10);
                    metadata.DataEndLon   = Math.Round(metadata.DataEndLon - (metadata.PixelScaleX / 2.0), 10);

                    metadata.PhysicalStartLat = metadata.DataStartLat;
                    metadata.PhysicalStartLon = metadata.DataStartLon;
                    metadata.PhysicalEndLat   = metadata.DataEndLat;
                    metadata.PhysicalEndLon   = metadata.DataEndLon;
                }

                metadata.SampleFormat = RasterSampleFormat.FLOATING_POINT;
                metadata.NoDataValue  = NODATA_value.ToString();
                return(metadata);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 8
0
 public FileMetadata(string filename, DEMFileDefinition fileFormat, string version = FILEMETADATA_VERSION)
 {
     this.Filename   = filename;
     this.FileFormat = fileFormat;
     this.Version    = version;
 }
Ejemplo n.º 9
0
 public FileMetadata ParseMetadata(IRasterFile rasterFile, DEMFileDefinition format, bool makeRelativePath = false)
 {
     return(rasterFile.ParseMetaData(format));
 }
Ejemplo n.º 10
0
        public FileMetadata ParseMetaData(DEMFileDefinition format)
        {
            FileMetadata metadata = new FileMetadata(_filename, format);

            int numPixels = _fileBytesCount == HGTFile.HGT1201 ? 1201 : 3601;

            if (format.Registration == DEMFileRegistrationMode.Grid)
            { ///
                metadata.Height      = numPixels;
                metadata.Width       = numPixels;
                metadata.PixelScaleX = 1d / (numPixels - 1);
                metadata.PixelScaleY = 1d / (numPixels - 1);
                metadata.pixelSizeX  = metadata.PixelScaleX;
                metadata.pixelSizeY  = -metadata.PixelScaleY;

                // fileName gives is coordinates of center of first lower left pixel (south west)
                // example N08E003.hgt
                string fileTitle = Path.GetFileNameWithoutExtension(_filename);
                int    latSign   = fileTitle.Substring(0, 1) == "N" ? 1 : -1;
                int    lonSign   = fileTitle.Substring(3, 1) == "E" ? 1 : -1;
                int    lat       = int.Parse(fileTitle.Substring(1, 2)) * latSign;
                int    lon       = int.Parse(fileTitle.Substring(4, 3)) * lonSign;
                metadata.DataStartLon     = lon;
                metadata.DataStartLat     = lat;
                metadata.DataEndLon       = lon + 1;
                metadata.DataEndLat       = lat + 1;
                metadata.PhysicalStartLat = metadata.DataStartLat + (metadata.pixelSizeY / 2.0);
                metadata.PhysicalStartLon = metadata.DataStartLon - (metadata.pixelSizeX / 2.0);
                metadata.PhysicalEndLon   = metadata.Width * metadata.pixelSizeX + metadata.DataStartLon;
                metadata.PhysicalEndLat   = metadata.DataStartLat + metadata.Height * Math.Abs(metadata.pixelSizeY);


                metadata.ScanlineSize = numPixels * 2; // 16 bit signed integers


                metadata.BitsPerSample = 16;
                // Add other information about the data
                metadata.SampleFormat = RasterSampleFormat.INTEGER;
                // TODO: Read this from tiff metadata or determine after parsing
                metadata.NoDataValue = "-32768";
            }
            else
            {
                throw new NotSupportedException("HGT files should always be grid registered, not cell registered.");
                ///
                //metadata.Height = numPixels;
                //metadata.Width = numPixels;

                //metadata.PixelScaleX = 1d / numPixels;
                //metadata.PixelScaleY = 1d / numPixels;
                //metadata.pixelSizeX = metadata.PixelScaleX;
                //metadata.pixelSizeY = -metadata.PixelScaleY;

                //// fileName gives is coordinates of center of first lower left pixel (south west)
                //// example N08E003.hgt
                //string fileTitle = Path.GetFileNameWithoutExtension(_filename);
                //int latSign = fileTitle.Substring(0, 1) == "N" ? 1 : -1;
                //int lonSign = fileTitle.Substring(3, 1) == "E" ? 1 : -1;
                //int lat = int.Parse(fileTitle.Substring(1, 2)) * latSign;
                //int lon = int.Parse(fileTitle.Substring(4, 3)) * lonSign;
                //metadata.OriginLongitude = lon;
                //metadata.OriginLatitude = lat + 1;
                //metadata.StartLat = metadata.OriginLatitude + (metadata.pixelSizeY / 2.0);
                //metadata.StartLon = metadata.OriginLongitude + (metadata.pixelSizeX / 2.0);

                //metadata.ScanlineSize = numPixels * 2; // 16 bit signed integers


                //metadata.BitsPerSample = 16;
                //// Add other information about the data
                //metadata.SampleFormat = RasterSampleFormat.INTEGER;
                //// TODO: Read this from tiff metadata or determine after parsing
                //metadata.NoDataValue = "-32768";

                //return metadata;
            }

            return(metadata);
        }