Example #1
0
        public override void Handle()
        {
            string service = this.context.Request.Params["SERVICE"];

            if (service == null)
            {
                WmsException.ThrowWmsException("Required parameter SERVICE not specified");
                return;
            }

            if (!this.Check("WMS", service))
            {
                WmsException.ThrowWmsException("Invalid service for GetCapabilities Request. Service parameter must be 'WMS'");
                return;
            }

            XmlDocument capabilities = Capabilities.GetCapabilities(this.map, this.description);

            this.context.Response.Clear();
            this.context.Response.ContentType = "text/xml";
            XmlWriter writer = XmlWriter.Create(this.context.Response.OutputStream);

            capabilities.WriteTo(writer);
            writer.Close();
            this.context.Response.End();
        }
Example #2
0
        public override void ProcessRequest(HttpContext context)
        {
            try
            {
                string s = context.Request.Params["BBOX"];
                if (String.IsNullOrEmpty(s))
                {
                    WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue, "Required parameter BBOX not specified", context);
                    return;
                }

                Map         map  = this.GetMap(context.Request);
                bool        flip = map.Layers[0].TargetSRID == 4326;
                BoundingBox bbox = WmsServer.ParseBBOX(s, flip);
                if (bbox == null)
                {
                    WmsException.ThrowWmsException("Invalid parameter BBOX", context);
                    return;
                }

                string ls = context.Request.Params["LAYERS"];
                if (!String.IsNullOrEmpty(ls))
                {
                    string[] layers = ls.Split(',');
                    foreach (ILayer layer in map.Layers)
                    {
                        if (!layers.Contains(layer.LayerName))
                        {
                            layer.Enabled = false;
                        }
                    }
                }

                IEnumerable <GeoJSON> items  = GetData(map, bbox);
                StringWriter          writer = new StringWriter();
                GeoJSONWriter.Write(items, writer);
                string buffer = writer.ToString();

                context.Response.Clear();
                context.Response.ContentType  = "text/json";
                context.Response.BufferOutput = true;
                context.Response.Write(buffer);
                context.Response.Flush();
                context.Response.SuppressContent = true;
                context.ApplicationInstance.CompleteRequest();
                //context.Response.End();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                throw;
            }
        }
Example #3
0
        public override void ProcessRequest(HttpContext context)
        {
            try
            {
                string s = context.Request.Params["BBOX"];
                if (String.IsNullOrEmpty(s))
                {
                    WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue, "Required parameter BBOX not specified");
                    return;
                }

                BoundingBox bbox = WmsServer.ParseBBOX(s);
                if (bbox == null)
                {
                    WmsException.ThrowWmsException("Invalid parameter BBOX");
                    return;
                }

                Map map = this.GetMap(context.Request);
                IEnumerable <GeoJSON> items = this.GetData(map, bbox);

                StringWriter writer = new StringWriter();
                GeoJSONWriter.Write(items, writer);
                string buffer = writer.ToString();

                context.Response.Clear();
                context.Response.ContentType  = "text/json";
                context.Response.BufferOutput = true;
                context.Response.Write(buffer);
                context.Response.End();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                throw;
            }
        }
