private void adjustXTableRst(IGeometry iGeometry) { double tN = 0; IRaster rs = rsUtil.createRaster(rsUtil.clipRasterFunction(rstMap, iGeometry, ESRI.ArcGIS.DataSourcesRaster.esriRasterClippingType.esriRasterClippingOutside)); IRasterCursor rsCur = ((IRaster2)rs).CreateCursorEx(null); while (rsCur.Next()) { IPixelBlock pb = rsCur.PixelBlock; for (int r = 0; r < pb.Height; r++) { for (int c = 0; c < pb.Width; c++) { object vlObj = pb.GetVal(0, c, r); if (vlObj == null) { continue; } else { string vl = vlObj.ToString(); int vlIndex = olabels.IndexOf(vl); nCnts[vlIndex] = nCnts[vlIndex] + 1; tN += 1; } } } } updateXTable(tN); }
private void splitFeature(IPolygon4 subPoly, ref HashSet <IPolygon4>[] polys) { IRaster clipRs = rsUtil.createRaster(rsUtil.clipRasterFunction(inputRaster, subPoly, esriRasterClippingType.esriRasterClippingOutside)); esriUtil.Statistics.dataPrepClusterBinary dpClus = new Statistics.dataPrepClusterBinary(clipRs, specificity, 100000, 0.001); IFunctionRasterDataset clusRs = rsUtil.calcClustFunctionBinary(inputRaster, dpClus); IFunctionRasterDataset clipRs2 = rsUtil.clipRasterFunction(clusRs, subPoly, esriRasterClippingType.esriRasterClippingOutside); IRasterDomainExtractor domExtract = new RasterDomainExtractorClass(); for (int i = 0; i < specificity; i++) { IFunctionRasterDataset bRs = rsUtil.calcEqualFunction(clipRs2, i); IRaster mRs = rsUtil.createRaster(rsUtil.setNullValue(bRs, 0)); IPolygon4 poly = (IPolygon4)domExtract.ExtractDomain(mRs, false); IGeometryBag geoBag = poly.ConnectedComponentBag; IGeometryCollection geoColl = (IGeometryCollection)geoBag; for (int j = 0; j < geoColl.GeometryCount; j++) { IPolygon4 sp = (IPolygon4)geoColl.get_Geometry(j); double subPolyArea = ((IArea)sp).Area; //double subPolyLength = subPoly.Length; if (subPolyArea > maxArea) { splitFeature(sp, ref polys); // should split polygons. smaller than acceptable go into polys[0] ok go into polys[1] } else if (subPolyArea < minArea) { polys[0].Add(sp); } else { polys[1].Add(sp); } //segid++; } } }
private double[][] coef = null; // slope coefficients for each band second double array = {intercept,slope,R2} private void getRegVals() { IRaster2 mRs = (IRaster2)rsUtil.createRaster(rsUtil.clipRasterFunction(referenceRaster, clipGeo, esriRasterClippingType.esriRasterClippingOutside)); IRaster2 sRs = (IRaster2)rsUtil.createRaster(rsUtil.clipRasterFunction(transformRaster, clipGeo, esriRasterClippingType.esriRasterClippingOutside)); IPnt pntSize = new PntClass(); pntSize.SetCoords(250, 250); IRasterCursor mRsCur = mRs.CreateCursorEx(pntSize); IRasterCursor sRsCur = sRs.CreateCursorEx(pntSize); IRasterCursor cRsCur = ((IRaster2)rsUtil.createRaster(clipRs)).CreateCursorEx(pntSize); IPixelBlock mPb, sPb, cPb; int bndCnt = minArray.Length; //int curCnt = 1; do { mPb = mRsCur.PixelBlock; sPb = sRsCur.PixelBlock; cPb = cRsCur.PixelBlock; for (int r = 0; r < cPb.Height; r += 50) { for (int c = 0; c < cPb.Width; c += 50) { for (int p = 0; p < bndCnt; p++) { double minVl = minArray[p]; double maxVl = maxArray[p]; int bCnt = 0; double ySumVl = 0; double xSumVl = 0; int adw = (cPb.Width - c); int adh = (cPb.Height - r); if (adw > 50) { adw = 50; } if (adh > 50) { adh = 50; } for (int br = 0; br < adh; br++) { for (int bc = 0; bc < adw; bc++) { int c2 = c + bc; int r2 = r + br; object vlObj = cPb.GetVal(p, c2, r2); if (vlObj == null) { //Console.WriteLine("Clip Not a number"); continue; } else { double vl = System.Convert.ToDouble(vlObj); if (vl <= maxVl && vl >= minVl) { object mVlObj = mPb.GetVal(p, c2, r2); object sVlObj = sPb.GetVal(p, c2, r2); if (mVlObj == null || sVlObj == null) { //Console.WriteLine("master or slave is null"); continue; } else { //Console.WriteLine(mVlObj.ToString() + ", " + sVlObj.ToString()); ySumVl += System.Convert.ToDouble(mVlObj); xSumVl += System.Convert.ToDouble(sVlObj); bCnt += 1; } } else { continue; } } } } if (bCnt == 0) { continue; } else { double yBlock = ySumVl / bCnt; double xBlock = xSumVl / bCnt; //Console.WriteLine(yBlock.ToString() + ", " + xBlock.ToString()); sumYArray[p] = sumYArray[p] + yBlock; sumXArray[p] = sumXArray[p] + xBlock; sumXYArray[p] = sumXYArray[p] + (yBlock * xBlock); sumX2Array[p] = sumX2Array[p] + (xBlock * xBlock); sumY2Array[p] = sumY2Array[p] + (yBlock * yBlock); blockCellCount[p] = blockCellCount[p] + 1; } } } } mRsCur.Next(); sRsCur.Next(); //Console.WriteLine(curCnt.ToString()); //curCnt++; } while (cRsCur.Next() == true); System.Runtime.InteropServices.Marshal.ReleaseComObject(cRsCur); System.Runtime.InteropServices.Marshal.ReleaseComObject(mRsCur); System.Runtime.InteropServices.Marshal.ReleaseComObject(sRsCur); for (int i = 0; i < bndCnt; i++) { double n = System.Convert.ToDouble(blockCellCount[i]); double meanX = sumXArray[i] / n; double meanY = sumYArray[i] / n; //double meanX2 = sumX2Array[i]/n; //double meanXY = sumXYArray[i]/n; //Console.WriteLine("numb of cells = " + n.ToString()); //Console.WriteLine(meanX.ToString() + ", " + meanY.ToString() + ", " + meanX2.ToString() + ", " + meanXY.ToString()); double slope = (n * sumXYArray[i] - (sumXArray[i] * sumYArray[i])) / (n * sumX2Array[i] - (System.Math.Pow(sumXArray[i], 2))); double intercept = meanY - (slope * meanX); double r2 = System.Math.Pow((n * sumXYArray[i] - (sumXArray[i] * sumYArray[i])) / (System.Math.Sqrt((n * sumX2Array[i] - (System.Math.Pow(sumXArray[i], 2)))) * System.Math.Sqrt(n * sumY2Array[i] - System.Math.Pow(sumYArray[i], 2))), 2); //Console.WriteLine("Intercept and Slope = " + intercept.ToString() + ", " + slope.ToString()); coef[i] = new double[3] { intercept, slope, r2 }; } }
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); }
private void btnClip_Click(object sender, EventArgs e) { string ftrNm = cmbSampleFeatureClass.Text; string rstNm = cmbRaster.Text; string outNm = txtOutRasterName.Text; bool chErase = chbErase.Checked; esriRasterClippingType clType = esriRasterClippingType.esriRasterClippingOutside; if (ftrNm == "" || ftrNm == null || rstNm == "" || rstNm == null) { MessageBox.Show("You must have a polygon and raster layer selected", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (outNm == "" || outNm == null) { MessageBox.Show("You must specify an output raster name", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (chErase) { clType = esriRasterClippingType.esriRasterClippingInside; } this.Visible = false; esriUtil.Forms.RunningProcess.frmRunningProcessDialog rp = new RunningProcess.frmRunningProcessDialog(false); DateTime dt = DateTime.Now; rp.addMessage("Clipping Raster. This may take a while..."); rp.stepPGBar(10); rp.TopMost = true; rp.Show(); try { IRaster rst = rstDic[rstNm]; IFeatureClass ftrCls = ftrDic[ftrNm]; IGeometry geo = geoUtil.createGeometry(ftrCls); IFunctionRasterDataset outraster = rsUtil.clipRasterFunction(rst, geo, clType); if (mp != null && addToMap) { rp.addMessage("Calculating Statistics..."); rp.Refresh(); IRasterLayer rstLyr = new RasterLayerClass(); //rsUtil.calcStatsAndHist(((IRaster2)outraster).RasterDataset); rstLyr.CreateFromDataset((IRasterDataset)outraster); rstLyr.Name = outNm; rstLyr.Visible = false; mp.AddLayer(rstLyr); } outrastername = outNm; this.DialogResult = DialogResult.OK; } catch (Exception ex) { rp.addMessage(ex.ToString()); } finally { DateTime dt2 = DateTime.Now; TimeSpan ts = dt2.Subtract(dt); string t = " in " + ts.Days.ToString() + " days " + ts.Hours.ToString() + " hours " + ts.Minutes.ToString() + " minutes and " + ts.Seconds.ToString() + " seconds ."; rp.stepPGBar(100); rp.addMessage("Finished Clipping Raster" + t); rp.enableClose(); this.Close(); } }