public FileMetadata ParseMetaData() { FileMetadata metadata = new FileMetadata(FilePath); /// 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.OriginLongitude = BitConverter.ToDouble(modelTransformation, 24); metadata.OriginLatitude = BitConverter.ToDouble(modelTransformation, 32); double startLat = metadata.OriginLatitude + (pixelSizeY / 2.0); double startLon = metadata.OriginLongitude + (pixelSizeX / 2.0); metadata.StartLat = startLat; metadata.StartLon = startLon; var scanline = new byte[TiffFile.ScanlineSize()]; metadata.ScanlineSize = TiffFile.ScanlineSize(); //TODO: Check if band is stored in 1 byte or 2 bytes. //If 2, the following code would be required var scanline16Bit = new ushort[TiffFile.ScanlineSize() / 2]; Buffer.BlockCopy(scanline, 0, scanline16Bit, 0, scanline.Length); // 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); }
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); }