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) ); } }
public void Image_Encode() { Map m = new Map(10, 10); Image i1 = new Image(10, 10); m.Background = new Color("green"); m.Render(i1); byte[] bytes1 = i1.Encode("png"); byte[] bytes2 = File.ReadAllBytes(@".\data\10x10green.png"); CollectionAssert.AreEqual(bytes1, bytes2); }
public void Image_Encode_InvalidFormat() { Image im = new Image(256, 256); im.Encode("foo"); }
public byte[] Render(Coord coord, string format, int tileWidth, int tileHeight) { byte[] tileBytes = this._tileSource.GetTile(coord, "pbf"); // Uncompress bytes if (tileBytes.Length > 0) { tileBytes = Decompress(tileBytes); } // Set vector tile bytes // mapnik vector tile assumes top left origin int y = coord.Y; if (!coord.TopOrigin) y = this._tileSource.GridSet.GridHeight(coord.Z) - coord.Y - 1; VectorTile vTile = new VectorTile(coord.Z, coord.X, y, Convert.ToUInt32(tileWidth), Convert.ToUInt32(tileHeight)); vTile.SetBytes(tileBytes); // Get coord envelope Envelope envelope = this._tileSource.GridSet.CoordToEnvelope(coord); // 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); Image img = new Image(Convert.ToInt32(_map.Width), Convert.ToInt32(_map.Height)); vTile.Render(_map, img); format = format.ToLower(); if (format == "png" || format == "jpg") { if (format == "png") { format = this._pngOptions; } if (format == "jpg") { format = this._jpegOptions; } return img.Encode(format); } // Format not expected so throw exception throw new InvalidTileFormatException( string.Format("Invalid tile FORMAT {0}", format) ); } }