Example #4
0
        public override void Handle()
        {
            string layers      = this.context.Request.Params["LAYERS"];
            string styles      = this.context.Request.Params["STYLES"];
            string crs         = this.context.Request.Params["CRS"];
            string queryBBOX   = this.context.Request.Params["BBOX"];
            string queryWidth  = this.context.Request.Params["WIDTH"];
            string queryHeight = this.context.Request.Params["HEIGHT"];
            string format      = this.context.Request.Params["FORMAT"];
            string queryLayers = this.context.Request.Params["QUERY_LAYERS"];
            string infoFormat  = this.context.Request.Params["INFO_FORMAT"];
            string xAxis       = this.context.Request.Params["X"];
            string yAxis       = this.context.Request.Params["Y"];
            string iParam      = this.context.Request.Params["I"];
            string jParam      = this.context.Request.Params["J"];

            if (layers == null)
            {
                WmsException.ThrowWmsException("Required parameter LAYERS not specified");
                return;
            }

            if (styles == null)
            {
                WmsException.ThrowWmsException("Required parameter STYLES not specified");
                return;
            }

            if (crs == null)
            {
                WmsException.ThrowWmsException("Required parameter CRS not specified");
                return;
            }

            if (!this.Check(String.Format("EPSG:{0}", this.map.Layers[0].SRID), crs))
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidCRS,
                                               "CRS not supported");
                return;
            }

            if (queryBBOX == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter BBOX not specified");
                return;
            }

            if (queryWidth == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter WIDTH not specified");
                return;
            }

            if (queryHeight == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter HEIGHT not specified");
                return;
            }

            if (format == null)
            {
                WmsException.ThrowWmsException("Required parameter FORMAT not specified");
                return;
            }

            if (queryLayers == null)
            {
                WmsException.ThrowWmsException("Required parameter QUERY_LAYERS not specified");
                return;
            }

            if (infoFormat == null)
            {
                WmsException.ThrowWmsException("Required parameter INFO_FORMAT not specified");
                return;
            }

            //parameters X&Y are not part of the 1.3.0 specification, but are included for backwards compatability with 1.1.1
            // (OpenLayers likes it when used together with 1.1.1 services)
            if (xAxis == null && iParam == null)
            {
                WmsException.ThrowWmsException("Required parameter I not specified");
                return;
            }

            if (yAxis == null && jParam == null)
            {
                WmsException.ThrowWmsException("Required parameter J not specified");
                return;
            }

            try
            {
                //sets the map size to the size of the client in order to calculate the coordinates of the projection of the client
                this.map.Size = new Size(Convert.ToInt16(queryWidth), Convert.ToInt16(queryHeight));
            }
            catch
            {
                WmsException.ThrowWmsException("Invalid parameters for HEIGHT or WITDH");
                return;
            }

            //sets the boundingbox to the boundingbox of the client in order to calculate the coordinates of the projection of the client
            BoundingBox bbox = this.ParseBBOX(queryBBOX);

            if (bbox == null)
            {
                WmsException.ThrowWmsException("Invalid parameter BBOX");
                return;
            }

            this.map.ZoomToBox(bbox);
            //sets the point clicked by the client
            Single x = 0;
            Single y = 0;

            //tries to set the x to the Param I, if the client send an X, it will try the X, if both fail, exception is thrown
            if (xAxis != null)
            {
                try { x = Convert.ToSingle(xAxis); }
                catch { WmsException.ThrowWmsException("Invalid parameters for I"); }
            }
            if (iParam != null)
            {
                try { x = Convert.ToSingle(iParam); }
                catch { WmsException.ThrowWmsException("Invalid parameters for I"); }
            }
            //same procedure for J (Y)
            if (yAxis != null)
            {
                try { y = Convert.ToSingle(yAxis); }
                catch { WmsException.ThrowWmsException("Invalid parameters for J"); }
            }
            if (jParam != null)
            {
                try { y = Convert.ToSingle(jParam); }
                catch { WmsException.ThrowWmsException("Invalid parameters for J"); }
            }

            Point p = this.map.ImageToWorld(new PointF(x, y));
            int   fc;

            try
            {
                fc = Convert.ToInt16(this.context.Request.Params["FEATURE_COUNT"]);
                if (fc < 1)
                {
                    fc = 1;
                }
            }
            catch { fc = 1; }

            String vstr = "GetFeatureInfo results: \n";

            string[] requestLayers = queryLayers.Split(new[] { ',' });
            foreach (string item in requestLayers)
            {
                bool found = false;

                foreach (ILayer mapLayer in this.map.Layers)
                {
                    if (!String.Equals(mapLayer.LayerName, item,
                                       StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    found = true;

                    if (!(mapLayer is ICanQueryLayer))
                    {
                        continue;
                    }

                    ICanQueryLayer layer   = mapLayer as ICanQueryLayer;
                    bool           enabled = layer.Enabled && layer.IsQueryEnabled;
                    if (!enabled)
                    {
                        continue;
                    }

                    Single         queryBoxMinX = x - (this.pixelSensitivity);
                    Single         queryBoxMinY = y - (this.pixelSensitivity);
                    Single         queryBoxMaxX = x + (this.pixelSensitivity);
                    Single         queryBoxMaxY = y + (this.pixelSensitivity);
                    Point          minXY        = this.map.ImageToWorld(new PointF(queryBoxMinX, queryBoxMinY));
                    Point          maxXY        = this.map.ImageToWorld(new PointF(queryBoxMaxX, queryBoxMaxY));
                    BoundingBox    queryBox     = new BoundingBox(minXY, maxXY);
                    FeatureDataSet fds          = new FeatureDataSet();
                    layer.ExecuteIntersectionQuery(queryBox, fds);
                    if (this.intersectDelegate != null)
                    {
                        fds.Tables[0] = this.intersectDelegate(fds.Tables[0], queryBox);
                    }

                    if (fds.Tables.Count != 0)
                    {
                        if (fds.Tables[0].Rows.Count != 0)
                        {
                            //if featurecount < fds...count, select smallest bbox, because most likely to be clicked
                            vstr = String.Format("{0}\n Layer: '{1}'\n Featureinfo:\n", vstr, item);
                            int[]    keys = new int[fds.Tables[0].Rows.Count];
                            double[] area = new double[fds.Tables[0].Rows.Count];
                            for (int l = 0; l < fds.Tables[0].Rows.Count; l++)
                            {
                                FeatureDataRow fdr = fds.Tables[0].Rows[l] as FeatureDataRow;
                                area[l] = fdr.Geometry.GetBoundingBox().GetArea();
                                keys[l] = l;
                            }
                            Array.Sort(area, keys);
                            if (fds.Tables[0].Rows.Count < fc)
                            {
                                fc = fds.Tables[0].Rows.Count;
                            }
                            for (int k = 0; k < fc; k++)
                            {
                                for (int j = 0; j < fds.Tables[0].Rows[keys[k]].ItemArray.Length; j++)
                                {
                                    vstr = String.Format("{0} '{1}'", vstr, fds.Tables[0].Rows[keys[k]].ItemArray[j].ToString());
                                }
                                if ((k + 1) < fc)
                                {
                                    vstr = String.Format("{0},\n", vstr);
                                }
                            }
                        }
                        else
                        {
                            vstr = String.Format("{0}\nSearch returned no results on layer: {1} ", vstr, item);
                        }
                    }
                    else
                    {
                        vstr = String.Format("{0}\nSearch returned no results on layer: {1}", vstr, item);
                    }
                }
                if (found)
                {
                    continue;
                }

                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined,
                                               String.Format("Unknown layer '{0}'", item));
                return;
            }

            this.context.Response.Clear();
            this.context.Response.ContentType = "text/plain";
            this.context.Response.Charset     = "windows-1252";
            this.context.Response.Write(vstr);
            this.context.Response.Flush();
            this.context.Response.End();
        }
