コード例 #1
0
        /// <summary>
        /// Get the pixel values in a region of the input raster.
        /// </summary>
        /// <param name="tlCorner"></param>
        /// <param name="brCorner"></param>
        /// <param name="raster"></param>
        /// <returns></returns>
        public static double[,] GetValues(Position tlCorner, Position brCorner, IRaster raster)
        {
            int colCount = brCorner.Column - tlCorner.Column + 1;
            int rowCount = brCorner.Row - tlCorner.Row + 1;

            IPnt regionSize = new PntClass();

            regionSize.SetCoords(colCount, rowCount);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(regionSize);
            IPnt        tl         = new PntClass();

            tl.SetCoords(tlCorner.Column, tlCorner.Row);
            raster.Read(tl, pixelBlock);

            double[,] values = new double[colCount, rowCount];
            for (int x = 0; x < colCount; x++)
            {
                for (int y = 0; y < rowCount; y++)
                {
                    Pixel cell = Editor.Edits[x + tlCorner.Column, y + tlCorner.Row];

                    if (cell == null)
                    {
                        values[x, y] = Convert.ToDouble(pixelBlock.GetVal(0, x, y));
                    }
                    else
                    {
                        values[x, y] = cell.NewValue;
                    }
                }
            }
            return(values);
        }
コード例 #2
0
        private void adjustXTableRst(IGeometry iGeometry)
        {
            double        tN    = 0;
            IRaster       rs    = rsUtil.createRaster(rsUtil.clipRasterFunction(rstMap, iGeometry, ESRI.ArcGIS.DataSourcesRaster.esriRasterClippingType.esriRasterClippingOutside));
            IRasterCursor rsCur = ((IRaster2)rs).CreateCursorEx(null);

            while (rsCur.Next())
            {
                IPixelBlock pb = rsCur.PixelBlock;
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj == null)
                        {
                            continue;
                        }
                        else
                        {
                            string vl      = vlObj.ToString();
                            int    vlIndex = olabels.IndexOf(vl);
                            nCnts[vlIndex] = nCnts[vlIndex] + 1;
                            tN            += 1;
                        }
                    }
                }
            }
            updateXTable(tN);
        }
コード例 #3
0
        /// <summary>
        /// 读取栅格数据。
        /// </summary>
        public static float[,] ReadRaster(IRasterLayer rasterLayer, float nullValue)
        {
            int columnCount = rasterLayer.ColumnCount;
            int rowCount    = rasterLayer.RowCount;

            IRaster2 raster  = rasterLayer.Raster as IRaster2;
            IPnt     fromPnt = new PntClass();

            fromPnt.SetCoords(0, 0);
            IPnt blockSize = new PntClass();

            blockSize.SetCoords(columnCount, rowCount);
            IPixelBlock pixedBlock = ((IRaster)raster).CreatePixelBlock(blockSize);

            rasterLayer.Raster.Read(fromPnt, pixedBlock);

            float[,] data = new float[rowCount, columnCount];

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    object value = pixedBlock.GetVal(0, j, i);
                    if (value != null)
                    {
                        data[i, j] = Convert.ToSingle(value);
                    }
                    else
                    {
                        data[i, j] = nullValue;
                    }
                }
            }
            return(data);
        }
コード例 #4
0
        private void getRasterMatrix()
        {
            n = 0;
            List <double[]>       inputMatrixLst = new List <double[]>();
            IRaster2              rs2            = (IRaster2)InRaster;
            IRasterBandCollection rsbc           = (IRasterBandCollection)rs2;
            IRasterProps          rsp            = (IRasterProps)rs2;
            //System.Array nDataVlArr = (System.Array)rsp.NoDataValue;
            IRasterCursor rsCur = rs2.CreateCursorEx(null);
            IPixelBlock   pb    = null;
            Random        rand  = new Random();

            do
            {
                pb = rsCur.PixelBlock;
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj == null)
                        {
                            continue;
                        }
                        else
                        {
                            if (rand.NextDouble() <= prop)
                            {
                                double[] vlBandArr = new double[rsbc.Count];
                                double   vl        = System.Convert.ToDouble(vlObj);
                                vlBandArr[0] = vl;
                                for (int p = 1; p < pb.Planes; p++)
                                {
                                    vlObj = pb.GetVal(p, c, r);
                                    vl    = System.Convert.ToDouble(vlObj);

                                    vlBandArr[p] = vl;
                                }
                                inputMatrixLst.Add(vlBandArr);
                                n++;
                            }
                        }
                    }
                }
            } while (rsCur.Next() == true);
            inputMatrix = inputMatrixLst.ToArray();
        }
コード例 #5
0
        //获取最大最小栅格值
        private void SetMaxMinValue(int flag)
        {
            if (flag == 1)
            {
                textBoxMax.Text = "高:255";
                textBoxMin.Text = "低:0";
                return;
            }
            uint valueMax = 50;
            uint valueMin = 50;

            IRasterLayer pRasterLayer = m_layer as IRasterLayer;
            IRaster      pRaster      = pRasterLayer.Raster;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            int          Height       = pRasterProps.Height;
            int          Width        = pRasterProps.Width;
            double       dX           = pRasterProps.MeanCellSize().X;
            double       dY           = pRasterProps.MeanCellSize().Y; //栅格的高度
            IEnvelope    extent       = pRasterProps.Extent;           //当前栅格数据集的范围
            rstPixelType pixelType    = pRasterProps.PixelType;        //当前栅格像素类型
            IPnt         pntSize      = new PntClass();

            pntSize.SetCoords(dX, dY);


            IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
            IPnt        pnt        = new PntClass();

            for (int i = 0; i < Height; i += 10)
            {
                for (int j = 0; j < Width; j += 10)
                {
                    pnt.SetCoords(i, j);
                    pRaster.Read(pnt, pixelBlock);
                    if (pixelBlock != null)
                    {
                        object obj  = pixelBlock.GetVal(0, 0, 0);
                        uint   temp = Convert.ToUInt32(obj);

                        if (temp > valueMax)
                        {
                            valueMax = temp;
                        }
                        else if (temp < valueMin)
                        {
                            valueMin = temp;
                        }
                    }
                }
            }
            textBoxMax.Text = "高:" + valueMax.ToString();
            textBoxMin.Text = "低:" + valueMin.ToString();
        }
