Select to view content in your preferred language

Issues creating geometry for an in-memory feature class

2546
4
Jump to solution
03-26-2019 08:37 AM
GregSmith2
Occasional Contributor

Hello,

I am writing functionality for my ArcGIS Pro add-in that generates a custom layer on the fly. I am trying to use the memory workspace (https://pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/basics/the-in-memory-workspace.htm) to avoid creating a geodatabase on the disk, as the results of my tool are meant to be temporary and I would like them to disappear upon application close. 

So far, I've been able to successfully create the feature class and its fields through the Geoprocessing API. However, I'm getting stuck when trying to populate the layer with data. My code appears to work, and upon completion, the attribute table is populated with rows. However, there is no geometry for any of the rows (no features exist in the map at all).

Here is how I am populating my layer:

string errorMessages = string.Empty;
			using (FeatureClass layerAsFeatureClass = layer.GetTable() as FeatureClass)
			{
				EditOperation op = new EditOperation();
				op.Callback(context =>
				{
					foreach (var feature in features)
					{
						using (RowBuffer rowBuffer = layerAsFeatureClass.CreateRowBuffer())
						{
							//Setting a bunch of attributes here...
							//
							//

							if (feature.Geometry == null)
							{
								continue;
							}

							//Now set the geometry
							rowBuffer[layerAsFeatureClass.GetDefinition().GetShapeField()] = feature.Geometry.ToEsriShape();

							using (Row row = layerAsFeatureClass.CreateRow(rowBuffer))
							{
								context.Invalidate(row);
							}
						}
					}
				}, layerAsFeatureClass);

				try
				{
					if (!op.Execute())
					{
						errorMessages = op.ErrorMessage;
					}
				}
				catch (GeodatabaseException gEx)
				{
					errorMessages = gEx.Message;
				}
				catch (Exception ex)
				{
					errorMessages = ex.Message;
				}
			}

"feature.Geometry" is a MapPoint. I found that setting the geometry without calling ".ToEsriShape()" resulted in errors. Is there another way that I should be setting the geometry for an in-memory feature class? I feel like I am missing something here. Any help would be appreciated.

0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi Greg,

I ran your code using Pro 2.3 and I see the same problem. I can see the exact same error with the Edit Operation method you are experiencing.   Good news: Your code works completely with Pro 2.4.  I was able to create an in-memory feature class and then add a point feature to it using edit operation. 

Pro 2.4 will be released in a few weeks.  Thank you for posting this issue.

Thanks

Uma

View solution in original post

4 Replies
UmaHarano
Esri Regular Contributor

Hi Greg

I tested your scenario with feature classes stored in-memory workspaces created using GP Tools. I was able to add features to these feature classes using EditOperation.

For example, the code snippet below worked on an in-memory feature layer in the active map-

return QueuedTask.Run(() =>
            {
                var fc = layer.GetTable() as FeatureClass;
                if (fc == null) return;
                var fcDefinition = fc.GetDefinition();

                var editOperation = new EditOperation();
                editOperation.Name = "Create outer ring";

                using (var cursor = fc.Search())
                {
                    while (cursor.MoveNext())
                    {
                        var feature = cursor.Current as Feature;
                        if (feature == null) continue;
                       //Use .Create or any method here instead
                        editOperation.Duplicate(layer, feature.GetObjectID(), 50000.0, 50000.0, 0.0);
                    }
                }
                editOperation.Execute();               
            });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

Thanks

Uma

0 Kudos
GregSmith2
Occasional Contributor

Hi Uma,

Thank you for your response. My apologies, I realize that I am very late on getting back to looking into this...

So I tried something similar to what you suggested, with the following code:

return QueuedTask.Run(() =>
{
    var sf = SpatialReferenceBuilder.CreateSpatialReference(3419);
    var point = MapPointBuilder.CreateMapPoint(2260034.44, 235444.97, sf);
    var op = new EditOperation();
    op.Create(layer, point);
    if (!op.Execute())
    {
        errors = op.ErrorMessage;
    }
});‍‍‍‍‍‍‍‍‍‍‍

And the EditOperation returns this error message: "Edit operation failed.". Additionally, I notice that if I try to create any features in my in-memory layer through the ArcGIS Pro editing UI, I get this error message:

Here is a code snippet of how I am creating the layer in-memory:

return QueuedTask.Run(() =>
{
    errorMessages = string.Empty;
    var parameters = Geoprocessing.MakeValueArray("memory", layerName, "POINT", "", "", "", 3419);
    var result = Geoprocessing.ExecuteToolAsync("management.CreateFeatureClass", parameters, null, null, null, GPExecuteToolFlags.AddOutputsToMap);
    if (result.Result.ErrorMessages.Count() > 0)
    {
        foreach (var item in result.Result.ErrorMessages)
        {
            errorMessages += $"{item.Text}{Environment.NewLine}";
        }
        errorMessages = errorMessages.TrimEnd();
    }‍‍‍‍‍‍‍‍‍‍‍
});

Is it possible that I'm doing something wrong with how I am creating this layer?

Thanks,

Greg

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Greg,

I ran your code using Pro 2.3 and I see the same problem. I can see the exact same error with the Edit Operation method you are experiencing.   Good news: Your code works completely with Pro 2.4.  I was able to create an in-memory feature class and then add a point feature to it using edit operation. 

Pro 2.4 will be released in a few weeks.  Thank you for posting this issue.

Thanks

Uma

GregSmith2
Occasional Contributor

Uma,

Thank you for looking into this! That is good news indeed, I will look forward to the Pro 2.4 release then .

0 Kudos