Exemplo n.º 1
0
        /// <summary>
        /// Create a raster layer and add it to a map.
        /// </summary>
        /// <returns>Task that contains a layer.</returns>
        private async Task AddRasterLayerToMap()
        {
            try
            {
                // Get the first map called "Map" from the current project.
                Map myMap = null;
                myMap = await GetMapFromProject(Project.Current, "Map");

                if (myMap == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to get map.");
                    return;
                }


                // Create 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://environment.data.gov.uk/image/services/SURVEY/VegetationObjectModel/ImageServer";
                // Note: A url can also point to
                // 1.) An image on disk or an 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.

                // Create an ImageServiceLayer object to hold the new layer.
                ImageServiceLayer rasterLayer = null;

                // The layer has to be created on the Main CIM Thread (MCT).
                await QueuedTask.Run(() =>
                {
                    // Create a layer based on the url. In this case the layer we are creating is an image service layer.
                    var layerParams = new LayerCreationParams(new Uri(dataSoureUrl));
                    rasterLayer     = LayerFactory.Instance.CreateLayer <ImageServiceLayer>(layerParams, myMap);

                    // Check if it is created.
                    if (rasterLayer == null)
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + dataSoureUrl);
                        return;
                    }

                    // Validate the colorizer to see if the layer is colorized correctly.
                    if (!(rasterLayer.GetColorizer() is CIMRasterRGBColorizer))
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Colorizer does not match for layer created from url: " + dataSoureUrl);
                    }
                });
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message);
                return;
            }
        }
        /// <summary>
        /// Set the stretch type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="stretschType">The stretch type to set on the layer.</param>
        /// <param name="statsType">The stretch statistics type to set on the layer. This lets you pick between Dataset, Area of View (to enable DRA) or Custom statistics.</param>
        /// <returns></returns>
        public static async Task SetStretchTypeAsync(RasterStretchType stretschType, RasterStretchStatsType statsType = RasterStretchStatsType.Dataset)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetStretchType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer of type ImageServiceLayer
                ImageServiceLayer imageServiceLayer = MapView.Active.GetSelectedLayers().OfType <ImageServiceLayer>().FirstOrDefault();
                if (imageServiceLayer != null)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = imageServiceLayer.GetColorizer();
                        // Set the stretch type and stretch statistics type on the colorizer.
                        // Theese parameters only apply to the Stretch and RGB colorizers.
                        if (newColorizer is CIMRasterRGBColorizer)
                        {
                            ((CIMRasterRGBColorizer)newColorizer).StretchType      = stretschType;
                            ((CIMRasterRGBColorizer)newColorizer).StretchStatsType = statsType;
                        }
                        else if (newColorizer is CIMRasterStretchColorizer)
                        {
                            ((CIMRasterStretchColorizer)newColorizer).StretchType = stretschType;
                            ((CIMRasterStretchColorizer)newColorizer).StatsType   = statsType;
                        }
                        else
                        {
                            MessageBox.Show("Selected layer must be visualized using the RGB or Stretch colorizer");
                        }
                        // Update the image service with the new colorizer
                        imageServiceLayer.SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set stretch type: " + exc.Message);
                return;
            }
        }
        /// <summary>
        /// Create a raster layer and add it to a map.
        /// </summary>
        /// <returns></returns>
        private async Task AddRasterLayerToMap()
        {
            try
            {
                // Get the first 2D map from the current project.
                Map myMap = null;
                myMap = await GetMapFromProject(Project.Current, "CIMPATH=map/map.xml");
                if (myMap == null)
                {
                    System.Windows.MessageBox.Show("Failed to get map.");
                    return;
                }

                // Create 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 serviceURL = @"http://imagery.arcgisonline.com/arcgis/services/LandsatGLS/GLS2010_Enhanced/ImageServer";
                // Note: A url can also point to  
                // 1.) An image on disk or an in a file geodatabase. e.g. string fileRasterURL = @"C:\temp\a.tif"; This results in a raster layer.
                // 2.) A mosaic dataset in a file gdb e.g. string mosaicURL = @"c:\temp\mygdb.gdb\MyMosaicDataset"; This results in a mosaic layer.
                // 3.) A raster or mosaic dataset in an enterprise geodatabase.

                // Create an ImageServiceLayer object to hold the new layer.
                ImageServiceLayer imageServiceLayer = null;

                // The layer has to be created on the Main CIM Thread (MCT).
                await QueuedTask.Run(() =>
                {
                    // Create a layer based on the url. In this case the layer we are creating is an image service layer.
                    imageServiceLayer = (ImageServiceLayer)Layer.Create(new Uri(serviceURL), myMap);
                    
                    // Check if it is created.
                    if (imageServiceLayer == null) 
                    {
                        System.Windows.MessageBox.Show("Failed to Create Image Service Layer for url:" + serviceURL);
                        return;
                    }
                    
                    // Validate the colorizer to see if the layer is colorized correctly.
                    if (!(imageServiceLayer.GetColorizer() is CIMRasterRGBColorizer)) 
                        System.Windows.MessageBox.Show("Colorizer does not match for layer created from url: " + serviceURL);
                });
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                System.Windows.MessageBox.Show("Exception caught: " + exc.Message);
                return;
            }
        }
        /// <summary>
        /// Set the resampling type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="resamplingType">The resampling type to set on the layer.</param>
        /// <returns></returns>
        public static async Task SetResamplingTypeAsync(RasterResamplingType resamplingType)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetResamplingType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer of type ImageServiceLayer
                ImageServiceLayer imageServiceLayer = MapView.Active.GetSelectedLayers().OfType <ImageServiceLayer>().FirstOrDefault();
                if (imageServiceLayer != null)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be get/set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = imageServiceLayer.GetColorizer();
                        // Set the resampling type on the colorizer.
                        newColorizer.ResamplingType = resamplingType;
                        // Update the image service with the new colorizer
                        imageServiceLayer.SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set resampling type: " + exc.Message);
            }
        }