예제 #1
0
        private void GetTask(IList <FileNameCustom> list, string saveDir)
        {
            IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass();

            foreach (FileNameCustom fileNameCustom in list)
            {
                string path = fileNameCustom.FilePath;
                if (MyUtils.Utils.CheckFileExists(path))
                {
                    string name     = System.IO.Path.GetFileNameWithoutExtension(path);
                    string saveName = saveDir + "\\" + name + ".img";

                    string pathName = System.IO.Path.GetDirectoryName(path);
                    string fileName = System.IO.Path.GetFileName(path);
                    //定义工作空间工厂并实例化
                    IWorkspace       pWS            = pWSF.OpenFromFile(pathName, 0);
                    IRasterWorkspace pRWS           = pWS as IRasterWorkspace;
                    IRasterDataset   pRasterDataset = pRWS.OpenRasterDataset(fileName);
                    string           str            = pRasterDataset.Format;
                    //影像金字塔的判断与创建
                    IRasterPyramid pRasPyrmid = pRasterDataset as IRasterPyramid;
                    if (pRasPyrmid != null)
                    {
                        if (!pRasPyrmid.Present)
                        {
                            pRasPyrmid.Create();
                        }
                    }
                    IRaster pRaster = pRasterDataset.CreateDefaultRaster();
                    RasterSaveAs(pRaster, saveName);
                }
            }
        }
예제 #2
0
        private Rectangle MouseRect = Rectangle.Empty; //初始化矩形裁剪包络线时

        #endregion 私有成员变量

        /// <summary>
        /// 打开栅格文件.
        /// </summary>
        /// <param name="rasfilename">栅格文件路径</param>
        public void openRasterFile(string rasfilename)
        {
            FileInfo finfo = new FileInfo(rasfilename);

            if (!finfo.Exists)
            {
                return;                // 文件不存在,返回
            }
            IWorkspaceFactory pWorkspaceFacotry = new RasterWorkspaceFactory();

            IWorkspace       pWorkspace       = pWorkspaceFacotry.OpenFromFile(finfo.DirectoryName, 0);
            IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace;
            IRasterDataset   pRasterDataset   = pRasterWorkspace.OpenRasterDataset(finfo.Name);

            // 影像金字塔的判断与创建
            IRasterPyramid pRasterPyamid = pRasterDataset as IRasterPyramid;

            if (pRasterPyamid != null)
            {
                if (!(pRasterPyamid.Present))
                {
                    pRasterPyamid.Create();
                }
            }
            // 多波段图像
            IRasterBandCollection pRasterBands = (IRasterBandCollection)pRasterDataset;
            int     pBandCount = pRasterBands.Count;
            IRaster pRaster    = null;

            if (pBandCount > 3)
            {
                pRaster = (pRasterDataset as IRasterDataset2).CreateFullRaster();
            }
            else
            {
                pRaster = pRasterDataset.CreateDefaultRaster();
            }

            IRasterLayer pRasterLayer = new RasterLayer();

            pRasterLayer.CreateFromRaster(pRaster);
            pBandCount = pRasterLayer.BandCount;

            ILayer pLayer = pRasterLayer as ILayer;

            m_mapControl.AddLayer(pLayer);
            m_mapControl.Refresh();
        }
