To save out a raster from an image service, follow these steps:
-
Initialize image server layer;
-
Get the raster from the image server layer and set the desired export extent;
-
Save the clipped raster to geodatabase or other formats.
public static void AccessImageServerLayer()
{
//Create an image server layer by passing a URL.
IImageServerLayer imageserverlayer=new ImageServerLayerClass();
string URL="http://server/arcgis/services/serviceName/imageserver";
imageserverlayer.Initialize(URL);
//For services that require https/authentication, use the following
//IImageServerLayer imageserverlayer=CreateSecuredISLayer(@"http://server:6080/arcgis/services", "serviceName");
//Get the raster from the image server layer.
IRaster raster=imageserverlayer.Raster;
//The raster from an image server is normally large;
//define the size of the raster.
IRasterProps rasterProps=(IRasterProps)raster;
IEnvelope clipEnvelope=new EnvelopeClass();
clipEnvelope.PutCoords(779000, 9628000, 786000, 9634000);
rasterProps.Extent=clipEnvelope;
rasterProps.Width=256;
rasterProps.Height=256;
//Save the clipped raster to the file geodatabase.
ISaveAs saveas=(ISaveAs)raster;
Type factoryType=Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFact=(IWorkspaceFactory)Activator.CreateInstance
(factoryType);
IWorkspace workspace=workspaceFact.OpenFromFile(@"c:\temp\fgdb.gdb", 0);
saveas.SaveAs("clipfromimageserverlayer", workspace, "gdb");
}
private static IImageServerLayer CreateSecuredISLayer(string agsUrl, string
serviceName)
{
IName soname=GetImageServerName(agsUrl, serviceName);
IImageServerLayer isLayer=new ImageServerLayerClass();
IDataLayer data_islayer=(IDataLayer)isLayer;
data_islayer.DataSourceName=soname;
return isLayer;
}
private static IName GetImageServerName(string hostOrUrl, string serviceName)
{
IPropertySet propSet=new PropertySetClass();
propSet.SetProperty("url", hostOrUrl);
propSet.SetProperty("ANONYMOUS", false);
//don't hardcode user/password
//propertySet.SetProperty("user", userName);
//propertySet.SetProperty("password", password);
IAGSServerConnectionName agsServerConnectName=new AGSServerConnectionNameClass
();
agsServerConnectName.ConnectionProperties=propSet;
IAGSServerObjectName agsSOName=new AGSServerObjectNameClass();
agsSOName.AGSServerConnectionName=agsServerConnectName;
agsSOName.Name=serviceName;
agsSOName.Type="ImageServer";
return (IName)agsSOName;
}
[VB.NET] Public Shared Sub AccessImageServerLayer()
'Create an image server layer by passing a URL.
Dim isLayer As IImageServerLayer=New ImageServerLayerClass()
Dim URL As String="http://server/arcgis/services/serviceName/imageserver"
isLayer.Initialize(URL)
'For services that require https/authentication, use the following
'Dim isLayer As IImageServerLayer=CreateSecuredISLayer("http://server:6080/arcgis/services", "serviceName")
'Get the raster from the image server layer.
Dim raster As IRaster=isLayer.Raster
'The raster from an image server is normally large;
'define the size of the raster.
Dim rasterProps As IRasterProps=DirectCast(raster, IRasterProps)
Dim clipEnvelope As IEnvelope=New EnvelopeClass()
clipEnvelope.PutCoords(779000, 9628000, 786000, 9634000)
rasterProps.Extent=clipEnvelope
rasterProps.Width=256
rasterProps.Height=256
'Save the clipped raster to the file geodatabase.
Dim saveas As ISaveAs=DirectCast(raster, ISaveAs)
Dim factoryType As Type=Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFact As IWorkspaceFactory=DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspace As IWorkspace=workspaceFact.OpenFromFile("c:\temp\fgdb.gdb", 0)
saveas.SaveAs("clipfromimageserverlayer", workspace, "gdb")
End Sub
Private Shared Function CreateSecuredISLayer(agsUrl As String, serviceName As String) As IImageServerLayer
Dim soname As IName=GetImageServerName(agsUrl, serviceName)
Dim isLayer As IImageServerLayer=New ImageServerLayerClass()
Dim data_islayer As IDataLayer=DirectCast(isLayer, IDataLayer)
data_islayer.DataSourceName=soname
Return isLayer
End Function
Private Shared Function GetImageServerName(hostOrUrl As String, serviceName As String) As IName
Dim propSet As IPropertySet=New PropertySetClass()
propSet.SetProperty("url", hostOrUrl)
propSet.SetProperty("ANONYMOUS", False)
'don't hardcode user/password
'propertySet.SetProperty("user", userName);
'propertySet.SetProperty("password", password);
Dim agsServerConnectName As IAGSServerConnectionName=New AGSServerConnectionNameClass()
agsServerConnectName.ConnectionProperties=propSet
Dim agsSOName As IAGSServerObjectName=New AGSServerObjectNameClass()
agsSOName.AGSServerConnectionName=agsServerConnectName
agsSOName.Name=serviceName
agsSOName.Type="ImageServer"
Return DirectCast(agsSOName, IName)
End Function
To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing | Deployment licensing |
---|---|
ArcGIS Desktop Advanced | ArcGIS Desktop Advanced |
ArcGIS Desktop Basic | ArcGIS Desktop Basic |
ArcGIS Desktop Standard | ArcGIS Desktop Standard |
Engine Developer Kit | Engine |