ArcGIS Pro 快速增加在内存表点中


// Works :
        protected async Task MemDB_Geoprocessing()
        {
            string gdbPath = "memory";
            //string gdbPath = CoreModule.CurrentProject.DefaultGeodatabasePath;

            string featureclassName = "testFC";
            string featureclassType = "POINT";
            int spatialRefID = 20351; //Z51
            var activeMap = MapView.Active.Map;

            // 1) Create the Feature class table - use GP tools
            var sr = SpatialReferenceBuilder.CreateSpatialReference(spatialRefID); 
            List<object> geoProcessingArgs = new List<object> { gdbPath, featureclassName, featureclassType, "", "DISABLED", "DISABLED", sr };

            GPExecuteToolFlags flags = GPExecuteToolFlags.None | GPExecuteToolFlags.AddOutputsToMap;  
            IGPResult result = null;
            var t = Task.Run(async () =>
            {
                result = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(geoProcessingArgs.ToArray()), null, null, null, flags);
            });
            await (t);


            // 2) Set up attribute structure 
            int numFields = 64 / 2; // 64 fields, 1 each of type text, numeric

            var fields = new Dictionary<string, string>();
            for (int fldCnt = 0; fldCnt < numFields; fldCnt++)
            {
                fields.Add($"Field{fldCnt}", $"Text # 255 myDefaultValue"); 
                fields.Add($"Field{fldCnt}_num", $"DOUBLE # # #");
            }

            AddFields(gdbPath + @"\" + featureclassName, fields);


            // 3) Add some test point features to the Feature layer
            var pointFeatureLayer = activeMap.FindLayers(featureclassName)[0] as FeatureLayer;
            var featureAttribs = new Dictionary<string, object>(); 
            Random rnd = new Random();

            var createOp = new EditOperation() { Name = "Generate points", SelectNewFeatures = false };
            long pointCnt = 0;
            for (pointCnt = 0; pointCnt < 50*1000; pointCnt++) // for each new point feature
            {
                featureAttribs.Clear();

                // geometry - random-ish coords
                Coordinate2D newCoordinate = new Coordinate2D(320000 + rnd.Next(-1000, 1000), 7140000 + rnd.Next(-1000, 1000)); 
                featureAttribs.Add("Shape", MapPointBuilder.CreateMapPoint(newCoordinate));

                // attribute values
                for (int fldCnt = 0; fldCnt < numFields; fldCnt++)
                {
                    featureAttribs.Add($"Field{fldCnt}", $"SomeText{fldCnt}");
                    featureAttribs.Add($"Field{fldCnt}_num", fldCnt);
                }

                createOp.Create(pointFeatureLayer, featureAttribs); // queue feature creation

            }

            // commit features
            Task<bool> tRes = createOp.ExecuteAsync(); // execute the batch edit operation
            await (tRes);

            if (!tRes.Result) throw new Exception(createOp.ErrorMessage);


            Task tse = Task.Run(async () => { await Project.Current.SaveEditsAsync(); });
            await(tse);

            Task tz = Task.Run(async () => {
                await MapView.Active.ZoomToAsync(pointFeatureLayer, false);  
            });
            await(tz);
        }

        private void AddFields(string tableNamePath, Dictionary<string, string> fields)
        {
            string fieldNames = "";
            List<object> geoProcessingArgs = null;
            foreach (KeyValuePair<string, string> item in fields)
            {
                fieldNames = String.Format("{0}{1} {2}; ", fieldNames, item.Key, item.Value);
            }

            geoProcessingArgs = new List<object>{tableNamePath,fieldNames};

            var t = Task.Run(async () =>
            {
                var result = Geoprocessing.ExecuteToolAsync("management.AddFields", Geoprocessing.MakeValueArray(geoProcessingArgs.ToArray()));
            });
            t.Wait();
        }

相关