Пример #1
0
        /// <summary>
        /// Read GeoTiff data info
        /// </summary>
        /// <param name="aFile">file path</param>
        public override void ReadDataInfo(string aFile)
        {
            this.FileName = aFile;
            FileStream   sr = new FileStream(aFile, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(sr);

            //Image file header (IFH)
            _byteOrder = ASCIIEncoding.ASCII.GetString(br.ReadBytes(2));
            int  version = BitConverter.ToUInt16(br.ReadBytes(2), 0);
            long offset  = BitConverter.ToUInt32(br.ReadBytes(4), 0);

            //Read IFDs
            ReadIFD(br, offset);


            br.Close();
            sr.Close();

            IFDEntry widthIFD  = this.FindTag(Tag.ImageWidth);
            IFDEntry heightIFD = this.FindTag(Tag.ImageLength);
            int      width     = (int)widthIFD.ValueOffset;
            int      height    = (int)heightIFD.ValueOffset;

            double[] X = new double[width];
            double[] Y = new double[height];
            IFDEntry modelTiePointTag   = FindTag(Tag.ModelTiepointTag);
            IFDEntry modelPixelScaleTag = FindTag(Tag.ModelPixelScaleTag);
            double   minLon             = modelTiePointTag.ValueD[3];
            double   maxLat             = modelTiePointTag.ValueD[4];
            double   xdelt = modelPixelScaleTag.ValueD[0];
            double   ydelt = modelPixelScaleTag.ValueD[1];

            for (int i = 0; i < width; i++)
            {
                X[i] = minLon + xdelt * i;
            }
            for (int i = 0; i < height; i++)
            {
                Y[height - i - 1] = maxLat - ydelt * i;
            }
            Dimension xdim = new Dimension(DimensionType.X);

            xdim.SetValues(X);
            Dimension ydim = new Dimension(DimensionType.Y);

            ydim.SetValues(Y);
            Variable var = new Variable();

            var.Name = "var";
            var.SetDimension(ydim);
            var.SetDimension(xdim);
            List <Variable> vars = new List <Variable>();

            vars.Add(var);
            this.Variables = vars;
        }
Пример #2
0
        private int readIntValue(BinaryReader br, IFDEntry ifd)
        {
            switch (ifd.Type.Code)
            {
            case 1:
            case 2:
                return(br.ReadByte());

            case 3:
                return(br.ReadUInt16());

            case 4:
            case 5:
                return(br.ReadInt32());
            }
            return(0);
        }
Пример #3
0
 private void readValues(BinaryReader br, IFDEntry ifd)
 {
     if (ifd.Type == FieldType.ASCII)
     {
         ifd.ValueS = readSValue(br, ifd);
     }
     else if (ifd.Type == FieldType.RATIONAL)
     {
         ifd.Value = new int[ifd.Length * 2];
         for (int i = 0; i < ifd.Length * 2; i++)
         {
             ifd.Value[i] = readIntValue(br, ifd);
         }
     }
     else if (ifd.Type == FieldType.FLOAT)
     {
         ifd.ValueD = new double[ifd.Length];
         for (int i = 0; i < ifd.Length; i++)
         {
             ifd.ValueD[i] = br.ReadSingle();
         }
     }
     else if (ifd.Type == FieldType.DOUBLE)
     {
         ifd.ValueD = new double[ifd.Length];
         for (int i = 0; i < ifd.Length; i++)
         {
             ifd.ValueD[i] = br.ReadDouble();
         }
     }
     else
     {
         ifd.Value = new int[ifd.Length];
         for (int i = 0; i < ifd.Length; i++)
         {
             ifd.Value[i] = readIntValue(br, ifd);
         }
     }
 }
Пример #4
0
        private double[] ReadData(BinaryReader br, int width, int height)
        {
            double[] values        = new double[width * height];
            IFDEntry tileOffsetTag = FindTag(Tag.TileOffsets);

            if (tileOffsetTag != null)
            {
                int      tileOffset  = tileOffsetTag.Value[0];
                IFDEntry tileSizeTag = FindTag(Tag.TileByteCounts);
                int      tileSize    = tileSizeTag.Value[0];
            }
            else
            {
                IFDEntry stripOffsetTag = FindTag(Tag.StripOffsets);
                if (stripOffsetTag != null)
                {
                    int      stripNum = stripOffsetTag.Length;
                    int      stripOffset;
                    IFDEntry stripSizeTag    = FindTag(Tag.StripByteCounts);
                    int      stripSize       = stripSizeTag.Value[0];
                    IFDEntry rowsPerStripTag = FindTag(Tag.RowsPerStrip);
                    int      rowNum          = rowsPerStripTag.Value[0];
                    int      n = 0;
                    for (int i = 0; i < stripNum; i++)
                    {
                        stripOffset = stripOffsetTag.Value[i];
                        br.BaseStream.Seek(stripOffset, SeekOrigin.Begin);
                        for (int j = 0; j < width * rowNum; j++)
                        {
                            values[n] = br.ReadUInt16();
                            n        += 1;
                        }
                    }
                }
            }

            return(values);
        }
Пример #5
0
 private String readSValue(BinaryReader br, IFDEntry ifd)
 {
     byte[] dst = br.ReadBytes(ifd.Length);
     return(ASCIIEncoding.ASCII.GetString(dst));
 }
Пример #6
0
        private void ReadIFD(BinaryReader br, long offset)
        {
            br.BaseStream.Seek(offset, SeekOrigin.Begin);
            int tagNum = BitConverter.ToUInt16(br.ReadBytes(2), 0);

            _tags = new List <IFDEntry>();
            int      i;
            bool     isGeoTiff  = false;
            IFDEntry geoTiffTag = new IFDEntry();
            long     position;

            for (i = 0; i < tagNum; i++)
            {
                IFDEntry aIFD = new IFDEntry();
                aIFD.Tag    = new Tag(BitConverter.ToUInt16(br.ReadBytes(2), 0));
                aIFD.Type   = FieldType.Get(BitConverter.ToUInt16(br.ReadBytes(2), 0));
                aIFD.Length = BitConverter.ToInt32(br.ReadBytes(4), 0);
                byte[] bytes = br.ReadBytes(4);
                aIFD.ValueOffset = BitConverter.ToUInt32(bytes, 0);
                position         = br.BaseStream.Position;

                if (aIFD.Length * aIFD.Type.Size <= 4)
                {
                    br.BaseStream.Seek(-4, SeekOrigin.Current);
                    readValues(br, aIFD);
                }
                else
                {
                    br.BaseStream.Seek(aIFD.ValueOffset, SeekOrigin.Begin);
                    readValues(br, aIFD);
                }
                br.BaseStream.Position = position;

                _tags.Add(aIFD);

                if (!isGeoTiff)
                {
                    if (aIFD.Tag.Code == 34735)
                    {
                        isGeoTiff  = true;
                        geoTiffTag = aIFD;
                    }
                }
            }
            offset = BitConverter.ToUInt16(br.ReadBytes(2), 0);

            if (isGeoTiff)
            {
                offset = geoTiffTag.ValueOffset;
                br.BaseStream.Seek(offset, SeekOrigin.Begin);
                GeoHeader geoHeader = new GeoHeader();
                geoHeader.KeyDirectoryVersion = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                geoHeader.KeyRevision         = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                geoHeader.MinorRevision       = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                geoHeader.NumberOfKeys        = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                List <KeyEntry> keyEntries = new List <KeyEntry>();
                for (i = 0; i < geoHeader.NumberOfKeys; i++)
                {
                    KeyEntry keyEntry = new KeyEntry();
                    keyEntry.KeyID           = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                    keyEntry.TIFFTagLocation = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                    keyEntry.Count           = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                    keyEntry.Value_Offset    = BitConverter.ToUInt16(br.ReadBytes(2), 0);
                    keyEntries.Add(keyEntry);
                }
            }
        }