public IFeatureClass createPolygons() { IRasterProps rsProp = (IRasterProps)inputRaster; IPnt rsPnt = rsProp.MeanCellSize(); double cellArea = rsPnt.X * rsPnt.Y; double tCells = minArea / cellArea; int rws = (int)Math.Sqrt(tCells); if (rws < 3) { rws = 3; } int clms = rws; esriUtil.Statistics.dataPrepClusterBinary dpClus = new Statistics.dataPrepClusterBinary(inputRaster, specificity, 100000, 0.001); IRaster clusRs = rsUtil.createRaster(rsUtil.calcClustFunctionBinary(inputRaster, dpClus)); Console.WriteLine("Exracting Domains"); extractDomains(clusRs, dpClus); //IFeatureCursor ftrCur = outftr.Search(null, false); //IFeature ftr = ftrCur.NextFeature(); //Console.WriteLine("removing small polygons"); //while (ftr != null) //{ // IArea pArea = (IArea)ftr.Shape; // double area = pArea.Area; // if (area < MinArea) // { // addToNeighboringFeature(ftr, outftr, dpClus); // } // ftr = ftrCur.NextFeature(); //} //Console.WriteLine("splitting large polygons"); //ftrCur = outftr.Search(null, false); //ftr = ftrCur.NextFeature(); //while (ftr != null) //{ // IArea pArea = (IArea)ftr.Shape; // double area = pArea.Area; // if (area > MaxArea) // { // splitFeature(ftr, outftr, true); // } // ftr = ftrCur.NextFeature(); //} //System.Runtime.InteropServices.Marshal.ReleaseComObject(ftrCur); return(outftr); }
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 void extractDomains(IRaster clusRs, Statistics.dataPrepClusterBinary dpClus) { if (outftr == null) { mfldIndex = new int[bCnt]; segIndex = 0; IFields flds = new FieldsClass(); IFieldsEdit fldsE = (IFieldsEdit)flds; IField fld = new FieldClass(); IFieldEdit fldE = (IFieldEdit)fld; fldE.Name_2 = "SegID"; fldE.Type_2 = esriFieldType.esriFieldTypeInteger; fldsE.AddField(fldE); //IField fld2 = new FieldClass(); //IFieldEdit fldE2 = (IFieldEdit)fld2; //fldE2.Name_2 = "Cluster"; //fldE2.Type_2 = esriFieldType.esriFieldTypeInteger; //fldsE.AddField(fldE2); //for (int i = 0; i < bCnt; i++) //{ // IField mFld = new FieldClass(); // IFieldEdit mFldE = (IFieldEdit)mFld; // mFldE.Name_2 = "Band" + i.ToString(); // mFldE.Type_2 = esriFieldType.esriFieldTypeDouble; // fldsE.AddField(mFld); //} outftr = geoUtil.createFeatureClass((IWorkspace2)OutWorkSpace, OutName, flds, esriGeometryType.esriGeometryPolygon, ((IRasterProps)clusRs).SpatialReference); segIndex = outftr.FindField("SegID"); //clustIndex = outftr.FindField("Cluster"); //for (int i = 0; i < bCnt; i++) //{ // mfldIndex[i] = outftr.FindField("Band" + i.ToString()); //} } IRasterDomainExtractor domExtract = new RasterDomainExtractorClass(); IRasterProps rsp = (IRasterProps)clusRs; IRasterCursor rsCur = ((IRaster2)clusRs).CreateCursorEx(null); IPnt cellSizeRs = rsp.MeanCellSize(); double pSizeX = cellSizeRs.X; double pSizeY = cellSizeRs.Y; int blockCnt = 1; do { IPixelBlock pb = rsCur.PixelBlock; IFunctionRasterDataset fDset = rsUtil.PixelBlockToRaster(pb, rsCur.TopLeft, clusRs); Console.WriteLine(blockCnt); //rsUtil.saveRasterToDataset(rsUtil.createRaster(fDset), "bTest"+blockCnt.ToString(), OutWorkSpace); HashSet <IPolygon4>[] polys = new HashSet <IPolygon4> [2];//small,good polygons for (int h = 0; h < 2; h++) { polys[h] = new HashSet <IPolygon4>(); } for (int i = 0; i < specificity; i++) { //double[] means = ((Accord.MachineLearning.BinarySplit)dpClus.Model).Clusters[i].Mean; IFunctionRasterDataset bRs = rsUtil.calcEqualFunction(fDset, 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 subPoly = (IPolygon4)geoColl.get_Geometry(j); double subPolyArea = ((IArea)subPoly).Area; double subPolyLength = subPoly.Length; if (subPolyArea > maxArea) { splitFeature(subPoly, ref polys); // should split polygons. smaller than acceptable go into polys[0] ok go into polys[1] } else if (subPolyArea < minArea) { polys[0].Add(subPoly); } else { polys[1].Add(subPoly); } //segid++; } //addToNeighboringFeature(ref polys); IFeatureCursor ftrCur = outftr.Insert(true); IFeatureBuffer nFtr = outftr.CreateFeatureBuffer(); Console.WriteLine("\tFTR Count small " + polys[0].Count.ToString()); Console.WriteLine("\tFTR Count big " + polys[1].Count.ToString()); foreach (IPolygon4 p in polys[1]) { nFtr.Shape = (IGeometry)p; nFtr.set_Value(segIndex, segid); ftrCur.InsertFeature(nFtr); segid++; } foreach (IPolygon p in polys[0]) { nFtr.Shape = (IGeometry)p; nFtr.set_Value(segIndex, segid); ftrCur.InsertFeature(nFtr); segid++; } ftrCur.Flush(); System.Runtime.InteropServices.Marshal.ReleaseComObject(nFtr); System.Runtime.InteropServices.Marshal.ReleaseComObject(ftrCur); } blockCnt++; } while (rsCur.Next() == true); System.Runtime.InteropServices.Marshal.ReleaseComObject(rsCur); }
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++; } } }
public IFeatureClass createPolygons() { IRasterProps rsProp = (IRasterProps)inputRaster; IPnt rsPnt = rsProp.MeanCellSize(); double cellArea = rsPnt.X*rsPnt.Y; double tCells = minArea/cellArea; int rws = (int)Math.Sqrt(tCells); if(rws<3) rws = 3; int clms = rws; esriUtil.Statistics.dataPrepClusterBinary dpClus = new Statistics.dataPrepClusterBinary(inputRaster, specificity,100000,0.001); IRaster clusRs = rsUtil.createRaster(rsUtil.calcClustFunctionBinary(inputRaster, dpClus)); Console.WriteLine("Exracting Domains"); extractDomains(clusRs,dpClus); //IFeatureCursor ftrCur = outftr.Search(null, false); //IFeature ftr = ftrCur.NextFeature(); //Console.WriteLine("removing small polygons"); //while (ftr != null) //{ // IArea pArea = (IArea)ftr.Shape; // double area = pArea.Area; // if (area < MinArea) // { // addToNeighboringFeature(ftr, outftr, dpClus); // } // ftr = ftrCur.NextFeature(); //} //Console.WriteLine("splitting large polygons"); //ftrCur = outftr.Search(null, false); //ftr = ftrCur.NextFeature(); //while (ftr != null) //{ // IArea pArea = (IArea)ftr.Shape; // double area = pArea.Area; // if (area > MaxArea) // { // splitFeature(ftr, outftr, true); // } // ftr = ftrCur.NextFeature(); //} //System.Runtime.InteropServices.Marshal.ReleaseComObject(ftrCur); return outftr; }