Example #1
0
        public byte[] Render(Envelope envelope, string format, int tileWidth, int tileHeight)
        {
            using (Bitmap img = new Bitmap(tileWidth, tileHeight))
            {
                using (Brush background = new SolidBrush(Color.White))
                using (Pen border = new Pen(new SolidBrush(Color.FromArgb(75, 255, 0, 0)), 50))
                using (Font text = new Font("arial", 10))
                using (Brush textColor = new SolidBrush(Color.Black))
                using (var g = Graphics.FromImage(img))
                {
                    var rect = new Rectangle(0, 0, tileWidth, tileHeight);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.FillRectangle(background, rect);
                    g.DrawRectangle(border, rect);
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("minx: " + envelope.Minx.ToString());
                    sb.AppendLine("miny: " + envelope.Miny.ToString());
                    sb.AppendLine("maxx: " + envelope.Maxx.ToString());
                    sb.AppendLine("maxy: " + envelope.Maxy.ToString());
                    g.DrawString(sb.ToString(),text ,textColor , new PointF(30, 30));
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, ImageFormat.Png);
                    return ms.ToArray();
                }
            }
        }
Example #2
0
        public static IGridSet CreateGridSet(string name, string srs, Envelope envelope, int tileSize = 256, int levels= 18, double step = 2, double pixelSize = .00028, bool topOrigin = false)
        {
            if (envelope == null)
                throw new ArgumentNullException("GridSetFactory Envelope cannot be null");

            if (tileSize <= 0)
                throw new ArgumentOutOfRangeException("GridSetFactory TileSize must be greater than 0");

            if (step <= 0)
                throw new ArgumentOutOfRangeException("GridSetFactory Step must be greater than 0");

            List<double> resolutions = new List<double>();
            for (int i = 0; i < levels; i++)
            {
                double r = (envelope.Maxx - envelope.Minx) / tileSize / Math.Pow(step, i);
                resolutions.Add(r);
            }

            return new GridSet(
                name,
                srs,
                envelope,
                resolutions,
                tileSize,
                tileSize,
                pixelSize,
                topOrigin
            );
        }
Example #3
0
File: Envelope.cs Project: jbrwn/tc
 public bool intersects(Envelope other)
 {
     if (this._minx <= other.Maxx &&
         this._maxx >= other.Minx &&
         this._miny <= other.Maxy &&
         this._maxy >= other.Miny)
     {
         return true;
     }
     return false;
 }
Example #4
0
File: Envelope.cs Project: jbrwn/tc
 public bool Equals(Envelope other)
 {
     if (this.Minx == other.Minx &&
         this.Miny == other.Miny &&
         this.Maxx == other.Maxx &&
         this.Maxy == other.Maxy)
     {
         return true;
     }
     return false;
 }
Example #5
0
        public bool IsEnvelopeValid(Envelope other)
        {
            if (this._extents == null)
                return true;

            foreach (Envelope extent in this._extents)
            {
                if (extent.intersects(other))
                    return true;
            }

            return false;
        }
Example #6
0
File: GridSet.cs Project: jbrwn/tc
        public GridSet(string name, string srs, Envelope envelope, IList<double> resolutions, int tileWidth = 256, int tileHeight = 256, double pixelSize = .00028, bool topOrigin = false)
        {
            // Set name
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("GridSet Name cannot be null or empty");
            this._name = name;

            // set SRS
            if (string.IsNullOrEmpty(srs))
                throw new ArgumentNullException("GridSet SRS cannot be null or empty");
            this._srs = srs;

            // set Envelope
            if (envelope == null)
                throw new ArgumentNullException("GridSet Envelope cannot be null");
            this._envelope = envelope;

            // Set resolutions list
            if (resolutions == null)
                throw new ArgumentNullException("GridSet Resolutions cannot be null");
            this._resolutions = resolutions as List<double>;

            // Set tile sizes
            if (tileWidth <= 0)
                throw new ArgumentOutOfRangeException("GridSet TileWidth must be greater than 0");
            this._tileWidth = tileWidth;
            if (tileHeight <= 0)
                throw new ArgumentOutOfRangeException("GridSet TileHeight must be greater than 0");
            this._tileHeight = tileHeight;

            // Set pixelSize
            if (pixelSize <= 0)
                throw new ArgumentOutOfRangeException("PixelSize must be greater than 0");
            this._pixelSize = pixelSize;

            // Set top orgin
            this._topOrigin = topOrigin;
        }
