Beispiel #1
0
        private void WriteAboutFile(string dstFilename)
        {
            //写出dat文件的相关信息
            IRasterDataProvider rd = GeoDataDriver.Open(dstFilename) as IRasterDataProvider;

            try
            {
                if (rd != null)
                {
                    MemoryRasterDataProvider mrd = rd as MemoryRasterDataProvider;
                    if (mrd == null)
                    {
                        return;
                    }
                    HdrFile hdr = mrd.ToHdrFile();
                    if (hdr != null)
                    {
                        string hdrfile = Path.Combine(Path.GetDirectoryName(dstFilename), Path.GetFileNameWithoutExtension(dstFilename) + ".hdr");
                        HdrFile.SaveTo(hdrfile, hdr);
                    }
                }
            }
            finally
            {
                if (rd != null)
                {
                    rd.Dispose();
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// 调用GDAL前生成.hdr文件
 /// </summary>
 protected override void CallGDALBefore()
 {
     base.CallGDALBefore();
     _d1bdHeader = SetFileHeader.Set1BDHeader(fileName);
     //generate hdr file
     using (FileStream fs = new FileStream(fileName, FileMode.Open))
     {
         long len = fs.Length;
         _factOfLine       = (int)(len / _sizeOfLine - 1);
         this._hdr         = new HdrFile();
         _hdr.Lines        = _factOfLine;
         _hdr.BandNames    = TryGetBandNames();
         _hdr.Bands        = _hdr.BandNames.Count();
         _hdr.Samples      = _samples;
         _hdr.HeaderOffset = _offset;
         if (_d1bdHeader.IsBigEndian)
         {
             _hdr.ByteOrder = enumHdrByteOder.Network_IEEE;
         }
         else
         {
             _hdr.ByteOrder = enumHdrByteOder.Host_intel;
         }
         _hdr.MajorFrameOffsets[0] = _maxFrameOffset;
         _hdr.MajorFrameOffsets[1] = _minFrameOffset;
         string fname = HdrFile.GetHdrFileName(this._fileName);
         _hdr.SaveTo(fname);
     }
 }
Beispiel #3
0
        public void ParseFromFile(string infile)
        {
            string  outfile = Path.Combine(Path.GetDirectoryName(infile), Path.GetFileNameWithoutExtension(infile) + "_COPY.hdr");
            HdrFile hdr     = HdrFile.LoadFrom(infile);

            Assert.NotNull(hdr);
            hdr.SaveTo(outfile);
            Console.Write(File.ReadAllText(outfile));
            //FileAssert.AreEqual(infile, outfile);
        }
Beispiel #4
0
        protected override void CallGDALBefore()
        {
            base.CallGDALBefore();
            AWXFile awxFile = new AWXFile();

            awxFile.Read(fileName);
            if (awxFile.L1Header.ProductType == 1 || awxFile.L1Header.ProductType == 2)
            {
                AWXLevel2HeaderImage l2HI = awxFile.L2Header as AWXLevel2HeaderImage;
                HdrFile hdr = new HdrFile();
                hdr.DataType = 0;
                if (l2HI.Reserve != 0)
                {
                    hdr.DataType = l2HI.Reserve;
                }
                if (awxFile.L1Header.ProductType == 2)
                {
                    AWXLevel2HeaderImagePolarOrbit l2HIP = awxFile.L2Header as AWXLevel2HeaderImagePolarOrbit;
                    if (hdr.DataType == 0)
                    {
                        if (l2HIP.BytesPerPixel == 2)
                        {
                            hdr.DataType = 2;
                        }
                        else
                        {
                            hdr.DataType = 1;
                        }
                    }
                }
                else
                {
                    l2HI = awxFile.L2Header as AWXLevel2HeaderImageGeostationary;
                    //if(hdr.DataType==0)
                    hdr.DataType = 1;
                }
                hdr.Lines = l2HI.ImageHeight;
                string[] bandNames;
                hdr.Bands        = CheckBands(awxFile.L1Header, l2HI, out bandNames);//需根据静止或极轨数据进行区分;
                hdr.BandNames    = bandNames;
                hdr.Samples      = l2HI.ImageWidth;
                hdr.HeaderOffset = awxFile.L1Header.RecordLength;
                hdr.Intertleave  = enumInterleave.BSQ;
                hdr.ByteOrder    = (awxFile.L1Header.Endian == 0) ? enumHdrByteOder.Host_intel : enumHdrByteOder.Network_IEEE;
                //TryCreateSpatialRefByHeader(l2HI);
                //hdr.HdrProjectionInfo = GetHdrProjectionInfo();
                hdr.MapInfo = GetHdrMapInfo(l2HI);
                //hdr.GeoPoints = GetHdrGeoPoints(l2HI);
                hdr.SaveTo(Path.ChangeExtension(fileName, "hdr"));
            }
        }
Beispiel #5
0
 private void PrintHdrInfo(HdrFile hdr)
 {
     Console.WriteLine("Hdr File:");
     Console.WriteLine(hdr.Samples.ToString());
     Console.WriteLine(hdr.Lines.ToString());
     Console.WriteLine(hdr.Bands.ToString());
     Console.WriteLine(hdr.Intertleave.ToString());
     Console.WriteLine(hdr.DataType.ToString());
     Console.WriteLine(hdr.ByteOrder.ToString());
     Console.WriteLine(hdr.HeaderOffset.ToString());
     Console.WriteLine("////");
     hdr.SaveTo(HdrFile.GetHdrFileName(_newFileName));
     Console.WriteLine("LOAD HDR FILE:");
     Console.Write(File.ReadAllText(HdrFile.GetHdrFileName(_newFileName)));
     Console.WriteLine("////END LOAD HDR FILE");
 }
Beispiel #6
0
        /// <summary>
        /// 调用GDAL前生成.hdr文件
        /// </summary>
        protected override void CallGDALBefore()
        {
            base.CallGDALBefore();
            string[] filenames = null;
            _infoDic = HJXML.ReadXMLFile(fileName, out filenames, out _existBands, out _lines, out _sample);
            //generate hdr file
            this._hdr         = new HdrFile();
            _hdr.Lines        = _lines;
            _hdr.BandNames    = TryGetBandNames();
            _hdr.Bands        = _existBands.Count;
            _hdr.Samples      = _sample;
            _hdr.HeaderOffset = 0;
            string fname = HdrFile.GetHdrFileName(this._fileName);

            _hdr.SaveTo(fname);
        }
Beispiel #7
0
        public override IRasterDataProvider Create(string fileName, int xSize, int ySize, int bandCount, enumDataType dataType, params object[] options)
        {
            ISpatialReference spatialRef;
            CoordEnvelope     coordEnvelope;
            bool       isWithHdr;
            int        extHeaderSize = 0;
            HdrMapInfo mapInfo;

            ParseOptions(xSize, ySize, options, out spatialRef, out coordEnvelope, out mapInfo, out isWithHdr, out extHeaderSize);
            MemoryRasterHeader h = GetHeader(spatialRef, coordEnvelope);

            h.BandCount          = bandCount;
            h.DataType           = (int)dataType;
            h.Width              = xSize;
            h.Height             = ySize;
            h.ExtendHeaderLength = extHeaderSize;
            byte[] headerBytes = new byte[1024];
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                int      headerSize = Marshal.SizeOf(typeof(MemoryRasterHeader));
                byte[]   buffer     = new byte[headerSize];
                GCHandle handle     = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                Marshal.StructureToPtr((object)h, handle.AddrOfPinnedObject(), true);
                handle.Free();
                fs.SetLength(headerSize + extHeaderSize + (long)xSize * (long)ySize * (long)bandCount * (long)DataTypeHelper.SizeOf(dataType));
                fs.Write(buffer, 0, buffer.Length);
                fs.Seek(0, SeekOrigin.Begin);
                fs.Read(headerBytes, 0, (int)Math.Min(headerBytes.Length, fs.Length));
            }

            HdrFile hdrFile = h.ToHdrFile();

            if (spatialRef != null)
            {
                hdrFile.HdrProjectionInfo = GetHdrProjectionInfo(spatialRef);
            }
            if (mapInfo != null)
            {
                hdrFile.MapInfo = mapInfo;
            }

            hdrFile.SaveTo(HdrFile.GetHdrFileName(fileName));
            IGeoDataProvider provider = BuildDataProvider(fileName, headerBytes, enumDataProviderAccess.Update, options);

            return(provider as IRasterDataProvider);
        }
Beispiel #8
0
        public string SaveGeoAdjustByChangeCoordEnvelope(CoordEnvelope envelopeAfterAdjust, IRasterDataProvider srcRaster)
        {
            if (srcRaster == null)
            {
                return(null);
            }
            string        drive       = "LDF";
            CoordEnvelope dstEnvelope = envelopeAfterAdjust;
            float         resx        = srcRaster.ResolutionX;
            float         resy        = srcRaster.ResolutionY;
            string        extension   = Path.GetExtension(srcRaster.fileName).ToUpper();
            List <string> opts        = new List <string>();

            if (extension == ".LDF" || extension == ".LD3" || extension == ".LD2" || extension == ".DAT")
            {
                if (extension == ".DAT")
                {
                    drive = "MEM";
                    opts.AddRange(new string[] {
                        "INTERLEAVE=BSQ",
                        "VERSION=MEM",
                        "WITHHDR=TRUE",
                        "SPATIALREF=" + (srcRaster.SpatialRef == null?"":srcRaster.SpatialRef.ToProj4String()),
                        "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeAfterAdjust.MinX + "," + envelopeAfterAdjust.MaxY + "}:{" + srcRaster.ResolutionX + "," + srcRaster.ResolutionY + "}"
                    });
                    string            fileName     = GreatFileName(srcRaster.fileName, extension);
                    IRasterDataDriver rasterDriver = GeoDataDriver.GetDriverByName(drive) as IRasterDataDriver;
                    if (rasterDriver == null)
                    {
                        throw new Exception("数据驱动获取失败:" + drive);
                    }
                    using (IRasterDataProvider tProviders = rasterDriver.Create(fileName, srcRaster.Width, srcRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray()))
                    {
                        int rowStep    = ClipCutHelper.ComputeRowStep(srcRaster, 0, srcRaster.Height);
                        int sample     = srcRaster.Width;
                        int typeSize   = ClipCutHelper.GetSize(srcRaster.DataType);
                        int bufferSize = sample * rowStep * typeSize;
                        for (int oRow = 0; oRow < srcRaster.Height; oRow += rowStep)
                        {
                            if (oRow + rowStep > srcRaster.Height)
                            {
                                rowStep = srcRaster.Height - oRow;
                            }
                            for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++)
                            {
                                byte[] databuffer = new byte[bufferSize];
                                unsafe
                                {
                                    fixed(byte *ptr = databuffer)
                                    {
                                        IntPtr buffer = new IntPtr(ptr);

                                        srcRaster.GetRasterBand(bandIndex).Read(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep);
                                        tProviders.GetRasterBand(bandIndex).Write(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep);
                                    }
                                }
                            }
                        }
                        return(tProviders.fileName);
                    }
                }
                else
                {
                    //    opts.AddRange(new string[]{
                    //"INTERLEAVE=BSQ",
                    //"VERSION=LDF",
                    //"WITHHDR=TRUE",
                    //"SPATIALREF=" + srcRaster.SpatialRef.ToProj4String(),
                    //"MAPINFO={" + 1 + "," + 1 + "}:{" + dstEnvelope.MinX + "," + dstEnvelope.MaxY + "}:{" +resx + "," + resy + "}",
                    ////"BANDNAMES="+ BandNameString(prjSettings.OutBandNos),
                    //});
                    //    if (srcRaster.DataIdentify != null)
                    //    {
                    //        string satellite = srcRaster.DataIdentify.Satellite;
                    //        string sensor = srcRaster.DataIdentify.Sensor;
                    //        DateTime dt = srcRaster.DataIdentify.OrbitDateTime;
                    //        bool asc = srcRaster.DataIdentify.IsAscOrbitDirection;
                    //        if (!string.IsNullOrWhiteSpace(satellite))
                    //        {
                    //            opts.Add("SATELLITE=" + satellite);
                    //        }
                    //        if (!string.IsNullOrWhiteSpace(sensor))
                    //        {
                    //            opts.Add("SENSOR=" + sensor);
                    //        }
                    //        if (dt != DateTime.MinValue && dt != DateTime.MaxValue)
                    //            opts.Add("DATETIME=" + dt.ToString("yyyy/MM/dd HH:mm"));
                    //        opts.Add("ORBITDIRECTION=" + (asc ? "ASC" : "DESC"));
                    //    }
                    //内存拷贝
                    string srcfileName = srcRaster.fileName;
                    string desfileName = GreatFileName(srcRaster.fileName, extension);
                    File.Copy(srcfileName, desfileName);
                    using (IRasterDataProvider dataPrd = GeoDataDriver.Open(desfileName) as IRasterDataProvider)
                    {
                        (dataPrd as ILdfDataProvider).Update(envelopeAfterAdjust);
                    }
                    string  hdrSrcName = Path.ChangeExtension(srcfileName, ".hdr");
                    string  hdrDesName = Path.ChangeExtension(desfileName, ".hdr");
                    HdrFile hdr        = HdrFile.LoadFrom(hdrSrcName);
                    if (hdr.MapInfo != null)
                    {
                        hdr.MapInfo.BaseMapCoordinateXY.Latitude  = envelopeAfterAdjust.Center.Y;
                        hdr.MapInfo.BaseMapCoordinateXY.Longitude = envelopeAfterAdjust.Center.X;
                    }
                    if (hdr.GeoPoints != null)
                    {
                        hdr.GeoPoints = null;
                    }
                    HdrFile.SaveTo(hdrDesName, hdr);
                    return(desfileName);
                }
            }
            //仅用于水情MVG
            else if (extension == ".MVG")
            {
                drive = "MEM";
                string ext = ".DAT";
                opts.AddRange(new string[] {
                    "INTERLEAVE=BSQ",
                    "VERSION=MEM",
                    "WITHHDR=TRUE",
                    "SPATIALREF=" + (srcRaster.SpatialRef == null?"":srcRaster.SpatialRef.ToProj4String()),
                    "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeAfterAdjust.MinX + "," + envelopeAfterAdjust.MaxY + "}:{" + srcRaster.ResolutionX + "," + srcRaster.ResolutionY + "}"
                });
                string            fileName     = GreatFileName(srcRaster.fileName, ext);
                IRasterDataDriver rasterDriver = GeoDataDriver.GetDriverByName(drive) as IRasterDataDriver;
                if (rasterDriver == null)
                {
                    throw new Exception("数据驱动获取失败:" + drive);
                }
                using (IRasterDataProvider tProviders = rasterDriver.Create(fileName, srcRaster.Width, srcRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray()))
                {
                    int rowStep    = ClipCutHelper.ComputeRowStep(srcRaster, 0, srcRaster.Height);
                    int sample     = srcRaster.Width;
                    int bufferSize = sample * rowStep;
                    for (int oRow = 0; oRow < srcRaster.Height; oRow += rowStep)
                    {
                        if (oRow + rowStep > srcRaster.Height)
                        {
                            rowStep = srcRaster.Height - oRow;
                        }
                        for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++)
                        {
                            switch (srcRaster.DataType)
                            {
                            case enumDataType.Int16:
                            {
                                Int16[] databuffer = new Int16[bufferSize];
                                unsafe
                                {
                                    fixed(Int16 *ptr = databuffer)
                                    {
                                        IntPtr buffer = new IntPtr(ptr);

                                        srcRaster.GetRasterBand(bandIndex).Read(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep);
                                        //修改值
                                        for (int i = 0; i < bufferSize; i++)
                                        {
                                            if (databuffer[i] > 1)
                                            {
                                                databuffer[i] = 0;
                                            }
                                        }
                                        tProviders.GetRasterBand(bandIndex).Write(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep);
                                    }
                                }
                                break;
                            }
                            }
                        }
                    }
                    return(tProviders.fileName);
                }
            }
            return(null);
        }