コード例 #6
0
        /// <summary>
        /// 读取数据并记录非空数据的行列号。
        /// </summary>
        public static float[,] ReadRasterAndGetNotNullRowColumn(IRasterLayer rasterLayer, out StructRasterMetaData structRasterMetaData,
                                                                out List <int> notNullRows, out List <int> notNullColumns)
        {
            int rowCount, columnCount;

            notNullRows    = new List <int>();
            notNullColumns = new List <int>();

            structRasterMetaData.ColumnCount = rasterLayer.ColumnCount;
            columnCount = structRasterMetaData.ColumnCount;
            structRasterMetaData.RowCount = rasterLayer.RowCount;
            rowCount = structRasterMetaData.RowCount;

            IEnvelope visiableExtent = rasterLayer.VisibleExtent;

            structRasterMetaData.XMin        = visiableExtent.XMin;
            structRasterMetaData.XMax        = visiableExtent.XMax;
            structRasterMetaData.YMin        = visiableExtent.YMin;
            structRasterMetaData.YMax        = visiableExtent.YMax;
            structRasterMetaData.NoDataValue = -9999f;

            IRaster2 raster  = rasterLayer.Raster as IRaster2;
            IPnt     fromPnt = new PntClass();

            fromPnt.SetCoords(0, 0);
            IPnt blockSize = new PntClass();

            blockSize.SetCoords(columnCount, rowCount);
            IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize);

            rasterLayer.Raster.Read(fromPnt, pixelBlock);
            float[,] data = new float[rowCount, columnCount];

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    object value = pixelBlock.GetVal(0, j, i);
                    if (value != null)
                    {
                        data[i, j] = Convert.ToSingle(value);
                        notNullRows.Add(i);
                        notNullColumns.Add(j);
                    }
                    else
                    {
                        data[i, j] = structRasterMetaData.NoDataValue;
                    }
                }
            }
            return(data);
        }
コード例 #7
0
        /// <summary>
        /// Gets value of pixel at the specified position.
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="raster"></param>
        /// <returns></returns>
        public static double GetValue(Position pos, IRaster raster)
        {
            IPnt regionSize = new PntClass();

            regionSize.SetCoords(1, 1);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(regionSize);
            IPnt        tl         = new PntClass();

            tl.SetCoords(pos.Column, pos.Row);
            raster.Read(tl, pixelBlock);

            return(Convert.ToDouble(pixelBlock.GetVal(0, 0, 0)));
        }
コード例 #8
0
        private void createDictionaryArray()
        {
            IEnvelope env1 = referenceRaster.RasterInfo.Extent;
            IEnvelope env2 = transformRaster.RasterInfo.Extent;

            env1.Intersect(env2);
            clipGeo = (IGeometry)env1;
            IFunctionRasterDataset minRs = rsUtil.calcArithmaticFunction(referenceRaster, transformRaster, esriRasterArithmeticOperation.esriRasterMinus);

            clipRs = rsUtil.clipRasterFunction(minRs, clipGeo, esriRasterClippingType.esriRasterClippingOutside);
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(512, 512);
            IRasterCursor rsCur = ((IRaster2)rsUtil.createRaster(clipRs)).CreateCursorEx(pntSize);
            int           pCnt  = rsCur.PixelBlock.Planes;

            do
            {
                IPixelBlock pbMinBlock = rsCur.PixelBlock;
                for (int r = 0; r < pbMinBlock.Height; r++)
                {
                    for (int c = 0; c < pbMinBlock.Width; c++)
                    {
                        for (int p = 0; p < pCnt; p++)
                        {
                            object vlObj = pbMinBlock.GetVal(p, c, r);
                            if (vlObj == null)
                            {
                                continue;
                            }
                            else
                            {
                                int vl = System.Convert.ToInt32(vlObj);
                                Dictionary <int, int> cDic = difDic[p];
                                int cnt = 0;
                                if (!cDic.TryGetValue(vl, out cnt))
                                {
                                    cDic.Add(vl, 1);
                                }
                                else
                                {
                                    cDic[vl] = cnt + 1;
                                }
                                cellCount[p] += 1;
                            }
                        }
                    }
                }
            } while (rsCur.Next() == true);
        }
コード例 #9
0
        private double[][] getValues(IPoint pnt, out double[] rwVls)
        {
            rwVls = new double[bndCnt];
            double[][] outJagArr = new double[(geoerro * geoerro)][];
            for (int i = 0; i < outJagArr.Length; i++)
            {
                outJagArr[i] = new double[bndCnt];
            }
            IRasterBandCollection rsBc = (IRasterBandCollection)FunctionRasterDataset;
            IRasterBand           rsB;
            IPnt pSize = new PntClass();

            pSize.SetCoords(geoerro, geoerro);
            IPnt      pLoc = new PntClass();
            int       clm, rw;
            IGeometry geo      = (IGeometry)FunctionRasterDataset.RasterInfo.Extent;
            IPoint    ul       = FunctionRasterDataset.RasterInfo.Extent.UpperLeft;
            IPnt      cellSize = FunctionRasterDataset.RasterInfo.CellSize;
            int       sub      = (geoerro / 2);

            rsUtil.getClmRw(ul, cellSize, pnt, out clm, out rw);
            int nclm = clm - sub;
            int nrw  = rw - sub;

            pLoc.SetCoords(System.Convert.ToDouble(nclm), System.Convert.ToDouble(nrw));
            for (int i = 0; i < bndCnt; i++)
            {
                rsB = rsBc.Item(i);
                IRawPixels  rpix = (IRawPixels)rsB;
                IPixelBlock pb   = rpix.CreatePixelBlock(pSize);
                rpix.Read(pLoc, pb);
                int pbCnt = 0;
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj != null)
                        {
                            outJagArr[pbCnt][i] = System.Convert.ToDouble(vlObj);
                        }
                        pbCnt++;
                    }
                }
            }
            rwVls = outJagArr[(geoerro * geoerro / 2)];
            return(outJagArr);
        }
