Select to view content in your preferred language

Rename or export the result of an AddJoin

3143
4
Jump to solution
12-08-2015 09:14 PM
NigelDsouza
Frequent Contributor

Hi, I have featureclass and a table who's data i wish to join. I am using the AddJoin class to perform this operation. However after the join i get a layer like "GPL0" in the TOC with the results. Why hasn't the join got applied to the featureclass? If I run the AddJoin tool manualy from the toolbox in ArcMap the result appears in the featureclass table. But the same does not display when I try to achieve it programatically. Is it possible to rename or export GPL0?

Here is my snippet.

 ITable pDerivedTable = OuputFGBLocation.OpenTable(tablename);
                       //ITableView pJoinTableView = new TableViewClass();
                       //pJoinTableView.Table = pDerivedTable;


                       //GDM Layer
                      ITable pGDMFClassTable = pGDMFclass as ITable;
                      //ITableView pGDMTableView = new TableViewClass();
                      //pGDMTableView.Table = pGDMFClassTable;
                


                       AddJoin pAddJoin = new AddJoin();
                       pAddJoin.in_layer_or_view = pGDMFClassTable;
                       pAddJoin.in_field = pGDMFClassTable.Fields.get_Field(pGDMFClassTable.Fields.FindField(AddInObjects.GDMID_FIELD_NAME));
                       pAddJoin.join_table = pDerivedTable;
                       pAddJoin.join_field = pDerivedTable.Fields.get_Field(pDerivedTable.Fields.FindField("ORIGGDMID"));                       
                       pAddJoin.join_type = "KEEP_COMMON";
                       pGp.Execute(pAddJoin,null);
                     

Regards,

Nigel.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Honored Contributor

The gp results shouldn't be coming back as null. I took a few minutes to write up the attached sample. Because you're using the gp tools I also used the gp tools instead of executing this entire workflow in ArcObjects. I've also pasted the code to create the join below. There is also a second sample in the attachment that shows how to commit the join by calling the copy features tool.

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using System;
using System.IO;


namespace ArcMapAddin
{
    public class AddJoinLayerToMap : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        private static readonly string m_srcPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        private static readonly string m_gdbPath = Path.Combine(m_srcPath, @"data\data.gdb");


        private const string m_fclName = "StateGeom";
        private const string m_tblName = "StateTabl";
        private const string m_joinFld = "STATE_ABBR";


        protected override void OnClick()
        {
            var fws = OpenWorkspace(m_gdbPath) as IFeatureWorkspace;
            var fcl = fws.OpenFeatureClass(m_fclName);
            var tab = fws.OpenTable(m_tblName);


            var lyr = new FeatureLayerClass { FeatureClass = fcl, Name = fcl.AliasName, Visible = true };


            var gp = new Geoprocessor { OverwriteOutput = true, AddOutputsToMap = false };


            try
            {
                var tool = new AddJoin { in_layer_or_view = lyr, in_field = m_joinFld, join_table = tab, join_field = m_joinFld, join_type = "KEEP_COMMON" };
                var result = gp.Execute(tool, null) as IGeoProcessorResult2;


                if (result.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                    ArcMap.Document.FocusMap.AddLayer(lyr);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }


        private static IWorkspace OpenWorkspace(string gdbPath)
        {
            var ftype = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGdbWorkspaceFactory");
            var wsf = Activator.CreateInstance(ftype) as IWorkspaceFactory;
            return wsf.OpenFromFile(gdbPath, 0);
        }
    }
}

View solution in original post

0 Kudos
4 Replies
FreddieGibson
Honored Contributor

Joins can only be applied to feature layer or table view objects. Have you tried grabbing an instance of the GPLayer object from the result of the tool?

How to access the features in an in-memory output layer using IFeatureCursor

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#/How_to_access_the_feat...

Otherwise if you're wanting to commit the join to a feature class have you tried exporting the resulting join layer to a feature class?

0 Kudos
NigelDsouza
Frequent Contributor

I tried what was given in the link you provided. But the gpresults are null... I don't know why? How do I export the result to a feature layer? Regards, Nigel.

0 Kudos
FreddieGibson
Honored Contributor

The gp results shouldn't be coming back as null. I took a few minutes to write up the attached sample. Because you're using the gp tools I also used the gp tools instead of executing this entire workflow in ArcObjects. I've also pasted the code to create the join below. There is also a second sample in the attachment that shows how to commit the join by calling the copy features tool.

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using System;
using System.IO;


namespace ArcMapAddin
{
    public class AddJoinLayerToMap : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        private static readonly string m_srcPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        private static readonly string m_gdbPath = Path.Combine(m_srcPath, @"data\data.gdb");


        private const string m_fclName = "StateGeom";
        private const string m_tblName = "StateTabl";
        private const string m_joinFld = "STATE_ABBR";


        protected override void OnClick()
        {
            var fws = OpenWorkspace(m_gdbPath) as IFeatureWorkspace;
            var fcl = fws.OpenFeatureClass(m_fclName);
            var tab = fws.OpenTable(m_tblName);


            var lyr = new FeatureLayerClass { FeatureClass = fcl, Name = fcl.AliasName, Visible = true };


            var gp = new Geoprocessor { OverwriteOutput = true, AddOutputsToMap = false };


            try
            {
                var tool = new AddJoin { in_layer_or_view = lyr, in_field = m_joinFld, join_table = tab, join_field = m_joinFld, join_type = "KEEP_COMMON" };
                var result = gp.Execute(tool, null) as IGeoProcessorResult2;


                if (result.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                    ArcMap.Document.FocusMap.AddLayer(lyr);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }


        private static IWorkspace OpenWorkspace(string gdbPath)
        {
            var ftype = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGdbWorkspaceFactory");
            var wsf = Activator.CreateInstance(ftype) as IWorkspaceFactory;
            return wsf.OpenFromFile(gdbPath, 0);
        }
    }
}
0 Kudos
NigelDsouza
Frequent Contributor

Thanks Freddie. I needed to pass the featureclass alias name as the input and not the featureclass object. Thanks so much for your efforts as always. Regards, Nigel.

0 Kudos