public static void Main(string[] args) { IMapDrawer drawer = new MapDrawer(); bool showHelp = false; string path = "out.png"; int w = -1; int h = -1; Rectangle extents = new Rectangle(); bool showVersion = false; OptionSet options = new OptionSet(); options.Add("e|extents=", "comma-delimited extents (e.g. -180,-90,180,90) ", delegate (string v) { extents = ParseExtents(v); }); options.Add("h|help", "show this message and exit", delegate (string v) { showHelp = v!= null; }); options.Add("o|output=", "the path of the PNG image to create", delegate (string v) { path = v; }); options.Add("w|width=", "the width of the image in pixels", delegate (string v) { w = int.Parse(v); }); options.Add("t|height=", "the height of the image in pixels", delegate (string v) { h = int.Parse(v); }); options.Add("v|version", "shows the version and exits", delegate (string v) { showVersion = v != null; }); List<string> rest = options.Parse(args); if (showVersion) { System.Console.WriteLine("Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); return; } if (showHelp) { ShowHelp(options); return; } if (rest.Count == 0) { System.Console.WriteLine("No map specified"); ShowHelp(options); return; } // search in the local directory for espg files // so Windows ppl don't have to have it installed ProjFourWrapper.CustomSearchPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); MapSerializer ms = new MapSerializer(); ms.AddDatabaseFeatureSourceType<PostGISFeatureSource>(); ms.AddDatabaseFeatureSourceType<SqlServerFeatureSource>(); Map map = ms.Deserialize(rest[0]); if (w > 0) map.Width = w; if (h > 0) map.Height = h; if (!extents.IsEmpty) map.Extents = extents; System.Console.WriteLine(map.Layers.Count + " Layer(s) loaded"); Bitmap b = drawer.Draw(map); if (System.IO.Path.GetExtension(path) != ".png") { path += ".png"; } b.Save(path, ImageFormat.Png); }
public Bitmap DrawTile(Map map, int x, int y, int zoomLevel) { Bitmap b; string prj = null; if (consumer == TileConsumer.GoogleMaps || consumer == TileConsumer.VirtualEarth) { // reproject to spherical mercator prj = ProjFourWrapper.SphericalMercatorProjection; } try { CheckZoomLevel(zoomLevel); // calculate number of tiles across int numTiles = CalculateNumberOfTilesAcross(zoomLevel); // get map units/pixel resolution for zoomlevel double resolution = CalculateMapUnitsPerPixel(zoomLevel); // set to tile size int adjTileSize = tileSize + (bleedInPixels * 2); // google tiles origin is top left int tileymin, tileymax; if (consumer == TileConsumer.GoogleMaps || consumer == TileConsumer.VirtualEarth) { tileymin = numTiles - y - 1; tileymax = numTiles - y; } else { tileymin = y; tileymax = y + 1; } double mapUnitsOffset = bleedInPixels * resolution; // - convert pixels to map units // - translate to origin // - offset if we need to draw a bleed Rectangle extents = new Rectangle((tileSize*x) * resolution + worldExtents.Min.X - (mapUnitsOffset), (tileSize*tileymin) * resolution + worldExtents.Min.Y - (mapUnitsOffset), (tileSize*(x+1)) * resolution + worldExtents.Min.X + (mapUnitsOffset), (tileSize*tileymax) * resolution + worldExtents.Min.Y + (mapUnitsOffset)); MapDrawer renderer = new MapDrawer(); // draw our map b = renderer.Draw(map, extents, prj, adjTileSize, adjTileSize); if (bleedInPixels > 0) { // crop to get our tile using (Bitmap old = b) { b = old.Clone(new System.Drawing.Rectangle(bleedInPixels, bleedInPixels, tileSize, tileSize), System.Drawing.Imaging.PixelFormat.DontCare); } } } catch (Exception ex) { if (!DrawExceptionsOnTile) throw; b = new Bitmap(tileSize, tileSize); // draw the exception on the tile using (Graphics gr = Graphics.FromImage(b)) { string msg = ex.Message; int numCharsPerRow = (tileSize) / 5; int numLineBreaks = (int) Math.Ceiling(Convert.ToSingle(msg.Length) / Convert.ToSingle(numCharsPerRow)); for (int ii=0; ii < numLineBreaks; ii++) { msg = msg.Insert(ii*numCharsPerRow, Environment.NewLine); } gr.DrawString(msg, new Font("Arial", 10, GraphicsUnit.Pixel), Brushes.Red, 10, 10); } } return b; }