public void readModelOutput(string InputFileName, string OutputFileName) { try { IRasterImportOp pRasterImportOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOp() as IRasterImportOp; IWorkspaceFactory PRasterWSFact = new RasterWorkspaceFactory() as IWorkspaceFactory; IWorkspace pRasterWS = PRasterWSFact.OpenFromFile(ArcTimData.StaticClass.infoTable.Rows[0]["ModelPath"].ToString(), 0) as IWorkspace; //ImportfromAscii must have an output filename that is less than 8 char //the output filename shouldn't have the path with it IRasterDataset pRasOut = pRasterImportOp.ImportFromASCII(InputFileName, pRasterWS, OutputFileName, "GRID", false); IRasterLayer pRasterLy = new RasterLayer(); pRasterLy.CreateFromDataset(pRasOut); IMxDocument pMxDoc = GetMxDocument(m_application); pMxDoc.FocusMap.AddLayer(pRasterLy); pMxDoc.FocusMap.MoveLayer(pRasterLy, pMxDoc.FocusMap.LayerCount - 1); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } //try //{ // IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactory() as IWorkspaceFactory; // IRasterWorkspace pRasterworkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(ArcTim5PropertiesMenu.StaticClass.pathName, 0); // IRasterLayer pRasterLayer = new RasterLayer(); // pRasterLayer.CreateFromDataset(pRasterworkspace.OpenRasterDataset(InputFileName)); // pRasterLayer.Name = OutputFileName; // IMxDocument pMxDoc = GetMxDocument(m_application); // pMxDoc.FocusMap.AddLayer(pRasterLayer); //} //catch (Exception ex) //{ // System.Diagnostics.Debug.WriteLine(ex.Message); //} }
private IGeoDataset ConvertAndUnionWatershed(IGeoDataset tWatershedGDS) { //Convert the raster IGeodataset into a Polygon IFeatureClass, in a memory-workspace IWorkspace inMemFeatWksp = CreateInMemoryWorkspace(); //IWorkspaceFactory pWSF = new ShapefileWorkspaceFactory(); //IWorkspace pWS = pWSF.OpenFromFile(out_folder,0); string current = GetTimeStamp(DateTime.Now); string outname = "resultWatershed" + current; IFeatureClass tWaterShedPolyFC; IGeoDataset tInitialPolygons; try { IConversionOp pConversionOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOp() as IConversionOp; tInitialPolygons = pConversionOp.RasterDataToPolygonFeatureData(tWatershedGDS, inMemFeatWksp, outname, false); tWaterShedPolyFC = tInitialPolygons as IFeatureClass; } catch { logger.LogMessage(ServerLogger.msgType.debug, "convert and union wshed", 8000, "Error in converting watershed to in-memory FC"); tWaterShedPolyFC = null; tInitialPolygons = null; } // attempt to add a CATCH_AREA field to the feature class bool setAreaOk = false; try { //setAreaOk = AddAreaField(tWaterShedPolyFC); setAreaOk = AddAField(tWaterShedPolyFC, "Total_Area", esriFieldType.esriFieldTypeDouble); } catch { logger.LogMessage(ServerLogger.msgType.debug, "convert and union wshed", 8000, "Error adding area field to output"); } IFeature tWaterShedFeature; // if there is more than one feature in the FC then union them using geometrybag if (tWaterShedPolyFC.FeatureCount(null) > 1) { logger.LogMessage(ServerLogger.msgType.infoStandard, "convert and union wshed", 8000, "Attempting to union multiple polygons..."); // there is more than one polygon i.e. diagonally connected. merge them into a single feature // with multiple rings using a geometrybag IGeometryBag tGeometryBag = new GeometryBagClass(); tGeometryBag.SpatialReference = tInitialPolygons.SpatialReference; IFeatureCursor tFCursor = tWaterShedPolyFC.Search(null, false); IGeometryCollection tGeomColl = tGeometryBag as IGeometryCollection; IFeature tCurrentFeature = tFCursor.NextFeature(); ITable tTable = tCurrentFeature.Table; while (tCurrentFeature != null) { object missing = Type.Missing; tGeomColl.AddGeometry(tCurrentFeature.Shape, ref missing, ref missing); tCurrentFeature = tFCursor.NextFeature(); } ITopologicalOperator tUnioned = new PolygonClass(); tUnioned.ConstructUnion(tGeometryBag as IEnumGeometry); logger.LogMessage(ServerLogger.msgType.infoStandard, "convert and union wshed", 8000, "Done with ConstructUnion, doing area"); try { IArea tmpArea = tUnioned as IArea; double tArea = tmpArea.Area; // delete the previously existing rows from the table tTable.DeleteSearchedRows(null); // replace them with a new row representing the unioned feature IRow tRow = tTable.CreateRow(); tRow.set_Value(tTable.FindField("SHAPE"), tUnioned); tRow.set_Value(tTable.FindField("ID"), -1); tRow.set_Value(tTable.FindField("GRIDCODE"), -1); if (setAreaOk) { tRow.set_Value(tTable.FindField("Total_Area"), tArea); } tRow.Store(); } catch (Exception ex) { logger.LogMessage(ServerLogger.msgType.error, "store unioned polygon", 8000, "Error setting fields of unioned polygon!" + ex.StackTrace + ex.Message); } } else { // There is only one polygon - i.e. there were not diagonally-disconnected bits // NB features are indexed starting at 1. Just for a laff. tWaterShedFeature = tWaterShedPolyFC.GetFeature(1); if (setAreaOk) { try { int tAreaFieldIdx = tWaterShedFeature.Fields.FindField("Total_Area"); IArea tArea = tWaterShedFeature.Shape as IArea; double tmpArea = tArea.Area; tWaterShedFeature.set_Value(tAreaFieldIdx, tmpArea); tWaterShedFeature.Store(); logger.LogMessage(ServerLogger.msgType.debug, "convert and union wshed", 8000, "Done adding area to one polygon"); } catch { logger.LogMessage(ServerLogger.msgType.debug, "convert and union wshed", 8000, "Error adding area field to single polygon output"); } } } return (IGeoDataset)tWaterShedPolyFC; }