Esempio n. 1
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;
            }
        }
Esempio n. 2
0
        protected override AbstractGetFeatureInfoResponse CreateFeatureInfo(Map map,
                                                                            IEnumerable <string> requestedLayers,
                                                                            float x, float y,
                                                                            int featureCount,
                                                                            string cqlFilter,
                                                                            int pixelSensitivity,
                                                                            WmsServer.InterSectDelegate intersectDelegate)
        {
            List <GeoJSON> items = new List <GeoJSON>();

            foreach (string requestLayer in requestedLayers)
            {
                ICanQueryLayer queryLayer = GetQueryLayer(map, requestLayer);
                FeatureDataSet fds;
                if (!TryGetData(map, x, y, pixelSensitivity, intersectDelegate, queryLayer, cqlFilter, out fds))
                {
                    continue;
                }

                IEnumerable <GeoJSON> data = GeoJSONHelper.GetData(fds);
                // reproject geometries if needed
                IMathTransform transform = null;
                if (queryLayer is VectorLayer)
                {
                    ICoordinateTransformation transformation = (queryLayer as VectorLayer).CoordinateTransformation;
                    if (transformation != null)
                    {
                        transform = transformation.MathTransform;
                    }
                }

                if (transform != null)
                {
#if DotSpatialProjections
                    throw new NotImplementedException();
#else
                    data = data.Select(d =>
                    {
                        IGeometry converted = GeometryTransform.TransformGeometry(d.Geometry, transform, map.Factory);
                        d.SetGeometry(converted);
                        return(d);
                    });
#endif
                }
                items.AddRange(data);
            }

            StringWriter sb = new StringWriter();
            GeoJSONWriter.Write(items, sb);
            return(new GetFeatureInfoResponseJson(sb.ToString()));
        }
Esempio n. 3
0
        public override void ProcessRequest(IContext context)
        {
            IContextRequest  request  = context.Request;
            IContextResponse response = context.Response;

            try
            {
                string s = request.GetParam("BBOX");
                if (String.IsNullOrEmpty(s))
                {
                    throw new WmsInvalidParameterException("BBOX");
                }


                Map             map    = GetMap(request);
                LayerCollection layers = map.Layers;
                ILayer          first  = layers.First();
                bool            flip   = first.TargetSRID == 4326;
                BoundingBox     bbox   = AbstractHandler.ParseBBOX(s, flip);
                if (bbox == null)
                {
                    throw new WmsInvalidBboxException(s);
                }

                string ls = request.GetParam("LAYERS");
                if (!String.IsNullOrEmpty(ls))
                {
                    string[] strings = ls.Split(',');
                    foreach (ILayer layer in layers)
                    {
                        if (!strings.Contains(layer.LayerName))
                        {
                            layer.Enabled = false;
                        }
                    }
                }

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

                IHandlerResponse result = new GetFeatureInfoResponseJson(buffer);
                result.WriteToContextAndFlush(response);
            }
            catch (WmsExceptionBase ex)
            {
                ex.WriteToContextAndFlush(response);
            }
        }
Esempio n. 4
0
        private ActionResult MakeResponse(IEnumerable <IGeometry> geometries)
        {
            if (geometries == null)
            {
                throw new ArgumentNullException("geometries");
            }

            // we should return a GeometryCollection object
            IGeometry[]         array  = geometries.ToArray();
            IGeometryCollection result = this.factory.CreateGeometryCollection(array);
            StringWriter        writer = new StringWriter();

            GeoJSONWriter.Write(result, writer);
            return(this.Json(new { geo = writer.ToString() }));
        }
Esempio n. 5
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;
            }
        }
Esempio n. 6
0
 public void ToJSON(Jayrock.Json.JsonTextWriter jwriter)
 {
     GeoJSONWriter.Write(this, jwriter);
 }
Esempio n. 7
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();
            }
        }
Esempio n. 8
0
 public void ToJSON(Jayrock.Json.JsonTextWriter jwriter)
 {
     GeoJSONWriter.Write(this, jwriter, _nonSerializedFields);
 }
Esempio n. 9
0
 public void ToJSON(JsonTextWriter writer)
 {
     GeoJSONWriter.Write(this, writer, _nonSerializedFields);
 }