ArcGIS Pro二次开发-添加一个栅格数据到当前窗口

using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows;
//using System.Windows.Forms;
 

using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
 
namespace ProAppModule1
{
    internal class Button1 : ArcGIS.Desktop.Framework.Contracts.Button
    {
        string AddFieldName = "gisoracle";
        protected override async void OnClick()
        {
            await AddRasterLayerToMapAsync();
        }
    
        public static async Task AddRasterLayerToMapAsync()
        {
            try
            {
                // Gets the first 2D map from the project that is called Map.
               
                Map _map = MapView.Active.Map;
 
                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("AddRasterLayerToMap: Failed to get map.");
                    return;
                }
 
                // Creates a url pointing to the source. In this case it is a url to an image service
                // which will result in an image service layer being created.
                //string dataSoureUrl = @"https://landsat2.arcgis.com/arcgis/services/Landsat/MS/ImageServer";
                string dataSourceUrl = @"F:\2020book\toolforpro\MyProject3\ff\images\000000080.tif";
                // Note: A url can also point to 
                // 1) An image on disk or in a file geodatabase, e.g., string dataSoureUrl = @"C:\temp\a.tif"; This results in a raster layer.
                // 2) A mosaic dataset in a file gdb, e.g., string dataSoureUrl = @"c:\temp\mygdb.gdb\MyMosaicDataset"; This results in a mosaic layer.
                // 3) A raster or mosaic dataset in an enterprise geodatabase.
 
                // Creates an ImageServiceLayer object to hold the new layer.
                RasterLayer imageLayer = null;
 
                // The layer has to be created on the Main CIM Thread (MCT).
                await QueuedTask.Run(() =>
                {
                    // Creates a layer based on the url. In this case the layer we are creating is an image service layer.
                    imageLayer = (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(dataSourceUrl), _map);
                    if (imageLayer == null)
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + dataSourceUrl);
                        return;
                    }
                });
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to add layer: " + exc.Message);
                return;
            }
        }
    }
}
=====================添加矢量数据==============================
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows;
//using System.Windows.Forms;




using System.Text;

using System.Threading;

using System.Threading.Tasks;

using ArcGIS.Desktop.Core.Geoprocessing;

using ArcGIS.Desktop.Framework;

using ArcGIS.Desktop.Framework.Contracts;

using ArcGIS.Desktop.Framework.Dialogs;

using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Core;

using ArcGIS.Desktop.Framework.Threading.Tasks;



namespace ProAppModule1
{
    internal class Button1 : ArcGIS.Desktop.Framework.Contracts.Button
    {
        string AddFieldName = "gisoracle";
        protected override async void OnClick()

        {
            await AddLayerToMapAsync();

        }
     
        public static async Task AddLayerToMapAsync()

        {

            try

            {

                // Gets the first 2D map from the project that is called Map.
                

                Map _map = MapView.Active.Map;



                if (_map == null)

                {

                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("AddLayerToMap: Failed to get map.");

                    return;

                }
                string dataSourceUrl = @"E:\LQ\Kk.gdb\ds\lq";

                

                Layer Layer = null;



                // The layer has to be created on the Main CIM Thread (MCT).

                await QueuedTask.Run(() =>

                {

                    // Creates a layer based on the url. In this case the layer we are creating is an image service layer.

                    Layer = (Layer)LayerFactory.Instance.CreateLayer(new Uri(dataSourceUrl), _map);

                    if (Layer == null)

                    {

                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + dataSourceUrl);

                        return;

                    }

                });

            }

            catch (Exception exc)

            {

                // Catch any exception found and display a message box.

                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to add layer: " + exc.Message);

                return;

            }

        }

    }
}

来自:https://github.com/Esri/arcgis-pro-sdk/tree/master/Examples 

猜你喜欢

转载自www.cnblogs.com/gisoracle/p/12467765.html
今日推荐