ArcGIS Pro代码创建一个随机点
public static class RandomExtension { /// <summary> /// Generate a random double number between the min and max values. /// </summary> /// <param name="random">Instance of a random class.</param> /// <param name="minValue">The min value for the potential range.</param> /// <param name="maxValue">The max value for the potential range.</param> /// <returns>Random number between min and max</returns> /// <remarks>The random result number will always be less than the max number.</remarks> public static double NextDouble(this Random random, double minValue, double maxValue) { return random.NextDouble() * (maxValue - minValue) + minValue; } /// <summary> /// /Generate a random coordinate (only x,y values) within the provided envelope. /// </summary> /// <param name="random">Instance of a random class.</param> /// <param name="withinThisExtent">Area of interest in which the random coordinate will be created.</param> /// <returns>A coordinate with random values (only x,y values) within the extent.</returns> public static Coordinate2D NextCoordinate2D(this Random random, Envelope withinThisExtent) { return new Coordinate2D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax), random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax)); } /// <summary> /// /Generate a random coordinate 3D (containing x,y,z values) within the provided envelope. /// </summary> /// <param name="random">Instance of a random class.</param> /// <param name="withinThisExtent">Area of interest in which the random coordinate will be created.</param> /// <returns>A coordinate with random values 3D (containing x,y,z values) within the extent.</returns> public static Coordinate3D NextCoordinate3D(this Random random, Envelope withinThisExtent) { return new Coordinate3D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax), random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax), 0); } } ======================= protected override async void OnClick() { // to work in the context of the active display retrieve the current map Map activeMap = MapView.Active.Map; // retrieve the first point layer in the map var pointFeatureLayer = activeMap.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where( lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPoint).FirstOrDefault(); if (pointFeatureLayer == null) return; // first generate some random points await ConstructSamplePoints(pointFeatureLayer); // activate the button completed state to enable the polyline button FrameworkApplication.State.Activate("geometry_points_constructed"); } /// <summary> /// Create random sample points in the extent of the spatial reference /// </summary> /// <param name="pointFeatureLayer">Point geometry feature layer used to the generate the points.</param> /// <returns>Task{bool}</returns> private Task<bool> ConstructSamplePoints(FeatureLayer pointFeatureLayer) { // create a random number generator var randomGenerator = new Random(); // the database and geometry interactions are considered fine-grained and must be executed on // the main CIM thread return QueuedTask.Run(() => { // start an edit operation to create new (random) point features var createOperation = new EditOperation() { Name = "Generate points", SelectNewFeatures = false }; // get the feature class associated with the layer var featureClass = pointFeatureLayer.GetTable() as FeatureClass; // define an area of interest. Random points are generated in the allowed // confines of the allow extent range var areaOfInterest = MapView.Active.Extent; MapPoint newMapPoint = null; // retrieve the class definition of the point feature class var classDefinition = featureClass.GetDefinition() as FeatureClassDefinition; // store the spatial reference as its own variable var spatialReference = classDefinition.GetSpatialReference(); // create 20 new point geometries and queue them for creation for (int i = 0; i < 20; i++) { // generate either 2D or 3D geometries if (classDefinition.HasZ()) newMapPoint = MapPointBuilder.CreateMapPoint(randomGenerator.NextCoordinate3D(areaOfInterest), spatialReference); else newMapPoint = MapPointBuilder.CreateMapPoint(randomGenerator.NextCoordinate2D(areaOfInterest), spatialReference); // queue feature creation createOperation.Create(pointFeatureLayer, newMapPoint); } // execute the edit (feature creation) operation return createOperation.ExecuteAsync(); }); }
分类:
ArcGIS Pro二次开发
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2020-03-23 ArcGIS Pro 二次开发添加连接,断开连接,导入mxd
2020-03-23 无法将类型“System.Threading.Tasks.Task<string>”隐式转换为“string”
2020-03-23 ArcGIS Pro 读写BLOB字段
2020-03-23 ArcGIS Pro二次开发--增加数据到地图,打开影像
2015-03-23 CAD增强属性块的还原(转)