public static bool DetermineOverlapArea3(IFeatureLayer pAppLayer, IFeatureLayer pEnsLayer, IFeature pAppFeature, IFeature pEnsFeature) { try { IQueryFilter queryFilter = new QueryFilterClass(); IDataset pDataset1 = null; IDataset pDataset2 = null; IFeatureSelection pFeatureSelection1 = null; IFeatureSelection pFeatureSelection2 = null; IFeatureClassName pInFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pInFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pInDatasetName1 = null; IDatasetName pInDatasetName2 = null; IFeatureClassName pOutFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pOutFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pOutDatasetName1 = null; IDatasetName pOutDatasetName2 = null; IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); IFeatureWorkspace pFeatureWorkspace = null; IWorkspaceName pWorkspaceName = new WorkspaceNameClass(); IFeatureClass pFeatureClass = null; pWorkspaceName = pWorkspaceFactory.Create("", "TEMP", null, 0); IName pName = pWorkspaceName as IName; IWorkspace pWorkspace = pName.Open() as IWorkspace; pFeatureWorkspace = pWorkspace as IFeatureWorkspace; if (pAppLayer != null) { pFeatureSelection1 = pAppLayer as IFeatureSelection; queryFilter.WhereClause = "APP_NO = '" + m_AppNo + "'"; pDataset1 = pAppLayer.FeatureClass as IDataset; pFeatureSelection1.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName1 = pDataset1.FullName as IFeatureClassName; pInDatasetName1 = pInFeatureClassName1 as IDatasetName; pOutDatasetName1 = pOutFeatureClassName1 as IDatasetName; pOutDatasetName1.Name = "APPTEMP"; pOutDatasetName1.WorkspaceName = pWorkspaceName; pOutFeatureClassName1.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName1.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName1.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName1, null, pFeatureSelection1.SelectionSet, null, pOutFeatureClassName1, 0); } if (pEnsLayer != null) { pFeatureSelection2 = pEnsLayer as IFeatureSelection; queryFilter.WhereClause = "BASIN_NAME = '" + m_Label + "'"; pDataset2 = pEnsLayer.FeatureClass as IDataset; pFeatureSelection2.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName2 = pDataset2.FullName as IFeatureClassName; pInDatasetName2 = pInFeatureClassName2 as IDatasetName; pOutDatasetName2 = pOutFeatureClassName2 as IDatasetName; pOutDatasetName2.Name = "ENSTEMP"; pOutDatasetName2.WorkspaceName = pWorkspaceName; pOutFeatureClassName2.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName2.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName2.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName2, null, pFeatureSelection2.SelectionSet, null, pOutFeatureClassName2, 0); } ESRI.ArcGIS.AnalysisTools.Clip pClip = new ESRI.ArcGIS.AnalysisTools.Clip(); pClip.in_features = pWorkspace + "APPTEMP"; pClip.clip_features = pWorkspace + "ENSTEMP"; pClip.out_feature_class = pWorkspace + "APP_ENS_OVERLAP"; ITrackCancel pTrackCancel = new CancelTrackerClass(); pTrackCancel.CancelOnClick = false; pTrackCancel.CancelOnKeyPress = true; ESRI.ArcGIS.Geoprocessor.Geoprocessor pGeoprocessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); pGeoprocessor.Execute(pClip, pTrackCancel); //Get the resulting intersection polygon. pFeatureClass = pFeatureWorkspace.OpenFeatureClass("APP_ENS_OVERLAP"); pFeatureLayer.FeatureClass = pFeatureClass; IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection; pFeatureSelection.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false); ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet as ISelectionSet; ICursor pCursor; pSelectionSet.Search(null, true, out pCursor); IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor; IFeature pIntersectFeature = pFeatureCursor.NextFeature(); IArea pIntersectArea = pIntersectFeature.Shape as IArea; IArea pAppArea = pAppFeature.Shape as IArea; IArea pEnsArea = pEnsFeature.Shape as IArea; int appOverlap = Convert.ToInt32((pIntersectArea.Area / pAppArea.Area) * 100); int ensOverlap = Convert.ToInt32((pIntersectArea.Area / pEnsArea.Area) * 100); if (appOverlap > 5 || ensOverlap > 5) return true; else return false; } catch (Exception ex) { LogError(ex.StackTrace, ex.Message, "DetermineOverlapArea", null); return false; } }
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass) as IEnumDataset; IDataset pDataset = pEnumDataset.Next(); while (pDataset != null) { MessageBox.Show(pDataset.Name); pDataset = pEnumDataset.Next(); }
I haven't tried using the IMemoryWorkSpaceFactory, but I have been able to use a geoprocessor process parameter like
"in_memory\output"
pDissolvedFClass = DissolveDataset(pResourceFClass, DissolveFieldName, StatisticsFieldName & " " & Stat, "in_memory\DissolvedResource") Friend Function DissolveDataset(ByVal InputName As Object, ByVal DissolveField As String, ByVal StatsFields As String, ByVal OutputName As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass Dim DissolveDS As New ESRI.ArcGIS.DataManagementTools.Dissolve Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Using releaser As New ESRI.ArcGIS.ADF.ComReleaser releaser.ManageLifetime(DissolveDS) DissolveDS.in_features = InputName DissolveDS.dissolve_field = DissolveField DissolveDS.statistics_fields = StatsFields DissolveDS.out_feature_class = OutputName Result = RunTool(DissolveDS, Nothing) If Result Is Nothing Then System.Windows.Forms.MessageBox.Show("Could not dissolve dataset") Return Nothing End If Return ReturnObjectfromResult(Result, "Feature Class") End Using Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Dissolve error") Return Nothing End Try End Function Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue Dim InMemFC As String Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities Try GPVal = result.GetOutput(0) InMemFC = GPVal.GetAsText() Select Case Type Case "Feature Class" Return GPUtil.OpenFeatureClassFromString(InMemFC) Case "Table" Return GPUtil.OpenTableFromString(InMemFC) End Select Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error") Return Nothing End Try End Function Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String) Dim ErrorMessage As String If pResult.MessageCount > 0 Then For Count As Integer = 0 To pResult.MessageCount - 1 ErrorMessage += pResult.GetMessage(Count) Next End If System.Windows.Forms.MessageBox.Show(ErrorMessage, Title) End Sub Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Result = CType(Globals.GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2) If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error") Globals.GP.ClearMessages() Catch ex As Exception ReturnMessages(Result, "Fail") System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor") End Try Return Result End Function
public static bool DetermineOverlapArea3(IFeatureLayer pAppLayer, IFeatureLayer pEnsLayer, IFeature pAppFeature, IFeature pEnsFeature) { try { IQueryFilter queryFilter = new QueryFilterClass(); IDataset pDataset1 = null; IDataset pDataset2 = null; IFeatureSelection pFeatureSelection1 = null; IFeatureSelection pFeatureSelection2 = null; IFeatureClassName pInFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pInFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pInDatasetName1 = null; IDatasetName pInDatasetName2 = null; IFeatureClassName pOutFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pOutFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pOutDatasetName1 = null; IDatasetName pOutDatasetName2 = null; IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); IFeatureWorkspace pFeatureWorkspace = null; IWorkspaceName pWorkspaceName = new WorkspaceNameClass(); pWorkspaceName = pWorkspaceFactory.Create("", "TEMP", null, 0); IName pName = pWorkspaceName as IName; IWorkspace pWorkspace = pName.Open() as IWorkspace; pFeatureWorkspace = pWorkspace as IFeatureWorkspace; if (pAppLayer != null) { pFeatureSelection1 = pAppLayer as IFeatureSelection; queryFilter.WhereClause = "APP_NO = '" + m_AppNo + "'"; pDataset1 = pAppLayer.FeatureClass as IDataset; pFeatureSelection1.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName1 = pDataset1.FullName as IFeatureClassName; pInDatasetName1 = pInFeatureClassName1 as IDatasetName; pOutDatasetName1 = pOutFeatureClassName1 as IDatasetName; pOutDatasetName1.Name = "APPTEMP"; pOutDatasetName1.WorkspaceName = pWorkspaceName; pOutFeatureClassName1.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName1.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName1.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName1, null, pFeatureSelection1.SelectionSet, null, pOutFeatureClassName1, 0); } if (pEnsLayer != null) { pFeatureSelection2 = pEnsLayer as IFeatureSelection; queryFilter.WhereClause = "BASIN_NAME = '" + m_Label + "'"; pDataset2 = pEnsLayer.FeatureClass as IDataset; pFeatureSelection2.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName2 = pDataset2.FullName as IFeatureClassName; pInDatasetName2 = pInFeatureClassName2 as IDatasetName; pOutDatasetName2 = pOutFeatureClassName2 as IDatasetName; pOutDatasetName2.Name = "ENSTEMP"; pOutDatasetName2.WorkspaceName = pWorkspaceName; pOutFeatureClassName2.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName2.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName2.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName2, null, pFeatureSelection2.SelectionSet, null, pOutFeatureClassName2, 0); } IFeatureClass pAppFeatureClass = pFeatureWorkspace.OpenFeatureClass("APPTEMP"); IFeatureClass pEnsFeatureClass = pFeatureWorkspace.OpenFeatureClass("ENSTEMP"); //Execute clip. Clip pClip = new Clip(); pClip.in_features = pAppFeatureClass; pClip.clip_features = pEnsFeatureClass; pClip.out_feature_class = "in_memory\\APP_ENS_OVERLAP"; Geoprocessor pGeoprocessor = new Geoprocessor(); GeoProcessorResult result = pGeoprocessor.Execute(pClip, null) as GeoProcessorResult; IGPValue pGPValue = result.GetOutput(0); string InMemoryFeatureClass = string.Empty; InMemoryFeatureClass = pGPValue.GetAsText(); //Get resulting feature class. IGPUtilities pGPUtilities = new GPUtilities(); IFeatureClass pOverlapEnsFeatureClass = pGPUtilities.OpenFeatureClassFromString(InMemoryFeatureClass); //Get the resulting intersection polygon. pFeatureLayer.FeatureClass = pOverlapEnsFeatureClass; IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection; pFeatureSelection.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false); ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet as ISelectionSet; ICursor pCursor; pSelectionSet.Search(null, true, out pCursor); IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor; IFeature pIntersectFeature = pFeatureCursor.NextFeature(); IArea pIntersectArea = pIntersectFeature.Shape as IArea; IArea pAppArea = pAppFeature.Shape as IArea; IArea pEnsArea = pEnsFeature.Shape as IArea; int appOverlap = Convert.ToInt32((pIntersectArea.Area / pAppArea.Area) * 100); int ensOverlap = Convert.ToInt32((pIntersectArea.Area / pEnsArea.Area) * 100); if (appOverlap > 5 || ensOverlap > 5) return true; else return false; } catch (Exception ex) { LogError(ex.StackTrace, ex.Message, "DetermineOverlapArea", null); return false; } }
public static bool DetermineOverlapArea3(IFeatureLayer pAppLayer, IFeatureLayer pEnsLayer, IFeature pAppFeature, IFeature pEnsFeature) { try { IQueryFilter queryFilter = new QueryFilterClass(); IDataset pDataset1 = null; IDataset pDataset2 = null; IFeatureSelection pFeatureSelection1 = null; IFeatureSelection pFeatureSelection2 = null; IFeatureClassName pInFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pInFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pInDatasetName1 = null; IDatasetName pInDatasetName2 = null; IFeatureClassName pOutFeatureClassName1 = new FeatureClassNameClass(); IFeatureClassName pOutFeatureClassName2 = new FeatureClassNameClass(); IDatasetName pOutDatasetName1 = null; IDatasetName pOutDatasetName2 = null; IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); IFeatureWorkspace pFeatureWorkspace = null; IWorkspaceName pWorkspaceName = new WorkspaceNameClass(); pWorkspaceName = pWorkspaceFactory.Create("", "TEMP", null, 0); IName pName = pWorkspaceName as IName; IWorkspace pWorkspace = pName.Open() as IWorkspace; pFeatureWorkspace = pWorkspace as IFeatureWorkspace; if (pAppLayer != null) { pFeatureSelection1 = pAppLayer as IFeatureSelection; queryFilter.WhereClause = "APP_NO = '" + m_AppNo + "'"; pDataset1 = pAppLayer.FeatureClass as IDataset; pFeatureSelection1.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName1 = pDataset1.FullName as IFeatureClassName; pInDatasetName1 = pInFeatureClassName1 as IDatasetName; pOutDatasetName1 = pOutFeatureClassName1 as IDatasetName; pOutDatasetName1.Name = "APPTEMP"; pOutDatasetName1.WorkspaceName = pWorkspaceName; pOutFeatureClassName1.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName1.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName1.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName1, null, pFeatureSelection1.SelectionSet, null, pOutFeatureClassName1, 0); } if (pEnsLayer != null) { pFeatureSelection2 = pEnsLayer as IFeatureSelection; queryFilter.WhereClause = "BASIN_NAME = '" + m_Label + "'"; pDataset2 = pEnsLayer.FeatureClass as IDataset; pFeatureSelection2.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); pInFeatureClassName2 = pDataset2.FullName as IFeatureClassName; pInDatasetName2 = pInFeatureClassName2 as IDatasetName; pOutDatasetName2 = pOutFeatureClassName2 as IDatasetName; pOutDatasetName2.Name = "ENSTEMP"; pOutDatasetName2.WorkspaceName = pWorkspaceName; pOutFeatureClassName2.FeatureType = esriFeatureType.esriFTSimple; pOutFeatureClassName2.ShapeType = esriGeometryType.esriGeometryAny; pOutFeatureClassName2.ShapeFieldName = "Shape"; IExportOperation pExportOperation = new ExportOperationClass(); pExportOperation.ExportFeatureClass(pInDatasetName2, null, pFeatureSelection2.SelectionSet, null, pOutFeatureClassName2, 0); } IFeatureClass pAppFeatureClass = pFeatureWorkspace.OpenFeatureClass("APPTEMP"); IFeatureClass pEnsFeatureClass = pFeatureWorkspace.OpenFeatureClass("ENSTEMP"); //Execute clip. Clip pClip = new Clip(); pClip.in_features = pAppFeatureClass; pClip.clip_features = pEnsFeatureClass; pClip.out_feature_class = "in_memory\\APP_ENS_OVERLAP"; Geoprocessor pGeoprocessor = new Geoprocessor(); pGeoprocessor.AddOutputsToMap = false; pGeoprocessor.OverwriteOutput = true; IGeoProcessorResult result = pGeoprocessor.Execute(pClip, null) as IGeoProcessorResult; //Get the resulting intersection polygon. IFeatureClass pOverlapEnsFeatureClass = pGeoprocessor.Open("in_memory\\APP_ENS_OVERLAP") as IFeatureClass; pFeatureLayer.FeatureClass = pOverlapEnsFeatureClass; IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection; pFeatureSelection.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false); ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet as ISelectionSet; //PROBLEM HERE ICursor pCursor; pSelectionSet.Search(null, true, out pCursor); IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor; IFeature pIntersectFeature = pFeatureCursor.NextFeature(); IArea pIntersectArea = pIntersectFeature.Shape as IArea; IArea pAppArea = pAppFeature.Shape as IArea; IArea pEnsArea = pEnsFeature.Shape as IArea; int appOverlap = Convert.ToInt32((pIntersectArea.Area / pAppArea.Area) * 100); int ensOverlap = Convert.ToInt32((pIntersectArea.Area / pEnsArea.Area) * 100); if (appOverlap > 5 || ensOverlap > 5) return true; else return false; } catch (Exception ex) { MessageBox.Show(ex.Message); //LogError(ex.StackTrace, ex.Message, "DetermineOverlapArea", null); return false; } }
Hi Carlos,
I'm glad to hear you're making progress. Since you don't have the code to show why the Clip process didn't run, examine the Results information in ArcMap to find out what happened. Could it be that it's not overwriting "in_memory\APP_ENS_OVERLAP"? Have you set the Geoprocessor to overwrite outputs, either through the Geoprocessing Options dialog or by setting
pGeoprocessor.OverwriteOutput = "true"
Public Sub InMemoryExample() ' Description: Sample code showing how an in memory table can be created ' Create Workspacefactory Dim pWorkSpaceFactory As IWorkspaceFactory2 Set pWorkSpaceFactory = New InMemoryWorkspaceFactory ' Create Workspace Dim pWorkspaceName As IWorkspaceName2 Set pWorkspaceName = pWorkSpaceFactory.Create("", "MyInMemoryWorkspace", Nothing, 0) ' Get a handle on Name Dim pName As IName Set pName = pWorkspaceName ' Get a handle on FeatureWorkspace Dim pFeatureWorkspace As IFeatureWorkspace Set pFeatureWorkspace = pName.Open ' Now create a fields object and add some fields to it ' Create Fields object and set the number of fields Dim pFields As IFields Set pFields = New Fields Dim pFieldsEdit As IFieldsEdit Set pFieldsEdit = pFields pFieldsEdit.FieldCount = 2 ' Create objectID field Dim pField As IField Set pField = New Field Dim pFieldEdit As IFieldEdit Set pFieldEdit = pField With pFieldEdit .Name = "OBJECTID" .Type = esriFieldTypeOID End With Set pFieldsEdit.Field(0) = pField ' Create Type field Set pField = New Field Set pFieldEdit = pField With pFieldEdit .Editable = True .Name = "VegType" .Type = esriFieldTypeString .Length = 4 .AliasName = "Vegetation Type Code" End With Set pFieldsEdit.Field(1) = pField ' Create Table Dim pTable As ITable Set pTable = pFeatureWorkspace.CreateTable("MyInMemTable", pFields, Nothing, Nothing, "") ' Create a sample row and add data Dim pRow As IRow Set pRow = pTable.CreateRow With pRow .Value(1) = "WOOD" .Store End With ' Add table to map document Dim pMXD As IMxDocument Set pMXD = ThisDocument Dim pMap As IMap Set pMap = pMXD.FocusMap Dim pStandAloneTableCollection As IStandaloneTableCollection Set pStandAloneTableCollection = pMap Dim pStandAloneTable As IStandaloneTable Set pStandAloneTable = New StandaloneTable Set pStandAloneTable.Table = pTable pStandAloneTableCollection.AddStandaloneTable pStandAloneTable pMXD.UpdateContents End Sub