예제 #3
0
        private void 打开栅格数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog = new OpenFileDialog();

            openfiledialog.CheckFileExists  = true;
            openfiledialog.Title            = @"打开栅格数据";
            openfiledialog.Filter           = @"tif文件(*.tif)|*.tif;|tiff文件(*.tiff)|*.tiff;|jpeg文件(*.jpeg)|*.jpeg;|jpg文件(*.jpg)|*.jpg;|png文件(*.png)|*.png;|bmp文件(*.bmp)|*.bmp;|img文件(*.img)|*.img";
            openfiledialog.RestoreDirectory = true;
            openfiledialog.ShowDialog();

            string pFullPath = openfiledialog.FileName;

            if (pFullPath == "")
            {
                return;
            }
            int    pIndext  = pFullPath.LastIndexOf("\\");
            string filePath = pFullPath.Substring(0, pIndext);
            string filename = pFullPath.Substring(pIndext + 1);


            IWorkspaceFactory pworkspacefacory = new RasterWorkspaceFactory(); //引用Geodatabase + DataSourceRaster
            IWorkspace        ws  = pworkspacefacory.OpenFromFile(filePath, 0);
            IRasterWorkspace  rws = ws as IRasterWorkspace;                    //强制转换
            IRasterDataset    rds = rws.OpenRasterDataset(filename);

            //影像金字塔的判断和创建(可有可无。创建金字塔的目的是减少时间)
            IRasterPyramid rp = rds as IRasterPyramid;

            if (rp != null)
            {
                if (!(rp.Present))
                {
                    rp.Create();
                }
            }

            //新建栅格图层
            IRasterLayer rl = new RasterLayer();//引用Carto

            rl.CreateFromRaster(rds.CreateDefaultRaster());
            //加载显示
            axMapControl1.AddLayer(rl, 0);
            axMapControl1.ActiveView.Refresh();
        }
예제 #4
0
        Task DownLoadFile_My(object obj, string saveDir)
        {
            IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass();
            Task task = new Task(new Action(() =>
            {
                lock (this)
                {
                    IList <FileNameCustom> lst = obj as IList <FileNameCustom>;
                    foreach (FileNameCustom fileNameCustom in lst)
                    {
                        string path = fileNameCustom.FilePath;
                        if (MyUtils.Utils.CheckFileExists(path))
                        {
                            string name     = System.IO.Path.GetFileNameWithoutExtension(path);
                            string saveName = saveDir + "\\" + name + ".img";

                            string pathName = System.IO.Path.GetDirectoryName(path);
                            string fileName = System.IO.Path.GetFileName(path);
                            //定义工作空间工厂并实例化
                            IWorkspace pWS                = pWSF.OpenFromFile(pathName, 0);
                            IRasterWorkspace pRWS         = pWS as IRasterWorkspace;
                            IRasterDataset pRasterDataset = pRWS.OpenRasterDataset(fileName);
                            string str = pRasterDataset.Format;
                            //影像金字塔的判断与创建
                            IRasterPyramid pRasPyrmid = pRasterDataset as IRasterPyramid;
                            if (pRasPyrmid != null)
                            {
                                if (!pRasPyrmid.Present)
                                {
                                    pRasPyrmid.Create();
                                }
                            }
                            IRaster pRaster = pRasterDataset.CreateDefaultRaster();
                            RasterSaveAs(pRaster, saveName);
                            Console.Write(aa++ + ":");
                            Console.WriteLine(saveName);
                        }
                    }
                }
            }));

            return(task);
        }
