Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rasterFile">土地利用类型文件</param>
        /// <param name="dblvFile">二值图文件</param>
        /// <returns></returns>
        private unsafe string GenerateNetImageFile(string rasterFile, string dblvFile)
        {
            using (IRasterDataProvider rasterprd = GeoDataDriver.Open(rasterFile) as IRasterDataProvider)
            {
                using (IRasterDataProvider dblvprd = GeoDataDriver.Open(dblvFile) as IRasterDataProvider)
                {
                    //step1:将文件转换为类型一致
                    IRasterDataProviderConverter converter = new RasterDataProviderConverter();
                    string dstFileName = MifEnvironment.GetFullFileName("1.dat");
                    using (IRasterDataProvider dstDataProvider = converter.ConvertDataType <UInt16, Byte>(dblvprd, enumDataType.Byte, dstFileName,
                                                                                                          (v) => { return((Byte)v); }))
                    {
                        //step2:裁切文件以保证大小一致
                        using (IRasterDataProvider sameSizeDataProvider = GetSameSizeDataProvider(rasterprd, dstDataProvider))
                        {
                            byte[] dataBlock = new byte[sameSizeDataProvider.Width * sameSizeDataProvider.Height];
                            fixed(byte *buffer = dataBlock)
                            {
                                IntPtr ptr = new IntPtr(buffer);

                                sameSizeDataProvider.GetRasterBand(1).Read(0, 0, sameSizeDataProvider.Width, sameSizeDataProvider.Height, ptr, enumDataType.Byte, sameSizeDataProvider.Width, sameSizeDataProvider.Height);
                                sameSizeDataProvider.Read(0, 0, sameSizeDataProvider.Width, sameSizeDataProvider.Height, ptr, enumDataType.Byte, sameSizeDataProvider.Width, sameSizeDataProvider.Height, 1, new int[] { 1 }, enumInterleave.BSQ);
                            }

                            if (sameSizeDataProvider == null)
                            {
                                return(null);
                            }
                            //step3:生成虚拟文件
                            VirtualRasterDataProvider vrd = new VirtualRasterDataProvider(new IRasterDataProvider[] { sameSizeDataProvider, dstDataProvider });
                            //step4:遍历虚拟文件生成结果
                            ArgumentProvider           ap      = new ArgumentProvider(vrd, null);
                            RasterPixelsVisitor <Byte> visitor = new RasterPixelsVisitor <Byte>(ap);
                            //生成结果
                            MemPixelFeatureMapper <Byte> memresult = new MemPixelFeatureMapper <Byte>("NIMG", 1000, new Size(vrd.Width, vrd.Height), vrd.CoordEnvelope, vrd.SpatialRef);
                            visitor.VisitPixel(new int[] { 1, 2 },
                                               (index, values) =>
                            {
                                if (values[0] == 0)
                                {
                                    memresult.Put(index, 0);
                                }
                                else if (values[1] == 0)
                                {
                                    memresult.Put(index, 100);
                                }
                                else if (values[1] == 1)
                                {
                                    memresult.Put(index, (byte)(100 + values[0]));
                                }
                            });
                            //保存文件
                            RasterIdentify idNIMG = new RasterIdentify(dblvFile);
                            idNIMG.ThemeIdentify      = "CMA";
                            idNIMG.ProductIdentify    = "SNW";
                            idNIMG.SubProductIdentify = "NIMG";
                            idNIMG.GenerateDateTime   = DateTime.Now;
                            idNIMG.Satellite          = dblvprd.DataIdentify.Satellite;
                            idNIMG.Sensor             = dblvprd.DataIdentify.Sensor;
                            string fileName = MifEnvironment.GetFullFileName(idNIMG.ToLongString() + ".dat");
                            if (File.Exists(fileName))
                            {
                                idNIMG.GenerateDateTime = DateTime.Now.AddSeconds(1);
                            }
                            IInterestedRaster <Byte> iirNIMG = new InterestedRaster <Byte>(idNIMG, new Size(sameSizeDataProvider.Width, sameSizeDataProvider.Height), sameSizeDataProvider.CoordEnvelope.Clone(), sameSizeDataProvider.SpatialRef);
                            iirNIMG.Put(memresult);
                            iirNIMG.Dispose();
                            if (File.Exists(dstFileName))
                            {
                                File.Delete(dstFileName);
                            }
                            return(iirNIMG.FileName);
                        }
                    }
                }
            }
        }
        public IVirtualRasterDataProvider CreateVirtualRasterPRD(ref Dictionary <string, FilePrdMap> filePrdMap)
        {
            bool sucess = GetSameSizeFilePrdMap(ref filePrdMap, false);

            if (!sucess)
            {
                return(null);
            }
            IRasterDataProvider          dstDataProvider = null;
            List <IRasterDataProvider>   prdList         = new List <IRasterDataProvider>();
            IRasterDataProviderConverter converter       = new RasterDataProviderConverter();
            FilePrdMap fpm     = null;
            string     dstPath = GetTemplateFilePath();
            string     dstFilename;

            foreach (string key in filePrdMap.Keys)
            {
                fpm         = filePrdMap[key];
                dstFilename = dstPath + Guid.NewGuid() + ".ldf";
                switch (fpm.Prd.DataType)
                {
                case enumDataType.Byte:
                    dstDataProvider = converter.ConvertDataType <Byte, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.Double:
                    dstDataProvider = converter.ConvertDataType <Double, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.Int16:
                    dstDataProvider = converter.ConvertDataType <Int16, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.Int32:
                    dstDataProvider = converter.ConvertDataType <Int32, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.Int64:
                    dstDataProvider = converter.ConvertDataType <Int64, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.UInt16:
                    dstDataProvider = converter.ConvertDataType <UInt16, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    //dstDataProvider = converter.ConvertDataType<UInt16, UInt16>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return (UInt16)(v / fpm.Zoom); });
                    break;

                case enumDataType.UInt32:
                    dstDataProvider = converter.ConvertDataType <UInt32, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.UInt64:
                    dstDataProvider = converter.ConvertDataType <UInt64, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;

                case enumDataType.Float:
                    dstDataProvider = converter.ConvertDataType <float, float>(fpm.Prd.fileName, enumDataType.Float, dstFilename, (v) => { return((float)(v / fpm.Zoom)); });
                    break;
                }
                //dstDataProvider = CreatDstDataProvider(fpm, dstFilename);
                fpm.Prd.Dispose();
                fpm.Prd      = dstDataProvider;
                fpm.Filename = dstDataProvider.fileName;
                prdList.Add(dstDataProvider);
            }
            return(new VirtualRasterDataProvider(prdList.ToArray()));
        }