Example #7
0
        public byte[] Render(Envelope envelope, string format, int tileWidth, int tileHeight)
        {
            // Lock map object for rendering
            // TO DO: better strategy is to create a pool of map objects
            lock (mapLock)
            {
                _map.Width = Convert.ToUInt32(tileWidth);
                _map.Height = Convert.ToUInt32(tileHeight);
                _map.ZoomToBox(envelope.Minx, envelope.Miny, envelope.Maxx, envelope.Maxy);

                format = format.ToLower();
                // Render Image
                if (format == "png" || format == "jpg")
                {
                    Image img = new Image(Convert.ToInt32(_map.Width), Convert.ToInt32(_map.Height));
                    _map.Render(img);
                    if (format == "png")
                    {
                        format = this._pngOptions;
                    }
                    if (format == "jpg")
                    {
                        format = this._jpegOptions;
                    }
                    return img.Encode(format);
                }

                // Render UTFGrid
                else if (format == "json")
                {
                    NETMapnik.Grid grd = new NETMapnik.Grid(_map.Width, _map.Height);
                    _map.Render(grd, Convert.ToUInt32(this._gridLayerIndex), this._gridFields);
                    string json = JsonConvert.SerializeObject(grd.Encode("utf", true, Convert.ToUInt32(this._gridResolution)));
                    return Encoding.UTF8.GetBytes(json);
                }

                // Render vector tile
                else if (format == "pbf")
                {
                    //tile coord (i.e., 0/0/0 not needed for pbf rendering
                    VectorTile vTile = new VectorTile(0,0,0, _map.Width,_map.Height);
                    _map.Render(vTile);
                    byte[] bytes =  vTile.GetBytes();

                    //compress vector tile bytes
                    if (bytes.Length > 0)
                    {
                        bytes = Compress(bytes);
                    }
                    return bytes;
                }
                // Format not expected so throw exception
                throw new InvalidTileFormatException(
                    string.Format("Invalid tile FORMAT {0}", format)
                );
            }
        }
Example #8
0
 public bool IsValid(int Z, Envelope env, string format)
 {
     return IsEnvelopeValid(env) && IsFormatValid(format) && IsZValid(Z);
 }
Example #9
0
        public byte[] Render(Envelope envelope, string format, int tileWidth, int tileHeight)
        {
            StringBuilder query = new StringBuilder();
            query.Append("request=GetMap");
            query.Append("&service=WMS");
            query.Append("&version=" + HttpUtility.UrlEncode(this._version));
            query.Append("&layers=" + HttpUtility.UrlEncode(this._layers));
            query.Append("&styles=" + HttpUtility.UrlEncode(this._styles));
            if (this._version.Equals("1.3.0")) { query.Append("&crs=" + HttpUtility.UrlEncode(this._crs)); }
            else { query.Append("&srs=" + HttpUtility.UrlEncode(this._crs)); }
            query.Append("&bbox=" + envelope.Minx.ToString() + "," + envelope.Miny.ToString() + "," + envelope.Maxx.ToString() + "," + envelope.Maxy.ToString());
            query.Append("&width=" + tileWidth.ToString());
            query.Append("&height=" + tileHeight.ToString());
            query.Append("&format=" + HttpUtility.UrlEncode(format));
            if (this._sld != null) { query.Append("&sld=" + HttpUtility.UrlEncode(this._sld)); }
            if (this._bgcolor != null) { query.Append("&bgcolor=" + HttpUtility.UrlEncode(this._bgcolor)); }
            if (this._transparent != null) { query.Append("&transparent=" + HttpUtility.UrlEncode(this._transparent)); }

            UriBuilder ub = new UriBuilder(this._url);
            ub.Query = query.ToString();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ub.Uri);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        responseStream.CopyTo(ms);
                        return ms.ToArray();
                    }
                }
            }
        }