2

I have a shapefile (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 shapefile from the Join results and add it to the TOC in ArcMap. How can I do this?

Any code example would be greatly appreciated.

Actually I wanted to join two shapefiles (a point shapefile and a polygon shapefile) and get a point shapefile for the JOIN results, but could not do that. So that's why I have a point shapefile and the corresponding dbf of the polygon shapefile to create the Join.

Any help on this also would be greatly appreciated.

nmtoken
  • 13,355
  • 5
  • 38
  • 87
user1506
  • 263
  • 1
  • 3
  • 13
  • http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriCarto/esriCarto_IDisplayRelationshipClass_Example.htm – Jim B Jan 14 '11 at 03:36
  • 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 05:47

3 Answers3

2

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.

nef001
  • 785
  • 7
  • 12
  • 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
0

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
artwork21
  • 35,114
  • 8
  • 66
  • 134
0
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
whuber
  • 69,783
  • 15
  • 186
  • 281
ericoneal
  • 825
  • 4
  • 13
  • 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