Example #1
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);
        }