private void createOutRaster() { OutRasterName = rsUtil.getSafeOutputName(wks, OutRasterName); outraster = rsUtil.returnRaster(rsUtil.createNewRaster(inRs, wks, OutRasterName, rasterUtil.rasterType.IMAGINE)); rsProps2 = (IRasterProps)OutRaster; System.Array noDataValues2 = (System.Array)rsProps2.NoDataValue; noDataVl2 = System.Convert.ToInt32(noDataValues2.GetValue(0)); if (noDataVl2 >= 0) { counter = Int32.MinValue + 2; } else { counter = Int32.MinValue + 2; } #region create VAT Table IFields flds = new FieldsClass(); IField fld = new FieldClass(); IFieldsEdit fldsE = (IFieldsEdit)flds; IFieldEdit fldE = (IFieldEdit)fld; fldE.Name_2 = "Value"; fldE.Type_2 = esriFieldType.esriFieldTypeInteger; fldE.Precision_2 = 50; fldsE.AddField(fld); IField fld2 = new FieldClass(); IFieldEdit fld2E = (IFieldEdit)fld2; fld2E.Name_2 = "Count"; fld2E.Type_2 = esriFieldType.esriFieldTypeInteger; fldE.Precision_2 = 50; fldsE.AddField(fld2); IField fld3 = new FieldClass(); IFieldEdit fld3E = (IFieldEdit)fld3; fld3E.Name_2 = "Perimeter"; fld3E.Type_2 = esriFieldType.esriFieldTypeInteger; fld3E.Precision_2 = 50; fldsE.AddField(fld3); vatTable = geoUtil.createTable(vWks, OutRasterName + "_vat", flds); valueIndex = vatTable.FindField("Value"); countIndex = vatTable.FindField("Count"); permIndex = vatTable.FindField("Perimeter"); #endregion }
public void ConvertGridMetricsToRaster(string metricsDir, string outDir, int[] metrics) { string[] metricFiles = System.IO.Directory.GetFiles(metricsDir, "*.csv"); string[] headerFiles = System.IO.Directory.GetFiles(metricsDir, "*.txt"); IPnt cellSize; IPnt[] tlPntArr; int[] rwArr; int[] clmArr; float noDataVl; IEnvelope ext = getExtentsFromHeader(headerFiles, out cellSize, out tlPntArr, out rwArr, out clmArr, out noDataVl); IWorkspace outWks = geoUtil.OpenRasterWorkspace(outDir); string exePath = "\"" + fusDir + "\\" + csv2Grid + "\""; IRasterEdit[] rsArr = new IRasterEdit[metrics.Length]; for (int i = 0; i < metrics.Length; i++) { int clm = metrics[i]; string metricName = MetricsArr[clm]; IRasterDataset rsDset = rsUtil.createNewRaster(ext, cellSize, outWks, metricName, 1, rstPixelType.PT_FLOAT, rasterUtil.rasterType.IMAGINE, null);//rsWk2.CreateRasterDataset(metricName+".img","IMAGINE Image",ext.LowerLeft,w,h,cellSize.X,cellSize.Y,1,rstPixelType.PT_FLOAT,null,true); IRasterBand rsB = ((IRasterBandCollection)rsDset).Item(0); IRasterProps rsP = (IRasterProps)rsB; rsP.NoDataValue = noDataVl; IRaster rs = ((IRasterDataset2)rsDset).CreateFullRaster(); rsArr[i] = (IRasterEdit)rs; } foreach (string s in metricFiles) { Console.WriteLine("Working on " + s); int pntIndex = System.Array.IndexOf(metricFiles, s); IPnt tl = tlPntArr[pntIndex]; int cl, rw; ((IRaster2)rsArr[0]).MapToPixel(tl.X, tl.Y, out cl, out rw); IPnt tlp = new PntClass(); tlp.X = cl; tlp.Y = rw; int rws = rwArr[pntIndex]; int clms = clmArr[pntIndex]; IPnt bSize = new PntClass(); bSize.SetCoords(clms, rws); System.Array[] pbSafeArr = new System.Array[metrics.Length]; IPixelBlock3[] pbArr = new IPixelBlock3[metrics.Length]; for (int i = 0; i < metrics.Length; i++) { IRasterEdit rsE = rsArr[i]; IRaster rs = (IRaster)rsE; IPixelBlock3 pb = (IPixelBlock3)rs.CreatePixelBlock(bSize); rs.Read(tlp, (IPixelBlock)pb); //pb.Mask(0); pbArr[i] = pb; System.Array vlArr = (System.Array)pb.get_PixelData(0); pbSafeArr[i] = vlArr; } using (System.IO.StreamReader sr = new System.IO.StreamReader(s)) { string hdr = sr.ReadLine(); while (!sr.EndOfStream) { string ln = sr.ReadLine(); string[] lnArr = ln.Split(new char[] { ',' }); int r = (rws - 1) - System.Convert.ToInt32(lnArr[0]); int c = System.Convert.ToInt32(lnArr[1]); for (int i = 0; i < metrics.Length; i++) { int clIndex = metrics[i]; float clVl = System.Convert.ToSingle(lnArr[clIndex]); if (clVl != noDataVl) { pbSafeArr[i].SetValue(clVl, c, r); } } } sr.Close(); } for (int i = 0; i < metrics.Length; i++) { IPixelBlock3 pb = pbArr[i]; pb.set_PixelData(0, pbSafeArr[i]); rsArr[i].Write(tlp, (IPixelBlock)pb); } } for (int i = 0; i < rsArr.Length; i++) { System.Runtime.InteropServices.Marshal.ReleaseComObject(rsArr[i]); } }