Example #5
0
        public override void Handle()
        {
            string queryLayers = this.context.Request.Params["LAYERS"];
            string styles      = this.context.Request.Params["STYLES"];
            string crs         = this.context.Request.Params["CRS"];
            string queryBBOX   = this.context.Request.Params["BBOX"];
            string queryWidth  = this.context.Request.Params["WIDTH"];
            string queryHeight = this.context.Request.Params["HEIGHT"];
            string format      = this.context.Request.Params["FORMAT"];
            string transparent = this.context.Request.Params["TRANSPARENT"];
            string background  = this.context.Request.Params["BGCOLOR"];

            if (queryLayers == null)
            {
                WmsException.ThrowWmsException("Required parameter LAYERS not specified");
                return;
            }

            if (styles == null)
            {
                WmsException.ThrowWmsException("Required parameter STYLES not specified");
                return;
            }

            if (crs == null)
            {
                WmsException.ThrowWmsException("Required parameter CRS not specified");
                return;
            }

            if (!this.Check(String.Format("EPSG:{0}", this.map.Layers[0].SRID), crs))
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidCRS, "CRS not supported");
                return;
            }

            if (queryBBOX == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter BBOX not specified");
                return;
            }

            if (queryWidth == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter WIDTH not specified");
                return;
            }

            if (queryHeight == null)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Required parameter HEIGHT not specified");
                return;
            }

            if (format == null)
            {
                WmsException.ThrowWmsException("Required parameter FORMAT not specified");
                return;
            }

            //Set background color of map
            if (!this.Check("TRUE", transparent))
            {
                if (background != null)
                {
                    try { this.map.BackColor = ColorTranslator.FromHtml(background); }
                    catch
                    {
                        WmsException.ThrowWmsException("Invalid parameter BGCOLOR");
                        return;
                    }
                }
                else
                {
                    this.map.BackColor = Color.White;
                }
            }
            else
            {
                this.map.BackColor = Color.Transparent;
            }

            //Parse map size
            int width;

            if (!Int32.TryParse(queryWidth, out width))
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Invalid parameter WIDTH");
                return;
            }
            if (this.description.MaxWidth > 0 && width > this.description.MaxWidth)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.OperationNotSupported,
                                               "Parameter WIDTH too large");
                return;
            }
            int height;

            if (!Int32.TryParse(queryHeight, out height))
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue,
                                               "Invalid parameter HEIGHT");
                return;
            }
            if (this.description.MaxHeight > 0 && height > this.description.MaxHeight)
            {
                WmsException.ThrowWmsException(WmsException.WmsExceptionCode.OperationNotSupported,
                                               "Parameter HEIGHT too large");
                return;
            }
            this.map.Size = new Size(width, height);

            BoundingBox bbox = this.ParseBBOX(queryBBOX);

            if (bbox == null)
            {
                WmsException.ThrowWmsException("Invalid parameter BBOX");
                return;
            }

            this.map.PixelAspectRatio = (width / (double)height) / (bbox.Width / bbox.Height);
            this.map.Center           = bbox.GetCentroid();
            this.map.Zoom             = bbox.Width;

            //Set layers on/off
            if (!String.IsNullOrEmpty(queryLayers))
            //If LAYERS is empty, use default layer on/off settings
            {
                string[] layers = queryLayers.Split(new[] { ',' });
                if (this.description.LayerLimit > 0)
                {
                    if (layers.Length == 0 &&
                        this.map.Layers.Count > this.description.LayerLimit ||
                        layers.Length > this.description.LayerLimit)
                    {
                        WmsException.ThrowWmsException(WmsException.WmsExceptionCode.OperationNotSupported,
                                                       "Too many layers requested");
                        return;
                    }
                }
                foreach (ILayer layer in this.map.Layers)
                {
                    layer.Enabled = false;
                }
                foreach (string layer in layers)
                {
                    ILayer lay = this.map.GetLayerByName(layer);
                    if (lay == null)
                    {
                        WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined,
                                                       String.Format("Unknown layer '{0}'", layer));
                        return;
                    }
                    lay.Enabled = true;
                }
            }

            bool json = this.Check("text/json", format);

            if (json)
            {
                List <GeoJSON> items = new List <GeoJSON>();

                //Only queryable data!
                IQueryable <ICanQueryLayer> collection = this.map.Layers.AsQueryable()
                                                         .OfType <ICanQueryLayer>().Where(l => l.Enabled && l.IsQueryEnabled);
                foreach (ICanQueryLayer layer in collection)
                {
                    //Query for data
                    FeatureDataSet ds = new FeatureDataSet();
                    layer.ExecuteIntersectionQuery(bbox, ds);
                    IEnumerable <GeoJSON> data = GeoJSONHelper.GetData(ds);

                    //Filter only visible items
                    //double f = bbox.GetArea() / (width * height);
                    //data = data.Where(i =>
                    //{
                    //    Geometry g = i.Geometry;
                    //    BoundingBox p = g.GetBoundingBox();
                    //    double area = p.GetArea();
                    //    return area == 0 || area > f;
                    //});

                    //Reproject geometries if needed
                    IMathTransform transform = null;
                    if (layer is VectorLayer)
                    {
                        ICoordinateTransformation transformation = (layer as VectorLayer).CoordinateTransformation;
                        transform = transformation == null ? null : transformation.MathTransform;
                    }
                    if (transform != null)
                    {
                        data = data.Select(d =>
                        {
                            Geometry converted = GeometryTransform.TransformGeometry(d.Geometry, transform);
                            d.SetGeometry(converted);
                            return(d);
                        });
                    }

                    items.AddRange(data);
                }

                StringWriter writer = new StringWriter();
                GeoJSONWriter.Write(items, writer);
                string buffer = writer.ToString();

                this.context.Response.Clear();
                this.context.Response.ContentType  = "text/json";
                this.context.Response.BufferOutput = true;
                this.context.Response.Write(buffer);
                this.context.Response.End();
            }
            else
            {
                //Get the image format requested
                ImageCodecInfo imageEncoder = this.GetEncoderInfo(format);
                if (imageEncoder == null)
                {
                    WmsException.ThrowWmsException("Invalid MimeType specified in FORMAT parameter");
                    return;
                }

                //Render map
                Image img = this.map.GetMap();

                //Png can't stream directy. Going through a memorystream instead
                MemoryStream ms = new MemoryStream();
                img.Save(ms, imageEncoder, null);
                img.Dispose();
                byte[] buffer = ms.ToArray();

                this.context.Response.Clear();
                this.context.Response.ContentType  = imageEncoder.MimeType;
                this.context.Response.BufferOutput = true;
                this.context.Response.BinaryWrite(buffer);
                this.context.Response.End();
            }
        }