using System; using System.Collections.ObjectModel; using System.Drawing; using ThinkGeo.MapSuite.Core; namespace GenerateTiling { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Map1.MapUnit = GeographyUnit.DecimalDegree; } protected void btnGenerateTiling_Click(object sender, EventArgs e) { // Set the ZoomLevels you want to generate the cache images Collection zoomLevels = GetAllZoomLevels(); // Set the tile width and height int tileWidth = 256; int tileHeight = 256; // Construct a new ShapeFileFeatureLayer, please speficy the file path for your own shape file. ShapeFileFeatureLayer shapeFileFeatureLayer = new ShapeFileFeatureLayer(@"C:\Program Files (x86)\ThinkGeo\Map Suite Desktop Full Edition 4.0\Samples\SampleData\Data\Countries02.shp"); shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1; shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ShapeFileFeatureLayer majorCitiesLayer = new ShapeFileFeatureLayer(@"C:\Program Files (x86)\ThinkGeo\Map Suite Desktop Full Edition 4.0\Samples\SampleData\Data\MajorCities.shp"); majorCitiesLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital1; majorCitiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Get the extent that you want to cache shapeFileFeatureLayer.Open(); RectangleShape cacheExtent = shapeFileFeatureLayer.GetBoundingBox(); shapeFileFeatureLayer.Close(); // Initialize the MapEngine object MapEngine mapEngine = new MapEngine(); mapEngine.ShowLogo = false; // Add contries02 layer to it mapEngine.StaticLayers.Add(shapeFileFeatureLayer); // Add major cities layes to it mapEngine.StaticLayers.Add(majorCitiesLayer); // Set the right map unit for your shape files GeographyUnit mapUnit = GeographyUnit.DecimalDegree; for (int i = 0; i < zoomLevels.Count; i++) { MapSuiteTileMatrix tileMatrix = new MapSuiteTileMatrix(zoomLevels[i].Scale, tileWidth, tileHeight, mapUnit); BitmapTileCache tileCache = new FileBitmapTileCache(txtCachePath.Text, string.Empty, TileImageFormat.Png, tileMatrix); RowColumnRange rowColumnRange = tileCache.TileMatrix.GetIntersectingRowColumnRange(cacheExtent); for (long row = rowColumnRange.MinRowIndex; row <= rowColumnRange.MaxRowIndex; row++) { for (long column = rowColumnRange.MinColumnIndex; column <= rowColumnRange.MaxColumnIndex; column++) { // Get tile extent for every tile image RectangleShape tileExtent = tileCache.TileMatrix.GetCell(row, column).BoundingBox; // Generate cache for every tile image using (Bitmap bitmap = new Bitmap(tileWidth, tileHeight)) { mapEngine.CurrentExtent = tileExtent; mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(bitmap, mapUnit); mapEngine.DrawDynamicLayers(bitmap, mapUnit); mapEngine.CloseAllLayers(); BitmapTile tile = new BitmapTile(bitmap, tileExtent, zoomLevels[i].Scale); tileCache.SaveTile(tile); } } } } } private Collection GetAllZoomLevels() { // We only add the top 5 ZoomLevels here for example. // If you want to add more scales, you need to optimize the way generating tiles otherwise // the bitmap we generated might be too big to be hold in memory. ZoomLevelSet zoomLevelSet = new ZoomLevelSet(); Collection resultZoomLevels = new Collection(); resultZoomLevels.Add(zoomLevelSet.ZoomLevel01); resultZoomLevels.Add(zoomLevelSet.ZoomLevel02); resultZoomLevels.Add(zoomLevelSet.ZoomLevel03); resultZoomLevels.Add(zoomLevelSet.ZoomLevel04); resultZoomLevels.Add(zoomLevelSet.ZoomLevel05); return resultZoomLevels; } protected void btnLoadMap_Click(object sender, EventArgs e) { Map1.StaticOverlay.ServerCache.CacheDirectory = txtCachePath.Text; } } }