zoukankan      html  css  js  c++  java
  • IPixelBlock chongxin zhengli

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using ESRI.ArcGIS.DataSourcesRaster;
    using ESRI.ArcGIS.DataSourcesFile;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.esriSystem;
    using ESRI.ArcGIS.Geometry;


    namespace _4个像素合并成1个像素
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private  IRasterDataset pPublicRasterdataset;
            private IRasterWorkspace2 pPublicRasterWorkspace2;
            private IRasterWorkspace pPublicRasterWorkspace;

           
            private void Pathbutton_Click(object sender, EventArgs e)
            {
                try
                {
                    OpenFileDialog opd = new OpenFileDialog();
                  
                    //opd.InitialDirectory = @"C:\";
                    opd.Filter = "IMAGINE(*.img)|*.img";
                    opd.FilterIndex = 1;
                    opd.RestoreDirectory = true;
                    string filepath, filename;

                    if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
                    {
                        PathtextBox.Text = opd.FileName;
                        int index = opd.FileName.LastIndexOf("\\");
                        filepath = opd.FileName.Substring(0, index + 1);
                        filename = opd.FileName.Substring(index + 1);

                        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
                        IWorkspace workspace;
                        workspace = workspaceFactory.OpenFromFile(filepath, 0); //inPath栅格数据存储路径

                        IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
                        IRasterDataset rastDataset;
                        rastDataset = rastWork.OpenRasterDataset(filename) as IRasterDataset;//inName栅格文件名

                        pPublicRasterdataset = rastDataset;
                        pPublicRasterWorkspace2 = (IRasterWorkspace2)rastWork;
                        pPublicRasterWorkspace = rastWork;


                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            private float GetMax(List<float> list)
            {
                float max = list[0];
                foreach (float d in list)
                {
                    if (d >= max)
                        max = d;
                }
                return max;
     
            }
            private float GetMin(List<float> list)
            {
                float min = list[0];
                foreach (float d in list)
                {
                    if (d <= min)
                        min = d;
                }
                return min;

            }

            //public IRasterWorkspace2 createRasterWorkspace(string pathName)
            //{
            //    // Create RasterWorkspace
            //    IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            //    return workspaceFactory.OpenFromFile(pathName, 0) as IRasterWorkspace2;
            //}

            //float = ushort
            private int windowWidth = 0;
            private void Generatebutton_Click(object sender, EventArgs e)
            {
                try
                {


                    windowWidth = int.Parse(MergetextBox.Text);

                    List<float> pixelblocklist;

                    IRaster pRaster = pPublicRasterdataset.CreateDefaultRaster();

                    IRasterProps rasterProps = (IRasterProps)pRaster;
                   
                    int dHeight = rasterProps.Height;//当前栅格数据集的行数
                    int dWidth = rasterProps.Width; //当前栅格数据集的列数
                    double dX = rasterProps.MeanCellSize().X; //栅格的宽度
                    double dY = rasterProps.MeanCellSize().Y; //栅格的高度
                    IEnvelope extent = rasterProps.Extent; //当前栅格数据集的范围
                    rstPixelType pixelType = rasterProps.PixelType; //当前栅格像素类型8bit、16bit等等
                    ISpatialReference spatialRefenence = rasterProps.SpatialReference;//

                    //set value to rasterdataset
                    IPnt pntSize = new DblPntClass();
                    pntSize.SetCoords(windowWidth * dX, windowWidth * dY); //创建像块大小为2dX*2dY,用坐标来表示的pixel

                    IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
                    IPnt pnt = new PntClass();
                    float[,] Mergevalues = new float[dWidth / windowWidth, dHeight / windowWidth];
                    float count = 0;
                    MainprogressBar.Minimum = 0;
                    MainprogressBar.Maximum = ((dWidth / windowWidth) - 1)/5;
                    MainprogressBar.Step = 1;

                    for (int i = 0; i < (dWidth - dWidth % windowWidth); )
                    {
                        for (int j = 0; j < (dHeight - dHeight % windowWidth); )
                        {

                            pnt.SetCoords(i, j);
                            pRaster.Read(pnt, pixelBlock);

                            if (pixelBlock != null)
                            {
                                pixelblocklist = new List<float>();

                                for (int k = 0; k < windowWidth; k++)
                                {
                                    for (int w = 0; w < windowWidth; w++)
                                    {
                                       
                                        pixelblocklist.Add(Convert.ToSingle(pixelBlock.GetVal(0, k, w)));

                                    }
                                }

       
                                Mergevalues[i / windowWidth, j / windowWidth] = (float)(GetMax(pixelblocklist) - GetMin(pixelblocklist));
                                pixelblocklist = null;

                            }

                            j += windowWidth;
                        }

                        //MainprogressBar.Update();
                        //MainprogressBar.Value = (int)count++;
                        //MainprogressBar.Update();
                        if (MainprogressBar.Value + 5 < MainprogressBar.Maximum)
                        {
                            MainprogressBar.Increment(5);
                        }

                       
                        i += windowWidth;
                    }

                    //generate new rasterdataset
                    IRasterDataset rasterDataset;
                    IPoint originPoint = new ESRI.ArcGIS.Geometry.Point();
                    //这里也要注意,原点为extent.XMin, extent.YMin + (dHeight % windowWidth) * dY
                    originPoint.PutCoords(extent.XMin, extent.YMin + (dHeight % windowWidth) * dY);


                    //只是create,并没有open
                    rasterDataset = pPublicRasterWorkspace2.CreateRasterDataset(NewNametextBox.Text + ".img", "IMAGINE Image", originPoint,
                        dWidth / windowWidth, dHeight / windowWidth, dX * windowWidth, dY * windowWidth, 1, pixelType, spatialRefenence, true);

                    //rasterDataset = pPublicRasterWorkspace.OpenRasterDataset("new.img");


                    IRawPixels rawPixels = null;
                    IPixelBlock3 pixelBlock3 = null;
                    IPnt pixelBlockOrigin = null;
                    IPnt pixelBlockSize = null;
                    IRasterBandCollection rasterBandCollection;
                    IRasterProps rasterPropsAgain;

                    // QI for IRawPixels and IRasterProps
                    rasterBandCollection = (IRasterBandCollection)rasterDataset;
                    rawPixels = (IRawPixels)rasterBandCollection.Item(0);
                    rasterPropsAgain = (IRasterProps)rawPixels;

                    // Create pixelblock
                    pixelBlockOrigin = new DblPntClass();
                   
                    //在这里我犯了致命错误,pixelblock原点从左上角开始计算,与创建rasterdataset不同(原点是右下角) zhangjun here
                    //pixelBlockOrigin.SetCoords(extent.XMin, extent.YMin + (dHeight % windowWidth) * dY);
                    pixelBlockOrigin.SetCoords(0, 0);

                    pixelBlockSize = new DblPntClass();
                    //pixelBlockSize.SetCoords(0, 0); //NOYIICE Width is X and Height is Y ERROR!
                    pixelBlockSize.SetCoords(rasterPropsAgain.Width, rasterPropsAgain.Height);

                    pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize);

                    // Read pixelblock
                    rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3);

                    // Get pixeldata array
                    System.Array pixelData;
                    pixelData = (System.Array)pixelBlock3.get_PixelDataByRef(0);


                    // Loop through all the pixels and assign value;    Width is X ;Height is Y; width*height = X*Y
                    for (int i = 0; i < rasterPropsAgain.Width; i++)
                        for (int j = 0; j < rasterPropsAgain.Height; j++)
                            pixelData.SetValue(Convert.ToUInt16(Mergevalues[i, j]), i, j);

                    pixelBlock3.set_PixelData(0, (System.Object)pixelData);


                    // Write the pixeldata back
                    System.Object cachePointer;

                    cachePointer = rawPixels.AcquireCache();

                    rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3);

                    rawPixels.ReturnCache(cachePointer);


                    MessageBox.Show("生成像块完毕!");

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

           }

        
        }
    }

  • 相关阅读:
    C#类继承情况下构造函数的执行过程
    vs快捷键大全(分类导航)
    vs常用快捷键
    Entity Framework实体无限循环问题
    entity framework实现left join功能
    理解requireJS-实现一个简单的模块加载器
    petshop4.0(王福朋)
    代码覆盖率工具 Istanbul 入门教程
    .net微信开发
    numpy的用法(一)
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1706391.html
Copyright © 2011-2022 走看看