Beispiel #9
0
        /// <summary>
        /// ldf文件另存为mvg文件
        /// </summary>
        /// <param name="ldfName">ldf文件名</param>
        /// <param name="mvgName">mvg文件名</param>
        /// <param name="bandNo">需要获取的波段号</param>
        /// <param name="options">

        /*
         *  VALUECOUNT = 2         //default:0
         *  VALUES = "{0,1}"       //
         *  VALUENAMES             //
         *  SPATIALREF=Proj4                  //default:wgs-84
         *  MAPINFO={X,Y}:{Col,Row}:{ResolutionX,ResolutionY} //default:null
         *  WITHHDR=[TRUE|FALSE]      //default:true
         */
        /// </param>
        public static void FromLDF(string ldfFileName, string mvgFileName, int bandNo, params object[] options)
        {
            IGeoDataDriver      ldfDrv  = null;
            IRasterDataProvider ldfPrd  = null;
            IRasterBand         ldfBand = null;
            IMvgDriver          mvgDrv  = null;
            IMvgDataProvider    mvgPrd  = null;
            IRasterBand         mvgBand = null;

            try
            {
                ldfDrv = GeoDataDriver.GetDriverByName("LDF");
                if (ldfDrv == null)
                {
                    return;
                }
                ldfPrd = ldfDrv.Open(ldfFileName, enumDataProviderAccess.ReadOnly) as IRasterDataProvider;
                if (ldfPrd == null)
                {
                    return;
                }
                Ldf1Header ldfHeader = (ldfPrd as ILdfDataProvider).Header as Ldf1Header;
                if (ldfHeader == null || (ldfHeader.DataType != enumDataType.Int16 && ldfHeader.DataType != enumDataType.UInt16))
                {
                    throw new NotSupportedException("只支持Int16和UInt16数据格式的LDF文件转换!");
                }
                ldfBand = ldfPrd.GetRasterBand(bandNo);
                if (ldfBand == null)
                {
                    return;
                }

                //创建MVG文件
                mvgDrv = GeoDataDriver.GetDriverByName("MVG") as IMvgDriver;
                if (mvgDrv == null)
                {
                    return;
                }
                mvgPrd = mvgDrv.Create(mvgFileName, ldfBand.Width, ldfBand.Height, 1, enumDataType.Int16, options) as IMvgDataProvider;
                if (mvgPrd == null)
                {
                    return;
                }
                MvgHeaderConvertor.FillMvgHeader(ldfHeader, mvgPrd.Header);
                //重新生成hdr头文件
                string hdrFileName = HdrFile.GetHdrFileName(mvgFileName);
                HdrFile.SaveTo(hdrFileName, mvgPrd.Header.ToHdrFile());

                //读数据并写入MVG文件中
                mvgBand = mvgPrd.GetRasterBand(1);
                if (mvgBand == null)
                {
                    return;
                }
                WriteDataFromLdfToMvg(ldfHeader, ldfBand, mvgBand);
            }
            finally
            {
                if (mvgPrd != null)
                {
                    (mvgPrd as MvgDataProvider).Disposed(ldfDrv, ldfPrd, ldfBand, mvgDrv, mvgPrd, mvgBand);
                }
            }
        }