Пример #1
0
        private static IEnumerable <GeoJSON> QueryData(BoundingBox bbox, ICanQueryLayer layer)
        {
            if (layer == null)
            {
                throw new ArgumentNullException("layer");
            }

            // Query for data
            FeatureDataSet ds = new FeatureDataSet();

            layer.ExecuteIntersectionQuery(bbox, ds);
            IEnumerable <GeoJSON> data = GeoJSONHelper.GetData(ds);

            // 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)
            {
                GeometryFactory gf = new GeometryFactory();
                data = data.Select(d =>
                {
                    Geometry converted = GeometryTransform.TransformGeometry(d.Geometry, transform, gf);
                    d.SetGeometry(converted);
                    return(d);
                });
            }
            return(data);
        }
Пример #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()));
        }
Пример #3
0
        public JsonResult GetData(string layer, int z, int x, int y)
        {
            if (String.IsNullOrEmpty(layer))
            {
                throw new ArgumentNullException("layer");
            }

            Map map = ShapefileHelper.Spherical();
            IQueryable <VectorLayer> coll = map.Layers
                                            .AsQueryable()
                                            .OfType <VectorLayer>()
                                            .Where(l => l.Enabled && l.IsQueryEnabled)
                                            .Where(l => String.Equals(l.LayerName, layer));
            VectorLayer query = coll.SingleOrDefault();

            if (query == null)
            {
                throw new ArgumentException("Layer not found: " + layer);
            }

            if (query.SRID != 4326)
            {
                throw new ArgumentException("Only EPSG:4326 supported");
            }

            using (Utf8Grid grid = new Utf8Grid(UtfGridResolution, x, y, z))
            {
                Envelope bbox = this.GetBoundingBoxInLatLngWithMargin(x, y, z);
                var      ds   = new FeatureCollectionSet();
                query.ExecuteIntersectionQuery(bbox, ds);
                IEnumerable <GeoJSON> data = GeoJSONHelper.GetData(ds);

                int i = 1;
                foreach (GeoJSON val in data)
                {
                    IGeometry geom = val.Geometry;
                    IDictionary <string, object> dict = val.Values;
                    grid.FillPolygon(geom, i, dict);
                    i = i + 1;
                }

                Utf8GridResults results = grid.CreateUtfGridJson();
                return(this.Json(new { keys = results.Keys, data = results.Data, grid = results.Grid, }, JsonRequestBehavior.AllowGet));
            }
        }
Пример #4
0
        private IEnumerable <GeoJSON> GetData(Map map, BoundingBox bbox)
        {
            List <GeoJSON> items = new List <GeoJSON>();

            // Only queryable data!
            IQueryable <ICanQueryLayer> collection = 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);

                // 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);
            }
            return(items);
        }
Пример #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();
            }
        }