Unable to add a new feature into a Layer which has Join to other table

307
4
07-11-2019 06:41 AM
NareshNagandla
New Contributor II

Hi,

Below code works if my Featureclass is not joined with any other Table. 

If I add a join to other Table, then it throws an "DataSet does not have a valid definition" expection in featureClass.GetDefinition() line. 

I believe it is not able to get the FeatureClassdefinition when it is joined with other table.

If it is not joined then it can get the FeatureClassDefinition and no problem in adding a new record.

Does anyone else have this scenario??  Can you please let me know how to resolve this??

Below is the code used to insert a record into a FeatureClass.

// Check for an active mapview
if (MapView.Active == null)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No MapView currently active. Exiting...", "Info");
return;
}

QueuedTask.Run(() =>
{

// Get the layer selected in the Contents pane, and prompt if there is none:
if (MapView.Active.GetSelectedLayers().Count == 0)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No feature layer selected in Contents pane. Exiting...", "Info");
return;
}
// Check to see if there is a selected feature layer
var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
if (featLayer == null)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected. Exiting...", "Info");
return;
}

try
{

//Create the edit operation
var createOperation = new ArcGIS.Desktop.Editing.EditOperation();
createOperation.Name = "Generate points";
createOperation.SelectNewFeatures = false;

// get the feature class associated with the layer
var featureClass = featLayer.GetFeatureClass() as FeatureClass;

// retrieve the class definition of the point feature class
var classDefinition = featureClass.GetDefinition() as FeatureClassDefinition;

// store the spatial reference as its own variable
var spatialReference = classDefinition.GetSpatialReference();

// determine the shape field name - it may not be 'Shape'
string shapeField = featLayer.GetFeatureClass().GetDefinition().GetShapeField();

var mpbuilder = new MapPointBuilder();

mpbuilder.X = Convert.ToDouble(_dblLong);
mpbuilder.Y = Convert.ToDouble(_dblLat);

//Create the point geometry
ArcGIS.Core.Geometry.MapPoint newMapPoint = ArcGIS.Core.Geometry.MapPointBuilder.CreateMapPoint(mpbuilder.X, mpbuilder.Y, spatialReference.Wkid);

// include the attributes via a dictionary
var atts = new Dictionary<string, object>();
atts.Add("SITE", _txtSiteNumber);
atts.Add("SEQ", _txtSeqNumber);
atts.Add("LATITUDE", _dblLat);
atts.Add("LONGITUDE", _dblLong);
atts.Add(shapeField, newMapPoint);

// queue feature creation
createOperation.Create(featLayer, atts);
createOperation.Execute();

}
catch (Exception exc)
{
// Catch any exception found and display a message box.
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to create Feature: " + exc.Message);
return;
}

});

Tags (1)
0 Kudos
4 Replies
JustinHawley2
New Contributor

Yeah i'm also having the same problem.  Found anything out yet?

0 Kudos
RichRuh
Esri Regular Contributor

Naresh and Justin,

Joins create a virtual feature class, which does not have a corresponding definition.  When you add a join to a layer, the original feature class is replaced with this new virtual feature class.

Here's the technique for getting the definition from a joined layer.

1. Get the (virtual) feature class from the layer using FeatureLayer.GetFeatureClass()

2. Get the join from the virtual feature class using Table.GetJoin()

3. Get the origin table from the join using Join.GetOriginTable()

4. Get the definition from the origin table

Here's a code sample:

private TableDefinition GetDefinitionFromLayer(FeatureLayer featureLayer)
{
  // Get feature class from the layer
  FeatureClass featureClass = featureLayer.GetFeatureClass();

  // Determine if feature class is a join
  if (featureClass.IsJoinedTable())
  {

    // Get join from feature class
    Join join = featureClass.GetJoin();

    // Get origin table from join
    Table originTable = join.GetOriginTable();

    // Return feature class definition from the join's origin table
    return originTable.GetDefinition();
  }
  else
  {
    return featureClass.GetDefinition();
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I hope this helps,

--Rich

0 Kudos
NareshNagandla
New Contributor II

Thanks Rich,

Did you try to create a new row into original table??

// include the attributes via a dictionary
                    var atts = new Dictionary<string, object>();
                    atts.Add("SITE", _txtSiteNumber);
                    atts.Add("SEQ", _txtSeqNumber);
                    atts.Add("UTM_X", _dblUTMX);
                    atts.Add("UTM_Y", _dblUTMY);
                    atts.Add("LATITUDE", lat);
                    atts.Add("LONGITUDE", longitude);
                    atts.Add(shapeField, newMapPoint);

                   
                    // queue feature creation                   
                    createOperation.Create(featLayer, atts);
                    createOperation.Execute();

For me it fails when I try to call Create function.

Regards,

Naresh

0 Kudos
SeanJones
Esri Regular Contributor

Naresh,

If the fields are from the joined table, the EditOperation will fail. Joined fields cannot be edited, or created, you have to edit the table separately. In this case you can create a separate dictionary for the joined fields and have a separate Create method for that table. Include the value that the join is based on. The UI will reflect the joined field values on the new feature after the execute.

// queue feature creation                   
createOperation.Create(featLayer, atts);
CreateOperation.Create(myTable, joinAtts);
createOperation.Execute();
0 Kudos