Map service GetLayerTile method
Gets the image tile from a layer cache for a specific location.
GetLayerTile(string MapName, int LayerID, int Level, int Row, int Column, string Format)
Parameter |
Description |
---|---|
MapName |
The name of the map (data frame) that contains a layer cache. |
LayerID |
The layer id of the layer on which a cache is available. |
Level |
The level of detail (scale) of the layer cache from which a tile will be retrieved. |
Row |
The row in which a tile is located. Row values start at 0 from top of each cache level. |
Column |
The column in which a tile is located. Column values start at 0 from the left of each cache level. |
Format |
The image format to retrieve from a cache. This defines the file extension to be used when retrieving the cached tile. A cache can be generated in multiple image formats. |
Return Value
A byte array (byte[ ]) containing the raw image content. The image format for the layer cache can be discovered using the TileImageInfo object returned from a call to GetTileImageInfo on the map service proxy. If the image tile is not available, a SOAP exception will be thrown. A tile may not be generated in situations where no layer data was present in the tile extent.
Remarks
In general, this method is used to retrieve image tiles that have been pre-generated (cached) for a single layer. The cache properties such as the number and scale of levels and the physical size of image tiles in pixels can be defined by the map service administrator, thus the values can be arbitrary.
Examples
C#
MapService_MapServer mapservice = new MapService_MapServer();
mapservice.Url = "http://localhost:6080/arcgis/services/MapLayerCache/MapServer";
string mapname = mapservice.GetDefaultMapName();
MapServerInfo mapinfo = mapservice.GetServerInfo(mapname);
MapDescription mapdesc = mapinfo.DefaultMapDescription;
LayerDescription[] layerdescriptions = mapdesc.LayerDescriptions;
// Pixel height and width of map display on the client. In this case, a Windows Form
// PictureBox control.
int picturewidth = pictureBox1.Width;
int pictureheight = pictureBox1.Height;
EnvelopeN mapextent = (EnvelopeN)mapdesc.MapArea.Extent;
// Use map scale resolution (map units per pixel) to determine tile level
double mapresolution = Math.Abs(mapextent.XMax - mapextent.XMin) / picturewidth;
System.Drawing.Bitmap imgbitmap = new System.Drawing.Bitmap(picturewidth, pictureheight);
System.Drawing.Graphics imggraphics = System.Drawing.Graphics.FromImage(imgbitmap);
imggraphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.LightGray),
0, 0, picturewidth, pictureheight);
int layerdesc_maxindex = layerdescriptions.Length - 1;
// Iterate through layers bottom up. Polygons on bottom, then lines, then points.
for (int d = layerdesc_maxindex; d >= 0; d--)
{
LayerDescription layerdesc = layerdescriptions[d];
if (mapservice.HasLayerCache(mapname, layerdesc.LayerID))
{
TileCacheInfo tci = mapservice.GetTileCacheInfo(mapservice.GetDefaultMapName());
LODInfo[] tcis = tci.LODInfos;
// Map units per pixel
double tileresolution = 0;
//Scale level
int tilelevel = 0;
foreach (LODInfo ldi in tcis)
{
tileresolution = ldi_resolution;
tilelevel = ldi.LevelID;
if (mapresolution >= ldi_resolution)
{
break;
}
}
// Measured from the origin
double minx = mapextent.XMin;
double miny = mapextent.YMin;
double maxx = mapextent.XMax;
double maxy = mapextent.YMax;
>// Origin of the cache (upper left corner)
double xorigin = ((PointN)tci.TileOrigin).X;
double yorigin = ((PointN)tci.TileOrigin).Y;
// Get minimum tile column
double minxtile = (minx - xorigin) / (tci.TileCols * tileresolution);
// Get minimum tile row
// From the origin, maxy is minimum y
double minytile = (yorigin - maxy) / (tci.TileRows * tileresolution);
// Get maximum tile column
double maxxtile = (maxx - xorigin) / (tci.TileCols * tileresolution);
// Get maximum tile row
// From the origin, miny is maximum y
double maxytile = (yorigin - miny) / (tci.TileRows * tileresolution);
// Return integer value for min and max, row and column
int mintilecolumn = (int)Math.Floor(minxtile);
int mintilerow = (int)Math.Floor(minytile);
int maxtilecolumn = (int)Math.Floor(maxxtile);
int maxtilerow = (int)Math.Floor(maxytile);
// Origin of the min tile
double xmintileorigin = xorigin + (mintilecolumn * (tci.TileCols * tileresolution));
double ymintileorigin = yorigin - (mintilerow * (tci.TileRows * tileresolution));
// Since the origin of the extent and origin of the min tile are different
// get the difference and use to place consolidated image graphic in correct location
double xadjust = Math.Abs(minx - xmintileorigin);
double yadjust = Math.Abs(maxy - ymintileorigin);
int xpixadjust = (int)(xadjust / tileresolution);
int ypixadjust = (int)(yadjust / tileresolution);
TileImageInfo tii = mapservice.GetTileImageInfo(mapservice.GetDefaultMapName());
int rowindex = 0;
// for each row in the map extent
for (int row = mintilerow; row <= maxtilerow;row++)
{
int colindex = 0;
// for each column in the row, in the map extent
for (int col = mintilecolumn; col <= maxtilecolumn; col++)
{
byte[] myByteArray = null;
try
{
// Return the byte array of the tile image
myByteArray = mapservice.GetLayerTile(mapservice.GetDefaultMapName(), layerdesc.LayerID,
tilelevel, row, col, tii.CacheTileFormat);
catch
{
// Tile may not be available because no data was present when creating the cache
}
// If Tile was found, add it to the consolidated image graphic
if (myByteArray != null)
{
System.Drawing.Image newImage;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(myByteArray, 0, myByteArray.Length))
{
ms.Write(myByteArray, 0, myByteArray.Length);
newImage = Image.FromStream(ms, true);
imggraphics.DrawImage(newImage, (tci.TileCols * colindex) - xpixadjust, (tci.TileRows * rowindex) - ypixadjust,
tci.TileCols, tci.TileRows);
}
}
colindex++;
}
rowindex++;
}
}
}
// Post-processing, if necessary... otherwise just use imgbitmap with PictureBox
System.Drawing.Bitmap picturebitmap = new System.Drawing.Bitmap(picturewidth, pictureheight);
System.Drawing.Graphics graphicsimage = System.Drawing.Graphics.FromImage(picturebitmap);
graphicsimage.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.LightGray),0, 0, picturewidth, pictureheight);
graphicsimage.DrawImage(imgbitmap, 0, 0, picturewidth, pictureheight);
pictureBox1.Image = picturebitmap;
VB.NET
Dim mapservice As MapService_MapServer = New MapService_MapServer()
mapservice.Url = "http://localhost:6080/arcgis/services/MapLayerCache/MapServer"
Dim mapname As String = mapservice.GetDefaultMapName()
Dim mapinfo As MapServerInfo = mapservice.GetServerInfo(mapname)
Dim mapdesc As MapDescription = mapinfo.DefaultMapDescription
Dim layerdescriptions() As LayerDescription = mapdesc.LayerDescriptions
' Pixel height and width of map display on the client. In this case, a Windows Form
' PictureBox control.
Dim picturewidth As Integer = pictureBox1.Width
Dim pictureheight As Integer = pictureBox1.Height
Dim mapextent As EnvelopeN = CType(mapdesc.MapArea.Extent, EnvelopeN)
' Use map scale resolution (map units per pixel) to determine tile level
Dim mapresolution As Double = Math.Abs(mapextent.XMax - mapextent.XMin) / picturewidth
Dim imgbitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(picturewidth, pictureheight)
Dim imggraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(imgbitmap)
imggraphics.FillRectangle(New System.Drawing.SolidBrush(System.Drawing.Color.LightGray), 0, 0, _
picturewidth, pictureheight)
Dim layerdesc_maxindex As Integer = layerdescriptions.Length - 1
' Iterate through layers bottom up. Polygons on bottom, then lines, then points.
Dim d As Integer
For d = layerdesc_maxindex To 0 Step d - 1
Dim layerdesc As LayerDescription = layerdescriptions(d)
If mapservice.HasLayerCache(mapname, layerdesc.LayerID) Then
Dim tci As TileCacheInfo = mapservice.GetTileCacheInfo(mapservice.GetDefaultMapName())
Dim tcis() As LODInfo = tci.LODInfos
' Map units per pixel
Dim tileresolution As Double = 0
' Scale level
Dim tilelevel As Integer = 0
Dim ldi As LODInfo
For Each ldi In tcis
Dim ldi_resolution As Double = ldi.Resolution
tileresolution = ldi_resolution
tilelevel = ldi.LevelID
If mapresolution >= ldi_resolution Then
Exit For
End If
Next
' Measured from the origin
Dim minx As Double = mapextent.XMin
Dim miny As Double = mapextent.YMin
Dim maxx As Double = mapextent.XMax
Dim maxy As Double = mapextent.YMax
' Origin of the cache (upper left corner)
Dim xorigin As Double = (CType(tci.TileOrigin, PointN)).X
Dim yorigin As Double = (CType(tci.TileOrigin, PointN)).Y
' Get minimum tile column
Dim minxtile As Double = (minx - xorigin) / (tci.TileCols * tileresolution)
' Get minimum tile row
' From the origin, maxy is minimum y
Dim minytile As Double = (yorigin - maxy) / (tci.TileRows * tileresolution)
' Get maximum tile column
Dim maxxtile As Double = (maxx - xorigin) / (tci.TileCols * tileresolution)
' Get maximum tile row
' From the origin, miny is maximum y
Dim maxytile As Double = (yorigin - miny) / (tci.TileRows * tileresolution)
' Return integer value for min and max, row and column
Dim mintilecolumn As Integer = CType(Math.Floor(minxtile), Integer)
Dim mintilerow As Integer = CType(Math.Floor(minytile), Integer)
Dim maxtilecolumn As Integer = CType(Math.Floor(maxxtile), Integer)
Dim maxtilerow As Integer = CType(Math.Floor(maxytile), Integer)
' Origin of the min tile
Dim xmintileorigin As Double = xorigin + (mintilecolumn * (tci.TileCols * tileresolution))
Dim ymintileorigin As Double = yorigin - (mintilerow * (tci.TileRows * tileresolution))
' Since the origin of the extent and origin of the min tile are different
' get the difference and use to place consolidated image graphic in correct location
Dim xadjust As Double = Math.Abs(minx - xmintileorigin)
Dim yadjust As Double = Math.Abs(maxy - ymintileorigin)
Dim xpixadjust As Integer = CType((xadjust / tileresolution), Integer)
Dim ypixadjust As Integer = CType((yadjust / tileresolution), Integer)
Dim tii As TileImageInfo = mapservice.GetTileImageInfo(mapservice.GetDefaultMapName())
Dim rowindex As Integer = 0
' for each row in the map extent
Dim row As Integer
For row = mintilerow To maxtilerow Step row + 1
Dim colindex As Integer = 0
' for each column in the row, in the map extent
Dim col As Integer
For col = mintilecolumn To maxtilecolumn Step col + 1
Dim myByteArray() As Byte = Nothing
Try
' Return the byte array of the tile image
myByteArray = mapservice.GetLayerTile(mapservice.GetDefaultMapName(), _ layerdesc.LayerID, tilelevel, row, col, tii.CacheTileFormat)
Catch
' Tile may not be available because no data was present when creating the cache
End Try
' If Tile was found, add it to the consolidated image graphic
If Not myByteArray Is Nothing Then
Dim NewImage As System.Drawing.Image
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(myByteArray, 0, _ myByteArray.Length)
Try
ms.Write(myByteArray, 0, myByteArray.Length)
NewImage = Image.FromStream(ms, True)
imggraphics.DrawImage(NewImage, (tci.TileCols * colindex) - xpixadjust, _ (tci.TileRows * rowindex) - ypixadjust, tci.TileCols, tci.TileRows)
Finally
ms.Dispose()
End Try
End If
colindex = colindex + 1
Next
rowindex = rowindex + 1
Next
End If
Next
' Post-processing, if necessary... otherwise just use imgbitmap with PictureBox
Dim picturebitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(picturewidth, pictureheight)
Dim graphicsimage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(picturebitmap)
graphicsimage.FillRectangle(New System.Drawing.SolidBrush(System.Drawing.Color.LightGray), _
0, 0, picturewidth, pictureheight)
graphicsimage.DrawImage(imgbitmap, 0, 0, picturewidth, pictureheight)
pictureBox1.Image = picturebitmap
Java
String serviceURL = "http://localhost:6080/arcgis/services/MapService/MapServer";
MapServerBindingStub mapService = new MapServerBindingStub(serviceURL);
String mapName = mapService.getDefaultMapName();
MapServerInfo mapInfo = mapService.getServerInfo(mapName);
MapDescription mapDesc = mapInfo.getDefaultMapDescription();
LayerDescription[] layerDescriptions = mapDesc.getLayerDescriptions();
int layerdescMaxindex = layerDescriptions.length - 1;
EnvelopeN mapExtent = (EnvelopeN)mapDesc.getMapArea().getExtent();
//Use map scale resolution (map units per pixel) to determine tile level
double mapResolution = Math.abs(mapExtent.getXMax() - mapExtent.getXMin()) / width;
//Iterate through layers bottom up. Polygons on bottom, then lines, then points.
for (int d = layerdescMaxindex; d >= 0; d--)
{
LayerDescription layerDesc = layerDescriptions[d];
if (mapService.hasLayerCache(mapName, layerDesc.getLayerID()))
{
TileCacheInfo tci = mapService.getTileCacheInfo(mapName);
LODInfo[] tcis = tci.getLODInfos();
//Map units per pixel
double tileResolution = 0;
//Scale level
int tileLevel = 0;
for (LODInfo ldi : tcis)
{
double ldiResolution = ldi.getResolution();
tileResolution = ldiResolution;
tileLevel = ldi.getLevelID();
if (mapResolution >= ldiResolution)
{
break;
}
}
//Measured from the origin
double minX = mapExtent.getXMin();
double minY = mapExtent.getYMin();
double maxX = mapExtent.getXMax();
double maxY = mapExtent.getYMax();
//Origin of the cache (upper left corner)
double xOrigin = ((PointN)tci.getTileOrigin()).getX();
double yOrigin = ((PointN)tci.getTileOrigin()).getY();
//Get minimum tile column
double minXTile = (minX - xOrigin) / (tci.getTileCols() * tileResolution);
//Get minimum tile row
//From the origin, maxY is minimum y
double minYTile = (yOrigin - maxY) / (tci.getTileRows() * tileResolution);
//Get maximum tile column
double maxXTile = (maxX - xOrigin) / (tci.getTileCols() * tileResolution);
//Get maximum tile row
//From the origin, minY is maximum y
double maxYTile = (yOrigin - minY) / (tci.getTileRows() * tileResolution);
//Return integer value for min and max, row and column
int minTileColumn = (int)Math.floor(minXTile);
int minTileRow = (int)Math.floor(minYTile);
int maxTileColumn = (int)Math.floor(maxXTile);
int maxTileRow = (int)Math.floor(maxYTile);
TileImageInfo tii = mapService.getTileImageInfo(mapName);
//for each row in the map extent
for (int row = minTileRow; row <= maxTileRow; row++)
{
//for each column in the row, in the map extent
for (int col = minTileColumn; col <= maxTileColumn; col++)
{
//Return the byte array of the tile image
byte[] tileByteArray = mapService.getLayerTile(mapName, layerDesc.getLayerID(), tileLevel, row, col, tii.getCacheTileFormat());
System.out.println("Tile Byte Array Length: " + tileByteArray.length);
}
}
}
}