Example #1
0
 /// <summary>
 /// project inworld coordinates to image coordinates
 /// </summary>
 /// <param name="agentPos">inworld coordinate of avatar in a region</param>
 /// <param name="bbox">requested region bounding box</param>
 /// <param name="width">image width</param>
 /// <param name="height">image height</param>
 /// <returns></returns>
 internal static PointF Projection(ref PointF agentPos, ref BBox bbox, int width, int height)
 {
     PointF result = new PointF();
     result.X = (agentPos.X - bbox.MinX) * width / bbox.Width;
     result.Y = height - (agentPos.Y - bbox.MinY) * height / bbox.Height;
     return result;
 }
Example #2
0
 public void Extends(BBox bbox)
 {
     MinX = Math.Min(MinX, bbox.MinX);
     MinY = Math.Min(MinY, bbox.MinY);
     MaxX = Math.Max(MaxX, bbox.MaxX);
     MaxY = Math.Max(MaxY, bbox.MaxY);
 }
        public string owsHandler(string request, string path, string param,
                                 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
        {
            switch (httpRequest.QueryString["SERVICE"])
            {
            case "WMS":
                if (httpRequest.QueryString["REQUEST"] == "GetMap")
                {
                    //parse query string
                    string[] layers    = httpRequest.QueryString["LAYERS"].Split(',');
                    BBox     bbox      = new BBox(httpRequest.QueryString["BBOX"]);
                    int      height    = Int32.Parse(httpRequest.QueryString["HEIGHT"]);
                    int      width     = Int32.Parse(httpRequest.QueryString["WIDTH"]);
                    int      elevation = Int32.Parse(httpRequest.QueryString["ELEVATION"]);
                    string   regionID  = httpRequest.QueryString["REGIONID"];

                    Bitmap objLayer  = new Bitmap(width, height);
                    float  scale     = (float)height / bbox.Width;
                    float  tmpScale  = scale;
                    string queryPath = "mapCache//";
                    float  diff      = 10000.0f;
                    foreach (float s in m_scales)
                    {
                        float tmp = Math.Abs(scale - s);
                        if (diff > tmp)
                        {
                            diff     = tmp;
                            tmpScale = s;
                        }
                    }
                    scale      = tmpScale;
                    queryPath += scale.ToString() + "//" + regionID + "//";
                    int section = 0;
                    try
                    {
                        section = m_sections[scale];
                    }
                    catch (Exception e)
                    {
                        m_log.ErrorFormat("[WebMapService]: No such scale in list");
                    }
                    float tileSize = scale * section;

                    //Get tiles included in the BBOX and merge them together
                    int      blockMinX = bbox.MinX / section * section;
                    int      blockMinY = bbox.MinY / section * section;
                    int      blockMaxX = (bbox.MaxX - 1) / section * section;
                    int      blockMaxY = (bbox.MaxY - 1) / section * section;
                    Graphics gfx       = Graphics.FromImage(objLayer);
                    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    for (int y = blockMinY; y <= blockMaxY; y += section)
                    {
                        for (int x = blockMinX; x <= blockMaxX; x += section)
                        {
                            string tileName = queryPath + Utility.IntToLong(x, y);
                            Bitmap tile     = null;
                            try
                            {
                                tile = new Bitmap(tileName);
                            }
                            catch (Exception e)
                            {
                                m_log.ErrorFormat("[WebMapService]: Tile file not found with {0} {1}", e.Message, e.StackTrace);
                                tile = new Bitmap((int)tileSize, (int)tileSize);
                                Graphics g = Graphics.FromImage(tile);
                                g.Clear(Color.Blue);
                                g.Dispose();
                            }
                            //Calculate boundary of tiles and map
                            int tileLeftX, tileRightX, tileBottomY, tileUpY;
                            int mapLeftX, mapRightX, mapBottomY, mapUpY;
                            int difLeftX   = bbox.MinX - x;
                            int difRightX  = bbox.MaxX - (x + section);
                            int difBottomY = bbox.MinY - y;
                            int difUpY     = bbox.MaxY - (y + section);
                            if (difLeftX > 0)
                            {
                                tileLeftX = difLeftX; mapLeftX = 0;
                            }
                            else
                            {
                                tileLeftX = 0; mapLeftX = -difLeftX;
                            }
                            if (difRightX > 0)
                            {
                                tileRightX = section; mapRightX = bbox.Width - difRightX;
                            }
                            else
                            {
                                tileRightX = section + difRightX; mapRightX = bbox.Width;
                            }
                            if (difBottomY > 0)
                            {
                                tileBottomY = section - difBottomY; mapBottomY = bbox.Height;
                            }
                            else
                            {
                                tileBottomY = section; mapBottomY = bbox.Height + difBottomY;
                            }
                            if (difUpY > 0)
                            {
                                tileUpY = 0; mapUpY = difUpY;
                            }
                            else
                            {
                                tileUpY = -difUpY; mapUpY = 0;
                            }
                            try
                            {
                                RectangleF srcRec  = new RectangleF((float)tileLeftX / section * tileSize, (float)tileUpY / section * tileSize, (float)(tileRightX - tileLeftX) / section * tileSize, (float)(tileBottomY - tileUpY) / section * tileSize);
                                RectangleF destRec = new RectangleF((float)mapLeftX / bbox.Width * width, (float)mapUpY / bbox.Height * height, (float)(mapRightX - mapLeftX) / bbox.Width * width, (float)(mapBottomY - mapUpY) / bbox.Height * height);
                                gfx.DrawImage(tile, destRec, srcRec, GraphicsUnit.Pixel);
                                tile.Dispose();
                            }
                            catch (Exception e)
                            {
                                tile.Dispose();
                                m_log.ErrorFormat("[WebMapService]: Failed to cut and merge tiles with {0} {1}", e.Message, e.StackTrace);
                            }
                        }
                    }
                    gfx.Dispose();

                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                    objLayer.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    objLayer.Dispose();
                    byte[] byteImage = stream.ToArray();
                    stream.Close();
                    stream.Dispose();
                    httpResponse.ContentType = "image/png";
                    return(Convert.ToBase64String(byteImage));
                }
                else if (httpRequest.QueryString["REQUEST"] == "GetCapabilities")
                {
                    httpResponse.ContentType = "text/xml";
                    string     capDes     = "";
                    TextReader textReader = new StreamReader("Capability.xml");
                    capDes = textReader.ReadToEnd();
                    textReader.Close();
                    return(capDes);
                }
                else
                {
                    return("Sorry, the request method is not supported by this service.");
                }

            case "WFS":
                if (httpRequest.QueryString["REQUEST"] == "DescribeFeatureType")
                {
                    if ((httpRequest.QueryString["TYPENAME"] == "agent"))
                    {
                        switch (httpRequest.QueryString["FORMAT"])
                        {
                        case "text":
                            string textResult = null;
                            foreach (MapRegion region in m_regions)
                            {
                                textResult += region.GetFeaturesByText();
                            }
                            return(textResult);

                        case "xml":
                            string xmlResult = null;
                            foreach (MapRegion region in m_regions)
                            {
                                xmlResult += region.GetFeaturesByXml();
                            }
                            return(xmlResult);

                        default:
                            return("Feature format not supported");
                        }
                    }
                    else
                    {
                        return("Query String is not supported");
                    }
                }
                break;
            }

            return("Unsupported Service");
        }
Example #4
0
        public string owsHandler(string request, string path, string param,
                                      OSHttpRequest httpRequest, OSHttpResponse httpResponse)
        {
            switch (httpRequest.QueryString["SERVICE"])
            {
                case "WMS":
                    if (httpRequest.QueryString["REQUEST"] == "GetMap")
                    {
                        //parse query string
                        string[] layers = httpRequest.QueryString["LAYERS"].Split(',');
                        BBox bbox = new BBox(httpRequest.QueryString["BBOX"]);
                        int height = Int32.Parse(httpRequest.QueryString["HEIGHT"]);
                        int width = Int32.Parse(httpRequest.QueryString["WIDTH"]);
                        int elevation = Int32.Parse(httpRequest.QueryString["ELEVATION"]);
                        string regionID = httpRequest.QueryString["REGIONID"];

                        Bitmap objLayer = new Bitmap(width, height);
                        float scale = (float)height / bbox.Width;
                        float tmpScale = scale;
                        string queryPath = "mapCache//";
                        float diff = 10000.0f;
                        foreach (float s in m_scales)
                        {
                            float tmp = Math.Abs(scale - s);
                            if (diff > tmp)
                            {
                                diff = tmp;
                                tmpScale = s;
                            }
                        }
                        scale = tmpScale;
                        queryPath += scale.ToString() + "//" + regionID + "//";
                        int section = 0;
                        try
                        {
                            section = m_sections[scale];
                        }
                        catch (Exception e)
                        {
                            m_log.ErrorFormat("[WebMapService]: No such scale in list");
                        }
                        float tileSize = scale * section;

                        //Get tiles included in the BBOX and merge them together
                        int blockMinX = bbox.MinX / section * section;
                        int blockMinY = bbox.MinY / section * section;
                        int blockMaxX = (bbox.MaxX - 1) / section * section;
                        int blockMaxY = (bbox.MaxY - 1) / section * section;
                        Graphics gfx = Graphics.FromImage(objLayer);
                        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        for (int y = blockMinY; y <= blockMaxY; y += section)
                            for (int x = blockMinX; x <= blockMaxX; x += section)
                            {
                                string tileName = queryPath + Utility.IntToLong(x, y);
                                Bitmap tile = null;
                                try
                                {
                                    tile = new Bitmap(tileName);
                                }
                                catch (Exception e)
                                {
                                    m_log.ErrorFormat("[WebMapService]: Tile file not found with {0} {1}", e.Message, e.StackTrace);
                                    tile = new Bitmap((int)tileSize, (int)tileSize);
                                    Graphics g = Graphics.FromImage(tile);
                                    g.Clear(Color.Blue);
                                    g.Dispose();
                                }
                                //Calculate boundary of tiles and map
                                int tileLeftX, tileRightX, tileBottomY, tileUpY;
                                int mapLeftX, mapRightX, mapBottomY, mapUpY;
                                int difLeftX = bbox.MinX - x;
                                int difRightX = bbox.MaxX - (x+section);
                                int difBottomY = bbox.MinY - y;
                                int difUpY = bbox.MaxY - (y+section);
                                if (difLeftX > 0) { tileLeftX = difLeftX; mapLeftX = 0; }
                                else { tileLeftX = 0; mapLeftX = -difLeftX; }
                                if (difRightX > 0) { tileRightX = section; mapRightX = bbox.Width - difRightX; }
                                else { tileRightX = section + difRightX; mapRightX = bbox.Width; }
                                if (difBottomY > 0) { tileBottomY = section - difBottomY; mapBottomY = bbox.Height; }
                                else { tileBottomY = section; mapBottomY = bbox.Height + difBottomY; }
                                if (difUpY > 0) { tileUpY = 0; mapUpY = difUpY; }
                                else { tileUpY = -difUpY; mapUpY = 0; }
                                try
                                {
                                    RectangleF srcRec = new RectangleF((float)tileLeftX / section * tileSize, (float)tileUpY / section * tileSize, (float)(tileRightX - tileLeftX) / section * tileSize, (float)(tileBottomY - tileUpY) / section * tileSize);
                                    RectangleF destRec = new RectangleF((float)mapLeftX / bbox.Width * width, (float)mapUpY / bbox.Height * height, (float)(mapRightX - mapLeftX) / bbox.Width * width, (float)(mapBottomY - mapUpY) / bbox.Height * height);
                                    gfx.DrawImage(tile, destRec, srcRec, GraphicsUnit.Pixel);
                                    tile.Dispose();
                                }
                                catch (Exception e)
                                {
                                    tile.Dispose();
                                    m_log.ErrorFormat("[WebMapService]: Failed to cut and merge tiles with {0} {1}", e.Message, e.StackTrace);
                                }
                            }
                        gfx.Dispose();

                        System.IO.MemoryStream stream = new System.IO.MemoryStream();
                        objLayer.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        objLayer.Dispose();
                        byte[] byteImage = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        httpResponse.ContentType = "image/png";
                        return Convert.ToBase64String(byteImage);
                    }
                    else if (httpRequest.QueryString["REQUEST"] == "GetCapabilities")
                    {
                        httpResponse.ContentType = "text/xml";
                        string capDes = "";
                        TextReader textReader = new StreamReader("Capability.xml");
                        capDes = textReader.ReadToEnd();
                        textReader.Close();
                        return capDes;
                    }
                    else
                    {
                        return "Sorry, the request method is not supported by this service.";
                    }
                case "WFS":
                    if (httpRequest.QueryString["REQUEST"] == "DescribeFeatureType")
                    {
                        if ((httpRequest.QueryString["TYPENAME"] == "agent"))
                        {
                            switch (httpRequest.QueryString["FORMAT"])
                            {
                                case "text":
                                    string textResult = null;
                                    foreach (MapRegion region in m_regions)
                                    {
                                        textResult += region.GetFeaturesByText();
                                    }
                                    return textResult;
                                case "xml":
                                    string xmlResult = null;
                                    foreach (MapRegion region in m_regions)
                                    {
                                        xmlResult += region.GetFeaturesByXml();
                                    }
                                    return xmlResult;
                                default:
                                    return "Feature format not supported";
                            }
                        }
                        else
                            return "Query String is not supported";
                    }
                    break;
            }

            return "Unsupported Service";
        }
        public string owsHandler(string request, string path, string param,
                                      OSHttpRequest httpRequest, OSHttpResponse httpResponse)
        {
            switch (httpRequest.QueryString["SERVICE"])
            {
                case "WMS":
                    if (httpRequest.QueryString["REQUEST"] == "GetMap")
                    {
                        //parse query string
                        string[] layers = httpRequest.QueryString["LAYERS"].Split(',');
                        BBox bbox = new BBox(httpRequest.QueryString["BBOX"]);
                        int height = Int32.Parse(httpRequest.QueryString["HEIGHT"]);
                        int width = Int32.Parse(httpRequest.QueryString["WIDTH"]);
                        int elevation = Int32.Parse(httpRequest.QueryString["ELEVATION"]);
                        string regionID = httpRequest.QueryString["REGIONID"];
                        foreach (MapRegion region in m_regions)
                        {
                            if (region.ID == regionID)
                            {
                                lock (region)
                                {
                                    region.Elevation = elevation;
                                    region.MapRegionBBox = bbox;
                                    region.MapRegionImg = new MapRegionImage(width, height);
                                    region.initialize(layers);

                                    Bitmap queryImg = region.generateMapRegionImg();
                                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                                    queryImg.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                                    queryImg.Dispose();
                                    byte[] byteImage = stream.ToArray();
                                    stream.Close();
                                    stream.Dispose();
                                    httpResponse.ContentType = "image/png";
                                    return Convert.ToBase64String(byteImage);
                                }
                            }
                        }

                    httpResponse.ContentType = "text/plain";
                    return "Something unexpected occurs!";
                    }
                    else if (httpRequest.QueryString["REQUEST"] == "GetCapabilities")
                    {
                        httpResponse.ContentType = "text/xml";
                        string capDes = "";
                        TextReader textReader = new StreamReader("Capability.xml");
                        capDes = textReader.ReadToEnd();
                        textReader.Close();
                        return capDes;
                    }
                    else
                    {
                        return "Sorry, the request method is not supported by this service.";
                    }
                //case "WFS":
                //    if (httpRequest.QueryString["REQUEST"] == "GetFeature")
                //    {
                //        if ((httpRequest.QueryString["TypeName"] == "agent"))
                //        {
                //            switch (httpRequest.QueryString["FORMAT"])
                //            {
                //                case "text":
                //                    return m_agentLayer.GetFeaturesByText();
                //                case "xml":
                //                    return m_agentLayer.GetFeaturesByXML();
                //            }
                //        }
                //        else
                //            return "Query String is not supported";
                //    }
                //    break;
                //default:
                //    return "Unsupport Service";
            }
            return "Unsupported Service";
        }
Example #6
0
        public string owsHandler(string request, string path, string param,
                                 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
        {
            switch (httpRequest.QueryString["SERVICE"])
            {
            case "WMS":
                if (httpRequest.QueryString["REQUEST"] == "GetMap")
                {
                    //parse query string
                    string[] layers    = httpRequest.QueryString["LAYERS"].Split(',');
                    BBox     bbox      = new BBox(httpRequest.QueryString["BBOX"]);
                    int      height    = Int32.Parse(httpRequest.QueryString["HEIGHT"]);
                    int      width     = Int32.Parse(httpRequest.QueryString["WIDTH"]);
                    int      elevation = Int32.Parse(httpRequest.QueryString["ELEVATION"]);
                    string   regionID  = httpRequest.QueryString["REGIONID"];
                    foreach (MapRegion region in m_regions)
                    {
                        if (region.ID == regionID)
                        {
                            lock (region)
                            {
                                region.Elevation     = elevation;
                                region.MapRegionBBox = bbox;
                                region.MapRegionImg  = new MapRegionImage(width, height);
                                region.initialize(layers);

                                Bitmap queryImg = region.generateMapRegionImg();
                                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                                queryImg.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                                queryImg.Dispose();
                                byte[] byteImage = stream.ToArray();
                                stream.Close();
                                stream.Dispose();
                                httpResponse.ContentType = "image/png";
                                return(Convert.ToBase64String(byteImage));
                            }
                        }
                    }

                    httpResponse.ContentType = "text/plain";
                    return("Something unexpected occurs!");
                }
                else if (httpRequest.QueryString["REQUEST"] == "GetCapabilities")
                {
                    httpResponse.ContentType = "text/xml";
                    string     capDes     = "";
                    TextReader textReader = new StreamReader("Capability.xml");
                    capDes = textReader.ReadToEnd();
                    textReader.Close();
                    return(capDes);
                }
                else
                {
                    return("Sorry, the request method is not supported by this service.");
                }
                //case "WFS":
                //    if (httpRequest.QueryString["REQUEST"] == "GetFeature")
                //    {
                //        if ((httpRequest.QueryString["TypeName"] == "agent"))
                //        {
                //            switch (httpRequest.QueryString["FORMAT"])
                //            {
                //                case "text":
                //                    return m_agentLayer.GetFeaturesByText();
                //                case "xml":
                //                    return m_agentLayer.GetFeaturesByXML();
                //            }
                //        }
                //        else
                //            return "Query String is not supported";
                //    }
                //    break;
                //default:
                //    return "Unsupport Service";
            }
            return("Unsupported Service");
        }