コード例 #10
0
ファイル: RasterReader.cs プロジェクト: gaufung/Accessibility
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="xIndex">水平方向的序号</param>
        /// <param name="yIndex">垂直方向的序号</param>
        /// <returns>获取的值,如果为null,表明该位置没有数据</returns>
        public object Read(int xIndex, int yIndex)
        {
            IRaster pRaster = GetRaster();
            IPnt    pnt     = new PntClass();

            pnt.SetCoords(xIndex, yIndex);
            IPnt pntSize = new PntClass();

            pntSize.SetCoords(1, 1);
            IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);

            pRaster.Read(pnt, pixelBlock);
            object obj = pixelBlock.GetVal(0, 0, 0);

            return(obj);
        }
コード例 #11
0
        private static Dictionary <string, double> getStrataProportion(IFunctionRasterDataset strataRaster, rasterUtil rsUtil)
        {
            IRaster2 rs2 = (IRaster2)rsUtil.createRaster(strataRaster);
            Dictionary <string, double> outDic = new Dictionary <string, double>();
            IRasterCursor rsCur = rs2.CreateCursorEx(null);
            //Console.WriteLine(((IRasterProps)rs2).Height.ToString() + ((IRasterProps)rs2).Height.ToString());
            int n = 0;

            do
            {
                IPixelBlock pb = rsCur.PixelBlock;
                //Console.WriteLine("PixelBLock w_h = " + pb.Width.ToString() + "_" + pb.Height.ToString());
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object vlObj = pb.GetVal(0, c, r);
                        if (vlObj != null)
                        {
                            string vl = vlObj.ToString();
                            double vlCnt;
                            if (outDic.TryGetValue(vl, out vlCnt))
                            {
                                outDic[vl] = vlCnt + 1;
                            }
                            else
                            {
                                outDic.Add(vl, 1);
                            }
                            n += 1;
                        }
                        else
                        {
                            //Console.WriteLine("VL Null");
                        }
                    }
                }
            } while (rsCur.Next() == true);
            //Console.WriteLine("OutDic Count = " + outDic.Count.ToString());
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rsCur);
            foreach (string s in outDic.Keys.ToArray())
            {
                double vl = outDic[s];
                outDic[s] = vl / n;
            }
            return(outDic);
        }
