Creating feature layers from custom data

1681
2
02-14-2018 05:38 PM
ZoltanBordas
New Contributor

I am sorry for the previous confusion, let me try to go into more details. I am trying to create a group layer and feature layers under the group layer. The source of the data is an outside source and the data is streamed into the plugin implementation through some TCP/IP based protocol. I have all the information (like XYZ) to create (for example) point features in a new, customized feature layer. When doing the same in ArcMap and in the old ESRi API we:
1. Created a new feature layer: IFeatureLayer newLayer = new FeatureLayerClass();
2. Created a new feature class and built up the field information IFeatureClass newClass = .... using esriGeometryType.esriGeometryPoint, 
3. Set the feature class on the new layer. newLayer.FeatureClass = newClass; newLayer.Name = ...; newLayer.Visible = true;
4. Added it to the previously created group layer. groupLayer.Add(newLayer);

I am trying to replicate the same logic using the new ArcGIS Pro SDK and I'm having trouble getting the same result.

Zoltan

Tags (1)
0 Kudos
2 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Zoltan,

 This workflow is now a bit more complicated because you have to use a GP Tool to create a new feature class, and then you can add that feature class as a layer to your group in your map's content.  Below is a code snippet that will do this - I collected the code from various ArcGIS Pro SDK community samples.  Just create a new project with a blank map, add a button with the code below and it should add a group and your new feature layer within that group.  Since the GPTool also creates a new feature layer in your map you need to remove that feature layer since it's now in the new group.  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace ProAppModule1
{
     internal class Button1 : Button
     {
          protected override void OnClick()
          {
               MakeFeatureClassInAdd2Group("MyPoints", "MyGroup");
          }

          private async void MakeFeatureClassInAdd2Group (string fcName, string grpName)
          {
               Map activeMap = MapView.Active.Map;
               // create the new Featureclass
               var gpResult = await CreateLayer(fcName, "POINT");
               if (gpResult.IsFailed)
               {
                    MessageBox.Show($@"Error {gpResult.ErrorCode} in GP Tool: {gpResult.ErrorMessages}");
               }
               await QueuedTask.Run(() => {
                    // create the group layer
                    GroupLayer grpNew = LayerFactory.Instance.CreateGroupLayer(activeMap,
                         0,  // add to the top ?
                         grpName);
                    // add the newly created feacture class as a layer to the group
                    var fcPath = System.IO.Path.Combine(Project.Current.DefaultGeodatabasePath, fcName);
                    LayerFactory.Instance.CreateFeatureLayer(
                         new Uri(fcPath),
                         grpNew,
                         layerName: $@"{fcName} - 1");
                    });
               // TODO: delete the layer which was added by the GPTool
          }

          /// <summary>
          /// Create a feature class in the default geodatabase of the project.
          /// </summary>
          /// <param name="featureclassName">Name of the feature class to be created.</param>
          /// <param name="featureclassType">Type of feature class to be created. Options are:
          /// <list type="bullet">
          /// <item>POINT</item>
          /// <item>MULTIPOINT</item>
          /// <item>POLYLINE</item>
          /// <item>POLYGON</item></list></param>
          /// <returns>Result of GP operation</returns>
          private async Task<IGPResult> CreateLayer(string featureclassName,
               string featureclassType)
          {
               List<object> arguments = new List<object>
               {        
        CoreModule.CurrentProject.DefaultGeodatabasePath, // use the default geodatabase
        featureclassName, // name of the feature class
        featureclassType, // type of geometry
        "",                                        // no template
        "DISABLED",                    // no z values
        "DISABLED"                    // no m values
               };
               await QueuedTask.Run(() =>
               {
                    arguments.Add(SpatialReferenceBuilder.CreateSpatialReference(3857)); // Projected Coordinate System     WGS 1984 Web Mercator Auxiliary Sphere
               });
               return await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(arguments.ToArray()));
          }
     }
}
ZoltanBordas
New Contributor

Wolf,

Thank you very much for the directions. Now I use the GeoProcessing API call to create the feature class and after locating it in the map's layer collection I can move it under the group layer I previously created (I will handle possible duplicates):

                // Find the new layer in the root folder of the map.
                Layer newLayer = activeMap.Layers.First((layer) => layer.Name == layerName);

                // ...  check for errors.

                // Move the new layer to the group Layer.
                await QueuedTask.Run(() => groupLayer.MoveLayer(newLayer, 0));

Now I have the feature class element under my group layer and it is exactly what I need at the moment.

Zoltan

0 Kudos