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); } }
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); }