Geoprocessing tool

835
3
Jump to solution
10-20-2021 11:34 PM
DavidMrázek
Occasional Contributor II

Hello,

could anyone tell me just looking at the code where could there be a bug? The original code that I managed was functional, but I wanted to simplify the code even more, and it no longer works.

 

 protected override async void OnClick()
        {
            FeatureLayer fl = MapView.Active.Map.FindLayers("O_Klad_ZTM10").First() as FeatureLayer;
            if (fl == null)
                return;
            await QueuedTask.Run(() =>
            {
                QueryFilter qf = new QueryFilter();
                string whereClause = "ZTM10_nom LIKE '0602%'";
                qf.WhereClause = whereClause;
                fl.Select(qf);
            });
            var path = @"D:\PomocnaVrstva.shp";
            var extent = 200;
            // CalculateBuffer(fl, path, extent, "Meters");

            object[] listParameter = {fl, path, extent, "Meters"};
            await StartGp("analysis.Buffer", listParameter);
            int indexNumber = 0;
            var mapView = MapView.Active;
            if (mapView != null)
            {
                Uri shpUri = new Uri(path);
                await QueuedTask.Run(async ()=>//from here it no longer takes place
                {
                    Layer shapeLayer = LayerFactory.Instance.CreateLayer(
                        shpUri, mapView.Map, indexNumber, "PomocnaVrstva");
                    // var selectedLayer = mapView.GetSelectedLayers()[0] as FeatureLayer; není potřeba
                    var shpLayer = shapeLayer as FeatureLayer;
                    var pathDis = @"D:\DissolvePomocnaVrstva";
                    //// DissolveLayer(shpLayer, pathDis);
                    object[] listOfPara = {shpLayer, pathDis};
                    await StartGp("management.Dissolve", listOfPara);
                    FeatureLayer dissFeat = MapView.Active.Map.FindLayers("DissolvePomocnaVrstva").First() as FeatureLayer;
                    var overlapType = "COMPLETELY_WITHIN";
                    var selectionType = "NEW_SELECTION";
                    var pathErase = @"D:\ErasePomocnaVrstva";
                    //SelectLayer(featLayer, overlapType, dissFeat, selectionType);
                    FeatureLayer featLayer = MapView.Active.Map.FindLayers("O_Klad_ZTM50").First() as FeatureLayer;
                    object[] listOfParameter = { featLayer, overlapType, dissFeat, selectionType };
                    await StartGp("management.SelectLayerByLocation", listOfParameter);
                    //EraseLayer(dissFeat, featLayer, pathErase);
                    object[] parameterList = { dissFeat, featLayer, pathErase };
                    await StartGp("analysis.Erase", parameterList);
                });
            }
        }

        private static async Task StartGp(string funcArc, object parame)
        {
            var parameters = Geoprocessing.MakeValueArray(parame);
           FunctionPart(funcArc, parameters);

        }
        //private static void CalculateBuffer(FeatureLayer featureLayer, string sOutputPath, double dDistance, string sUnits)
        //{
        //    var parameters = Geoprocessing.MakeValueArray(featureLayer, sOutputPath,
        //        string.Format(CultureInfo.InvariantCulture, "{0} {1}", dDistance, sUnits));
        //    var buffer = "analysis.Buffer";
        //    FunctionPart(buffer, parameters);
        //}
        //private static void DissolveLayer(FeatureLayer featureLayer, string sOutputPath)
        //{
        //    var parameters = Geoprocessing.MakeValueArray(featureLayer, sOutputPath);
        //    var dissolve = "management.Dissolve";
        //    FunctionPart(dissolve, parameters);
        //}
        //private static void EraseLayer(FeatureLayer featureLayer, FeatureLayer eraseFeatureLayer, string sOutputPath)
        //{
        //    var parameters = Geoprocessing.MakeValueArray(featureLayer, eraseFeatureLayer, sOutputPath);
        //    var erase = "analysis.Erase";
        //    FunctionPart(erase, parameters);
        //}
        //private static void SelectLayer(FeatureLayer featureLayer, string overlapType, FeatureLayer selectFeature, string selectionType)
        //{
        //    var parameters = Geoprocessing.MakeValueArray(featureLayer, overlapType, selectFeature, selectionType);
        //    var selectLayer = "management.SelectLayerByLocation";
        //    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 will be very grateful for every idea.

Thank you!

0 Kudos
1 Solution

Accepted Solutions
DavidMrázek
Occasional Contributor II

There were three mistakes and terribly unnecessary:
1. suffix at path to files
2. u buffer should be input conditions three, I wrote two and that is the size of the clip and the unit separately
3. object list is different than parameter in StartGp (object [] and object)

View solution in original post

0 Kudos
3 Replies
ABishop
MVP Regular Contributor

@DavidMrázek 

When you run this code, what error do you get?

Amanda Bishop, GISP
DavidMrázek
Occasional Contributor II

There were three mistakes and terribly unnecessary:
1. suffix at path to files
2. u buffer should be input conditions three, I wrote two and that is the size of the clip and the unit separately
3. object list is different than parameter in StartGp (object [] and object)

0 Kudos
CharlesMacleod
Esri Regular Contributor

I'm curious about the use of "ConfigureAwait(false)"....are u trying to change the synchronization context from the QueuedTask? FWIW QueuedTask It is a single thread _by design_......if you remove that statement what happens?