コード例 #12
0
        private Dictionary <double, object[]> zoneValueDic = null;//value = [count,max,min,sum,sum2,dictionary<int,int>]->dictionary is for unique, entropy, and ASM
        private void calcZoneValues()
        {
            bool   makeDic              = (ZoneClassCount || ZoneTypes.Contains(rasterUtil.zoneType.VARIETY) || ZoneTypes.Contains(rasterUtil.zoneType.ENTROPY) || ZoneTypes.Contains(rasterUtil.zoneType.ASM) || ZoneTypes.Contains(rasterUtil.zoneType.MINORITY) || ZoneTypes.Contains(rasterUtil.zoneType.MODE) || ZoneTypes.Contains(rasterUtil.zoneType.MEDIAN));
            IPnt   zMeanCellSize        = zRs.RasterInfo.CellSize;
            IPnt   vMeanCellSize        = vRs.RasterInfo.CellSize;
            int    intersectWidthCells  = System.Convert.ToInt32(intEnv.Width / zMeanCellSize.X);
            int    intersectHeightCells = System.Convert.ToInt32(intEnv.Height / zMeanCellSize.Y);
            IPoint tl        = intEnv.UpperLeft;
            int    bH        = 512;
            int    bW        = 512;
            int    wCellsMax = intersectWidthCells;
            int    hCellsMax = intersectHeightCells;
            IPnt   zPntLoc   = new PntClass();
            IPnt   vPntLoc   = new PntClass();
            IPnt   zPntSize  = new PntClass();
            IRasterFunctionHelper inZoneHelper = new RasterFunctionHelperClass();

            inZoneHelper.Bind(InZoneRaster);
            IRasterFunctionHelper inValueHelper = new RasterFunctionHelperClass();

            inValueHelper.Bind(InValueRaster);
            IRaster2 zr = (IRaster2)inZoneHelper.Raster;
            IRaster2 vr = (IRaster2)inValueHelper.Raster;
            int      zclm, zrw, vclm, vrw;

            zr.MapToPixel(tl.X + (zMeanCellSize.X / 2), tl.Y - (zMeanCellSize.Y / 2), out zclm, out zrw);
            vr.MapToPixel(tl.X + (zMeanCellSize.X / 2), tl.Y - (zMeanCellSize.Y / 2), out vclm, out vrw);
            int ozclm = zclm;
            int ozrw  = zrw;
            int ovclm = vclm;
            int ovrw  = vrw;

            zPntLoc.SetCoords(zclm, zrw);
            vPntLoc.SetCoords(vclm, vrw);
            for (int brw = 0; brw < hCellsMax; brw += bH)
            {
                int rH = hCellsMax - brw;//Height of block
                if (rH > bH)
                {
                    rH = bH;
                }
                for (int bclm = 0; bclm < wCellsMax; bclm += bW)
                {
                    int cW = wCellsMax - bclm;//Width of block
                    if (cW > bW)
                    {
                        cW = bW;
                    }
                    zPntSize.SetCoords(cW, rH);
                    IPixelBlock zPb = ((IRaster)zr).CreatePixelBlock(zPntSize);
                    IPixelBlock vPb = ((IRaster)vr).CreatePixelBlock(zPntSize);
                    inZoneHelper.Read(zPntLoc, null, (IRaster)zr, zPb);
                    inValueHelper.Read(vPntLoc, null, (IRaster)vr, vPb);
                    for (int i = 0; i < vPb.Planes; i++)
                    {
                        zoneValueDic = zoneValueDicArr[i];
                        //double vNoDataVl = System.Convert.ToDouble(((System.Array)vProps.NoDataValue).GetValue(i));
                        //System.Array vPix = (System.Array)vPb.get_SafeArray(i);
                        for (int r = 0; r < rH; r++)
                        {
                            for (int c = 0; c < cW; c++)
                            {
                                object zObj = zPb.GetVal(0, c, r);
                                if (zObj == null)
                                {
                                    continue;
                                }
                                else
                                {
                                    double z    = System.Convert.ToDouble(zObj);
                                    object vObj = vPb.GetVal(i, c, r);

                                    if (vObj == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        double   v = System.Convert.ToDouble(vObj);
                                        object[] zoneValue;
                                        if (zoneValueDic.TryGetValue(z, out zoneValue))
                                        {
                                            double cnt = System.Convert.ToDouble(zoneValue[0]);
                                            zoneValue[0] = cnt += 1;
                                            double maxVl = System.Convert.ToDouble(zoneValue[1]);
                                            if (v > maxVl)
                                            {
                                                maxVl        = v;
                                                zoneValue[1] = maxVl;
                                            }
                                            double minVl = System.Convert.ToDouble(zoneValue[2]);
                                            if (v < minVl)
                                            {
                                                minVl        = v;
                                                zoneValue[2] = minVl;
                                            }
                                            double s = System.Convert.ToDouble(zoneValue[3]);
                                            zoneValue[3] = s + v;
                                            double s2 = System.Convert.ToDouble(zoneValue[4]);
                                            zoneValue[4] = s2 + v * v;
                                            if (makeDic)
                                            {
                                                Dictionary <double, int> uDic = (Dictionary <double, int>)zoneValue[5];
                                                int cntVl = 0;
                                                if (uDic.TryGetValue(v, out cntVl))
                                                {
                                                    uDic[v] = cntVl += 1;
                                                }
                                                else
                                                {
                                                    uDic.Add(v, 1);
                                                }
                                                zoneValue[5] = uDic;
                                            }
                                            zoneValueDic[z] = zoneValue;
                                        }
                                        else
                                        {
                                            zoneValue    = new object[6];
                                            zoneValue[0] = 1d;
                                            zoneValue[1] = v;
                                            zoneValue[2] = v;
                                            zoneValue[3] = v;
                                            zoneValue[4] = v * v;
                                            if (makeDic)
                                            {
                                                Dictionary <double, int> uDic = new Dictionary <double, int>();
                                                uDic.Add(v, 1);
                                                zoneValue[5] = uDic;
                                            }
                                            zoneValueDic.Add(z, zoneValue);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //do this at the end
                    zclm = zclm + cW;
                    vclm = vclm + cW;
                    zPntLoc.SetCoords(zclm, zrw);
                    vPntLoc.SetCoords(vclm, vrw);
                }
                zrw = zrw + rH;
                vrw = vrw + rH;
                //reset PixelBlock columns
                zclm = ozclm;
                vclm = ovclm;
                zPntLoc.SetCoords(zclm, zrw);
                vPntLoc.SetCoords(vclm, vrw);
            }
        }
コード例 #13
0
        private void calcZoneValuesFtr()
        {
            //Console.WriteLine("made it to the feature calculations");
            bool makeDic = (ZoneClassCount || ZoneTypes.Contains(rasterUtil.zoneType.VARIETY) || ZoneTypes.Contains(rasterUtil.zoneType.ENTROPY) || ZoneTypes.Contains(rasterUtil.zoneType.ASM) || ZoneTypes.Contains(rasterUtil.zoneType.MINORITY) || ZoneTypes.Contains(rasterUtil.zoneType.MODE) || ZoneTypes.Contains(rasterUtil.zoneType.MEDIAN));
            //
            //HashSet<byte> hByt = new HashSet<byte>();
            //
            ISpatialReference sr     = vRs.RasterInfo.SpatialReference;
            IEnvelope         vrsEnv = vRs.RasterInfo.Extent;
            ISpatialFilter    spFilt = new SpatialFilterClass();

            spFilt.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;
            spFilt.Geometry      = vrsEnv;
            spFilt.GeometryField = ftrCls.ShapeFieldName;
            IFeatureCursor fCur      = ftrCls.Search(spFilt, true);
            int            zoneIndex = fCur.FindField(InZoneField);
            IFeature       ftr       = fCur.NextFeature();

            while (ftr != null)
            {
                IGeometry geo  = ftr.Shape;
                double    z    = System.Convert.ToDouble(ftr.get_Value(zoneIndex));
                IPolygon4 poly = (IPolygon4)geo;
                if (needToProject)
                {
                    poly.Project(sr);
                }
                IGeometryBag        geoBag  = poly.ExteriorRingBag;
                IGeometryCollection geoColl = (IGeometryCollection)geoBag;
                for (int g = 0; g < geoColl.GeometryCount; g++)
                {
                    IGeometry geo2                  = geoColl.Geometry[g];
                    IFunctionRasterDataset rs       = rsUtil.clipRasterFunction(vRs, geo2, esriRasterClippingType.esriRasterClippingOutside);
                    IEnvelope             rsEnv     = rs.RasterInfo.Extent;
                    IRasterFunctionHelper rsFHelper = new RasterFunctionHelperClass();
                    rsFHelper.Bind(rs);
                    //Console.WriteLine((rsEnv.Width / 30).ToString() + ", " + (rsEnv.Height / 30).ToString());
                    IRasterCursor rsCur = ((IRaster2)rsFHelper.Raster).CreateCursorEx(null);
                    do
                    {
                        IPixelBlock pb = rsCur.PixelBlock;
                        for (int p = 0; p < pb.Planes; p++)
                        {
                            zoneValueDic = zoneValueDicArr[p];
                            object[] zoneValue;
                            double   cnt   = 0;
                            double   maxVl = Double.MinValue;
                            double   minVl = Double.MaxValue;
                            double   s     = 0;
                            double   s2    = 0;
                            Dictionary <double, int> uDic = null;
                            if (zoneValueDic.TryGetValue(z, out zoneValue))
                            {
                                cnt   = System.Convert.ToDouble(zoneValue[0]);
                                maxVl = System.Convert.ToDouble(zoneValue[1]);
                                minVl = System.Convert.ToDouble(zoneValue[2]);
                                s     = System.Convert.ToDouble(zoneValue[3]);
                                s2    = System.Convert.ToDouble(zoneValue[4]);
                                uDic  = (Dictionary <double, int>)zoneValue[5];
                            }
                            else
                            {
                                zoneValue    = new object[6];
                                zoneValue[0] = cnt;
                                zoneValue[1] = maxVl;
                                zoneValue[2] = minVl;
                                zoneValue[3] = s;
                                zoneValue[4] = s2;
                                uDic         = null;
                                if (makeDic)
                                {
                                    uDic = new Dictionary <double, int>();
                                }
                                zoneValue[5] = uDic;
                            }
                            for (int r = 0; r < pb.Height; r++)
                            {
                                for (int c = 0; c < pb.Width; c++)
                                {
                                    object vlo = pb.GetVal(p, c, r);
                                    if (vlo == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        double vl = System.Convert.ToDouble(vlo);
                                        cnt++;
                                        if (vl > maxVl)
                                        {
                                            maxVl = vl;
                                        }
                                        if (vl < minVl)
                                        {
                                            minVl = vl;
                                        }
                                        s  += vl;
                                        s2 += vl * vl;
                                        if (makeDic)
                                        {
                                            int cntVl = 0;
                                            if (uDic.TryGetValue(vl, out cntVl))
                                            {
                                                uDic[vl] = cntVl += 1;
                                            }
                                            else
                                            {
                                                uDic.Add(vl, 1);
                                            }
                                        }
                                    }
                                }
                            }
                            zoneValue[0]    = cnt;
                            zoneValue[1]    = maxVl;
                            zoneValue[2]    = minVl;
                            zoneValue[3]    = s;
                            zoneValue[4]    = s2;
                            zoneValue[5]    = uDic;
                            zoneValueDic[z] = zoneValue;
                        }
                    } while (rsCur.Next());
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rsCur);
                }
                ftr = fCur.NextFeature();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(fCur);
        }
コード例 #14
0
        static void Main(string[] args)
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst });//{esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst});
            System.DateTime dt = System.DateTime.Now;

            System.DateTime dt2;
            TimeSpan        ts;

            rasterUtil         rsUtil  = new rasterUtil();
            geoDatabaseUtility geoUtil = new geoDatabaseUtility();
            string             pred    = @"C:\Users\jshogland\Documents\John\projects\FtStewart\models\TreeEstimates\HdwPresentPred.bch";
            string             mdl     = @"C:\Users\jshogland\Documents\John\projects\FtStewart\models\TreeEstimates\HdwPresent.mdl";

            double[] minArr, maxArr;
            Console.WriteLine("Getting Min Max");
            batchCalculations.getMinMaxArr(mdl, out minArr, out maxArr);
            Console.WriteLine("First Min Max: " + minArr[0].ToString() + ";" + maxArr[0].ToString());
            IFunctionRasterDataset fdset = rsUtil.extactModelDomainFunction(pred, minArr, maxArr);

            Console.WriteLine(fdset.RasterInfo.PixelType.ToString());
            IRaster rs      = rsUtil.createRaster(fdset);
            IPnt    pntSize = new PntClass();

            pntSize.SetCoords(100, 100);
            IPixelBlock pb = rs.CreatePixelBlock(pntSize);

            rs.Read(pntSize, pb);
            int cnt = 0;

            for (int r = 0; r < pb.Height; r++)
            {
                for (int c = 0; c < pb.Width; c++)
                {
                    object vl = pb.GetVal(0, c, r);
                    if (vl != null)
                    {
                        if (System.Convert.ToInt32(vl) == 0)
                        {
                            cnt++;
                        }
                    }
                }
            }
            Console.WriteLine("Total zeros = " + cnt.ToString());
            //string dir = @"C:\Users\jshogland\Documents\John\projects\UMGradSchool\Classes\Mining Big Data\Assignments\NatureServe\train\ALB";
            //string[] fls = System.IO.Directory.GetFiles(dir, "*.jpg");
            //foreach (string flPath in fls)
            //{
            //    string nm = System.IO.Path.GetFileNameWithoutExtension(flPath);
            //    Console.WriteLine(nm);
            //    Bitmap bm = new Bitmap(flPath);
            //    Accord.Imaging.Moments.CentralMoments cm = new Accord.Imaging.Moments.CentralMoments(bm, 3);
            //    //Console.WriteLine(cm.Mu00.ToString());
            //    Console.WriteLine("\t"+cm.Mu01.ToString());
            //    Console.WriteLine("\t" + cm.Mu02.ToString());
            //    Console.WriteLine("\t" + cm.Mu03.ToString());
            //    Console.WriteLine("\t" + cm.Mu10.ToString());
            //    Console.WriteLine("\t" + cm.Mu20.ToString());
            //    Console.WriteLine("\t" + cm.Mu30.ToString());
            //}

            //string plots = @"C:\Users\jshogland\Documents\John\projects\UMGradSchool\Project\papers\Co-registration\Data\coReg.gdb\SampleLocations";
            //string ls= @"C:\Users\jshogland\Documents\John\projects\UMGradSchool\Project\papers\Co-registration\Data\FloridaLandsat.tif";
            //string naip= @"C:\Users\jshogland\Documents\John\projects\UMGradSchool\Project\papers\Co-registration\Data\GeorgiaNaipAgAl.tif";
            //rasterUtil rsUtil = new rasterUtil();



            //HashSet<string> uStr = new HashSet<string>();
            //int[] filesLines = getClasses(prjDir,ref uStr);
            //Console.WriteLine("Files = " + filesLines[0].ToString());
            //Console.WriteLine("Lines = " + filesLines[1].ToString());
            //Console.WriteLine(uStr.Count().ToString());



            //geoDatabaseUtility geoUtil = new geoDatabaseUtility();
            //rasterUtil rsUtil = new rasterUtil();
            //featureUtil ftrUtil = new featureUtil(rsUtil);
            ////string origPath = @"C:\Users\jshogland\Documents\John\projects\SpatialAdjust\Shift.gdb\OriginalSample";
            ////string nwPath = @"C:\Users\jshogland\Documents\John\projects\SpatialAdjust\Shift.gdb\RandomtShiftSample";
            ////CreateNewPoints(origPath, nwPath, rsUtil, ftrUtil, geoUtil, true);
            //string fiaTestPath = @"C:\Users\jshogland\Documents\John\projects\SpatialAdjust\Shift.gdb\RandomtShiftSample";
            //string fiaOutPath = @"C:\Users\jshogland\Documents\John\projects\SpatialAdjust\Shift.gdb\adjustSample";
            //string rsPath = @"C:\Users\jshogland\Documents\John\projects\SpatialAdjust\models\predictorsOneVariable.bch";
            //IFunctionRasterDataset fDset = rsUtil.createIdentityRaster(rsPath);
            //IFeatureClass ftrCls = geoUtil.getFeatureClass(fiaTestPath);
            //int error = 15;
            //adjustCoregistrationErrors aE = new adjustCoregistrationErrors(rsUtil);
            //aE.FunctionRasterDataset = fDset;
            //aE.DependentField = "Pred15";
            //aE.GeometricErrorCells = error;
            //aE.OutFtrClassPath = fiaOutPath;
            //aE.PlotFeatureClass = ftrCls;
            //aE.adjustErrors();
            dt2 = System.DateTime.Now;
            ts  = dt2.Subtract(dt);
            Console.WriteLine("Total Seconds = " + ts.TotalSeconds.ToString());

            m_AOLicenseInitializer.ShutdownApplication();
        }
コード例 #15
0
        private void buildRaster()
        {
            n = 0;
            IRaster2              rs2        = (IRaster2)InRaster;
            IRasterProps          rsP        = (IRasterProps)rs2;
            int                   tCells     = rsP.Width * rsP.Height;
            double                selectProp = System.Convert.ToDouble(MaxSampleSize) / System.Convert.ToDouble(tCells);
            IRasterBandCollection rsbc       = (IRasterBandCollection)rs2;
            IRasterCursor         rsCur      = rs2.CreateCursorEx(null);
            IPixelBlock           pb         = null;
            Random                rn         = new Random();

            double[] vlBandArr = new double[rsbc.Count];
            do
            {
                pb = rsCur.PixelBlock;
                int w = pb.Width;
                int h = pb.Height;
                tCells = w * h;
                int nCells = (int)(tCells * selectProp);
                if (nCells == 0)
                {
                    nCells = 1;
                }
                for (int z = 0; z < nCells; z++)
                {
                    int  cellloc = rn.Next(1, tCells);
                    int  r       = ((cellloc - 1) / w);
                    int  c       = (cellloc - (r * w)) - 1;
                    bool chVl    = true;
                    for (int p = 0; p < pb.Planes; p++)
                    {
                        object vlobj = pb.GetVal(p, c, r);
                        if (vlobj == null)
                        {
                            chVl = false;
                            break;
                        }

                        vlBandArr[p] = System.Convert.ToDouble(vlobj);
                    }
                    if (chVl)
                    {
                        for (int v = 0; v < vlBandArr.Length; v++)
                        {
                            sumClms[v]     += vlBandArr[v];
                            sumCross[v, v] += Math.Pow(vlBandArr[v], 2);
                            for (int j = 0 + v + 1; j < vlBandArr.Length; j++)
                            {
                                double vl1 = vlBandArr[v];
                                double vl2 = vlBandArr[j];
                                double p12 = vl1 * vl2;
                                sumCross[v, j] += p12;
                                sumCross[j, v] += p12;
                            }
                        }
                        n++;
                    }
                    else
                    {
                        continue;
                    }
                }
            } while (rsCur.Next() == true);
            int sampN = n;

            for (int i = 0; i < sumClms.Length; i++)
            {
                double var = (sumCross[i, i] / (sampN)) - (Math.Pow(sumClms[i], 2) / Math.Pow((sampN), 2));
                cov[i, i] = var;
                double sVl = sumClms[i] / sampN;
                for (int j = 0 + i + 1; j < sumClms.Length; j++)
                {
                    double vl1 = (sumCross[i, j] / (sampN));
                    double vl2 = (sVl * (sumClms[j] / (sampN)));
                    double p12 = vl1 - vl2;
                    cov[i, j] = p12;
                    cov[j, i] = p12;
                }
            }
        }
コード例 #16
0
        private void buildModelrst()
        {
            n = 0;
            IRaster2 rsV2 = (IRaster2)InValueRaster;
            IRaster2 rsS2 = (IRaster2)InStrataRaster;
            IRasterBandCollection rsbc   = (IRasterBandCollection)rsV2;
            IRasterCursor         rsSCur = rsS2.CreateCursorEx(null);
            IPixelBlock           pbS    = null;

            double[] vlBandArr = new double[rsbc.Count];
            Dictionary <string, int> stratDic = new Dictionary <string, int>();

            double[] sumClms = null;
            double[,] sumCross = null;
            double[,] cov      = null;
            double[,] scov     = null;
            //System.Array[] pbVArrs = new System.Array[rsbc.Count];
            do
            {
                pbS = rsSCur.PixelBlock;
                IPnt pbSize = new PntClass();
                pbSize.SetCoords(pbS.Width, pbS.Height);
                IPixelBlock pb = InValueRaster.CreatePixelBlock(pbSize);
                IPnt        ptLoc = new PntClass();
                double      mx, my;
                rsS2.PixelToMap(System.Convert.ToInt32(rsSCur.TopLeft.X), System.Convert.ToInt32(rsSCur.TopLeft.Y), out mx, out my);
                int px, py;
                rsV2.MapToPixel(mx, my, out px, out py);
                ptLoc.SetCoords(px, py);
                InValueRaster.Read(ptLoc, pb);
                for (int r = 0; r < pb.Height; r++)
                {
                    for (int c = 0; c < pb.Width; c++)
                    {
                        object sobj = pbS.GetVal(0, c, r);
                        if (sobj == null)
                        {
                            continue;
                        }
                        bool chVl = true;
                        for (int p = 0; p < pb.Planes; p++)
                        {
                            object vlobj = pb.GetVal(p, c, r);
                            if (vlobj == null)
                            {
                                chVl = false;
                                break;
                            }
                            vlBandArr[p] = System.Convert.ToDouble(vlobj);
                        }
                        if (chVl)
                        {
                            string strataValue = sobj.ToString();
                            int    sCnt;
                            if (stratDic.TryGetValue(strataValue, out sCnt))
                            {
                                stratDic[strataValue] = sCnt + 1;
                                int lIndex = lbl.IndexOf(strataValue);
                                sumClms  = sumClmsLst[lIndex];
                                sumCross = sumCrossLst[lIndex];
                            }
                            else
                            {
                                stratDic.Add(strataValue, 1);
                                int lIndex = lbl.Count;
                                lbl.Add(strataValue);
                                sumClms  = new double[VariableFieldNames.Length];
                                sumCross = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                cov      = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                scov     = new double[VariableFieldNames.Length, VariableFieldNames.Length];
                                sumClmsLst.Add(sumClms);
                                sumCrossLst.Add(sumCross);
                                covLst.Add(cov);
                                scovLst.Add(scov);
                            }
                            for (int v = 0; v < vlBandArr.Length; v++)
                            {
                                sumClms[v]     += vlBandArr[v];
                                sumCross[v, v] += Math.Pow(vlBandArr[v], 2);
                                for (int j = 0 + v + 1; j < vlBandArr.Length; j++)
                                {
                                    double vl1 = vlBandArr[v];
                                    double vl2 = vlBandArr[j];
                                    double p12 = vl1 * vl2;
                                    sumCross[v, j] += p12;
                                    sumCross[j, v] += p12;
                                }
                            }
                            n++;
                        }
                    }
                }
            } while (rsSCur.Next() == true);
            for (int l = 0; l < lbl.Count; l++)
            {
                string lblVl = lbl[l];
                int    sampN = stratDic[lblVl];
                double r     = sampN / (sampN - 1);
                proportionsLst.Add(System.Convert.ToDouble(sampN) / n);
                sumClms = sumClmsLst[l];
                meansLst.Add((from double d in sumClms select d / sampN).ToArray());
                sumCross = sumCrossLst[l];
                cov      = covLst[l];
                scov     = scovLst[l];
                for (int i = 0; i < sumClms.Length; i++)
                {
                    double var = (sumCross[i, i] / sampN) - (Math.Pow(sumClms[i], 2) / Math.Pow((sampN), 2));
                    cov[i, i]  = var;
                    scov[i, i] = var * r;
                    for (int j = 0 + i + 1; j < sumClms.Length; j++)
                    {
                        double vl1 = (sumCross[j, i] / sampN);
                        double vl2 = (sumClms[j] / sampN) * (sumClms[i] / (sampN));
                        double p12 = vl1 - vl2;
                        cov[i, j]  = p12;
                        cov[j, i]  = p12;
                        scov[i, j] = p12 * r;
                        scov[j, i] = p12 * r;
                    }
                }
            }

            k = lbl.Count;
            makeKMeans();
        }
コード例 #17
0
        private bool getRGB(IFunctionRasterDataset rs, out double r, out double g, out double b)
        {
            r = 0;
            g = 0;
            b = 0;
            IRasterBandCollection rsbc = (IRasterBandCollection)rs;

            double[]      vlArr  = new double[rsbc.Count];
            int[]         cntArr = new int[vlArr.Length];
            IRaster2      rs2    = (IRaster2)rsUtil.createRaster(rs);
            IRasterCursor rsCur  = rs2.CreateCursorEx(null);

            do
            {
                IPixelBlock pb = rsCur.PixelBlock;
                for (int bd = 0; bd < vlArr.Length; bd++)
                {
                    for (int rw = 0; rw < pb.Height; rw++)
                    {
                        for (int cl = 0; cl < pb.Width; cl++)
                        {
                            object vlObj = pb.GetVal(bd, cl, rw);
                            if (vlObj != null)
                            {
                                vlArr[bd]  += System.Convert.ToDouble(vlObj);
                                cntArr[bd] += 1;
                            }
                        }
                    }
                }
            } while (rsCur.Next());
            int  cntr     = cntArr[0];
            int  cntg     = cntArr[1];
            int  cntb     = cntArr[2];
            int  bcheck   = 0;
            bool cntZerro = false;

            if (cntr > 0)
            {
                r = vlArr[0] / cntr;
            }
            else
            {
                bcheck += 1;
            }
            if (cntg > 0)
            {
                g = vlArr[1] / cntg;
            }
            else
            {
                bcheck += 1;
            }
            if (cntb > 0)
            {
                b = vlArr[2] / cntb;
            }
            if (bcheck == 2)
            {
                cntZerro = true;
            }
            return(cntZerro);
        }
コード例 #18
0
        /// <summary>
        /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels.
        /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling.
        /// The log raster is the natural log of the raster.
        /// </summary>
        /// <param name="pTlc">Point to start the reading from in the Raster</param>
        /// <param name="pRaster">Reference Raster for the PixelBlock</param>
        /// <param name="pPixelBlock">PixelBlock to be filled in</param>
        public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock)
        {
            try
            {
                // Call Read method of the Raster Function Helper object.
                //IRasterProps rsP = (IRasterProps)pRaster;
                //rsP.PixelType = convPType;
                myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock);
                for (int p = 0; p < pPixelBlock.Planes; p++)
                {
                    System.Array outArr = (System.Array)pPixelBlock.get_SafeArray(p);
                    for (int r = 0; r < pPixelBlock.Height; r++)
                    {
                        for (int c = 0; c < pPixelBlock.Width; c++)
                        {
                            object vlObj = pPixelBlock.GetVal(p, c, r);
                            if (vlObj != null)
                            {
                                object newVl = convertVl(vlObj);
                                outArr.SetValue(newVl, c, r);
                            }
                            else
                            {
                                outArr.SetValue(null, c, r);
                            }
                        }
                    }
                    pPixelBlock.set_SafeArray(p, outArr);
                }
                //int pBHeight = pPixelBlock.Height;
                //int pBWidth = pPixelBlock.Width;
                //IPnt pbSize = new PntClass();
                //pbSize.SetCoords(pBWidth, pBHeight);
                //IRasterBandCollection rsBc = (IRasterBandCollection)inrs;
                //for (int p = 0; p < pPixelBlock.Planes; p++)
                //{
                //    IRasterBand rsB = rsBc.Item(p);
                //    IRawPixels rP = (IRawPixels)rsB;
                //    IPixelBlock pb = rP.CreatePixelBlock(pbSize);
                //    rP.Read(pTlc, pb);
                //    System.Array outArr = (System.Array)((IPixelBlock3)pPixelBlock).get_PixelData(p);
                //    for (int r = 0; r < pBHeight; r++)
                //    {
                //        for (int c = 0; c < pBWidth; c++)
                //        {
                //            object outVlObj = pb.GetVal(0, c, r);
                //            if (outVlObj != null)
                //            {
                //                object adVl = convertVl(outVlObj);
                //                outArr.SetValue(adVl, c, r);
                //            }
                //            else
                //            {
                //                //Console.WriteLine("outVlObj = null for r,c " + r.ToString() + ", " + c.ToString());
                //            }



                //        }
                //    }
                //    ((IPixelBlock3)pPixelBlock).set_PixelData(p, outArr);
                //}
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc);
                Console.Write(exc.ToString());
                throw myExc;
            }
        }
