示例#1
0
        protected override void OnClick()
        {
            // Once the button has been clicked, disable the Add-In button.
            this.Enabled = false;

            // Controls the launch and display of the GUI window.
            gui_AddRaster = new GUI_AddRaster();
            gui_AddRaster.StartPosition = FormStartPosition.CenterScreen;            // Open GUI on active window as ArcMap, and centered.
            gui_AddRaster.Show(Control.FromHandle((IntPtr)ArcMap.Application.hWnd)); // Show the GUI window in front of ArcMap at all times.
        }
        // Function with arguments that have been passed from each sensor.
        public void BandCounts(GUI_AddRaster theGUI)
        {
            try
            {
                // Folder path only.
                string rasterFolderPath = Path.GetDirectoryName(theGUI.textBox_PathURL.Text);

                // Image file name only.
                string rasterFileName = Path.GetFileName(theGUI.textBox_PathURL.Text);

                // Needed for working with rasters.
                IWorkspaceFactory wSF = new RasterWorkspaceFactoryClass();

                // Set the raster image workspace.
                IWorkspace wS = wSF.OpenFromFile(rasterFolderPath, ArcMap.Application.hWnd);

                // Prepare the raster workspace.
                IRasterWorkspace rasterWS = wS as IRasterWorkspace;

                IRasterDataset rasterDataset;

                try
                {
                    // Open the raster image.
                    rasterDataset = rasterWS.OpenRasterDataset(rasterFileName);
                }
                catch
                {
                    // If the raster image is invalid with no bands to count,
                    // catch the error and display this message.
                    MessageBox.Show("Invalid raster. No bands to be counted.");
                    return;
                }

                // Prepare raster image as raster layer.
                IRasterLayer rasterLayer = new RasterLayerClass();

                // Create the raster layer from raster image.
                rasterLayer.CreateFromDataset(rasterDataset);

                // Access band total for image.
                IRasterBandCollection rasterBC        = rasterDataset as IRasterBandCollection;
                IEnumRasterBand       pEnumRasterBand = rasterBC.Bands;
                totalBands = rasterBC.Count;

                // The following code is for accessing individual band names, if needed.
                // These are unused for the code in its current format.
                //int curBandCount = rasterBC.Count;
                //IRasterBand pRasterBand;
                //string bandName;
                //while (curBandCount > 0)
                //{
                //pRasterBand = pEnumRasterBand.Next();
                //bandName = pRasterBand.Bandname.ToString();
                //curBandCount = curBandCount - 1;
                //}
            }

            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                MessageBox.Show("Exception caught: " + nL + exc.Message + nL + exc.StackTrace);
                return;
            }
        }
        // Function with arguments that have been passed from each sensor.
        public void bandCombination(GUI_AddRaster theGUI, int int_RedBandIndex, int int_GreenBandIndex, int int_BlueBandIndex)
        {
            try
            {
                // Folder path only.
                string rasterFolderPath = System.IO.Path.GetDirectoryName(theGUI.textBox_PathURL.Text);

                // Image file name only.
                string rasterFileName = System.IO.Path.GetFileName(theGUI.textBox_PathURL.Text);

                // Map document.
                IMxDocument mxDoc = ArcMap.Application.Document as IMxDocument;

                // Needed for working with rasters.
                IWorkspaceFactory wSF = new RasterWorkspaceFactoryClass();

                // Set the raster image workspace.
                IWorkspace wS = wSF.OpenFromFile(rasterFolderPath, ArcMap.Application.hWnd);

                // Prepare the raster workspace.
                IRasterWorkspace rasterWS = wS as IRasterWorkspace;

                // Open the raster image.
                IRasterDataset rasterDataset = rasterWS.OpenRasterDataset(rasterFileName);

                // Prepare raster image as raster layer.
                IRasterLayer rasterLayer = new RasterLayerClass();

                // Create the raster layer from raster image.
                rasterLayer.CreateFromDataset(rasterDataset);

                // Add raster layer to map.
                mxDoc.AddLayer(rasterLayer);

                // Required for displaying raster layer with specific band combinations (the renderer).
                IRasterRGBRenderer2 rgbRen = new RasterRGBRendererClass();
                IRasterRenderer     rasRen = rgbRen as IRasterRenderer;

                // Assign band combination based on parameters that were fed into the function.
                rgbRen.RedBandIndex   = int_RedBandIndex;
                rgbRen.GreenBandIndex = int_GreenBandIndex;
                rgbRen.BlueBandIndex  = int_BlueBandIndex;

                // Rename the raster layer to sensor type + study type + image name, with spaces and file extension removed.
                rasterLayer.Name = theGUI.combo_SensorType.SelectedItem.ToString().Replace(" ", string.Empty) + "_" +
                                   theGUI.combo_StudyType.SelectedItem.ToString().Replace(" ", string.Empty) + "__" +
                                   System.IO.Path.GetFileNameWithoutExtension(rasterFileName).Replace(" ", string.Empty);

                // Update the renderer with band combinations.
                rasterLayer.Renderer = rasRen;
                rasRen.Update();

                if (theGUI.checkBox_RasterExtent.Checked)
                {
                    // Zoom to raster image extent if the checkbox is checked.
                    mxDoc.ActiveView.Extent = rasterLayer.AreaOfInterest;
                }

                // Refresh the map and update the table of contents.
                mxDoc.ActiveView.Refresh();
                mxDoc.UpdateContents();
            }

            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                MessageBox.Show("Exception caught: " + nL + exc.Message + nL + exc.StackTrace);
                return;
            }
        }