예제 #5
0
        /// <summary>
        /// 打开栅格数据
        /// </summary>
                private void openShanGe(string Path)
        {
            string RasterPath = System.IO.Path.GetDirectoryName(Path);
            string RasterName = System.IO.Path.GetFileName(Path);
                        //工作空间实例化
                        IWorkspaceFactory pRasterWsF = new RasterWorkspaceFactory();
            IWorkspace                 pWs           = pRasterWsF.OpenFromFile(RasterPath, 0);
            IRasterWorkspace           pRasterWs     = pWs as IRasterWorkspace;
                        IRasterDataset pRasterDS     = pRasterWs.OpenRasterDataset(RasterName);
                        //影像金字塔的判断和创建
                        IRasterPyramid pRasterPyramid = pRasterDS as IRasterPyramid;

            if ((pRasterPyramid != null) && (!pRasterPyramid.Present))
            {
                if (MessageBox.Show("是否开始创建金字塔?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.OK)
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    //开始
                    pRasterPyramid.Create();
                    //结束
                    sw.Stop();
                    MessageBox.Show("创建完成,Time:" + sw.ElapsedMilliseconds.ToString() + "ms");
                }
                else
                {
                    MessageBox.Show("放弃创建金字塔");
                }
            }
                        //新建栅格图层
                        IRasterLayer pRasterLayer = new RasterLayer();
                        pRasterLayer.CreateFromRaster(pRasterDS.CreateDefaultRaster());

            IMap pmap = mapControl.Map;
                        //加载显示
            pmap.AddLayer(pRasterLayer);

                        //mapControl.AddLayer(pRasterLayer, 0);
            mapControl.ActiveView.Refresh();

            OpenStatus();
        }
예제 #6
0
        /// <summary>
        /// 波段计算和金字塔构建
        /// </summary>
        /// <param name="pRasterDataset"></param>
        public static void CalculateStatsAndPyramids(IRasterDataset pRasterDataset)
        {
            IRasterBandCollection pBandColl = (IRasterBandCollection)pRasterDataset;

            //波段统计
            for (int i = 0; i < pBandColl.Count; i++)
            {
                IRasterBand pRasterBand = pBandColl.Item(i);
                pRasterBand.ComputeStatsAndHist();
                //IRasterStatistics pRasterStatistics= pRasterBand.Statistics;
                //double Maximum=pRasterStatistics.Maximum;
                //IRasterHistogram pRasterHistogram=pRasterBand.Histogram;
            }
            IRasterPyramid pRasterPyramids = (IRasterPyramid)pRasterDataset;

            if (pRasterPyramids.Present == false)
            {
                pRasterPyramids.Create();
            }
        }
예제 #7
0
        /// <summary>
        /// 打开栅格文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private ILayer openRasterFile(string fileName)
        {
            string            workSpacePath  = System.IO.Path.GetDirectoryName(fileName);
            string            RasterFileName = System.IO.Path.GetFileName(fileName);
            IWorkspaceFactory wsf            = new RasterWorkspaceFactoryClass();
            IRasterWorkspace  rws            = (IRasterWorkspace)wsf.OpenFromFile(workSpacePath, 0);//打开工作空间
            IRasterDataset    rasterDataset  = rws.OpenRasterDataset(RasterFileName);
            //影像金字塔判断与创建
            IRasterPyramid rasPyrmid = rasterDataset as IRasterPyramid;

            if (rasPyrmid != null)
            {
                if (!(rasPyrmid.Present))
                {
                    rasPyrmid.Create();
                }
            }
            IRaster      raster = rasterDataset.CreateDefaultRaster();
            IRasterLayer rLayer = new RasterLayerClass();
            ILayer       layer  = rLayer as ILayer;

            return(layer);
        }
예제 #8
0
    public static void LoadGeoData(AxMapControl axMapControl1, AxMapControl axMapControl2, string strFileN)
    {
        string strFExtenN = System.IO.Path.GetExtension(strFileN);

        switch (strFExtenN)
        {
        case ".shp":
        {
            string strPath = System.IO.Path.GetDirectoryName(strFileN);
            string strFile = System.IO.Path.GetFileNameWithoutExtension(strFileN);
            axMapControl1.AddShapeFile(strPath, strFile);
            axMapControl2.ClearLayers();
            axMapControl2.AddShapeFile(strPath, strFile);
            axMapControl2.Extent = axMapControl2.FullExtent;
            break;
        }

        case ".bmp":
        case ".tif":
        case ".jpg":
        case ".img":
        {
            IWorkspaceFactory pWSF           = new RasterWorkspaceFactoryClass();
            string            pathName       = System.IO.Path.GetDirectoryName(strFileN);
            string            fileName       = System.IO.Path.GetFileName(strFileN);
            IWorkspace        pWS            = pWSF.OpenFromFile(pathName, 0);
            IRasterWorkspace  pRWS           = pWS as IRasterWorkspace;
            IRasterDataset    pRasterDataSet = pRWS.OpenRasterDataset(fileName);
            IRasterPyramid    pRasPyramid    = pRasterDataSet as IRasterPyramid;
            if (pRasPyramid != null)
            {
                if (!(pRasPyramid.Present))
                {
                    pRasPyramid.Create();
                }
            }
            IRaster      pRaster      = pRasterDataSet.CreateDefaultRaster();
            IRasterLayer pRasterLayer = new RasterLayerClass();
            pRasterLayer.CreateFromRaster(pRaster);
            ILayer pLayer = pRasterLayer as ILayer;
            axMapControl1.AddLayer(pLayer, 0);
            axMapControl2.ClearLayers();
            axMapControl2.AddLayer(pLayer, 0);
            axMapControl2.Extent = axMapControl2.FullExtent;
            break;
        }

        case ".mxd":
        {
            if (axMapControl1.CheckMxFile(strFExtenN))
            {
                axMapControl1.LoadMxFile(strFExtenN);
            }
            else
            {
                MessageBox.Show("所选择的文件不是Mxd文件!", "提示信息");
            }
            break;
        }

        default:
            break;
        }
    }
        /// <summary>
        /// 装在图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 装载图片(object sender, EventArgs e)
        {
            GC.Collect();
            aa         = DateTime.Now;
            quality[0] = (int)ImwriteFlags.JpegQuality;
            quality[1] = 100;
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title       = "请选择文件夹";
            dialog.Filter      = "图片和文本(*.tif)|*.tif|All files(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string        filejpg  = null;
                List <string> list     = new List <string>();
                List <Rect>   rects    = new List <Rect>();
                List <Rect>   rects1   = new List <Rect>();
                List <int>    width    = new List <int>();
                List <int>    height   = new List <int>();
                List <Mat>    newjpg   = new List <Mat>();
                string        path     = null;
                List <Mat>    bjpg     = new List <Mat>();
                List <string> tifname  = new List <string>();
                List <string> imgnames = new List <string>();
                List <int>    X        = new List <int>();
                List <int>    Y        = new List <int>();
                List <Mat>    tif      = new List <Mat>();
                List <int>    cols     = new List <int>();
                List <int>    rows     = new List <int>();
                path = System.IO.Path.GetDirectoryName(dialog.FileName);
                string txtname = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName);
                filejpg = dialog.FileName.ToString();
                if (!File.Exists(path + "\\" + txtname + ".txt"))
                {
                    MessageBox.Show("txt文件不存在!");
                    return;
                }
                System.IO.FileStream txtfs = new System.IO.FileStream(path + "\\" + txtname + ".txt", FileMode.Open);
                StreamReader         txtsr = new StreamReader(txtfs, Encoding.UTF8);
                string strLine;
                //读取文件中的一行
                do
                {
                    strLine = txtsr.ReadLine();
                    list.Add(strLine);
                } while (strLine != null);//判断是否为空,表示到文件最后一行了
                list.Remove(list[list.Count - 1]);
                foreach (string item in list)
                {
                    if (item.Contains("width:"))
                    {
                        string[] Array1 = item.Split(':');
                        width.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("height:"))
                    {
                        string[] Array1 = item.Split(':');
                        height.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("fname:"))
                    {
                        string[] Array1 = item.Split(':');
                        tifname.Add(Array1[1] + ":" + Array1[2]);
                    }
                    else if (item.Contains("X:"))
                    {
                        string[] Array1 = item.Split(':');
                        X.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("Y:"))
                    {
                        string[] Array1 = item.Split(':');
                        Y.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("col:"))
                    {
                        string[] Array1 = item.Split(':');
                        cols.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("row:"))
                    {
                        string[] Array1 = item.Split(':');
                        rows.Add(Convert.ToInt32(Array1[1]));
                    }
                    else if (item.Contains("imgname:"))
                    {
                        string[] Array1 = item.Split(':');
                        imgnames.Add(Array1[1]);
                    }
                }
                txtfs.Close();
                txtsr.Close();
                int           maxcol = cols.Max();
                int           maxrow = rows.Max();
                List <int>    XS     = new List <int>();
                List <int>    YS     = new List <int>();
                int           xx     = 0;
                int           yy     = 0;
                HashSet <int> xh     = new HashSet <int>(width);
                HashSet <int> yh     = new HashSet <int>(height);
                List <int>    xs     = xh.ToList <int>();
                List <int>    ys     = yh.ToList <int>();
                for (int i = 0; i < maxcol - (cols.Min() - 1); i++)
                {
                    XS.Add(xx);
                    xx += xs[i];
                }
                for (int i = 0; i < maxrow - (rows.Min() - 1); i++)
                {
                    YS.Add(yy);
                    yy += ys[i];
                }
                int a = 0;
                int b = 0;
                for (int j = 0; j < height.Count; j++)
                {
                    rects.Add(new Rect(XS[a], YS[b], width[j], height[j]));
                    if (a == maxcol - (cols.Min() - 1) - 1)
                    {
                        b++;
                        a = 0;
                        continue;
                    }
                    a++;
                }
                List <Mat> newimg = new List <Mat>();
                Mat        bigimg = new Mat(filejpg);
                for (int i = 0; i < rects.Count; i++)
                {
                    newimg.Add(new Mat(bigimg, rects[i]));
                    newjpg.Add(newimg[i]);
                }
                for (int i = 0; i < newjpg.Count; i++)
                {
                    if (File.Exists(tifname[i]))
                    {
                        tif.Add(new Mat(tifname[i]));
                        rects1.Add(new Rect(X[i], Y[i], width[i], height[i]));
                        Mat imageROI = new Mat(tif[i], rects1[i]);
                        newjpg[i].CopyTo(imageROI);
                        Cv2.ImWrite(tifname[i], tif[i], quality);
                    }
                    else
                    {
                        tif.Add(new Mat());
                        rects1.Add(new Rect());
                    }
                }
                for (int i = 0; i < imgnames.Count; i++)
                {
                    if (imgnames[i].Equals("null") || imgnames[i] == null)
                    {
                        continue;
                    }
                    for (int j = 0; j < this.MapControl.LayerCount; j++)
                    {
                        ILayer mILayer = this.MapControl.get_Layer(j);
                        if (mILayer.Name == imgnames[i])
                        {
                            this.MapControl.DeleteLayer(j);//删除图层
                            IDataLayer      mIDataLayer      = (IDataLayer)mILayer;
                            IName           mIName           = mIDataLayer.DataSourceName;
                            IDataset        mIDataset        = (IDataset)mIName.Open();
                            IRasterPyramid3 mIRasterPyramid3 = mIDataset as IRasterPyramid3;
                            mIRasterPyramid3.DeletePyramid();//删除缓存
                            break;
                        }
                    }
                    string            strFolderPath      = System.IO.Path.GetDirectoryName(tifname[i]);
                    string            strFileName        = System.IO.Path.GetFileName(tifname[i]);
                    IWorkspaceFactory mIWorkspaceFactory = new RasterWorkspaceFactoryClass();
                    IWorkspace        mIWorkspace        = mIWorkspaceFactory.OpenFromFile(strFolderPath, 0);
                    IRasterWorkspace  mIRasterWorkspace  = mIWorkspace as IRasterWorkspace;
                    IRasterDataset    mIRasterDataset    = mIRasterWorkspace.OpenRasterDataset(strFileName);
                    IRasterPyramid    mIRasterPyramid    = mIRasterDataset as IRasterPyramid;
                    if (mIRasterPyramid != null)
                    {
                        if (mIRasterPyramid.Present == false)
                        {
                            mIRasterPyramid.Create();//创建缓存
                        }
                    }
                    IRaster      mIRaster      = mIRasterDataset.CreateDefaultRaster();
                    IRasterLayer mIRasterLayer = new RasterLayerClass();
                    mIRasterLayer.CreateFromRaster(mIRaster);
                    ILayer newILayer = mIRasterLayer as ILayer;
                    this.MapControl.AddLayer(newILayer, 0);//加入地图
                }
                DateTime bb = DateTime.Now;
                MessageBox.Show("完成(" + (bb - aa).TotalSeconds + "s)");
            }
        }