Dear list,<br>here is a simple <a href="http://ASP.NET">ASP.NET</a> ashx to server MapGuide Tiles Directly.<br><br>It' different from the one found on "<a href="http://trac.osgeo.org/mapguide/wiki/CodeSamples/Tiles/ServingTilesViaHttp">http://trac.osgeo.org/mapguide/wiki/CodeSamples/Tiles/ServingTilesViaHttp</a>" because it doesn't require any web server configuration.<br>
The only point is to use my ashx as a proxy to MapGuide.<br>Testing would be nice. If it's all OK, and someone wants to post it on wiki... or translate in PHP...<br>BUT don't test it with a non server OS. For example in WindowsXP I get lot of empty tiles. This is not the case with Windows Server 2003.<br>
REMINDER FOR ME: don't loose time! Test on real server OSs!<br>NOTE: <b>call somewhere "<span style="font-family: courier new,monospace;">MapGuideApi.MgInitializeWebTier(MgMapHelper.WebconfigIniPhysicalPath);</span>" before using this handler!</b><br>
[<b>App_Start is a good choise</b>]<br><br><br>Code:<br><br><span style="font-family: courier new,monospace;"><%@ WebHandler Language="C#" Class="GetMGTiles" %></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">//#define LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">using System;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">using System.Web;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">using System.Text;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">using System.Net;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">using System.Collections.Specialized;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">using MapGuideHelper;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">using OSGeo.MapGuide;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">using System.IO;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">//This is different from [<a href="http://trac.osgeo.org/mapguide/wiki/CodeSamples/Tiles/ServingTilesViaHttp">http://trac.osgeo.org/mapguide/wiki/CodeSamples/Tiles/ServingTilesViaHttp</a> ]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">//where the web server must be configured.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">//Here you use GetMGTiles.ashx as OpenLayers mapguide tiled layer address [not HttpTile!].</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">//NOTE: if you try this with a non server OS [such as WindowsXP] you will certainly get missed tiles</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">public class GetMGTiles : IHttpHandler</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #region Compile Time Configuration</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private readonly int BUFF_SIZE = 20480;//4096</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #endregion</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #region Properties to be configured [if needed] on application start</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static string _tileDirectory = "C:/OSGeo/MapGuide/Server/Repositories/TileCache/";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> public static string TileDirectory</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> get { return _tileDirectory; }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> set { _tileDirectory = value; }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static string _imageFileFormat = ".png"; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> private static string _contentType = "image/png"; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public static string ImageFileFormat</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> get { return _imageFileFormat; }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> set</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> _contentType = String.Concat("image/", value);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> _imageFileFormat = String.Concat(".", value);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static double _tileRowsPerFolder = 30.0; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> private static int _tileRowsPerFolderInt = 30; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public static int TileRowsPerFolder</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> get { return _tileRowsPerFolderInt; }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> set { _tileRowsPerFolder = _tileRowsPerFolderInt = value; }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> private static double _tileColumnsPerFolder = 30.0; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static int _tileColumnsPerFolderInt = 30; //depends on "serverconfig.ini"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> public static int TileColumnsPerFolder</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> get { return _tileColumnsPerFolderInt; }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> set { _tileColumnsPerFolder = _tileColumnsPerFolderInt = value; }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #endregion</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #region input request params</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> int _tileRow, _tileCol, _scaleIndex;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> string _baseLayerGroupname;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> string _mapDefinition;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #endregion</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> protected void ServeMgTile(HttpResponse response)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> MgUserInformation userInfo = null;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MgSiteConnection siteConnection = null;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> MgTileService tileService = null;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MgResourceIdentifier resIdent = null;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> MgByteReader mgReader = null;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ///////MapGuideApi.MgInitializeWebTier(MgMapHelper.WebconfigIniPhysicalPath);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> userInfo = new MgUserInformation("Anonymous", "");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> siteConnection = new MgSiteConnection();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> siteConnection.Open(userInfo);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> tileService = siteConnection.CreateService(MgServiceType.TileService) as MgTileService;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> resIdent = new MgResourceIdentifier(_mapDefinition);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> mgReader = tileService.GetTile(resIdent, _baseLayerGroupname, _tileCol, _tileRow, _scaleIndex);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //--------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //Debugging helper:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // context.Response.Cache.SetNoStore();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> // context.Response.AppendHeader("Pragma", "no-cache");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //--------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> int readBytes;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> byte[] buffer = new byte[BUFF_SIZE];</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while ((readBytes = mgReader.Read(buffer, BUFF_SIZE)) > 0)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> response.OutputStream.Write(buffer, 0, readBytes);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> finally</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (null != mgReader) mgReader.Dispose();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (null != resIdent) resIdent.Dispose();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (null != tileService) tileService.Dispose();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (null != siteConnection) siteConnection.Dispose();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (null != userInfo) userInfo.Dispose();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> protected void StreamFile(string filePath, HttpResponse response)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //response.AddHeader("content-disposition", String.Concat("attachment; filename=",Path.GetFileName(filePath)));</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> long fileSize = fileStream.Length;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#if false</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //all at once:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> byte[] buffer = new byte[(int)fileSize]; </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> fileStream.Read(buffer, 0, (int)fileStream.Length);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fileStream.Close();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> response.BinaryWrite(buffer);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#else</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> int readBytes;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> byte[] buffer = new byte[BUFF_SIZE];</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while ((readBytes = fileStream.Read(buffer, 0, BUFF_SIZE)) > 0)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> response.OutputStream.Write(buffer, 0, readBytes);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> fileStream.Close();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public void ProcessRequest(HttpContext context)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //Startup</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //It's always GET, but this costs nothing:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> NameValueCollection nvInput = context.Request.HttpMethod == "POST" ? <br>
context.Request.Form : nvInput = context.Request.QueryString;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#if false</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //string requestedUrl = context.Request.Path.ToLower();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (nvInput == null) //it's never null</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Clear();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.ContentType = "text/plain";</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Write("Hello World");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (nvInput.Count == 0)//don't mind if is 0 => we will just get null strings when trying to read them</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Clear();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.ContentType = "text/plain";</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Write("Hello World");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //Input params reading</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> _mapDefinition = HttpUtility.UrlDecode(nvInput["mapdefinition"]);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (String.IsNullOrEmpty(_mapDefinition))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (nvInput["operation"].ToUpper() != "GETTILEIMAGE")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //we don't read nvInput["version"]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> _baseLayerGroupname = HttpUtility.UrlDecode(nvInput["basemaplayergroupname"]);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (String.IsNullOrEmpty(_baseLayerGroupname))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (!Int32.TryParse(nvInput["tilecol"], out _tileCol))<br> {<br> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;<br>
return;<br> }<br> if (!Int32.TryParse(nvInput["tilerow"], out _tileRow))<br> {<br> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;<br>
return;<br> }<br> if (!Int32.TryParse(nvInput["scaleindex"], out _scaleIndex))<br> {<br> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;<br>
return;<br> }<br></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //Tile file name</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //The tiles base dir, if for example we want</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // "Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition" </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> // , is</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // "Samples_Sheboygan_MapsTiled_Sheboygan/"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> string mapDefDir = _mapDefinition.Replace("Library://", "").Replace("/", "_").Replace(".MapDefinition", "/");</span><br style="font-family: courier new,monospace;">
<br> <br><span style="font-family: courier new,monospace;"> //---------------------------------<br> //Taken from Openlayers:<br> int tileRowGroup = (int)(Math.Floor(Math.Abs(_tileRow / _tileRowsPerFolder))) * TileRowsPerFolder;<br>
int tileColGroup = (int)(Math.Floor(Math.Abs(_tileCol / _tileColumnsPerFolder))) * TileColumnsPerFolder;<br> int imgPartA = _tileRow % TileRowsPerFolder;<br> int imgPartB = _tileCol % TileColumnsPerFolder;<br>
//---------------------------------<br></span><span style="font-family: courier new,monospace;"> StringBuilder sb = new StringBuilder(256);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append(_tileDirectory);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(mapDefDir);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("S");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(this._scaleIndex);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("/");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(this._baseLayerGroupname);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("/R");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(tileRowGroup);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("/C");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(tileColGroup);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("/");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(imgPartA);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append("_");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sb.Append(imgPartB);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> sb.Append(_imageFileFormat);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //We give file if possible, let mapGuide build the tile otherwise</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //---------------------------------------------------------------------------------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.ContentType = _contentType;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> string file = sb.ToString();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (System.IO.File.Exists(file))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#if false</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.WriteFile(file);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> StreamFile(file, context.Response);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> catch (System.IO.IOException ex)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //FileExists is not enough:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //MapGuide could possibly be writing the file while we where trying to get access!!!!</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#if LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest - Tried StreamFile [System.IO.IOException] - serve file via MapGuide", ex);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ServeMgTile(context.Response);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.Flush();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Close();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.ContentType = _contentType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ServeMgTile(context.Response);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.Flush();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.Close();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch (ArgumentNullException ex)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest [ArgumentNullException]", ex);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.NotFound;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> System.Diagnostics.Trace.WriteLine(ex.ToString());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch (System.Security.SecurityException ex)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#if LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest [SecurityException]", ex);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.Forbidden;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> System.Diagnostics.Trace.WriteLine(ex.ToString());</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> catch (UriFormatException ex)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#if LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest [UriFormatException]", ex);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.BadRequest;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> System.Diagnostics.Trace.WriteLine(ex.ToString());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch (TimeoutException ex)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#if LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest [TimeoutException]", ex);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.RequestTimeout;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> System.Diagnostics.Trace.WriteLine(ex.ToString());</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> catch (Exception ex)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#if LOG_WITH_LOG4NET</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> log4net.ILog logger = log4net.LogManager.GetLogger("exceptions");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> logger.Error("GetMGTiles.ashx::ProcessRequest [Exception]", ex);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> System.Diagnostics.Trace.WriteLine(ex.ToString());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public bool IsReusable</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> get</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return false;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br>
<br>
------------<br>Test page [slightly modified from the wiki]<br>Assumes <span style="font-family: courier new,monospace;">GetMGTiles.ashx is accessible in <a href="http://localhost/">http://localhost/</a></span>ash/<span style="font-family: courier new,monospace;">GetMGTiles.ashx</span><br>
-----------<br><br><span style="font-family: courier new,monospace;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><br><head><br> <title>Mapguide Http Tile Cache Demo</title><br>
<br> <script type="text/javascript" src="<a href="http://www.openlayers.org/api/OpenLayers.js">http://www.openlayers.org/api/OpenLayers.js</a>"></script><br><br></head><br><body onload="initTiled()"><br>
<br> <script type="text/javascript"><br> var windowSize = 550; //200;//<br> var extent;<br><br> var addrs = "<a href="http://localhost/">http://localhost/</a>";<br> //var addrs = "<a href="http://192.168.1.247/">http://192.168.1.247/</a>";<br>
<br> var mg_url = addrs + "mapguide/mapagent/mapagent.fcgi?USERNAME=Anonymous&";<br> var http_url = addrs + "sheboyganTiles";<br> var my_proxy_url = addrs + "ash/GetMGTiles.ashx";<br>
var mg_map, http_or_tileproxy_map;<br> var eventsLog = "";<br> var c = 0;<br> //var bUseHttpUrl = false; //http_url || my_proxy_url <br><br> function initTiled() {<br><br> document.getElementById('map_mg').style.width = windowSize;<br>
document.getElementById('map_http').style.width = windowSize;<br><br> document.getElementById('map_mg').style.height = windowSize;<br> document.getElementById('map_http').style.height = windowSize;<br>
<br> OpenLayers.DOTS_PER_INCH = 96;<br><br> //extent = new OpenLayers.Bounds(-87.865114442365922,43.665065564837931,-87.595394059497067,43.823852564430069); <br> var extent = new OpenLayers.Bounds(-87.764986990963, 43.691398128788, -87.6955215109, 43.79752000048);<br>
var tempScales = [1000, 1930.6977300000001, 3727.5937199999998, 7196.8567300000004, 13894.95494, 26826.95795, 51794.746789999997, 100000];<br><br> var mapOptions = {<br> maxExtent: extent,<br>
restrictedExtent: extent,<br> scales: tempScales<br> };<br><br> var params = {<br> mapdefinition: 'Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition',<br>
basemaplayergroupname: "Base Layer Group"<br> }<br><br> mg_map = new OpenLayers.Map('map_mg', mapOptions);<br> http_or_tileproxy_map = new OpenLayers.Map('map_http', mapOptions);<br>
<br> var mgOptions = {<br> singleTile: false,<br> useHttpTile: false,<br> buffer: 1<br> }<br><br> /*var httpOptions = {<br> singleTile: false,<br>
useHttpTile: true,<br> buffer: 1<br> }*/<br><br> var myProxyOptions = {<br> singleTile: false,<br> useHttpTile: false,<br> buffer: 1<br>
}<br><br> mg_layer = new OpenLayers.Layer.MapGuide("MapGuide OS tiled layer", mg_url, params, mgOptions);<br> //if (bUseHttpUrl) http_layer = new OpenLayers.Layer.MapGuide("MapGuide OS tiled layer", http_url, params, httpOptions);<br>
//else<br> http_layer = new OpenLayers.Layer.MapGuide("MG OS proxy - tiled layer", my_proxy_url, params, myProxyOptions);<br><br> mg_map.addLayer(mg_layer);<br><br> mg_map.addControl(new OpenLayers.Control.MousePosition());<br>
http_or_tileproxy_map.addControl(new OpenLayers.Control.MousePosition());<br><br> http_or_tileproxy_map.addLayer(http_layer);<br><br> mg_map.setCenter(extent.getCenterLonLat());<br> http_or_tileproxy_map.setCenter(extent.getCenterLonLat());<br>
<br> mg_map.events.register("zoomend", mg_map, setZoomHttp);<br> //http_or_tileproxy_map.events.register("zoomend", http_or_tileproxy_map, setZoomMg);<br><br> mg_map.events.register("moveend", mg_map, moveHttp);<br>
//http_or_tileproxy_map.events.register("moveend", http_or_tileproxy_map, moveMg);<br><br> }<br><br> function moveHttp() {<br> if (mg_map.getCenter() != http_or_tileproxy_map.getCenter())<br>
http_or_tileproxy_map.setCenter(mg_map.getCenter());<br> }<br><br> function moveMg() {<br> if (mg_map.getCenter() != http_or_tileproxy_map.getCenter())<br> mg_map.setCenter(http_or_tileproxy_map.getCenter());<br>
}<br><br><br><br> function setZoomMg() {<br> if (mg_map.getZoom() != http_or_tileproxy_map.getZoom())<br> mg_map.zoomTo(http_or_tileproxy_map.getZoom());<br> }<br> function setZoomHttp() {<br>
if (mg_map.getZoom() != http_or_tileproxy_map.getZoom())<br> http_or_tileproxy_map.zoomTo(mg_map.getZoom());<br><br> }<br> <br> </script><br><br> <div style="float: left"><br>
<h1>Served by Mapguide</h1><br> <div id="map_mg" style="border: medium inset Maroon; float: left; background-color: #F5DEB3;"><br> </div><br> <br />This cannot be proxied, it lacks cache headers<br>
</div><br> <div style="float: left"><br> <h1>Served by Http</h1><br> <div id="map_http" style="border: medium inset Maroon; float: left; background-color: #F5DEB3;"><br>
</div><br> <br />This is can be proxied by proxy servers<br> </div><br></body><br></html><br></span><br style="font-family: courier new,monospace;"><br><br><br>------------<br>
[OFFTOPIC] A suggestion about MapGuide Installer -<br><br>it would be nice that besides the installer, there was a zipped archive with the installed files, to give the possibility of manually installing MapGuide without running an installer. This could be clearly set to UNSUPPORTED, and that no help would be given in the list to who desires to use the zipped archive.<br>
<br>This is because, in my opinion, one cannot understand the system, if not by using a manual install. It's the path I followed when I for the first time used MapGuide. <br><br>To provide such a thing is very simple: install in C:\OsGeo, zip folder and publish in "<a href="http://mapguide.osgeo.org/download" target="_blank">http://mapguide.osgeo.org/download</a>". Only for windows, linux always needs compilation because of different shared objects between distributions.<br>
<br>At this point one can extract the zip wherever she wants, change ini files, set virtual directories, enable PHP, change folder permissions, and the game is done!<br>------------<br><br>Pietro Ianniello<br><br>