How do I download a raster via a raster map service address?

1854
1
12-13-2011 04:42 PM
IrohBrown
New Contributor
I have a raster map service address that points to a raster, e.g. http://server/ArcGIS/rest/services/rsdp/L542322008047_ALBEDO/ImageServer. I would like to use ArcObjects to connect to the server and download a portion of this raster.

What classes do I need to use to accomplish this? I know that IGISServerConnection is required to make a ServerContext, but beyond that I am not sure what ArcObjects are needed to access the above raster.

Also, is it possible to obtain from the server either a portion of the raster or the entire raster at smaller resolution than the original?
0 Kudos
1 Reply
IrohBrown
New Contributor
Although I originally said "raster map service address", what I really wanted was to download a clipped portion of a raster from an ImageServer specified by a URL address.

I solved this one on my own. For anyone who might need to do this as well, I had to use an ImageServerLayerClass to connect to the ImageServer. Note that the address passed to IImageServerLayer.Initialize() must be the SOAP version of the address (which can be obtained by opening the REST version in a browser and clicking on "SOAP"). From there, I obtained the Raster object from IImageServerLayer in order to provide georeferenced clipping coordinates (this allows me to copy only a portion of the image) and a pixel resolution for the resulting image. Finally, I use a RasterWorkspaceClass as an IWorkspace and the Raster as an ISaveAs interface to save the file as a GeoTIFF. Here's the code I used, more or less:

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesRaster;
using System.Diagnostics;

namespace ArcProxy
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new ArcProxy.LicenseInitializer();
    
        [STAThread()]
        static void Main(string[] args)
        {
            // ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB },
            new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst, esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork, esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst, esriLicenseExtensionCode.esriLicenseExtensionCodeSchematics, esriLicenseExtensionCode.esriLicenseExtensionCodeMLE, esriLicenseExtensionCode.esriLicenseExtensionCodeDataInteroperability, esriLicenseExtensionCode.esriLicenseExtensionCodeTracking });
            // ESRI License Initializer generated code.

            string path = System.IO.Directory.GetCurrentDirectory();
            string filename = "galle.tif";

            // Connect to an image serve via URL
            IImageServerLayer imageServerLayer = new ImageServerLayerClass();
            string URL = "http://vega/ArcGIS/services/GalleCrater/ImageServer";
            imageServerLayer.Initialize(URL);

            // Get the raster from the image server layer
            IRaster raster = imageServerLayer.Raster;

            // Rasters from image servers are large; define the size of the raster.
            IRasterProps rasterProps = (IRasterProps)raster;
            IEnvelope clipEnvelope = new EnvelopeClass();
            clipEnvelope.PutCoords(735000, 4335000, 740000, 4340000);
            rasterProps.Extent = clipEnvelope;
            rasterProps.Width = 2048;
            rasterProps.Height = 2048;
 
            // Open a workspace for saving the image
            IWorkspace workspace = openWorkspace(path);

            // Ensure the image does not currently exist
            System.IO.File.Delete(path + @"\" + filename);
            
            // Save the image to the file
            System.Console.WriteLine("Saving...");
            ISaveAs saveas = (ISaveAs)raster;
            IDataset dataset = saveas.SaveAs(filename, workspace, "TIFF");

            // Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }

        /// <summary>
        /// Open a workspace for writing a raster to an image file.
        /// </summary>
        static IWorkspace openWorkspace(string path)
        {
            IWorkspaceFactory2 wsFactory = new RasterWorkspaceFactoryClass();
            IWorkspace workspace = wsFactory.OpenFromFile(path, 0);
            return workspace;
        }
    }
}
0 Kudos