コード例 #19
0
        private void buildModelRst()
        {
            sumX.Clear();
            sumX2.Clear();
            cateDic.Clear();
            lbl.Clear();
            int v1 = VariableFieldNames.Length;

            double[] s  = new double[(v1 * v1 - v1) / 2];
            double[] s2 = new double[s.Length];
            n = 0;
            IRasterCursor rsCur    = ((IRaster2)InStrataRaster).CreateCursorEx(null);
            IPnt          vCurSize = new PntClass();

            do
            {
                IPixelBlock pbS = rsCur.PixelBlock;
                vCurSize.SetCoords(pbS.Width, pbS.Height);
                IPixelBlock pbV = InValueRaster.CreatePixelBlock(vCurSize);
                InValueRaster.Read(rsCur.TopLeft, pbV);
                for (int r = 0; r < pbS.Height; r++)
                {
                    for (int c = 0; c < pbS.Width; c++)
                    {
                        object strata = pbS.GetVal(0, c, r);
                        if (strata == null)
                        {
                            continue;
                        }
                        else
                        {
                            string strataStr = strata.ToString();
                            int    strataIndex;
                            int    cnt;
                            if (cateDic.TryGetValue(strataStr, out cnt))
                            {
                                cnt         = cnt + 1;
                                strataIndex = lbl.IndexOf(strataStr);
                                s           = sumX[strataIndex];
                                s2          = sumX2[strataIndex];
                            }
                            else
                            {
                                cnt = 1;
                                cateDic.Add(strataStr, 0);
                                lbl.Add(strataStr);
                                strataIndex = lbl.Count - 1;
                                s           = new double[s.Length];
                                s2          = new double[s.Length];
                                sumX.Add(s);
                                sumX2.Add(s2);
                            }
                            bool     checkVl = true;
                            double[] vlArr   = new double[pbV.Planes];
                            for (int p = 0; p < pbV.Planes; p++)
                            {
                                object vl = pbV.GetVal(p, c, r);
                                if (vl == null)
                                {
                                    checkVl = false;
                                    break;
                                }
                                else
                                {
                                    vlArr[p] = System.Convert.ToDouble(vl);
                                }
                            }
                            if (checkVl)
                            {
                                int vlCnter = 1;
                                int sCnter  = 0;
                                for (int i = 0; i < vlArr.Length - 1; i++)
                                {
                                    for (int k = vlCnter; k < vlArr.Length; k++)
                                    {
                                        double m  = vlArr[i] - vlArr[k];
                                        double m2 = m * m;
                                        s[sCnter]  = s[sCnter] + m;
                                        s2[sCnter] = s2[sCnter] + m2;
                                        sCnter    += 1;
                                    }
                                    vlCnter += 1;
                                }
                                cateDic[strataStr] = cnt;
                                n += 1;
                            }
                        }
                    }
                }
            } while (rsCur.Next() == true);
        }