Пример #1
0
        public CodeToCoordDef()
        {
            string codeFile = "CodeToCoordDic.txt";

            _identify = codeFile;
            string dicfile = System.AppDomain.CurrentDomain.BaseDirectory + @"SystemData\" + codeFile;

            if (File.Exists(dicfile))//坐标编码文件不存在。
            {
                _dic = CodeToCoordDef.GetDic(dicfile);
            }
        }
Пример #2
0
        private PrjEnvelope GetEnvelopeFromFilename(string filename, CodeToCoordDef codeDic)
        {
            string filenameOnly = Path.GetFileName(filename);
            Match  match        = _regex.Match(filenameOnly);

            if (!match.Success)
            {
                return(null);
            }
            Group boundGroup = match.Groups["bound"];

            if (boundGroup == null || !boundGroup.Success || string.IsNullOrWhiteSpace(boundGroup.Value))
            {
                return(null);
            }
            string      bound    = boundGroup.Value;
            PrjEnvelope envelope = codeDic.Find(bound);//地理范围

            return(envelope);
        }
Пример #3
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            string path           = txtInputDir.Text;
            string filter         = txtInputFilter.Text;
            string projectionInfo = txtProjectionInfo.Text;
            string outdir         = txtOutputDir.Text;
            string bandNames      = txtDataNames.Text;
            string resolutionStr  = txtResolution.Text;

            if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path))
            {
                ShowMsg("参数不正确,输入路径为空或不存在");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectionInfo))
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(bandNames))//输入参数不正确,没有定义波段
            {
                ShowMsg("参数不正确,没有定义波段");
                return;
            }
            if (string.IsNullOrWhiteSpace(outdir))
            {
                ShowMsg("参数不正确,没有定义输出路径");
                return;
            }
            string[] files = GetFiles(path, filter);
            if (files == null || files.Length == 0)
            {
                ShowMsg("输入路径下获取文件为空");
                return;
            }
            double resolution;

            if (!double.TryParse(resolutionStr, out resolution))
            {
                ShowMsg("分辨率输入错误");
                return;
            }
            CodeToCoordDef    coordCode  = new CodeToCoordDef();
            ISpatialReference spatialRef = SpatialReferenceFactory.GetSpatialReferenceByWKT(projectionInfo, enumWKTSource.EsriPrjFile);

            try
            {
                if (!Directory.Exists(outdir))
                {
                    Directory.CreateDirectory(outdir);
                }
                StartProgress();
                for (int fi = 0; fi < files.Length; fi++)
                {
                    string file = files[fi];
                    Progress((int)((fi + 1) * 100f / files.Length));
                    PrjEnvelope envelope = GetEnvelopeFromFilename(file, coordCode);
                    if (envelope == null || envelope.IsEmpty)
                    {
                        continue;
                    }
                    string[] args = new string[] { "datasets=" + bandNames };
                    using (IRasterDataProvider raster = RasterDataDriver.Open(file, args) as IRasterDataProvider)
                    {
                        if (raster == null)
                        {
                            continue;
                        }
                        if (raster.BandCount == 0)
                        {
                            continue;
                        }
                        using (IRasterDataProvider outRaster = CreateOutRaster(outdir, raster, spatialRef, envelope, resolution, resolution, bandNames))
                        {
                            short[]  buffer = new short[raster.Width * raster.Height];
                            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                            try
                            {
                                IntPtr ptr = handle.AddrOfPinnedObject();
                                for (int i = 0; i < raster.BandCount; i++)
                                {
                                    raster.GetRasterBand(i + 1).Read(0, 0, raster.Width, raster.Height, ptr, raster.DataType, raster.Width, raster.Height);
                                    outRaster.GetRasterBand(i + 1).Write(0, 0, raster.Width, raster.Height, ptr, raster.DataType, raster.Width, raster.Height);
                                }
                            }
                            finally
                            {
                                if (handle.IsAllocated)
                                {
                                    handle.Free();
                                }
                            }
                        }
                    }
                }
                UpdateArgs();
                ShowMsg("执行结束");
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
            finally
            {
                FinishProgress();
            }
        }