public void buildModel(string modelPath) { outmodelpath = modelPath; using (System.IO.StreamReader sr = new System.IO.StreamReader(outmodelpath)) { dataPrepBase.modelTypes mType = (dataPrepBase.modelTypes)Enum.Parse(typeof(dataPrepBase.modelTypes), sr.ReadLine()); if (mType != dataPrepBase.modelTypes.Cluster) { System.Windows.Forms.MessageBox.Show("Not a Cluster Model!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } cType = (clusterType)System.Enum.Parse(typeof(clusterType), sr.ReadLine()); inpath = sr.ReadLine(); VariableFieldNames = sr.ReadLine().Split(new char[] { ',' }); n = System.Convert.ToInt32(sr.ReadLine()); prop = System.Convert.ToDouble(sr.ReadLine()); k = System.Convert.ToInt32(sr.ReadLine()); lbl = sr.ReadLine().Split(new char[] { ',' }).ToList(); switch (cType) { case clusterType.KMEANS: setKMeansCluster(sr); break; case clusterType.BINARY: setBinaryCluster(sr); break; case clusterType.GAUSSIANMIXTURE: setGaussianCluster(sr); break; default: break; } sr.Close(); } }
/// <summary> /// sample a raster using a given offset /// </summary> /// <param name="inFtrCls"></param> /// <param name="sampleRst"></param> /// <param name="inName"></param> /// <param name="azmithDistance"></param> /// <param name="typeOfCluster"></param> /// <returns></returns> public string[] sampleRaster(IFeatureClass inFtrCls, IRaster sampleRst, string inName, Dictionary<double,double> azmithDistance, clusterType typeOfCluster) { IRaster2 sr = (IRaster2)sampleRst; IRasterBandCollection rsBC = (IRasterBandCollection)sr; IEnumRasterBand rsBE = rsBC.Bands; IRasterBand rsB = rsBE.Next(); string rsName = inName; if (rsName == null) { rsName = ((IDataset)sr.RasterDataset).Name; } int cntB = 0; int[] fldIndex = new int[rsBC.Count]; string[] fldNames = new string[rsBC.Count]; while (rsB != null) { string fldName = rsName + "_Band" + (cntB + 1).ToString(); //fldName = geoUtil.getSafeFieldName(inFtrCls, fldName); esriFieldType fldType = esriFieldType.esriFieldTypeDouble; fldName = geoUtil.createField(inFtrCls, fldName, fldType); fldNames[cntB] = fldName; fldIndex[cntB] = inFtrCls.FindField(fldName); cntB++; rsB = rsBE.Next(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(rsBE); IGeometry geo = (IGeometry)((IRasterProps)sampleRst).Extent; ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = geo; spFlt.GeometryField = inFtrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor sCur = inFtrCls.Update(spFlt, true); IFeature sRow = sCur.NextFeature(); while (sRow != null) { geo = sRow.Shape; IPoint pnt = (IPoint)geo; int x, y; x = 0; y = 0; for (int i = 0; i < ((IRasterBandCollection)sr).Count; i++) { double rsVl = 0; List<double> rsVlLst = new List<double>(); try { sr.MapToPixel(pnt.X, pnt.Y, out x, out y); rsVlLst.Add(System.Convert.ToDouble(sr.GetPixelValue(i, x, y))); foreach (KeyValuePair<double, double> kVp in azmithDistance) { double az = kVp.Key; double ds = kVp.Value; double nX = pnt.X + (System.Math.Sin(az * Math.PI / 180) * ds); double nY = pnt.Y + (System.Math.Cos(az * Math.PI / 180) * ds); sr.MapToPixel(nX, nY, out x, out y); rsVlLst.Add(System.Convert.ToDouble(sr.GetPixelValue(i, x, y))); } switch (typeOfCluster) { case clusterType.SUM: rsVl = rsVlLst.Sum(); break; case clusterType.MEAN: rsVl = rsVlLst.Average(); break; case clusterType.MEDIAN: rsVlLst.Sort(); rsVl = rsVlLst[(rsVlLst.Count-1) / 2]; break; case clusterType.MODE: Dictionary<double, int> cntDic = new Dictionary<double, int>(); int maxLc = 0; double maxKy = rsVlLst[0]; foreach (double d in rsVlLst) { if (cntDic.ContainsKey(d)) { int cntVl = cntDic[d] + 1; if(cntVl>maxLc) { maxLc = cntVl; maxKy = d; } cntDic[d] = cntVl; } else { cntDic.Add(d, 1); } } rsVl = maxKy; break; default: break; } sRow.set_Value(fldIndex[i], rsVl); } catch { Console.WriteLine(rsVl.ToString()); } } sCur.UpdateFeature(sRow); sRow = sCur.NextFeature(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(sCur); return fldNames; }