Select to view content in your preferred language

CreateFeatureClass Error

1577
2
Jump to solution
11-01-2021 05:47 AM
Labels (1)
DavidMrázek
Frequent Contributor

Good day,
I'm trying to create a featureClass, but I'm making a mistake somewhere but I don't know where ...

 protected override async void OnClick()
        {
            await CreateMyLineAsync();
        }
        private static async Task CreateMyLineAsync()
        {
            await QueuedTask.Run(async () =>
            {
                var spatialReference = SpatialReferenceBuilder.CreateSpatialReference(5514);
                await CreateFeatureClass(@"D:\classToClass", "Linie.shp", "POLYLINE", "", "DISABLED", "DISABLED", spatialReference);
            });
        }
        private static async Task CreateFeatureClass(string Output, string name, string geometryType, string template, string has_m
            , string has_z, SpatialReference spatialReference)
        {
            var parameters = Geoprocessing.MakeValueArray(Output, name, geometryType, spatialReference);
            var selectLayer = "management.CreateFeatureclass";
            await FunctionPart(selectLayer, parameters);
        }
        private static async Task FunctionPart(string stringFunc, IReadOnlyList<string> parameters)
        {
            try
            {
                await Geoprocessing.ExecuteToolAsync(stringFunc, parameters).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString(), Assembly.GetExecutingAssembly().FullName);
                throw;
            }
        }

I'll be very happy if someone tells me where the mistake is.The application passes the code but does not create either a shapefile in the file or a layer in the map.

 

Thank you

0 Kudos
1 Solution

Accepted Solutions
DanielHuantes
New Contributor

I really do not like that SDK developers have to call Geoprocessing.ExecuteToolAsync for anything.  Unfortunately, as of today, you do need to call it for a lot of things... 😞   That being said I have a few  recommendations for you.

1. When you're using Geoprocessing.ExecuteToolAsync you're basically doing what a user can do via the ArcGIS Pro User interface.  So make sure you can actually use the GP Tool via the UI first.  That will provide some insight and clarify and misunderstandings you might have about the GP Tool does or does not do.

2.  Running the GP Tool through the UI produces "Details" when complete which will reveal the proper syntax for some of the arguments.  As an example I would have never figured out, on my own, how to properly format Field Maps without doing this.

3.  For this particular GP Tool, I use "CreateFeatureClass_management" but I think the syntax you're using is valid too.  I also use this tool to only create actualy feature classes in a geodatabase so don't know about Shapefiles but the documentation syntax does show shapefiles so should work too.  

Here's my syntax for an actual Feature Class if it helps

 

            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 m values
                "ENABLED",                                        // no z values
                a_spatialReference
            };

            IGPResult l_returnResult = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(arguments.ToArray()));

 

Just before hitting Reply, I reviewed your code again and your parameters are also incorrect.  You left off the template, has_m, and has_z arguments so in essence you're passing your Spatial Reference as the Template argument.   Hope that helps. 

 

BTW.... The main problem with having to call the GP Tool this way as there is no syntax checking by the C# compiler for that as the parameters are passed as an instance of a List<object>.  So calling a GP Tool is a lot of trial and error with no help from the compiler... 😞 

View solution in original post

2 Replies
DanielHuantes
New Contributor

I really do not like that SDK developers have to call Geoprocessing.ExecuteToolAsync for anything.  Unfortunately, as of today, you do need to call it for a lot of things... 😞   That being said I have a few  recommendations for you.

1. When you're using Geoprocessing.ExecuteToolAsync you're basically doing what a user can do via the ArcGIS Pro User interface.  So make sure you can actually use the GP Tool via the UI first.  That will provide some insight and clarify and misunderstandings you might have about the GP Tool does or does not do.

2.  Running the GP Tool through the UI produces "Details" when complete which will reveal the proper syntax for some of the arguments.  As an example I would have never figured out, on my own, how to properly format Field Maps without doing this.

3.  For this particular GP Tool, I use "CreateFeatureClass_management" but I think the syntax you're using is valid too.  I also use this tool to only create actualy feature classes in a geodatabase so don't know about Shapefiles but the documentation syntax does show shapefiles so should work too.  

Here's my syntax for an actual Feature Class if it helps

 

            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 m values
                "ENABLED",                                        // no z values
                a_spatialReference
            };

            IGPResult l_returnResult = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(arguments.ToArray()));

 

Just before hitting Reply, I reviewed your code again and your parameters are also incorrect.  You left off the template, has_m, and has_z arguments so in essence you're passing your Spatial Reference as the Template argument.   Hope that helps. 

 

BTW.... The main problem with having to call the GP Tool this way as there is no syntax checking by the C# compiler for that as the parameters are passed as an instance of a List<object>.  So calling a GP Tool is a lot of trial and error with no help from the compiler... 😞 

DavidMrázek
Frequent Contributor

Thank you very much for your answer. Yes, one of the bugs was incorrect parameter placement, moreover, this method does not create a new shapefile but must take an already created one, so I had it wrong as well.

0 Kudos