create new layer from selected features

2483
7
Jump to solution
04-18-2012 05:56 AM
sailiTang
New Contributor III
Hi,

    Now I am working on an add-ins. I am using VB.NET and ArcInfo 10. I am using following code to do selection for a layer:

Dim featureselection As IFeatureSelection = TryCast(pMap.Layer(i), IFeatureSelection)

Dim queryFilter As IQueryFilter = New QueryFilter

queryFilter.WhereClause = "Key = 'LK'"

featureselection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

On the next step, I would like to export the selected features to a geodatabase, and then load it to the map as a layer.

Anyone knows how to write the code to export the selected features to a geodatabase? Thanks.


Saili
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Here's the code I use in one of my applications. It uses the Geoprocessing task "Feature Class to Feature Class"

   Friend Function ExportSelectedFeatures(ByVal pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal Query As String, ByVal OutPath As String, ByVal OutFC As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass          Dim CopyFC2FC As New ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass         Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2          Try             Using releaser As New ESRI.ArcGIS.ADF.ComReleaser                 releaser.ManageLifetime(CopyFC2FC)                  CopyFC2FC.in_features = pFClass                 CopyFC2FC.where_clause = Query                 CopyFC2FC.out_path = OutPath                 CopyFC2FC.out_name = OutFC                  Result = RunTool(CopyFC2FC, Nothing, True)                 If Result Is Nothing Then Return Nothing                  Return ReturnObjectfromResult(Result, "Feature Class")              End Using         Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Export Selected Features")             Return Nothing         End Try      End Function     Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult          Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult          Try             System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor                          Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor             GP.OverwriteOutput = True             GP.AddOutputsToMap = AddOutput              Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)             If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then                 If GP.MessageCount > 0 Then                     For Count = 0 To Globals.GP.MessageCount - 1                         Message += GP.GetMessage(Count) & vbNewLine                     Next                     System.Windows.Forms.MessageBox.Show(Message, "GP Failure")                 End If                               Return Nothing             End If             Return Result         Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor")             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)                 Case "Feature Layer"                     Return GPUtil.OpenFeatureLayerFromString(InMemFC)             End Select          Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")             Return Nothing         End Try      End Function

View solution in original post

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor
Here's the code I use in one of my applications. It uses the Geoprocessing task "Feature Class to Feature Class"

   Friend Function ExportSelectedFeatures(ByVal pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal Query As String, ByVal OutPath As String, ByVal OutFC As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass          Dim CopyFC2FC As New ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass         Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2          Try             Using releaser As New ESRI.ArcGIS.ADF.ComReleaser                 releaser.ManageLifetime(CopyFC2FC)                  CopyFC2FC.in_features = pFClass                 CopyFC2FC.where_clause = Query                 CopyFC2FC.out_path = OutPath                 CopyFC2FC.out_name = OutFC                  Result = RunTool(CopyFC2FC, Nothing, True)                 If Result Is Nothing Then Return Nothing                  Return ReturnObjectfromResult(Result, "Feature Class")              End Using         Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Export Selected Features")             Return Nothing         End Try      End Function     Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult          Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult          Try             System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor                          Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor             GP.OverwriteOutput = True             GP.AddOutputsToMap = AddOutput              Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)             If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then                 If GP.MessageCount > 0 Then                     For Count = 0 To Globals.GP.MessageCount - 1                         Message += GP.GetMessage(Count) & vbNewLine                     Next                     System.Windows.Forms.MessageBox.Show(Message, "GP Failure")                 End If                               Return Nothing             End If             Return Result         Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor")             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)                 Case "Feature Layer"                     Return GPUtil.OpenFeatureLayerFromString(InMemFC)             End Select          Catch ex As Exception             System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")             Return Nothing         End Try      End Function
0 Kudos
sailiTang
New Contributor III
kenbuja;190847 wrote:
Here's the code I use in one of my applications. It uses the Geoprocessing task "Feature Class to Feature Class"


Thank you so much, Kenbuja! Your code works for me!! 😄 But only ESRI.ArcGIS.ADF.ComReleaser has a problem, so I commend it out. Thank you again.

Saili
0 Kudos
KenBuja
MVP Esteemed Contributor
You have to add in the correct reference for the ADF assembly. You can't find it by using "Add ArcGIS Reference", but instead, use the Add Reference, select the .NET tab, and select ESRI.ArcGIS.ADF.Connection.Local

And don't forget to mark this question as answered.
0 Kudos
sailiTang
New Contributor III
You have to add in the correct reference for the ADF assembly. You can't find it by using "Add ArcGIS Reference", but instead, use the Add Reference, select the .NET tab, and select ESRI.ArcGIS.ADF.Connection.Local

And don't forget to mark this question as answered.


Hi Kenbuja,

   I did that,  add ESRI.ARCGIS.ADF.Connection.Local using Add Reference. And I added "Imports ESRI.ArcGIS.ADF.Connection.Local " to my code. But I get a error message is "'ComReleaser' is ambiguous in the namespace 'ESRI.ArcGIS.ADF'".

By the way, thank you for you reminding. I will mark it.
0 Kudos
KenBuja
MVP Esteemed Contributor
Try using the fully qualified name as in the code

Using releaser As New ESRI.ArcGIS.ADF.ComReleaser

And when marking a question as answered, you should really mark the post that supplied the correct answer, not the original question 🙂
0 Kudos
sailiTang
New Contributor III
Try using the fully qualified name as in the code

Using releaser As New ESRI.ArcGIS.ADF.ComReleaser

And when marking a question as answered, you should really mark the post that supplied the correct answer, not the original question 🙂



Yes, I tried that, but the same error message. I even get a warnings: "'ESRI.ArcGIS.ADF.ComReleaser' is obsolete: 'The assembly containing this class is obsolete.  Reference the ESRI.ArcGIS.ADF.Connection.Local assembly instead.'". So I changed that to ESRI.ArcGIS.ADF.Connection.Local.ComReleaser, but said:it is not defined.

Saili
0 Kudos
KenBuja
MVP Esteemed Contributor
These are the references I'm using for my application where that code originally came from

[ATTACH=CONFIG]13671[/ATTACH]
0 Kudos