Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have shape file (Point) and a table which I join using the following code.

public ITable JoinLayer_Table(IFeatureLayer pFeatLayer, IStandaloneTable pStTable)
        {

            IDisplayTable pDispTable;
            IFeatureClass pFCLayer;
            ITable pTLayer;
            pDispTable = (IDisplayTable)pFeatLayer;
            pFCLayer = (IFeatureClass)pDispTable.DisplayTable;
            pTLayer = (ITable)pFCLayer;

            IDisplayTable pDispTable2;
            ITable pTTable;
            pDispTable2 = (IDisplayTable)pStTable;
            pTTable = pDispTable2.DisplayTable; 

            IRelationshipClass pRelClass;
            IMemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory();
            pRelClass = pMemRelFact.Open("TabletoLayer", (IObjectClass)pTLayer, "HouseNo", (IObjectClass)pTTable, "UNIT_ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToMany);


            IRelQueryTableFactory pRelQueryTableFact = new RelQueryTableFactory();
            ITable pRelQueryTab = (ITable)pRelQueryTableFact.Open(pRelClass, true, null, null, "", true, true);
            return pRelQueryTab;
        }

The join results is an ITable.

Now I need to make a shape file from the Join results and add it to the TOC in ArcMap. How can I do this?

Any code example would be greatly appreciated.

PS: Actually I wanted to join 2 shape files ( a point shape file and a polygon shape file) and get a point shape file for the JOIN results, but could not do that . So thats why I have a point shape file and the corresponding dbf of the polygo shape file to create the Join. Any help on this also would be greatly appreciated.

share|improve this question
    
    
Thanks for the link which really helps. But it does only what I did above ( join the feature to table). My question is how can I convert this results ( which is a ITable in my case) to a shapefile? Any code example would be very helpful. –  user1506 Jan 14 '11 at 5:47
add comment

3 Answers

You could try using ESRI.ArcGIS.GeoDatabaseUI.IExportOperation.

In the code below srcTable is your pRelQueryTab.

You need to set up source and destination dataset names.

IDatasetName srcName = (srcTable as IDataset).FullName as IDatasetName;
IDatasetName destName = new FeatureClassNameClass() as IDatasetName;

destName.Name = "C:\somePath\shapefile name without extension";

IWorkspaceName destWsName = new WorkspaceNameClass();
destWsName.PathName = "C:\somePath";
destWsName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory.1";

destName.WorkspaceName = destWsName;

IFeatureClassName destFcName = destName as IFeatureClassName;
destFcName.FeatureType = esriFeatureType.esriFTSimple;
destFcName.ShapeFieldName = "Shape";
destFcName.ShapeType = srcLayer.FeatureClass.ShapeType;

IQueryFilter queryF = null;
ISelectionSet selnSet = null;
IGeometryDef geomDef = null;

IExportOperation exOp = new ExportOperationClass();
exOp.ExportFeatureClass(srcName, queryF, selnSet, geomDef, destFcName, 0);

Not sure if it will work but I use similar code to export layers with an existing join.

share|improve this answer
    
Thanks you for the reply. Can you share the code for the ExportFeatureClass method? ( which is called in the last line of your code. Txs –  user1506 Jan 17 '11 at 22:11
    
ExportFeatureClass is a method on the IExportOperation interface. –  nef001 Jan 18 '11 at 10:22
add comment

I could not find a C# examples, however here are two VB ArcObjects samples for creating a new shapefile and adding it to ArcMap.

To export your join layer to a new shapefile use the IExportOperation (here is a VB example ).

Add the new shapefile into ArcMap use something like this:

    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pWorkspaceFactory As IWorkspaceFactory
    Dim pFeatureWorkspace As IFeatureWorkspace
    Dim pFeatureLayer As IFeatureLayer
    Dim pFeatureClass As IFeatureClass

    ' Specify the workspace and the feature class.
    Set pWorkspaceFactory = New ShapefileWorkspaceFactory
    Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("c:\data\chap4\", 0)
    Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("emidastrm")

    ' Prepare a feature layer.
    Set pFeatureLayer = New FeatureLayer
    Set pFeatureLayer.FeatureClass = pFeatureClass
    pFeatureLayer.name = pFeatureLayer.FeatureClass.AliasName

    ' Add the feature layer to the active map
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    pMap.AddLayer pFeatureLayer
    ' Refresh the active view.
    pMxDoc.ActiveView.Refresh
share|improve this answer
add comment
Public Function ExportjoinedShapefile(sLocation As String, ShpFilename As String) As IFeatureClass

Dim sProcName As String
sProcName = "ExportFeatureLayer"

On Error GoTo EH

    Dim pmxdoc As IMxDocument
    Set pmxdoc = ThisDocument

    Dim pmap As IMap
    Set pmap = pmxdoc.FocusMap

    Dim pflayer As IFeatureLayer
    Set pflayer = pmap.Layer(0)

    Set ExportFeatureLayer = Nothing

    Dim pInFc As IFeatureClass
    Set pInFc = pflayer.FeatureClass

    Dim pDispTable As IDisplayTable
    Set pDispTable = pflayer

    Dim pTable As ITable
    Set pTable = pDispTable.DisplayTable

  ' Get the FcName from the featureclass
    Dim pInDataset As IDataset
    Set pInDataset = pTable 'pInFc

    Dim pInDsName As IDatasetName
    Set pInDsName = pInDataset.FullName

  ' Create a new feature class name
  ' Define the output feature class name
    Dim pOutFeatureClassName As IFeatureClassName
    Set pOutFeatureClassName = New FeatureClassName

    Dim pOutDatasetName As IDatasetName
    Set pOutDatasetName = pOutFeatureClassName

    pOutDatasetName.Name = ShpFilename   ' here you set the output shpfile name


    Dim pOutWorkspaceName As IWorkspaceName
    Set pOutWorkspaceName = New WorkspaceName

  ' output path of new shapefile
    pOutWorkspaceName.PathName = sLocation ' here is your location
    pOutWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"
    Set pOutDatasetName.WorkspaceName = pOutWorkspaceName

  ' output shapefile characteristics
    pOutFeatureClassName.FeatureType = esriFTSimple
    pOutFeatureClassName.ShapeType = pInFc.ShapeType
    pOutFeatureClassName.ShapeFieldName = "Shape"

  ' Export
    Dim pExportOp As IExportOperation
    Set pExportOp = New ExportOperation
    pExportOp.ExportFeatureClass pInDsName, Nothing, Nothing, Nothing, pOutDatasetName, 0

    Dim pOutFeatureClass As IFeatureClass
  ' get the feature classes from the name
  ' see pg 72 Vol2, Exploring ArcObjects Book
    Dim pOutName As IName
    Set pOutName = pOutFeatureClassName    'QI
    Set pOutFeatureClass = pOutName.Open

  '
  ' maybe check if export was successful
  '

  ' set the function return
    Set ExportFeatureLayer = pOutFeatureClass

    Exit Function

EH:

    MsgBox "Error number: " & CStr(Err.Number) & _
           vbCrLf & "Error description: " & Err.Description & _
           vbCrLf & "Location:" & sProcName, _
           vbExclamation + vbOKOnly, "Your Title"

End Function
share|improve this answer
    
Thank you for your contribution, Eric! (To format code, select it after pasting it into the textbox, then press the "code sample" button found just above the textbox.) –  whuber Aug 25 '11 at 21:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.