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